Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task 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^

Task 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:

$ ./calc3 < formulas.txt 25 1012

example 2)

On input: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1; 1^ + 1^ + 1^ + 1^ + 1^ + 1^ + 1^ + 1^ + 1^ + 1^ + 1^ + 2^;

Expected:

16

15

A hint:

To take into account ^, don't 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.

ALSO DONT HAVE THE CODE READ IN ANY FILE PLEASE! THE SYSTEM WILL INOUT NUMBERS ON ITS OWN.

I dont need a code like this one that was given before when I asked the uestion and that I asked to be corrected and wasn't, don't read in a file please>

#include #include using namespace std; int main(int argc, char** argv) {

char old_opr='+',new_opr; int number,ans=0; ifstream file; file.open(argv[1]); while(file>>number) { file>>new_opr; if(new_opr=='^') { number*=number; file>>new_opr; } if(old_opr=='+') { ans+=number; } else if(old_opr=='-') { ans-=number; } if(new_opr==';') { cout<

/* output

formula.txt file

5^;

1000+6^-5^+1;

after running

./a.out formula.txt

25 1012

*/

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

What is the gain or loss on this transaction?

Answered: 1 week ago

Question

Supportive frame work for the sell ?

Answered: 1 week ago

Question

It is made up DNA threads?

Answered: 1 week ago

Question

It initiates and regulates cell division?

Answered: 1 week ago

Question

It contains chromatin fibres?

Answered: 1 week ago