(Solved) : 1a Want Make Simple Calculator Add Subtract Integers Accept Arbitrarily Long Mathematical Q31043516 . . .
1a.
We want to make a simple calculator that can add and subtractintegers, and will accept arbitrarily long mathematical formulascomposed of symbols +, -, and non-negative integer numbers.
Imagine you have a file formula.txt with the summation formulasuch as:
100 + 50 – 25 + 0 + 123 – 1
If you redirect the file into your program, it should computeand print the answer:
$ ./calc < formula.txt247
It may sound tricky, but it is actually easy to write such aprogram, and you already know all the needed tools. Just thinkcarefully how to put it all together.
Specifically, write a program calc.cpp that reads from the cin asequence of one or more non-negative integers written to be addedor subtracted. Space characters can be anywhere in the input. Afterthe input ends (end-of-file is reached), the program should computeand print the result of the input summation.
Possible input for your program may look like this:
1510 + 3 + 0 + 25 5+6- 7 -8 + 9 + 10 – 11 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
(Each of the inputs above is a separate file containing onesingle formula, even if it spans multiple lines.)
The corresponding outputs should be: 15, 38, 4, and 16.
A hint on how to handle possible space characters in theinput:
You can use >> operator to read the numbers and the +/-characters, because >> will be skipping all spaces betweenthe input terms. It is also suggested to use the char type forreading the +/- operator characters, not string, because it willwork well even when numbers and the operator symbol are adjacentand not separated by spaces (such as in 10+5+3).
1b.
Calc2: Reading multiple formulas.
Write a better version of the calculator, calc2.cpp, that canevaluate multiple arithmetic expressions. Let’s use the semicolonsymbol that must be used at the end of each expression in theinput.
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 shouldevaluate all of the expressions and print them each on its ownline:
$ ./calc2 < formulas.txt15 384
Expert Answer
Answer to 1a Want Make Simple Calculator Add Subtract Integers Accept Arbitrarily Long Mathematical Q31043516 . . .
OR