Write a program calc.c that implements a simple arithmetic calculator. Input to
ID: 3557126 • Letter: W
Question
Write a program calc.c that implements a simple arithmetic calculator. Input to the calculator
consists of lines composed of integer constants separated by the five arithmetic operators used
in C: +, -, *, /, and %. For each line of input, your program should display the result of applying
the operators to the surrounding terms. To read the individual values and operators, you should
use the scanner module as extended in exercise 9, so that spacing is ignored. Your program
should exit when the user enters a blank line.
To reduce the complexity of the problem, your calculator should ignore C?s rules of precedence
and instead apply the operators in left-to-right order. Thus, in your calculator program, the
expression
2 + 3 * 4
has the value 20 and not 14, as it would in C.
The following is a sample run of the calc.c program:
This program implements a simple calculator. When the > prompt appears, enter an expression consisting of integer constants and the operators +, -, *, /, and %. To stop, enter a blank line.
> 2 + 2
4
> 1 + 2 + 3 + 4 + 5
15
> 10 % 4
2
> 4 + 9 - 2 * 16 + 1 / 3 * 6 - 67 + 8 * 2 - 3 + 26 - 1 / 34 + 3 / 7 + 2 - 5
0
>
Also include
#define TRUE 1
#define FALSE 0
----
Note: This is from an intro course to comp sci. We have covered only basic input/output, basic functions, introduction to strings, and switches. This is from a chapter on modular development and just introduced tokens and global variables, so please reflect in the answer.
Need this in a few hours :( Help would be greatly appreciated!!!
Explanation / Answer
#include #include #include char** split(const char *str, const char *delimiter){ char *text, *p, *first, **array; int c; char** ret; text=strdup(str); if(text==NULL) return NULL; for(c=0,p=text;NULL!=(p=strtok(p, delimiter));p=NULL, c++)//preprocess counting item if(c==0) first=p; //first item ret=(char**)malloc(sizeof(char*)*c+1);//+1 for NULL(put last) if(ret==NULL){ free(text); return NULL; } strcpy(text, str+(first-text));//again array=ret; for(p=text;NULL!=(p=strtok(p, delimiter));p=NULL){ *array++=p; } *array=NULL; return ret; } void free4split(char** sa){ char **array=sa; if(sa!=NULL){ free(array[0]);//free the string free(sa); //free array } } typedef struct token { char kind;//'+','-','*','/','N' and ' ' int value; } Token; Token token_manager(const char* str){ static char** tokens = NULL; static int pos = -1; Token token; if(str != NULL){ //call by "" rewind if(*str == ''){ --pos; token.kind = ' '; return token; } free4split(tokens); pos = -1; tokens = split(str, " "); token.kind = ' '; return token; } //str == NULL if(tokens == NULL){ token.kind = ' '; return token; } if(tokens[++pos]==NULL){ free4split(tokens); pos = -1; tokens = NULL; token.kind = ' '; return token; } if(0==strcmp(tokens[pos], "*")){ token.kind = '*'; return token; } if(0==strcmp(tokens[pos], "/")){ token.kind = '/'; return token; } if(0==strcmp(tokens[pos], "+")){ token.kind = '+'; return token; } if(0==strcmp(tokens[pos], "-")){ token.kind = '-'; return token; } token.kind = 'N';//need error check!!! token.value = atoi(tokens[pos]); return token; } int read_num(){ Token t; t = token_manager(NULL); if(t.kind == 'N'){ return t.value; } else { fprintf(stderr, "funny expression!"); exit(-1); } } int muldiv_exp(){ Token t; int left; left = read_num(); t = token_manager(NULL); for(;;){ switch(t.kind){ case '*': left *= read_num(); t = token_manager(NULL); break; case '/': left /= read_num();//need error check for divide 0 t = token_manager(NULL); break; default: token_manager("");//rewind return left; } } } int addsub_exp(){ Token t; int left; left = muldiv_exp(); t = token_manager(NULL); for(;;){ switch(t.kind){ case '+': left += muldiv_exp(); t = token_manager(NULL); break; case '-': left -= muldiv_exp(); t = token_manager(NULL); break; default: token_manager("");//rewind return left; } } } int calc(const char *str){ token_manager(str); return addsub_exp(); } // expression: // separate by space // number is signed int // operator '+', '-', '*' and '/' // ex.) // 5 + 4 - 7 - -10 // -5 + 5 - -7 // -5 + 5 * -7 // 3 / 2 + 4 * 5- -1 * -3 int main(){ const char *exp[] = { "5 + 4 - 7 - -10", "-5 + 5 - -7", "-5 + 5 * -7", "3 / 2 + 4 * 5 - -1 * -3" }; int i ; for(i=0;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.