WhaleSpunk

Untitled

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