Question
Task C. Calc2: Reading multiple formulas. Write a better version of the calculator, calc2.cpp, that can evaluate multiple arithmetic expressions. Lets use the semicolon symbol
Task C. Calc2: Reading multiple formulas.
Write a better version of the calculator, calc2.cpp, that can evaluate multiple arithmetic expressions. Lets use the semicolon symbol that must be used at the end of each expression in the input.
Assuming that the input file formulas.txt looks as follows:
15 ; 10 + 3 + 0 + 25 ; 5 + 6 - 7 - 8 + 9 + 10 - 11 ;
When we run the program with that input, the output should evaluate all of the expressions and print them each on its own line:
$ ./calc2Task D. Calc3: Squares.
Write an even better calculator program calc3.cpp that can understand squared numbers. We are going to use a simplified notation X^ to mean X2. For example, 10^ + 7 - 51^ should mean 102 + 7 512.
Example:
When reading input file formulas.txt
5^; 1000 + 6^ - 5^ + 1;the program should report:
$ ./calc3A hint:
To take into account ^, dont add or subtract new numbers right away after reading them. Instead, remember the number, read the next operator and if it is a ^, square the remembered number, then add or subtract it.
An additional note on how to test calculator programs
In addition to writing your formulas into files, remember that your program still accepts the input from the keyboard (Hey, do you see the benefit of input redirection? The program can work great on both keyboard and file inputs!)
When typing the input from the keyboard, the key combination Ctrl+D emulates the End-of-filesituation, telling the program that the input has ended.
So, you can test your program like this:
$ ./calc 15 - 4 + 13 - 2 + 123 (finalizing your input by pressing Enter and Ctrl+D).
Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the units cm, m, and in are supported
#include#include using namespace std; int main() { bool done = false; string unit1 = ""; string unit2 = ""; double factor1 = 0; // conversion factor from first unit to cm double factor2 = 0; // conversion factor from cm to second unit while (!done) { bool again = false; // true to repeat the same conversion cout > command; if (command == "in") { factor1 = 2.54; unit1 = command; } else if (command == "cm") { . . . } else if (command == "m") { . . . } else if (command == "again") { again = true; } else if (command == "quit") { done = true; } else { cout > unit2; if (unit2 == "in") { factor2 = 1.0 / 2.54; } else if (unit2 == "m") { . . . } else if (. . .) { } else { cout > value; // Convert and print result cout
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started