Question
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
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
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