Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

$ ./calc2  

Task D. Calc3: Squares.

image text in transcribed

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:

$ ./calc3  

A 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 + 1   23 

(finalizing your input by pressing Enter and Ctrl+D).

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

Evaluating Group Performance?

Answered: 1 week ago