Created
September 6, 2018 15:49
-
-
Save nihal-singh/4b0dac4a91637e7c38a2575ef2168086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Infix to Postfix Expression | |
#include <iostream> | |
#include <string> | |
#include <stack> | |
using namespace std; | |
//********************************************* | |
//function to convert Infix to PPostfix | |
string InfixToPostfix(string ); | |
//check whether a particular character is operand or not | |
bool IsOperand(char ); | |
//check whether a particular character is operator or not | |
bool IsOperator(char ); | |
//check for operator precedence | |
bool hasHigherPrecedence(char ,char ); | |
//finds weight to calculate operator precedence | |
int getWeight(char ); | |
//************************************************** | |
int main(){ | |
string expression; | |
cout << "Enter a infix expression :" << endl; | |
getline(cin,expression); | |
string postfix = InfixToPostfix(expression); | |
cout << "Postfix Expression : " << postfix << endl ; | |
return 0; | |
} | |
string InfixToPostfix(string expression){ | |
stack<char> S; | |
string postfix = ""; | |
for (int i=0;i<expression.length();i++){ | |
if (expression[i] == ' ' || expression[i] == ',')continue; | |
if(IsOperand(expression[i])){ | |
postfix += expression[i]; | |
continue; | |
} | |
if(expression[i] == '('){ | |
S.push(expression[i]); | |
continue; | |
} | |
if(expression[i] == ')'){ | |
while(!S.empty() && S.top() != '('){ | |
postfix += S.top(); | |
S.pop(); | |
} | |
S.pop(); | |
continue; | |
} | |
if(IsOperator(expression[i])){ | |
while(!S.empty() && S.top() != '(' && hasHigherPrecedence(S.top(), expression[i])){ | |
postfix += S.top(); | |
S.pop(); | |
} | |
S.push(expression[i]); | |
continue; | |
} | |
} | |
while (!S.empty()){ | |
postfix += S.top(); | |
S.pop(); | |
} | |
return postfix; | |
} | |
bool IsOperand(char exp){ | |
if(exp >= '0' && exp <= '9')return true; | |
if(exp >= 'a' && exp <= 'z')return true; | |
if(exp >= 'A' && exp <= 'Z')return true; | |
return false; | |
} | |
bool IsOperator(char exp){ | |
if(exp == '+' || exp == '-' || exp == '*' || exp == '/' || exp == '^' )return true; | |
return false; | |
} | |
bool hasHigherPrecedence(char op1, char op2){ | |
int op1Weight = getWeight(op1); | |
int op2Weight = getWeight(op2); | |
if(op1Weight == op2Weight){ | |
if(op1 == '^')return false; //bcoz exponent(^) is right associative. | |
return true; | |
} | |
return (op1Weight > op2Weight) ? true :false ; | |
} | |
int getWeight(char exp){ | |
int weight = -1; | |
switch(exp){ | |
case '+': | |
case '-': | |
weight = 1; | |
break; | |
case '*': | |
case '/': | |
weight = 2; | |
break; | |
case '^': | |
weight = 3; | |
break; | |
} | |
return weight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment