Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- //#define DEBUG
- int check(char *op1)
- {
- int j,lenght;
- lenght=strlen(op1);
- for(j=0; j<lenght; j++)
- {
- if((op1[j]=='*')||(op1[j]=='/'))
- {
- return 1;
- }
- }
- return 0;
- }
- int check2(char *op1)
- {
- int j,lenght;
- lenght=strlen(op1);
- for(j=0; j<lenght; j++)
- {
- if(op1[j]=='^')
- {
- return 1;
- }
- }
- return 0;
- }
- int verifier(char *op1)
- {
- int lenght,i;
- lenght=strlen(op1);
- for(i=0; i<lenght; i++)
- {
- if((op1[i]=='+')||(op1[i]=='-')||(op1[i]=='*')||(op1[i]=='/')||(op1[i]=='^'))
- return 1;
- }
- return 0;
- }
- char *funct1(char *op1)
- {
- int lenght,i,j,k=0,stop=0,an;
- double var_a=0,var_b=0,result=0;
- char aux[100],aux2[100],result_string[500];
- lenght=strlen(op1);
- aux2[0]=0;
- strcpy(aux2,op1);
- for(i=0; i<lenght; i++)
- {
- if(((op1[i]>='0')&&(op1[i]<='9'))||((op1[i]=='-')&&(i==0))||(op1[i]=='.'))
- {
- #ifdef DEBUG
- printf("\nEste caracter es numero. ");
- #endif
- for(j=0; j<lenght; j++)
- {
- if(((op1[i+j]>='0')&&(op1[i+j]<='9'))||((op1[i+j]=='-')&&(j==0))||(op1[i+j]=='.'))//falta algo aqui, checar.
- {
- #ifdef DEBUG
- printf("%c",op1[i+j]);
- #endif
- aux[j]=op1[i+j];
- }
- else
- {
- aux[j]='\0';
- i=i+j-1;//evita que parsee de nuevo al tener ya numeros grandes.
- break;
- }
- }
- sscanf(aux,"%lf",&var_a);
- #ifdef DEBUG
- //printf("\nCadena (%s) valor de a %li\n",aux,var_a);
- //printf("\nConvertido a entero, es %i",var_a);
- #endif
- }
- else
- {
- aux[0]='\0';
- k=i+1;
- for(j=0; j+k<lenght; j++)
- {
- if(((op1[k+j]>='0')&&(op1[k+j]<='9'))||op1[j+k]=='.'||(op1[k]=='-'))
- {
- #ifdef DEBUG
- printf("%c",op1[i+j]);
- #endif
- aux[j]=op1[k+j];
- }
- else
- {
- an=k+j;
- break;
- }
- }
- aux[j]='\0';
- sscanf(aux,"%lf",&var_b);
- #ifdef DEBUG
- printf("\nCadena(%s) valor de b %li\n",aux,var_b);
- //ASINGNAR!!
- printf("\nEl caracter %c no es numero. ",op1[i]);//Guarda los valores a usar aca
- #endif
- if(op1[i]=='+')
- {
- if (check2(op1))
- {
- stop=i;
- continue;
- }
- if (check(op1))
- {
- stop=i;
- continue;
- }
- result=var_a+var_b;
- #ifdef DEBUG
- printf("\nEl resultado es %li + %li = %li",var_a,var_b,result);
- #endif
- }
- if(op1[i]=='-')
- {
- if (check2(op1))
- {
- stop=i;
- continue;
- }
- if (check(op1))
- {
- stop=i;
- continue;
- }
- result=var_a-var_b;
- #ifdef DEBUG
- printf("\nEl resultado es %li - %li = %li",var_a,var_b,result);
- #endif
- }
- if(op1[i]=='*')
- {
- if (check2(op1))
- {
- stop=i;
- continue;
- }
- result=var_a*var_b;
- #ifdef DEBUG
- printf("\nEl resultado es %li * %li = %li",var_a,var_b,result);
- #endif
- }
- if(op1[i]=='/')
- {
- if (check2(op1))
- {
- stop=i;
- continue;
- }
- result=var_a/var_b;
- #ifdef DEBUG
- printf("\nEl resultado es %li / %li = %li",var_a,var_b,result);
- #endif
- }
- if(op1[i]=='^')
- {
- result=pow(var_a,var_b);
- }
- i=i+j+1;
- break;
- }
- }
- if(stop!=0)
- aux2[stop+1]='\0';
- else
- aux2[0]='\0';
- #ifdef DEBUG
- printf("\nLa cadena auxiliar es %s\n",aux2);
- #endif
- sprintf(result_string,"%s%.2lf",aux2,result);
- k=strlen(result_string);
- for(j=0; j+i<lenght; j++)
- {
- result_string[j+k]=op1[an+j];
- }
- result_string[j+k]='\0';
- #ifdef DEBUG
- printf("\n1 La cadena resultante es %s",result_string);
- #endif
- return result_string;
- }
- int checkneg(char *op)
- {
- int i,x;
- x=strlen(op);
- for(i=1; i<x; i++)
- {
- if((op[i]=='+')||(op[i]=='-')||(op[i]=='*')||(op[i]=='/')||(op[i]=='^'))
- return 0;
- }
- return 1;
- }
- char solve(char *op)
- {
- int x;
- double result;
- char *res_string;
- x=checkneg(op);
- if((op[0]=='-')&&(x==1))
- {
- #ifdef DEBUG
- printf("\n**La cadena resultante es %s",op);
- #endif
- }
- else
- {
- while(verifier(op))
- {
- res_string=funct1(op);
- strcpy(op,res_string);
- #ifdef DEBUG
- printf("\n**1La cadena resultante es %s",op);
- #endif
- if(checkneg(op))
- break;
- }
- }
- sscanf(res_string,"%lf",&result);
- return result;
- }
- int main()
- {
- int choice;
- char op[500];
- #ifdef DEBUG
- printf("Modo de debugeo\n");
- #endif
- do
- {
- system("cls");
- printf("*******Arithcalc 1.0*******\n");
- printf("Porfavor inserta la expresion aritmetica a resolver\n");
- fflush(stdin);
- gets(op);
- solve(op);
- #ifdef DEBUG
- printf("\nValidacion en cadena: %s",op);
- #endif
- printf("\nResultado: %s\n",op);
- printf("\nDeseas hacer otra operacion?\n1) Si 2) No\n");
- scanf("%d",&choice);
- }
- while(choice==1);
- system("cls");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement