WhaleSpunk

Untitled

May 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. typedef struct node{
  7. int estado[6];
  8. int sequencia[6];
  9. int transicao_come_from;
  10. struct node *brother;
  11. struct node *son;
  12. }node;
  13.  
  14. node* create_node(int estado, int *sequencia,int transicao_come_from,int places);
  15. void add_son(node* father,node* new_son);
  16. void print(node* root,int places);
  17.  
  18. int main()
  19. {
  20. int places,transitions,check=1,flag=0;
  21. int i;
  22. int matriz_val[1000][3];
  23.  
  24. char teste[10],teste1[10],teste2[10];
  25. char state_inicial[20];
  26. int len, index=0,indice_matriz=0,indice_state=0;
  27. int matriz_state[6]={0};
  28.  
  29. struct node *head;
  30.  
  31. scanf("%d %d",&places, &transitions);
  32. int arrayTransitons[transitions][2][places];
  33. memset(arrayTransitons,0, sizeof(arrayTransitons));
  34. while(check==1){
  35.  
  36. if(flag==1){
  37.  
  38. for(i=0;i<places;i++){
  39. scanf("%s",&state_inicial);
  40. if(isdigit(state_inicial[0])){
  41. matriz_state[i]=atoi(state_inicial);
  42. //indice_state++;
  43. }
  44. }
  45.  
  46. check=0;
  47. }else{
  48. scanf("%s",&teste);
  49.  
  50. if(isdigit(teste[0])){
  51. scanf("%s %s",&teste1,&teste2);
  52. matriz_val[index][0]=atoi(teste);
  53. matriz_val[index][1]=atoi(teste1);
  54. matriz_val[index][2]=atoi(teste2);
  55. if(atoi(teste2) == 1){
  56. arrayTransitons[atoi(teste)-1][1][atoi(teste1)-1] = 1;
  57. }else{
  58. arrayTransitons[atoi(teste1)-1][0][atoi(teste)-1] = 1;
  59. }
  60.  
  61. index++;
  62. }else{
  63. //printf("flag=1\n");
  64. flag=1;
  65. }
  66.  
  67. }
  68.  
  69. }
  70.  
  71. printf("\n%d %d\n",places,transitions);
  72. for(i=0;i<index;i++){
  73. printf("%d %d %d\n",matriz_val[i][0],matriz_val[i][1],matriz_val[i][2]);
  74. }
  75. printf("STATE\n");
  76. for(i=0;i<places;i++){
  77. printf("%d ",matriz_state[i]);
  78. }
  79. printf("\n\n");
  80. int k,l;
  81. printf("[");
  82. for(i = 0; i<transitions; i++){
  83. printf("[");
  84. for(k =0;k<2;k++){
  85. printf("[");
  86. for(l = 0; l<places;l++){
  87. printf("%d, ", arrayTransitons[i][k][l]);
  88. }
  89. printf("],");
  90. }
  91. printf("],");
  92. }
  93. printf("]\n");
  94.  
  95. //considero aqui que do place 1 sair sempre o estado 1
  96. head=create_node(1,matriz_state,1,places);
  97. node * aux= head;
  98. addNextTransition(aux, transitions, places, arrayTransitons);
  99.  
  100.  
  101. printf("PAI: ");
  102. for(i = 0; i<places; i++){
  103. printf("%d ", head->sequencia[i]);
  104. }
  105. printf("\nFILHO: ");
  106. for(i = 0; i<places; i++){
  107. printf("%d ", head->son->sequencia[i]);
  108. }
  109. while(addNextTransition(head->son, transitions, places, arrayTransitons) !=0);
  110. printf("\nFILHO FILHO: ");
  111. for(i = 0; i<places; i++){
  112. printf("%d ", head->son->son->sequencia[i]);
  113. }
  114. printf("\nFILHO FILHO IRMAO: ");
  115. for(i = 0; i<places; i++){
  116. printf("%d ", head->son->son->brother->sequencia[i]);
  117. }
  118. while(addNextTransition( head->son->son, transitions, places, arrayTransitons) !=0);
  119. printf("\nFILHO FILHO FILHO: ");
  120. for(i = 0; i<places; i++){
  121. printf("%d ", head->son->son->son->sequencia[i]);
  122. }
  123. while(addNextTransition( head->son->son->son, transitions, places, arrayTransitons) !=0);
  124. printf("\nFILHO FILHO FILHO FILHO: ");
  125. for(i = 0; i<places; i++){
  126. printf("%d ", head->son->son->son->son->sequencia[i]);
  127. }
  128. printf("\nFILHO FILHO FILHO FILHO IRMAO: ");
  129. for(i = 0; i<places; i++){
  130. printf("%d ", head->son->son->son->son->brother->sequencia[i]);
  131. }
  132. // memcpy(head->sequencia,matriz_state,sizeof(matriz_state));
  133. // head->estado=1;
  134. // head->transicao_come_from=1;
  135.  
  136.  
  137. //create_node(1,matriz_state,1,places);
  138. /*int num=1;//ja considero a transicao 1
  139. int atual=1;
  140. int first=0;
  141.  
  142. node* atual_node=head;
  143. node* aux_node=head;
  144. while(num<transitions){
  145. //vou ver por transicoes
  146. if(matriz_val[num][0]!=num){
  147.  
  148. }else{
  149. while(num==matriz_val[atual][0]){//mesma transicao
  150. if(first==0){
  151. //para verificar e garantir que a head começa no inicio da transicao 1
  152. if(matriz_val[atual][0]==1 && matriz_val[atual][2]==2){
  153. head->estado=matriz_val[atual][1];
  154. first=1;
  155. }
  156. }
  157. if(matriz_val[atual][2]==1){
  158. matriz_state[matriz_val[atual][1]-1]+=1;
  159.  
  160. }else if(matriz_val[atual][2]==2){
  161. matriz_state[matriz_val[atual][1]-1]-=1;
  162. }
  163.  
  164. if((matriz_val[atual][1]>matriz_val[atual][0])&&matriz_val[atual][2]==1){
  165. add_son(atual_node,create_node(matriz_val[atual][1],matriz_state,matriz_val[atual][0],places));
  166. }
  167.  
  168. atual++;
  169. }
  170. if(atual_node->brother!=NULL){
  171. atual_node=atual_node->brother;
  172. }else if(aux_node->son!=NULL){
  173. atual_node=aux_node->son;
  174. aux_node=aux_node->son;
  175. }else{
  176. break;
  177. }
  178.  
  179. }
  180.  
  181. num++;
  182. }*/
  183. //add_son(head,create_node(2,matriz_state,2,places));
  184. // printf("\nsequencia:\n");
  185. // for(i=0;i<places;i++){
  186. // printf("%d ",head->son->sequencia[i]);
  187. // }
  188. // printf("\nestado:\n");
  189. // printf("%d \n",head->son->estado);
  190. // printf("transicao:\n");
  191. // printf("%d \n",head->son->transicao_come_from);
  192. //
  193. // add_son(head,create_node(3,matriz_state,3,places));
  194. // printf("\n----brother---\nsequencia:\n");
  195. //
  196. /* printf("print\n");
  197. print(head,places, arrayTransitons, places);
  198.  
  199. printf("fim_print\n");*/
  200.  
  201. // for(i=0;i<places;i++){
  202. // printf("e1: %d \n",head->son->estado);
  203. // printf("s1: %d \n",head->son->sequencia[i]);
  204. // printf("e2: %d \n",head->son->brother->estado);
  205. // printf("s2: %d \n",head->son->brother->sequencia[i]);
  206. // }
  207. // printf("\nestado:\n");
  208. // printf("%d \n",head->son->brother->estado);
  209. // printf("transicao:\n");
  210. // printf("%d \n",head->son->brother->transicao_come_from);
  211.  
  212. return 0;
  213. }
  214.  
  215. node* create_node(int estado, int *sequencia,int transicao_come_from, int places){
  216.  
  217. int i;
  218. //printf("seq:\n");
  219. /*for(i=0;i<places;i++){
  220. printf("%d ",sequencia[i]);
  221. }*/
  222. node* new_node;
  223. new_node= (node *)malloc(sizeof(node));
  224. if(estado != -1){
  225. new_node->estado[estado-1]=1;
  226.  
  227. for(i=0; i<places; i++){
  228. new_node->sequencia[i] = sequencia[i];
  229.  
  230. }
  231. new_node->transicao_come_from=transicao_come_from;
  232. //printf("\ncriou no\n\n");
  233. }
  234. return new_node;
  235.  
  236. }
  237. int addNextTransition(node* father, int num_transitions, int num_places, int arrayTransitions[num_transitions][2][num_places]){
  238.  
  239. int i,j,k,p;
  240. int done=0;
  241. int sequencia_aux[6];
  242. int estado = get_estado(father, num_places);
  243.  
  244.  
  245. printf("\n");
  246. if(estado != -1){
  247. //printf("\nSEQUENCIA: ");
  248. for(i = 0; i<num_places;i++){
  249. sequencia_aux[i] = father->sequencia[i];
  250. //printf("%d ", sequencia_aux[i]);
  251.  
  252. }
  253. done = 1;
  254. // printf("\nestado: %d\n", estado+1);
  255. for(i=0; i<num_transitions; i++){
  256. //printf("\nTRANSICAO: %d\n", i+1);
  257. if(arrayTransitions[i][0][estado] ==1){
  258. node *new_node = create_node(-1,sequencia_aux, 0,num_places);
  259. for(j = 0; j<num_places; j++){
  260. if(arrayTransitions[i][0][j] == 1){
  261. if(sequencia_aux[j] == 0){
  262. break;
  263. }
  264. if(sequencia_aux[j]>0){
  265. sequencia_aux[j]-=1;
  266. }
  267. }
  268. }
  269.  
  270. for(j = 0; j<num_places; j++){
  271. if(arrayTransitions[i][1][j]==1){
  272. //printf("\n--->adiciona no estado: %d\n", j+1);
  273. sequencia_aux[j]+=1;
  274. new_node->estado[j]=1;
  275. }else{
  276. new_node->estado[j] = 0;
  277. //sequencia_aux[j] = 0;
  278. }
  279. }
  280. for(k=0; k<num_places; k++){
  281. new_node->sequencia[k] = sequencia_aux[k];
  282.  
  283. }
  284. new_node->transicao_come_from = i;
  285.  
  286.  
  287. add_son(father, new_node);
  288. break;
  289. }
  290.  
  291. }
  292. memset(sequencia_aux, 0, sizeof(sequencia_aux));
  293. for(p = 0; p<num_places;p++){
  294. sequencia_aux[p] = father->sequencia[p];
  295. }
  296. }else{
  297. return done;
  298. }
  299. return done;
  300.  
  301. }
  302.  
  303. int get_estado(node* father, int num_places){
  304. int i=0;
  305. for(i=0; i<num_places; i++){
  306. if(father->estado[i] != 0 && father->estado[i] != -1){
  307. //printf("\nFATHER ESTADO: %d\n", i+1);
  308. father->estado[i] = -1;
  309. return i;
  310. }
  311. }
  312. return-1;
  313. }
  314.  
  315.  
  316. void add_son(node* father,node* new_son){
  317. int i;
  318. // printf("\n---add_son\n");
  319.  
  320. //printf("aqui: %d\n",new_son->estado);
  321.  
  322. if(father->son!=NULL){
  323. node* aux;
  324. //aux= (node *)malloc(sizeof(node));
  325. aux=father->son;
  326. // printf("ja tem pelo menos 1 filho\n");
  327. while(aux->brother!=NULL){
  328. //erro AQUIIIIIIIIIIIIIIIIII
  329. //printf("tem filho\n");
  330. aux=aux->brother;
  331. //printf("continuacao\n");
  332. }
  333. //printf("adiciona novo filho\n");
  334. aux->brother=new_son;
  335. aux=aux->brother;
  336. // printf("estado: %d",aux->estado);
  337. //
  338. // printf("seq:\n");
  339. // for(i=0;i<4;i++){
  340. // printf("%d ",aux->sequencia[i]);
  341. // }
  342.  
  343. //printf("\nsai add\n");
  344. }else{
  345. //printf("primeiro filho\n");
  346. father->son=new_son;
  347. }
  348. }
  349.  
  350. void print(node* root,int places){
  351. node* aux=root;
  352. node* aux2=root;
  353. int i;
  354. while(aux->sequencia[0]!=NULL){
  355. for(i=0;i<places;i++){
  356. printf("%d ",aux->sequencia[i]);
  357.  
  358. }
  359.  
  360. printf("\n");
  361. printf("estado: %d\n\n",aux->estado);
  362. if(aux->brother!=NULL){
  363. printf("vai para irmao \n");
  364. aux=aux->brother;
  365. }else{
  366. if(aux2->son!=NULL){
  367. aux=aux2->son;
  368. aux2=aux2->son;
  369. }else{
  370. break;
  371. }
  372.  
  373. }
  374. }
  375. }
Add Comment
Please, Sign In to add comment