Question: Task: We want to make a simple calculator that can add and subtract integers and will accept arbitrarily long mathematical formulas composed of symbols +,

Task:

We want to make a simple calculator that can add and subtract integers and will accept arbitrarily long mathematical formulas composed of symbols +, -, and non-negative integer numbers.

Imagine you have a file formula.txt with the summation formula such as:

100 + 50 - 25 + 0 + 123 - 1 

If you redirect the file into your program, it should compute and print the answer:

$ ./calc < formula.txt 247 

Specifically, write a program calc.cpp that reads from the cin a sequence of one or more non-negative integers written to be added or subtracted. Space characters can be anywhere in the input. After the input ends (end-of-file is reached), the program should compute and print the result of the input summation.

Possible input that has spaces and characters:

1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 

Possible output: 16

You can use >> operator to read the numbers and the +/- characters, because >> will be skipping all spaces between the input terms.

WHat would the program be here? How would i successfully read in the file and get the right output?

Thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!