This is a c++ program. Please follow all directions The postfix calculator. This
ID: 654023 • Letter: T
Question
This is a c++ program. Please follow all directions
The postfix calculator. This is our second complex project and you can use functions you developed for the little calculator: skipBlanks, getNumber and doOperation. Use a string to hold a line of input. Use a special array to hold ints (often called stack). You may get input from a file or the console.
Your processor does int arithmetic using the usual operations in the fashion of an hp calculator, sometimes called Polish postfix notation. It should be able to handle an entire line of input, blank separators between int values and operators are allowed but not required. For each line of input, generate the postfix evaluation, or an error. Be sure to show all error-handling.
Example 1: 20 20 * 10 10 + -
For example 1, the answer is 380.
Example 2: 10 10 ++
For example 2, the processor outputs ERROR: too many operators.
Example 3: 10 10 10 & *
For example 3, the processor outputs illegal character.
Example 4: 10 10
For example 4, output is missing operator, or too many operands.
Example 5: 20 0 /
For example 5, output is division by zero error.
Explanation / Answer
using namespace std;
int main(int argc, char** argv) {
HPStack stack;
string line;
while (getline(cin, line)) {
stringstream expression(line);
string token;
while (expression >> token) {
if (isdigit(token[0])) {
stack.push(atof(token.data()));
//From here I am having trouble, I don't know what the code is.
} else if (token == "+") { // Addition code
} else if (token == "-") { // Subtraction code
} else if (token == "/") { // Division code
} else if (token == "*") { // Multiplication code
double x = stack.pop();
double y = stack.pop();
stack.push(y + x);
}
}
cout << stack.peek();
}
return 0;
}
Below is the code so far for the stack that I've created:
#include "HPStack.h"
HPStack::HPStack() {
}
HPStack::HPStack(const HPStack& orig) {
}
HPStack::~HPStack() {
}
Below is my code for the header file:
/*
* File: HPStack.h
* Author: Brenton
*
* Created on 20 September 2013, 12:10 AM
*/
#ifndef HPSTACK_H
#define HPSTACK_H
class HPStack {
public:
HPStack();
void push(double);
double pop();
private:
double stack;
double x, y, z, t;
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.