Question: The program calc.c , which implements a simple 5 - function calculator, has a bug: a spurious invalid operator message is printed after each computation.
The program calc.c which implements a simple function calculator, has a bug: a spurious invalid operator message is printed after each computation. Find out why and fix it
Call your fixed program calc.c
CALC program to implement a simple arithmetic calculator
Usage: calc
Inputs from standard input
operator, one of
operands, both integers
Exit Code: if all is well, on error
Matt Bishop, ECS A
April original version
#include
#include
MACROS
These are to encode the operations
#define ERROR unknown operation
#define ADD addition operation
#define SUBTRACT subtraction operation
#define MULTIPLY multiplication operation
#define DIVIDE division operation
#define REMAINDER modulus operation
GETOP get an integer operand; we do operationspecific checks later
parameters: opno the number of the operand as in stnd etc.
returns: int the integer value of the operand just read
exceptions: none
int getopint opno
int val; value read
int rv; value returned from scanf
loop until you get an integer or EOF
do
prompt and read a value
printftoperand d: opno;
rv scanfd &val;
oops bogus value
if rv
printftoperand must be an integer
;
loop until a valid value
while rv ;
if it's EOF, say so and quit
if rv EOF
exitEXITSUCCESS;
otherwise, say what you read
returnval;
VALOP return value of operation
parameters: int a character representing the operator
returns: int operator code see above or ERROR
exceptions: none
int valopint op
switchop map character into code
case : returnADD;
case : returnSUBTRACT;
case : returnMULTIPLY;
case : returnDIVIDE;
case : returnREMAINDER;
unknown character balk
returnERROR;
ISDIVOP return if argument is a division operator
parameters: int a character representing the operator
returns: int if argument is or otherwise
exceptions: none
int isdivopint op
returnop DIVIDE op REMAINDER;
APPLY perform desired calculation
parameters: int first operand
int second operand
int operation
assume: if operation is a division one, the
second operand is nonzero
returns: int result
exceptions: none
int applyint op int op int op
int answer; the answer
do the operation or complain
switchop
case ADD: add them
answer op op;
break;
case SUBTRACT: subtract second from first
answer op op;
break;
case MULTIPLY: multiply them
answer op op;
break;
case DIVIDE: divide first by second
answer op op;
break;
case REMAINDER: remainder when first divided by second
answer op op;
break;
default: should never happen
printfInternal inconsistency: bad operator d in apply
op;
exitEXITFAILURE;
now return the answer
returnanswer;
int mainvoid
int ch; input character
int op; operation, derived from ch
int op op; operands
int result; result to be printed
prompt the user for an operation
printfoperation ;
loop until user says to quit
whilech getchar EOF
check for endoffile
if ch EOF
break;
convert the character read to an operator
if op valopch ERROR
printfinvalid operator c
ch;
printfoperation ;
consume the newline character
getchar;
continue;
get the operands
op getop;
op getop;
if division operation by complain
otherwise do the operation
if isdivopop && op
printfCant have a denominator of
;
else
result applyop op op;
printfd c d d
op ch op result;
prompt again
printfoperation ;
make it look nice
putchar
;
that's all, folks!
returnEXITSUCCESS;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
