Question: Help with C++ Programming: Here's the question: Write a better version of the calculator, calc2.cpp, that can evaluate multiple arithmetic expressions. Lets use the semicolon
Help with C++ Programming:
Here's the question:
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 < formulas.txt 15 38 4
This question is referring to an earlier program which was programmed already, here's the code for that program
#include
#include
using namespace std;
int main(){
char c; //Declaring ints/chars int sums = 0; char previousOp = '+'; int previousNum = 0; int finalNum = 0;
while (cin >>c){ //Using while loop with user input
if(c == '+' || c == '-'){
if(previousOp == '+'){
sums += finalNum;
previousNum = -1; finalNum = 0;
}
else if(previousOp == '-'){ //Satisfying an elif loop
sums -= finalNum;
previousNum = -1;
finalNum = 0;
}
if(c == '+'){
previousOp = '+';
} else if(c == '-'){ //Satisfying second part of if loop with elif.
previousOp = '-';
}
}
else{ //Else loop to satisfy other conditions.
int num = c - '0';
if(previousNum == -1){
finalNum = num;
previousNum = num;
}
else{ //Else loop to satisfy other conditions.
finalNum = finalNum*10 + num;
previousNum = num;
}
}
}
if(previousOp == '+'){
sums += finalNum;
} else if(previousOp == '-'){
sums -= finalNum;
}
cout< return 0; } Any help would be greatly appreciated, thank you very much.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
