Question: Write a program to model a simple calculator. Each line should consist of the next operation to be performed. The possible list of operations is
Write a program to model a simple calculator. Each line should consist of the next operation to be performed. The possible list of operations is given below:
+ add subtract multiply / divide power (e.g., 32 )
q quit
You will need to create two void functions to make this work. First, youll need to create scan_data with two output parameters: the operator (e.g., +), and the operand (i.e., some number). This will ask the user to enter both items on the same line. In other words, use a single scanf function.
The second function is do_next_op. This function will need two input parameters (the operator and operand) and one input/output parameter (the result), and the function will perform the actual operation. Note that there is a finite set of characters that the operator can be. Although there are several ways to determine which operator will be used based on the input, my recommendation is a switch. Each time you perform an operation that isnt q, the function should display the current result.
Your main should continuously call both functions until the operator is q. Note, when I wrote the solution, I encountered a problem with continuously calling scan_data, where the values were not being stored correctly. Including flushall() immediately after calling scan_data fixed the problem. When the user is done performing calculations (by entering q and any number), main should print the final result.
A sample run follows.
Enter an operation: + 5.0
The result is so far 5.000000.
Enter an operation: 2
The result is so far 25.000000.
Enter an operation: / 2
The result is so far 12.500000.
Enter an operation: q 0
The final result is 12.500000.
***C CODE, NOT C++***
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
