[NOTE: Use only functions, no pointers for this problem] Write a program to mode
ID: 3614589 • Letter: #
Question
[NOTE: Use only functions, no pointers for this problem]Write a program to model a simple calculator. Each data line shouldconsist of the next operation to be performed from the list belowand the right operand. Assume the left operand is the accumulatorvalue (initial value of 0). You need a functionscan_data with two output parameters that returnsthe operator and right operand scanned from a data line. You need afunction do_next_op that performs the requiredoperation. do_next_op has two inputparameters (operator and operand) and one input/output parameter(the accumulator). The valid operators are:
+ add
- subtract
* multiply
/ divide
^ power (raise left operand to power ofright operand)
q quit
Your calculator should display the accumulator value after eachoperation
Sample run follows:
+ 5.0
result so far is 5.0
^ 2
result so far is 25.0
/ 2.0
result so far is 12.5
q 0
final result is 12.5
Explanation / Answer
please rate - thanks #include #include #include void do_next_op(double,char,double*); void scan_data(char*,double*); intmain() { char oper='-'; doublevar1=0,var2; scan_data(&oper,&var2); while(oper!='q') { do_next_op( var2,oper,&var1); printf("result so far %.2f ",var1); scan_data(&oper,&var2); } printf("final result %.2f ",var1); getch(); return0; } void scan_data(char* o,double* v) {char ch; scanf("%c %lf",o,v); while(getchar()!=' '); return; } void do_next_op(double var2,char oper,double* var1) {switch(oper) {case'+':*var1+=var2; break; case'-':*var1-=var2; break; case'*':*var1*=var2; break; case'/':if(var2==0) *var1=0; else *var1/=var2; break; case'^':if(var2==0) *var1=0; else *var1=pow(*var1,var2); break; } return; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.