In c++, I need to write reverse postfix calculator using a stack. Postfix notati
ID: 3778251 • Letter: I
Question
In c++, I need to write reverse postfix calculator using a stack. Postfix notation, and is parenthesis-free as long as operator arities are fixed. Here is the program logic: read in a string while the string is not “stop” if the string is +, pop the last 2 values from the stack and push back their sum else if the string is *,pop the last 2 values from the stack and push back their product else if the string is -, pop the last 2 values from the stack and push back the second – the first else if the string is /, pop the last 2 values from the stack and push back the second / the first else if string is =, print the top of the stack and pop the stack else ( //the string is a number), convert to a double and push it on the stack; read the next string Here is some example input and output:
type in a postFix express or stop to stop 1 3 + = 4.00000 10 5 / = 2.00000 10 6 2 + 3 - / = 2.00000 1.1 2.2 * = 2.42000 stop Here are some extra hints: To convert a string word into a double use double num = atof(word.c_str()); For nicer output include <iomanip> And in the string put fixed<<showpoint<<setprecision(5)
In c++, I need to write reverse postfix calculator using a stack. Postfix notation, and is parenthesis-free as long as operator arities are fixed. Here is the program logic: read in a string while the string is not “stop” if the string is +, pop the last 2 values from the stack and push back their sum else if the string is *,pop the last 2 values from the stack and push back their product else if the string is -, pop the last 2 values from the stack and push back the second – the first else if the string is /, pop the last 2 values from the stack and push back the second / the first else if string is =, print the top of the stack and pop the stack else ( //the string is a number), convert to a double and push it on the stack; read the next string Here is some example input and output:
type in a postFix express or stop to stop 1 3 + = 4.00000 10 5 / = 2.00000 10 6 2 + 3 - / = 2.00000 1.1 2.2 * = 2.42000 stop Here are some extra hints: To convert a string word into a double use double num = atof(word.c_str()); For nicer output include <iomanip> And in the string put fixed<<showpoint<<setprecision(5)
In c++, I need to write reverse postfix calculator using a stack. Postfix notation, and is parenthesis-free as long as operator arities are fixed. Here is the program logic: read in a string while the string is not “stop” if the string is +, pop the last 2 values from the stack and push back their sum else if the string is *,pop the last 2 values from the stack and push back their product else if the string is -, pop the last 2 values from the stack and push back the second – the first else if the string is /, pop the last 2 values from the stack and push back the second / the first else if string is =, print the top of the stack and pop the stack else ( //the string is a number), convert to a double and push it on the stack; read the next string Here is some example input and output:
type in a postFix express or stop to stop 1 3 + = 4.00000 10 5 / = 2.00000 10 6 2 + 3 - / = 2.00000 1.1 2.2 * = 2.42000 stop Here are some extra hints: To convert a string word into a double use double num = atof(word.c_str()); For nicer output include <iomanip> And in the string put fixed<<showpoint<<setprecision(5)
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib>
using namespace std;
int main() {
int continue_input = 1;
string str;
double op1, op2, store;
stack<double> s;
while(continue_input) {
cin >> str;
if(str.compare("stop") == 0) {
continue_input = 0;
continue;
} else if(str.compare("+") == 0) {
op1 = s.pop();
op2 = s.pop();
s.push(op1 + op2);
} else if(str.compare("*") == 0) {
op1 = s.pop();
op2 = s.pop();
s.push(op1 * op2);
} else if(str.compare("-") == 0) {
op1 = s.pop();
op2 = s.pop();
s.push(op2 - op1);
} else if(str.compare("/") == 0) {
op1 = s.pop();
op2 = s.pop();
s.push(op2 / op1);
} else if(str.compare("=") == 0) {
cout << fixed << showpoinnt << setprecision(5) << s.pop();
} else {
s.push(atof(str));
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.