Advertisement
Guest User

Untitled

a guest
Nov 27th, 2012
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. //#define DEBUG
  6.  
  7. int check(char *op1)
  8. {
  9.     int j,lenght;
  10.     lenght=strlen(op1);
  11.     for(j=0; j<lenght; j++)
  12.     {
  13.         if((op1[j]=='*')||(op1[j]=='/'))
  14.         {
  15.             return 1;
  16.         }
  17.     }
  18.     return 0;
  19. }
  20.  
  21. int check2(char *op1)
  22. {
  23.     int j,lenght;
  24.     lenght=strlen(op1);
  25.     for(j=0; j<lenght; j++)
  26.     {
  27.         if(op1[j]=='^')
  28.         {
  29.             return 1;
  30.         }
  31.     }
  32.     return 0;
  33. }
  34.  
  35. int verifier(char *op1)
  36. {
  37.     int lenght,i;
  38.     lenght=strlen(op1);
  39.  
  40.     for(i=0; i<lenght; i++)
  41.     {
  42.         if((op1[i]=='+')||(op1[i]=='-')||(op1[i]=='*')||(op1[i]=='/')||(op1[i]=='^'))
  43.             return 1;
  44.     }
  45.     return 0;
  46. }
  47.  
  48. char *funct1(char *op1)
  49. {
  50.     int lenght,i,j,k=0,stop=0,an;
  51.     double var_a=0,var_b=0,result=0;
  52.     char aux[100],aux2[100],result_string[500];
  53.  
  54.     lenght=strlen(op1);
  55.     aux2[0]=0;
  56.     strcpy(aux2,op1);
  57.  
  58.     for(i=0; i<lenght; i++)
  59.     {
  60.  
  61.         if(((op1[i]>='0')&&(op1[i]<='9'))||((op1[i]=='-')&&(i==0))||(op1[i]=='.'))
  62.         {
  63.             #ifdef DEBUG
  64.             printf("\nEste caracter es numero. ");
  65.             #endif
  66.             for(j=0; j<lenght; j++)
  67.             {
  68.                 if(((op1[i+j]>='0')&&(op1[i+j]<='9'))||((op1[i+j]=='-')&&(j==0))||(op1[i+j]=='.'))//falta algo aqui, checar.
  69.                 {
  70.                     #ifdef DEBUG
  71.                     printf("%c",op1[i+j]);
  72.                     #endif
  73.                     aux[j]=op1[i+j];
  74.  
  75.                 }
  76.                 else
  77.                 {
  78.                     aux[j]='\0';
  79.                     i=i+j-1;//evita que parsee de nuevo al tener ya numeros grandes.
  80.                     break;
  81.                 }
  82.             }
  83.             sscanf(aux,"%lf",&var_a);
  84.             #ifdef DEBUG
  85.             //printf("\nCadena (%s) valor de a %li\n",aux,var_a);
  86.             //printf("\nConvertido a entero, es %i",var_a);
  87.             #endif
  88.  
  89.         }
  90.  
  91.         else
  92.         {
  93.             aux[0]='\0';
  94.             k=i+1;
  95.             for(j=0; j+k<lenght; j++)
  96.             {
  97.                 if(((op1[k+j]>='0')&&(op1[k+j]<='9'))||op1[j+k]=='.'||(op1[k]=='-'))
  98.                 {
  99.                     #ifdef DEBUG
  100.                     printf("%c",op1[i+j]);
  101.                     #endif
  102.                     aux[j]=op1[k+j];
  103.                 }
  104.                 else
  105.                 {
  106.                     an=k+j;
  107.                     break;
  108.                 }
  109.             }
  110.             aux[j]='\0';
  111.             sscanf(aux,"%lf",&var_b);
  112.             #ifdef DEBUG
  113.             printf("\nCadena(%s) valor de b %li\n",aux,var_b);
  114.             //ASINGNAR!!
  115.             printf("\nEl caracter %c no es numero. ",op1[i]);//Guarda los valores a usar aca
  116.             #endif
  117.  
  118.             if(op1[i]=='+')
  119.             {
  120.                 if (check2(op1))
  121.                 {
  122.                     stop=i;
  123.                     continue;
  124.                 }
  125.                 if (check(op1))
  126.                 {
  127.                     stop=i;
  128.                     continue;
  129.                 }
  130.                 result=var_a+var_b;
  131.                 #ifdef DEBUG
  132.                 printf("\nEl resultado es %li + %li = %li",var_a,var_b,result);
  133.                 #endif
  134.             }
  135.  
  136.             if(op1[i]=='-')
  137.             {
  138.                 if (check2(op1))
  139.                 {
  140.                     stop=i;
  141.                     continue;
  142.                 }
  143.  
  144.                 if (check(op1))
  145.                 {
  146.                     stop=i;
  147.                     continue;
  148.                 }
  149.  
  150.                 result=var_a-var_b;
  151.                 #ifdef DEBUG
  152.                 printf("\nEl resultado es %li - %li = %li",var_a,var_b,result);
  153.                 #endif
  154.             }
  155.  
  156.             if(op1[i]=='*')
  157.             {
  158.                 if (check2(op1))
  159.                 {
  160.                     stop=i;
  161.                     continue;
  162.                 }
  163.                 result=var_a*var_b;
  164.                 #ifdef DEBUG
  165.                 printf("\nEl resultado es %li * %li = %li",var_a,var_b,result);
  166.                 #endif
  167.             }
  168.  
  169.             if(op1[i]=='/')
  170.             {
  171.                 if (check2(op1))
  172.                 {
  173.                     stop=i;
  174.                     continue;
  175.                 }
  176.                 result=var_a/var_b;
  177.                 #ifdef DEBUG
  178.                 printf("\nEl resultado es %li / %li = %li",var_a,var_b,result);
  179.                 #endif
  180.             }
  181.             if(op1[i]=='^')
  182.             {
  183.                 result=pow(var_a,var_b);
  184.             }
  185.             i=i+j+1;
  186.             break;
  187.         }
  188.     }
  189.     if(stop!=0)
  190.         aux2[stop+1]='\0';
  191.     else
  192.         aux2[0]='\0';
  193.     #ifdef DEBUG
  194.     printf("\nLa cadena auxiliar es %s\n",aux2);
  195.     #endif
  196.     sprintf(result_string,"%s%.2lf",aux2,result);
  197.     k=strlen(result_string);
  198.  
  199.     for(j=0; j+i<lenght; j++)
  200.     {
  201.         result_string[j+k]=op1[an+j];
  202.     }
  203.     result_string[j+k]='\0';
  204.     #ifdef DEBUG
  205.     printf("\n1 La cadena resultante es %s",result_string);
  206.     #endif
  207.  
  208.     return result_string;
  209. }
  210.  
  211. int checkneg(char *op)
  212. {
  213.     int i,x;
  214.     x=strlen(op);
  215.     for(i=1; i<x; i++)
  216.     {
  217.         if((op[i]=='+')||(op[i]=='-')||(op[i]=='*')||(op[i]=='/')||(op[i]=='^'))
  218.             return 0;
  219.     }
  220.     return 1;
  221. }
  222.  
  223. char solve(char *op)
  224. {
  225.     int x;
  226.     double result;
  227.     char *res_string;
  228.  
  229.     x=checkneg(op);
  230.     if((op[0]=='-')&&(x==1))
  231.     {
  232. #ifdef DEBUG
  233.         printf("\n**La cadena resultante es %s",op);
  234. #endif
  235.     }
  236.     else
  237.     {
  238.         while(verifier(op))
  239.         {
  240.             res_string=funct1(op);
  241.             strcpy(op,res_string);
  242. #ifdef DEBUG
  243.             printf("\n**1La cadena resultante es %s",op);
  244. #endif
  245.             if(checkneg(op))
  246.                 break;
  247.         }
  248.     }
  249.  
  250.     sscanf(res_string,"%lf",&result);
  251.     return result;
  252. }
  253.  
  254. int main()
  255. {
  256.     int choice;
  257.     char op[500];
  258. #ifdef DEBUG
  259.     printf("Modo de debugeo\n");
  260. #endif
  261.     do
  262.     {
  263.         system("cls");
  264.         printf("*******Arithcalc 1.0*******\n");
  265.         printf("Porfavor inserta la expresion aritmetica a resolver\n");
  266.         fflush(stdin);
  267.         gets(op);
  268.         solve(op);
  269. #ifdef DEBUG
  270.         printf("\nValidacion en cadena: %s",op);
  271. #endif
  272.         printf("\nResultado: %s\n",op);
  273.         printf("\nDeseas hacer otra operacion?\n1) Si 2) No\n");
  274.         scanf("%d",&choice);
  275.     }
  276.     while(choice==1);
  277.     system("cls");
  278.     return 0;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement