Last active
December 28, 2018 16:04
-
-
Save bruno-galdino/71f6f56cc459c8f57b4888c02d2bdd39 to your computer and use it in GitHub Desktop.
Revisions
-
Bruno Soares Galdino revised this gist
Dec 28, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,7 +62,7 @@ int *eval( char *begin, char *end ) switch(*operator ) { case '*': *result = *parcial1 * *parcial2; break; case '/': if(!*parcial2) exit(0); *result = *parcial1 / *parcial2; break;/*fecha o programa em divisão por zero*/ case '+': *result = *parcial1 + *parcial2; break; case '-': *result = *parcial1 - *parcial2; break; } @@ -80,7 +80,7 @@ int *eval( char *begin, char *end ) int main(int argc, char const *argv[]) { char exp[ ] = "40-3*6/0";/*não utilizar espaços em branco, a rotina não trata isso por enquanto*/ int *ret = eval( exp, exp + sizeof exp ); if( ret ) -
Bruno Soares Galdino revised this gist
Dec 28, 2018 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,10 @@ /* Bruno Soares Galdino avaliação de expressões aritméticas - em desenvolvimento */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> @@ -56,7 +62,7 @@ int *eval( char *begin, char *end ) switch(*operator ) { case '*': *result = *parcial1 * *parcial2; break; case '/': *result = if(!*parcial2) exit(0); *parcial1 / *parcial2; break;/*fecha o programa em divisão por zero*/ case '+': *result = *parcial1 + *parcial2; break; case '-': *result = *parcial1 - *parcial2; break; } @@ -74,7 +80,7 @@ int *eval( char *begin, char *end ) int main(int argc, char const *argv[]) { char exp[ ] = "40-3*6";/*não utilizar espaços em branco, a rotina não trata isso por enquanto*/ int *ret = eval( exp, exp + sizeof exp ); if( ret ) -
Bruno Soares Galdino created this gist
Dec 28, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,86 @@ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <ctype.h> bool isValue( const char *exp ) { while(isdigit(*exp++) ); return !*(exp-1); } char *findNextOperator( char *exp ) { char *aux = NULL; while( *exp ) { if( *exp == '+' || *exp == '-' ) { return exp; } else if( !aux && (*exp == '*' || *exp == '/') ) { aux = exp; } exp++; } return aux; } int *eval( char *begin, char *end ) { char temp = *end; *end = '\0'; int *result = NULL; int *parcial1,*parcial2; char *operator; if( isValue(begin) ) { result = malloc(sizeof(int) ); *result = atoi(begin); } else { operator = findNextOperator( begin ); if( operator ) { result = malloc(sizeof(int) ); parcial1 = eval( begin, operator); parcial2 = eval( operator + 1, end); switch(*operator ) { case '*': *result = *parcial1 * *parcial2; break; case '/': *result = *parcial1 / *parcial2; break;/*tratar divião por 0*/ case '+': *result = *parcial1 + *parcial2; break; case '-': *result = *parcial1 - *parcial2; break; } free( parcial1); free( parcial2); } } *end = temp; return result; } int main(int argc, char const *argv[]) { char exp[ ] = "40-3*6"; int *ret = eval( exp, exp + sizeof exp ); if( ret ) { printf("%d",*ret); } return 0; }