Main:
#include <iostream>
#include "Expression.h"
#include "NumberExpression.h"
#include "UnaryExpression.h"
#include <stack>
#include <sstream>
using namespace std;
int main()
{
stack<Expression> s;
string word = "";
while (!cin.eof()) {
cin >> word;
if (word == "NEG" || word == "ABS") {
// pop one Expression off the stack
// evaluate the expression we popped off
// push a new UnaryExpression onto the stack
Expression x = s.top();
s.pop();
UnaryExpression newItem(x.evaluate(), word);
s.push(newItem);
} else {
// must be a number
// create a new NumberExpression with this number and push it onto the stack
stringstream ss(word);
int num = -1;
ss >> num;
NumberExpression newItem(num);
s.push(newItem);
}
}
Expression answer = s.top();
s.pop();
cout << answer.evaluate() << "\n";
// pop Expression off the stack
// Evaluate it and print out the expression in infix as well as the value
return 0;
}
Expression.h
#ifndef EXPRESSION_H_INCLUDED
#define EXPRESSION_H_INCLUDED
#include <iostream>
class Expression {
public:
Expression();
~Expression();
virtual int evaluate();
virtual void prettyprint(std::ostream&);
int getFirstOperand() const;
std::string getExpression();
protected:
int firstOperand;
std::string expr;
};
#endif // EXPRESSION_H_INCLUDED
Expression.cc:
#include "Expression.h"
Expression::Expression() {
firstOperand = 0;
expr = "";
}
Expression::~Expression() {
}
int Expression::getFirstOperand() const {
return firstOperand;
}
std::string Expression::getExpression() {
return expr;
}
int Expression::evaluate() {
return 0;
}
void Expression::prettyprint(std::ostream& out) {
}
UnaryExpression.h:
#ifndef UNARYEXPRESSION_H_INCLUDED
#define UNARYEXPRESSION_H_INCLUDED
#include <string>
#include <iostream>
#include "Expression.h"
class UnaryExpression : public Expression {
private:
std::string oper;
public:
UnaryExpression(int operand, std::string Operator);
~UnaryExpression();
std::string getOperator() const;
int evaluate();
void prettyprint(std::ostream&);
};
#endif // UNARYEXPRESSION_H_INCLUDED
UnaryExpression.cc:
#include "UnaryExpression.h"
#include "Expression.h"
#include <string>
UnaryExpression::UnaryExpression(int operand, std::string Operator) : Expression() {
firstOperand = operand;
oper = Operator;
}
UnaryExpression::~UnaryExpression() {
}
std::string UnaryExpression::getOperator() const {
return oper;
}
int UnaryExpression::evaluate() {
int firstOperand = getFirstOperand();
if (oper == "NEG") {
return -1 * firstOperand;
} else if (oper == "ABS") {
if (firstOperand < 0) {
return -1 * firstOperand;
} else {
return firstOperand;
}
}
return 0; // shouldn't ever happen
}
void UnaryExpression::prettyprint(std::ostream& out) {
if (oper == "NEG") {
out << "-" << evaluate();
} else if (oper == "ABS") {
out << "|" << evaluate() << "|";
}
}
NumberExpression.h:
#ifndef NUMBEREXPRESSION_H_INCLUDED
#define NUMBEREXPRESSION_H_INCLUDED
#include "Expression.h"
#include "NumberExpression.h"
#include <iostream>
class NumberExpression : public Expression {
public:
NumberExpression(int operand);
~NumberExpression();
int evaluate();
void prettyprint(std::ostream&);
};
#endif // NUMBEREXPRESSION_H_INCLUDED
NumberExpression.cc:
#include "NumberExpression.h"
#include "Expression.h"
#include <iostream>
NumberExpression::NumberExpression(int operand) : Expression() {
firstOperand = operand;
}
NumberExpression::~NumberExpression() {
}
int NumberExpression::evaluate() {
return firstOperand;
}
void NumberExpression::prettyprint(std::ostream& out) {
}
I think the problem is when I pop something off the stack that is a UnaryExpression and assign to an Expression object but that shouldn't be a problem since a UnaryExpression is an Expression. That's just a guess I'm not really sure what is happening. I find inheritance in C++ a little strange...