Write a program to model a simple calculator. Each data line should consist of t
ID: 3777722 • Letter: W
Question
Write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right operand. Assume the left operand is the accumulator value (initial value of 0). You need a function scan_data with two output parameters that returns the operator and right operand scanned from a data line. You need a function do_next_op that performs the required operation. do_next_op has two input parameters (the operator and operand) and one input/output parameter (the accumulator). The valid operators are: Your calculator should display the accumulator value after each operation. A sample run follows. + 5.0 result so far is 5.0A 2result so far is 25.0/2.0result so far is 12.5q 0final result is 12.5Explanation / Answer
#include<stdio.h>
void do_next_op(char,float,float);
float acc=0.0;
int main()
{
char ch;
float operand,result;
do
{
printf("Enter the operator and operand ");
scanf("%c %f",&ch,&operand);
if(ch=='0')
{
printf(" the final result is %f",acc);
exit(0);
}
do_next_op(ch,operand,acc);
}while(1);
}
void do_next_op(char ch,float op,float acc)
{
int i;
switch(ch)
{
case '+':acc=acc+op;
printf(" the result so far is %f",acc);
break;
case '-':acc=acc-op;
printf(" the result so far is %f",acc);
break;
case '/':acc=acc/op;
printf(" the result so far is %f",acc);
break;
case '*':acc=acc*op;
printf(" the result so far is %f",acc);
break;
case '^':for(i=0;i<op;i++)
acc=acc*acc;
printf(" the result so far is %f",acc);
break;
case 'q':printf(" the final result is %f",acc);
exit(0);
default: printf(" the final result is %f",acc);
exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.