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.
Explanation / Answer
# include # include # include # include struct node { char data; node *next; }; node *top=NULL; node *bottom=NULL; node *entry; node *last_entry; node *second_last_entry; void push(constchar); constchar pop( ); void infix_to_postfix(constchar *); int main( ) { clrscr( ); char Infix_expression[100]={NULL}; coutInfix_expression; infix_to_postfix(Infix_expression); getch( ); return 0; } /*************************************************************************///----------------------------- push(const char) ----------------------///*************************************************************************/void push(constchar Symbol) { entry=new node; if(bottom==NULL) { entry->data=Symbol; entry->next=NULL; bottom=entry; top=entry; } else { entry->data=Symbol; entry->next=NULL; top->next=entry; top=entry; } } /*************************************************************************///-------------------------------- pop( ) -----------------------------///*************************************************************************/constchar pop( ) { char Symbol=NULL; if(bottom==NULL) coutnext=NULL; } return Symbol; } /*************************************************************************///--------------------- infix_to_postfix(const char *) ----------------///*************************************************************************/void infix_to_postfix(constchar *Infix) { char Infix_expression[100]={NULL}; char Postfix_expression[100]={NULL}; strcpy(Infix_expression,"("); strcat(Infix_expression,Infix); strcat(Infix_expression,")"); char Symbol[5]={NULL}; char Temp[5]={NULL}; for(int count=0;countRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.