Trying to make a program to convert infix to postfix in c++. I am stuck here try
ID: 3798263 • Letter: T
Question
Trying to make a program to convert infix to postfix in c++. I am stuck here trying to get a result of infix to postfix to print. Could anyone help me print my result of postfix and maybe leave comments so I can understand better? Thank you. #include #include #include #include using namespace std; class Expression{ public: string inToPost(); string convertThis; // Expression that we want converted Expression(string input, int direction); //constructor bool isOperator(char character); bool isOperand(char character); int isHigherWeight(char character); bool isHigherPrecedence(char op1, char op2); string printResult(); private: string infix; string postfix; string prefix; }; //Constructor function Expression::Expression(string input, int direction){ 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 = 0; // or -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 return op1Weight > op2Weight ? true: false;{ } } string Expression::inToPost(){ stack Stack; string postfix = ""; // Initialize postfix as empty string. for(int i = 0;i< convertThis.length();i++){ //go through array of string convertThis if (convertThis[i] == '('){ //1-Read the left parenthesis and push it onto the stack Stack.push(convertThis[i]); } else if(isOperand(convertThis[i])){ //2-else if( the next input is a number or letter) cout << convertThis[i]; //3-Read the operand and write it to the output } else if(isOperator(convertThis[i])){ //4-else if the next input is operator cout << Stack.top(); Stack.pop(); //5-Print the top operation and pop it } //6- while(!Stack.empty() && Stack.top() != '(' && isHigherPrecedence(Stack.top(),convertThis[i])){ Stack.push(convertThis[i]); //7- Read the next input symbol, and push this symbol onto the stack } // 8- Read and discard the next input symbol(which should be a right parenthesis). if (convertThis[i] == ')'){ i+1; // 9- Print the top operation and pop it; Keep printing and popping until while (!Stack.top() == '('){ cout << Stack.top(); Stack.pop(); } } Stack.pop(); //10- Finally, pop the left parenthesis. while(!Stack.empty()){ cout << Stack.top(); } return postfix; } } string Expression::printResult(){ return postfix; } int main(){ string convertThis; int choice; 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,convertThis); //Expression printResult; //cout << printResult.printResult(); }
Explanation / Answer
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
class Expression{
public:
void inToPost();
Expression(string const &input); //constructor
bool isOperator(char character);
bool isOperand(char character);
int getWeight(char character);
bool isHigherPrecedence(char op1, char op2);
char* printResult();
private:
char* infix;
char* postfix;
char* prefix;
string convertThis; // Expression that we want converted
};
//Constructor function
Expression::Expression(string const &input) : convertThis(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::getWeight(char character){
int weight = 0; // or -1?
switch(character){
case '+':
case '-':
weight = 1;
break; // for case +,- weight is 1
case '*':
case '/':
weight = 2;
break; // for case *,/ weight is 2
default : weight = 0;
// for any other the weight is 0
}
return weight;
}
//Function that compares weights of two different Operators.
bool Expression::isHigherPrecedence(char oper1, char oper2){
int op1Weight = getWeight(oper1);
int op2Weight = getWeight(oper2);
// If operators have equal precedence, return true
// return false
return (op1Weight > op2Weight ? true: false);{
}
}
void Expression::inToPost(){
stack<char> Stack;
int size = convertThis.length();
postfix[size]; // Initialize postfix as empty string.
int index = 0;
int weight = 0;
for(int i = 0; i < size ;i++){ //go through array of string convertThis
if (convertThis[i] == '('){ //1-Read the left parenthesis and push it onto the stack
Stack.push(convertThis[i]);
continue;
}
if (convertThis[i] == ')'){
while (!Stack.empty() && Stack.top() != '(') {
postfix[index] = Stack.top();
Stack.pop();
index++;
}
// pop off the opening parenthesis also
if (!Stack.empty()) {
Stack.pop();
}
continue;
}
/* else if(isOperand(convertThis[i])){ //2-else if( the next input is a number or letter)
postfix[index]=convertThis[i];
index++;
}
*/ //dont really require this , as we are only interested in operators and parenthesis, rest can be treated as operands
weight = getWeight(convertThis[i]);
if (weight == 0) {
// it is an operand
// simply append it to postfix expression
postfix[index]=convertThis[i];
index++;
}
else {
// it is an operator
if (Stack.empty()) {
// simply push the operator onto stack if
// stack is empty
Stack.push(convertThis[i]);
}
else {
// pop of all the operators from the stack and
// append it to the postfix expression till we
// see an operator with a lower precedence that
// the current operator
while (!Stack.empty() && Stack.top() != '(' &&
weight <= getWeight(Stack.top())) {
postfix[index] = Stack.top();
Stack.pop();
index++;
}
// push the current operator onto stack
Stack.push(convertThis[i]);
}
}
}
// pop of the remaining operators present in the stack
// and append it to postfix expression
while (!Stack.empty()) {
postfix[index] = Stack.top();
Stack.pop();
index++;
}
postfix[index] = ''; // null terminate the postfix expression
}
char* Expression::printResult(){
return postfix;
}
int main(){
string convertThis;
int choice;
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,convertThis);
Expression expression (convertThis); // Create new object of expression class
if(choice == 1){
expression.inToPost();
}//
char* result = expression.printResult();
cout<<endl<<"result : "<<result;
return 0;
}
if you are using switch case , use break statements otherwise the matched cased and all the below cases will be executed too. Also i think you need to brush up your concepts on clases as you did not really make objects to use the class methods and variables.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.