Advertisement
jamilton

Untitled

Oct 2nd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6.  
  7. int strtm_neg(char str[], int i)
  8. {
  9. int k=i-1, j=0;
  10. int f1;
  11. while(str[k]!='*'&& str[k]!='/'&& str[k]!='-' && str[k]!='+' && str[k]!='\0' && k>=0)
  12.     {  
  13.     j++;
  14.     k--;
  15.     }
  16. f1=j;
  17. return f1;
  18. }
  19.  
  20. int strtm_pos(char str[], int i)
  21. {
  22. int k=i+1, j=0;
  23. int f1;
  24. while(str[k]!='*'&& str[k]!='/'&& str[k]!='-' && str[k]!='+' && str[k]!='\0' && k<strlen(str))
  25.     {  
  26.     j++;
  27.     k++;
  28.     }
  29. f1=j;
  30. return f1;
  31. }
  32.  
  33. float conv_neg(char x[], int i){
  34. char *r1=NULL;
  35. int k=i-1, j=0, t1=0, z;
  36. float f1;
  37. while(x[k]!='*'&& x[k]!='/'&& x[k]!='-' && x[k]!='+' && x[k]!='\0' && k>=0)
  38.     {  
  39.     j++;
  40.     k--;
  41.     }
  42. t1 = j;
  43. r1 = (char*) calloc(t1,sizeof(char));
  44. for(z=0; z<j; z++)
  45.     {
  46.     r1[z]=x[i-j+z];    
  47.     }
  48. sscanf(r1, "%f", &f1);
  49. free(r1);
  50. return f1;
  51. }
  52.  
  53. float conv_pos(char x[], int i){
  54. char *r2=NULL;
  55. int k=i+1, j=0, t2=0, z;
  56. float f2;
  57. while(x[k]!='*'&& x[k]!='/'&& x[k]!='-' && x[k]!='+' && k<strlen(x) && x[k]!='\0')
  58.     {  
  59.     j++;
  60.     k++;
  61.     }
  62. t2 = j;
  63. r2 = (char*) calloc(t2,sizeof(char));
  64. for(z=0; z<j; z++)
  65.     {
  66.     r2[z]=x[i+z+1];    
  67.     }
  68. sscanf(r2, "%f", &f2);
  69. free(r2);
  70. return f2;
  71. }
  72.  
  73. void calcula_div(char op[]){
  74. int i, j;
  75. float x, y, z;
  76. char p[10]="", aux1[10]="", aux2[10]="";
  77. char st2[10];
  78. for(i=0; i<strlen(op); i++)
  79.     {
  80.     if(op[i]=='/')
  81.         {
  82.         y = conv_neg(op, i);
  83.         x = conv_pos(op, i);
  84.         z = y/x;
  85.         //essa linha de baixo tá possuída pelo capeta
  86.         sprintf(st2, "%f", x);  
  87.         sprintf(p, "%f", z);
  88.         int ty=strtm_neg(op, i);
  89.         int tx=strtm_pos(op, i);
  90.         if(ty+tx+1<strlen(op))
  91.         {
  92.             for(j=0; j<(i-ty); j++)
  93.                 {
  94.                 aux1[j]=op[j];
  95.                 }        
  96.             for(j=0; j<strlen(op); j++)
  97.                 {
  98.                 aux2[j]=op[(i+1+tx)+j];
  99.                 }
  100.         }
  101.         strcat(aux1, p);
  102.         strcat(aux1, aux2);
  103.         strcpy(op, aux1);
  104.         }
  105.     }
  106. }
  107.  
  108. void calcula_mult(char op[]){
  109. int i, j;
  110. float x, y, z;
  111. char p[100]="", aux1[100]="", aux2[100]="";
  112. for(i=0; i<strlen(op); i++)
  113.     {
  114.     if(op[i]=='*')
  115.         {
  116.         y = conv_neg(op, i);
  117.         x = conv_pos(op, i);
  118.         z = y*x;  
  119.         sprintf(p, "%f", z);
  120.         int ty=strtm_neg(op, i);
  121.         int tx=strtm_pos(op, i);
  122.         if(ty+tx+1<strlen(op))
  123.         {
  124.             for(j=0; j<(i-ty); j++)
  125.                 {
  126.                 aux1[j]=op[j];
  127.                 }        
  128.             for(j=0; j<strlen(op); j++)
  129.                 {
  130.                 aux2[j]=op[(i+1+tx)+j];
  131.                 }
  132.         }
  133.         strcat(aux1, p);
  134.         strcat(aux1, aux2);
  135.         strcpy(op, aux1);
  136.         }
  137.     }
  138. }
  139.  
  140. void calcula_sub(char op[]){
  141. int i, j;
  142. float x, y, z;
  143. char p[10]="", aux1[10]="", aux2[10]="";
  144. for(i=0; i<strlen(op); i++)
  145.     {
  146.     if(op[i]=='-' && i!=0)
  147.         {
  148.         y = conv_neg(op, i);
  149.         x = conv_pos(op, i);
  150.         z = y-x;
  151.         sprintf(p, "%f", z);
  152.         int ty=strtm_neg(op, i);
  153.         int tx=strtm_pos(op, i);
  154.         if(ty+tx+1<strlen(op))
  155.         {
  156.             for(j=0; j<(i-ty); j++)
  157.                 {
  158.                 aux1[j]=op[j];
  159.                 }        
  160.             for(j=0; j<strlen(op); j++)
  161.                 {
  162.                 aux2[j]=op[(i+1+tx)+j];
  163.                 }
  164.         }
  165.         strcat(aux1, p);
  166.         strcat(aux1, aux2);
  167.         strcpy(op, aux1);
  168.         }
  169.     }
  170. }
  171.  
  172. void calcula_add(char op[]){
  173. int i, j;
  174. float x, y, z;
  175. char p[100]="", aux1[100]="", aux2[100]="";
  176. for(i=0; i<strlen(op); i++)
  177.     {
  178.     if(op[i]=='+')
  179.         {
  180.         y = conv_neg(op, i);
  181.         x = conv_pos(op, i);
  182.         z = y+x;
  183.         sprintf(p, "%f", z);
  184.         int ty=strtm_neg(op, i);
  185.         int tx=strtm_pos(op, i);
  186.         for(j=0; j<(i-ty); j++)
  187.             {
  188.             aux1[j]=op[j];
  189.             }        
  190.         for(j=0; j<strlen(op); j++)
  191.             {
  192.             aux2[j]=op[(i+1+tx)+j];
  193.             }
  194.         strcat(aux1, p);
  195.         strcat(aux1, aux2);
  196.         strcpy(op, aux1);
  197.         }
  198.     }
  199. }
  200.  
  201. void calcula_tudo(char op[]){
  202. calcula_mult(op);
  203. calcula_div(op);
  204. calcula_add(op);
  205. calcula_sub(op);
  206. }
  207.  
  208. int main(){
  209. char operacoes[100][100] = {"1+2*3", "1-2*3/4", "1+1/2", "2/3+3*4"};
  210. char ret[100][100];
  211. char op[100];
  212. int i, b;
  213. float rft, teste;
  214. for(i=0; i<4; i++)
  215.     {
  216.     strcpy(op, operacoes[i]);
  217.     calcula_tudo(op);
  218.     strcpy(ret[i], op);
  219.     }
  220. for(i=0; i<4; i++)
  221.     {
  222.     printf("%s = %s\n", operacoes[i], ret[i]);
  223.     }
  224. return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement