C++ USE GETLINE. For this lab you are to use the STL stack which you can obtain
ID: 3919364 • Letter: C
Question
C++ USE GETLINE.
For this lab you are to use the STL stack which you can obtain by putting #include<stack> at the top of your program, and having the std namespace open, of course.
Write a program that evaluates an arithmetic expression in postfix (RPN) notation. The basic algorithm is to use a single stack of integers. If, when parsing the input you encounter a number you push the number onto the stack. If you encounter an operator (+, -, *, / are the only ones we are working with) you apply the operator to the top two elements on the stack. The result is then pushed back onto the stack. At the end of the whole operation there should be a single number on the stack which is a single number, the value of the expression.
In the files calc_useful.h and calc_useful.cc, I have given you a couple of simple functions, one which identifies if the char it receives is or is not an operator, and the other which evaluates two operands when given a character that is an operator. I have also written a main to help you with parsing the input.
The calculator should be embedded in a loop in the main that runs until the user chooses to quit. (Everything that you write for this assignment can be done in the main, just #include “calc_useful.h” and compile both .cc files.)
Use your calculator to evaluate:
12 679 5 + 4 * 8 / + (Answer = 354)
34 6 + 16 * 18 – (Answer = 622)
12 6 – 8 + 7 * + (Should produce an error)
56 78 84 + 33 * (Should produce an error)
Plus: Two problems that you make up on your own. These should each have a minimum of four operands.
Run a script of the above interaction including the two problems that you made up on your own.
calc_useful.h--------------------
calc_useful.cc------------------------
stackmain.cc----------------------------------------
Explanation / Answer
#include <stack>
#include <iostream>
#include "calc_useful.h"
using namespace std;
int main(){
// declare your stack here
stack<int> stk;
char c;
int st = 0;
int onenum, twonum;
do{
stk.empty();
st = 0;
cout<<"Please enter your expression: ";
c = cin.get();// priming read for the sentinel loop.
while(c != ' '){
if(isdigit(c)){
cin.putback(c);
cin>>onenum;
stk.push(onenum);
// stack operation here.
}
else if(isop(c)){
// if the stack is empty here you have an error.
if(stk.empty()){
cout<<" Wrong Expression !!! ";
st = 1;
break;
}
onenum = stk.top();
stk.pop();
if(stk.empty()){
cout<<" Wrong Expression !!! ";
st = 1;
break;
}
twonum = stk.top();
stk.pop();
onenum = evaluate(onenum,twonum,c);
stk.push(onenum);
// here is where you have to pop a couple of numbers,
// apply the operator to the numbers
// and then push the result back into the stack
}
c = cin.get(); // reading at the bottom of the sentinel loop
}
// this is where you get your final answer off the stack
// it should be the only number left on the stack at this point
if(stk.empty()){
cout<<" Wrong Expression !!! ";
st = 1;
}
> stk.pop();
if(!stk.empty()){
cout<<"Error. Insufficient operators for operands. ";
st = 1;
}
if(st == 0)
cout<<"The answer is: "<< onenum<<endl;
cout<<" Do you want to evaluate more expression ? (y/n) : ";
cin>>c;
cin.ignore();
}while(c == 'y' || c == 'Y');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.