Advertisement
Guest User

Untitled

a guest
Nov 21st, 2012
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int check(char *op1)
  6. {
  7.     int j,lenght;
  8.     lenght=strlen(op1);
  9.     for(j=0; j<lenght; j++)
  10.     {
  11.         if((op1[j]=='*')||(op1[j]=='/'))
  12.         {
  13.             return 1;
  14.         }
  15.     }
  16.     return 0;
  17. }
  18.  
  19. int verifier(char *op1)
  20. {
  21.     int lenght,i;
  22.     lenght=strlen(op1);
  23.  
  24.     for(i=0; i<lenght; i++)
  25.     {
  26.         if((op1[i]=='+')||(op1[i]=='-')||(op1[i]=='*')||(op1[i]=='/'))
  27.             //  if(op1[0]!='-')//Taponñ
  28.             return 1;
  29.     }
  30.     return 0;
  31. }
  32.  
  33. char *funct1(char *op1)
  34. {
  35.     int lenght,i,j,k=0,stop=0;
  36.     double var_a=0,var_b=0,result=0;
  37.     char aux[100],aux2[100],result_string[500];
  38.  
  39.     lenght=strlen(op1);
  40.     aux2[0]=0;
  41.     strcpy(aux2,op1);
  42.  
  43.     for(i=0; i<lenght; i++)
  44.     {
  45.         //aux2[i]=op1[i];
  46.         //aux2[i+1]='\0';
  47.  
  48.         if(((op1[i]>='0')&&(op1[i]<='9'))||((op1[i]=='-')&&(i==0))||(op1[j]=='.'))
  49.         {
  50.             /*printf("\nEste caracter es numero. ");*/
  51.             for(j=0; j<lenght; j++)
  52.             {
  53.                 if(((op1[i+j]>='0')&&(op1[i+j]<='9'))||((op1[j]=='-')&&(j==0))||(op1[j]=='.'))//falta algo aqui, checar.
  54.                 {
  55.                     /*printf("%c",op1[i+j]);*/
  56.                     aux[j]=op1[i+j];
  57.                     //if(op1[0]=='-')//aqui
  58.                     //aux[0]='-';//aqui
  59.                 }
  60.                 else
  61.                 {
  62.                     aux[j]='\0';
  63.                     i=i+j-1;//evita que parsee de nuevo al tener ya numeros grandes.
  64.                     break;
  65.                 }
  66.             }
  67.             sscanf(aux,"%lf",&var_a);
  68.             //printf("\nCadena (%s) valor de a %li\n",aux,var_a);
  69.             //printf("\nConvertido a entero, es %i",var_a);
  70.  
  71.         }
  72.  
  73.         else
  74.         {
  75.             aux[0]='\0';
  76.             k=i+1;
  77.             for(j=0; j+k<lenght; j++)
  78.             {
  79.                 if(((op1[k+j]>='0')&&(op1[k+j]<='9'))||op1[j+k]=='.')
  80.                 {
  81.                     /*printf("%c",op1[i+j]);*/
  82.                     aux[j]=op1[k+j];
  83.                 }
  84.                 else
  85.                 {
  86.  
  87.                     //i=i+j-1;//evita que parsee de nuevo al
  88.                     //tener ya numeros grandes.
  89.                     break;
  90.                 }
  91.             }
  92.             aux[j]='\0';
  93.             sscanf(aux,"%lf",&var_b);
  94.             //printf("\nCadena(%s) valor de b %li\n",aux,var_b);
  95.             //ASINGNAR!!
  96.             /*printf("\nEl caracter %c no es numero. ",op1[i]);*/
  97.             //Guarda los valores a usar aca
  98.  
  99.             if(op1[i]=='+')
  100.             {
  101.                 //stop=i;
  102.                 if (check(op1))
  103.                 {
  104.                     stop=i;
  105.                     continue;
  106.                 }
  107.                 result=var_a+var_b;
  108.                 //printf("\nEl resultado es %li + %li = %li",var_a,var_b,result);
  109.             }
  110.  
  111.  
  112.             if(op1[i]=='-')
  113.             {
  114.  
  115.                 if (check(op1))
  116.                 {
  117.                     stop=i;
  118.                     continue;
  119.                 }
  120.  
  121.                 result=var_a-var_b;
  122.                 //printf("\nEl resultado es %li - %li = %li",var_a,var_b,result);
  123.             }
  124.  
  125.             if(op1[i]=='*')
  126.             {
  127.                 result=var_a*var_b;
  128.                 //printf("\nEl resultado es %li * %li = %li",var_a,var_b,result);
  129.             }
  130.  
  131.             if(op1[i]=='/')
  132.             {
  133.                 result=var_a/var_b;
  134.                 //printf("\nEl resultado es %li / %li = %li",var_a,var_b,result);
  135.             }
  136.             i=i+j+1;
  137.             break;
  138.  
  139.         }
  140.  
  141.  
  142.     }
  143.     if(stop!=0)
  144.         aux2[stop+1]='\0';
  145.     else
  146.         aux2[0]='\0';
  147.     //printf("\nLa cadena auxiliar es %s\n",aux2);
  148.     sprintf(result_string,"%s%.2lf",aux2,result);
  149.     k=strlen(result_string);
  150.  
  151.     for(j=0; j+i<lenght; j++)
  152.     {
  153.         result_string[j+k]=op1[i+j];
  154.     }
  155.     result_string[j+k]='\0';
  156.     //printf("\n1 La cadena resultante es %s",result_string);
  157.  
  158.     return result_string;
  159. }
  160.  
  161. int checkneg(char *op)
  162. {
  163.     int i,x;
  164.     x=strlen(op);
  165.     for(i=1; i<x; i++)
  166.     {
  167.         if((op[i]=='+')||(op[i]=='-')||(op[i]=='*')||(op[i]=='/'))
  168.             return 0;
  169.     }
  170.     return 1;
  171. }
  172.  
  173. char solve(char *op)
  174. {
  175.     int x;
  176.     double result;
  177.     char *res_string;
  178.  
  179.     x=checkneg(op);
  180.     if((op[0]=='-')&&(x==1))
  181.     {
  182.         //printf("**La cadena resultante es %s",op);
  183.     }
  184.     else
  185.     {
  186.         while(verifier(op))
  187.         {
  188.             res_string=funct1(op);
  189.             strcpy(op,res_string);
  190.             if(checkneg(op))
  191.               break;
  192.         }
  193.     }
  194.  
  195.     sscanf(res_string,"%lf",&result);
  196.     return result;
  197. }
  198.  
  199. int main()
  200. {
  201.     char op[500];
  202.  
  203.     fflush(stdin);
  204.     scanf("%s",op);
  205.     solve(op);
  206.     printf("\nValidacion en cadena: %s",op);
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement