Question
I have completed the first part. Infix/Postfix/Prefix program. There are some skeleton where we have to follow as part of our project so try and
I have completed the first part. Infix/Postfix/Prefix program.
There are some skeleton where we have to follow as part of our project so try and keep code without changing much.
The first function inToPost works perfectly. My main issue I am having is with my Infix to Prefix and Postfix to Infix.. "inToPre" , "postToIn" functions.
I have tried following Pseudo code but I cannot complete these two.
If you can help using comments that would be nice. I believe I can finish the rest if you help me with these two functions.
It must be done in C++.
Thank you.
#include #include #include
using namespace std;
class Expression{ public:
string inToPost(string); string inToPre(string); string postToIn(string); string preToIn(string); string convertThis; // Expression that we want converted double evaluate(); // Evaluate expression
Expression(string input, int direction); //constructor
bool isOperator(char character); bool isOperand(char character); int isHigherWeight(char character); bool isHigherPrecedence(char op1, char op2); void printResult();
private: string infix; string postfix; string prefix;
};
//Constructor function Expression::Expression(string input, int direction){ convertThis = input; switch (direction){ case 1: infix = input; case 2: postfix = input; case 3: prefix = input;
} }
//Operator Function checks to see if character is a legal symbol bool Expression::isOperator(char character){ if((character == '*')||(character == '+')||(character == '-')||(character == '/')) return true; else return false; }
//Operand Function checks to see if character is a legal character bool Expression::isOperand(char character){ if(character >= 'a' && character <= 'z') return true; if(character >= 'A' && character <= 'Z') return true; if(character >= '0' && character <= '9') return true; else return false; }
//Function determines the weight of Operator. int Expression::isHigherWeight(char character){ int weight = -1; // switch(character){ case '+': case '-': weight = 1; case '*': case '/': weight = 2; } return weight; }
//Function that compares weights of two different Operators. bool Expression::isHigherPrecedence(char oper1, char oper2){ int op1Weight = isHigherWeight(oper1); int op2Weight = isHigherWeight(oper2);
// If operators have equal precedence, return true // return false if (op1Weight == op2Weight){ return true; }
return op1Weight > op2Weight ? true: false;{
}
} string Expression::inToPost(string myExpression){
stack Stack; string postfix = ""; // Initialize postfix as empty string.
for(int i = 0;i< myExpression.length();i++){ //go through array of string convertThis
if (myExpression[i] == ' ' || myExpression[i] == ',') continue;
else if(isOperator(myExpression[i])){ while(!Stack.empty() && Stack.top() != '(' && isHigherPrecedence(Stack.top(),myExpression[i])){ postfix += Stack.top(); //appending stack char to postfix string Stack.pop(); } Stack.push(myExpression[i]); }
else if(isOperand(myExpression[i])){ postfix += myExpression[i]; //appending expression at pos[i] to postfix string }
else if(myExpression[i] == '('){ Stack.push(myExpression[i]); } else if (myExpression[i] == ')'){
while(!Stack.empty() && Stack.top() != '('){ postfix += Stack.top(); //appending stack char to postfix string Stack.pop(); } Stack.pop(); }
}
while(!Stack.empty()){ postfix += Stack.top(); //appending stack char to postfix string Stack.pop();
} return postfix;
}
string Expression::inToPre(string myExpression){
// // stack operandStack; // stack operatorStack; // string prefix = ""; //make sure nothing is in string infix // string rightOperand; // string leftOperand; // string opERAND; // string opERATOR; // // // // // // for(int i = 0;i< myExpression.length();i++){ //While expression still remains, go through input // if (myExpression[i] == ' ' || myExpression[i] == ',') continue; // // // if(isOperand(myExpression[i])){ // operandStack.push(myExpression[i]); // } // else if (myExpression[i] = '(' || operatorStack.empty() || isHigherPrecedence(operatorStack.top(),myExpression[i])){ // operatorStack.push(myExpression[i]); // } // // else if (myExpression[i] == ')'){ // while(!operatorStack.top() == '('){ // while not a left parenthesis // opERATOR = operatorStack.top(); // operator // operatorStack.pop(); // rightOperand = operandStack.top(); //Right operand // operandStack.pop(); // leftOperand = operandStack.top(); //Left operand // operandStack.pop(); // opERAND = opERATOR + leftOperand + rightOperand; // } // } // operatorStack.pop(); // popping left parenthesis // // // // if (!isHigherPrecedence(operatorStack.top(),myExpression[i])){ // // while(!operatorStack.empty()){ // opERATOR = operatorStack.top(); // operatorStack.pop(); // rightOperand = operandStack.top(); // operandStack.pop(); // leftOperand = operandStack.top(); // operandStack.pop(); // opERAND += opERATOR + leftOperand + rightOperand; // } // // operatorStack.push(myExpression[i]); // } // // while(!operatorStack.empty()){ // rightOperand = operandStack.top(); // operandStack.pop(); // leftOperand = operandStack.top(); // operandStack.pop(); // // opERAND += opERATOR + leftOperand + rightOperand; // // // // } // } // //
return prefix;
}
string Expression::postToIn(string myExpression){
stack Stack; string infix = ""; // Initialize postfix as empty string. string leftParenthesis = "("; string rightParenthesis = ")"; string leftValue; string rightValue; string myOperator; string currentinfix;
for(int i = 0;i< myExpression.length();i++){
if(isOperand(myExpression[i])){ Stack.push(myExpression[i]); }
else if (isOperator(convertThis[i])){
rightValue = Stack.top(); Stack.pop(); leftValue = Stack.top(); Stack.pop(); myOperator = convertThis[i]; infix = leftParenthesis + leftValue + myOperator + rightValue + rightParenthesis; }
//this function prints only 23+... but as soon as I go past this like abc+- for example we get errors.
} return infix; } string Expression::preToIn(string expression){
}
double Expression::evaluate(){
}
void Expression::printResult(){
cout << postToIn(convertThis);
}
int main(){
string convertThis;
int choice; string str;
cout << "|-----Here is my conversion menu-----|" << endl; cout << "|----What are you converting to?-----|" << endl << endl; cout << "1- Infix to postfix" << endl; cout << "2- Infix to prefix" << endl; cout << "3- postfix to infix?" << endl; cout << "4- prefix to infix?" << endl; //cin >> choice; //cin.ignore();
cout << "Now enter the expression you want to convert "; getline(cin,str);
Expression myexp(str, 1); myexp.printResult();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started