WhaleSpunk

Untitled

Apr 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int **array;
  6. void parse(char * equacao, int type, int index){
  7. //int i;
  8. char * i;
  9. char aux[20];
  10. char cToStr[2];
  11. cToStr[1] = '\0';
  12. char var[20];
  13. char operation[20];
  14. char res[20];
  15. memset(var, 0, sizeof(var));
  16. memset(aux, 0, sizeof(aux));
  17. memset(operation, 0, sizeof(aux));
  18. memset(res, 0, sizeof(aux));
  19.  
  20.  
  21. printf("equacao parse: %s\n",equacao);
  22. int flag=0;
  23. int flag2=0;
  24. printf("dentro\n\n");
  25. //while(strcmp(equacao[i],"\n"
  26.  
  27. /*for(i=equacao; *i;i++){
  28. printf("i: %c\n",*i);
  29.  
  30. }*/
  31. array[index] = calloc(47, sizeof(int));
  32.  
  33. for(i=equacao; *i; i++){
  34. printf("entrou for\n");
  35. if(*i=='x'){
  36.  
  37. flag=1;
  38. }else{
  39. printf("inside i:%c\n",*i);
  40. if(flag==1){
  41. //posso guardar aux
  42. //vou identificar o x
  43.  
  44. if(*i=='-'||*i=='+'){
  45. printf("entrou if\n");
  46. array[index][atoi(var)] = atoi(aux);
  47. printf("aux: %s \t-> X%s\n",aux,var);
  48. memset(var, 0, sizeof(var));
  49. memset(aux, 0, sizeof(aux));
  50. flag=0;
  51. cToStr[0] = *i;
  52. strcat(aux,cToStr);
  53. memset(cToStr, 0, sizeof(cToStr));
  54. cToStr[1] = '\0';
  55. }else{
  56.  
  57. if(*i == '<'||*i=='>'||*i=='='||flag2==1){
  58. flag2=1;
  59. if(operation[0] == '=' || operation[1]=='='){
  60.  
  61. cToStr[0] = *i;
  62. strcat(res,cToStr);
  63. memset(cToStr, 0, sizeof(cToStr));
  64. cToStr[1] = '\0';
  65.  
  66. }else{
  67. cToStr[0] = *i;
  68. strcat(operation,cToStr);
  69. memset(cToStr, 0, sizeof(cToStr));
  70. cToStr[1] = '\0';
  71. }
  72.  
  73.  
  74.  
  75. }else{
  76. cToStr[0] = *i;
  77. strcat(var,cToStr);
  78. memset(cToStr, 0, sizeof(cToStr));
  79. cToStr[1] = '\0';
  80. }
  81. }
  82.  
  83. }else{
  84.  
  85. cToStr[0] = *i;
  86. strcat(aux,cToStr);
  87. memset(cToStr, 0, sizeof(cToStr));
  88. cToStr[1] = '\0';
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96. } if(type == 1){
  97. array[index][atoi(var)] = atoi(aux);
  98.  
  99. if(strcmp(operation,"<=")==0){
  100. array[index][46] = 1;
  101.  
  102. }else if(strcmp(operation,"=")==0){
  103. array[index][46] = 2;
  104.  
  105. }else if(strcmp(operation,">=")==0){
  106. array[index][46] = 3;
  107.  
  108. }
  109.  
  110. array[index][47] = atoi(res);
  111. printf("aux: %s \t-> X%s \t -> %s \t ->%s\n",aux,var,operation, res);
  112.  
  113. }
  114. else{
  115. array[index][atoi(var)] = atoi(aux);
  116. printf("aux: %s \t-> X%s\n",aux,var);
  117. }
  118.  
  119. }
  120.  
  121. int main()
  122. {
  123. char word[50];
  124. char equacao[150];
  125. char st[5];
  126. char statement[150];
  127. int flag=0;
  128. int i=0, j=0,k=0;
  129. array = calloc(47, sizeof(int*));
  130. while(scanf("%s",&word)!= EOF){
  131.  
  132. if(strcmp(word,"maximize")==0){
  133. flag=1;
  134. }else{
  135. flag =0;
  136. }
  137.  
  138. scanf("%s",&equacao);
  139. printf("word %s\n",word);
  140. printf("equacao %s\n", equacao);
  141. parse(equacao, 0,i);
  142. i++;
  143. scanf("%s", &st);
  144. if(strcmp(st,"st")==0){
  145. printf("\n------------STS----------\n");
  146. scanf("%s",&statement);
  147.  
  148. while(strcmp(statement,"BINARY")!=0){
  149.  
  150. printf("->Statement: %s\n", statement);
  151. parse(statement,1,i);
  152. i++;
  153.  
  154.  
  155. scanf("%s",&statement);
  156. }
  157. }
  158.  
  159. /*if(flag == 1){
  160. maximize();
  161.  
  162. }*/
  163.  
  164.  
  165. }
  166. for(j=0; j<i; j++){
  167. for(k=0; k<=47; k++){
  168.  
  169. printf("%d ",array[j][k]);
  170.  
  171. }
  172. printf("\n");
  173.  
  174. }
  175. }
Add Comment
Please, Sign In to add comment