The program is about infix_to_postfix conversation and postfix evaluation using
ID: 3532858 • Letter: T
Question
The program is about infix_to_postfix conversation and postfix evaluation using one class (Expression).
I have this class:
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include "expression_exception.h"
using namespace std;
class Expression {
friend ostream &operator <<(ostream &os, const Expression &exp);
public:
Expression(string infix);
double evaluate();
string getInfix();
string getPostfix();
private:
void compile();
double evaluate(char optr, double operand1, double operand2);
int Expression::precLevel(char optr);
int Expression::isLeftAssociative(char optr);
string infix;
string postfix;
};
#endif
Then I have:
#ifndef EXPRESSION_EXCEPTION_H
#define EXPRESSION_EXCEPTION_H
#include <string>
class ExpressionException {
public:
ExpressionException(std::string message) : message(message) {}
std::string getMessage() {return "*** ExpressionException: " + message;}
private:
std::string message;
};
#endif
This is how I will use my class:
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include "expression.h"
#include "expression_exception.h"
using namespace std;
main() {
string infix;
cout << "Enter an infix expression: ";
while (getline(cin, infix)) {
try {
Expression exp = infix;
cout << exp.evaluate() << endl;
} catch (ExpressionException e) {
cerr << e.getMessage() << endl;
}
cout << "Enter a infix expression: ";
}
}
I badly need help to make it all work together.