Converting an Infix expression to postfix is not a big deal. Even though for a beginner it could be frustrating for hours.
Enough breaking your computers. Its time to finish it off!.
Infix expression:
The Infix expressions are the expressions which we usually use in mathematical form.
Eg: a*(b+d*c)
Postfix expression:
These expressions are used by the computer to evaluate the given infix expressions. They simply convert infix expression to postfix using stack data structure.
Eg: a b d c * + *
Stack data structure is pre-request for this conversion which we will not discuss here. Just google it for more information.
I have written a simple program for this conversion process. Almost it is self explanatory. Share if you like this.
This file contains hidden or 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
#include<iostream> | |
#include<stack>//import Stack STL | |
#include<string>//import String STL | |
#include<conio.h> | |
using namespace std; | |
string infixToPostfix(string expr); //Function declaration | |
int precendence(char arg); | |
bool isoperand(char arg); | |
bool isoperator(char arg); | |
int operatorweight(char arg); | |
bool highprecendence(char a, char b); | |
int main() | |
{ | |
string exp;//Variable to get input expression | |
cout<<"Enter the infix expression:"; | |
getline(cin,exp); | |
cout<<"Output Postfix Expression:"<<infixToPostfix(exp); | |
getch(); | |
} | |
string infixToPostfix(string expr)//Function to perform all conversion operation | |
{ | |
stack<char> stk;//Declaring a stack for conversion purpose | |
string postfix = "";//Initialize the output string as empty; | |
for(int i = 0;i < expr.length(); i++)//Scan the infix string one by one | |
if(expr[i] == '(') | |
{ | |
stk.push(expr[i]); | |
} | |
else if(expr[i] == ')') | |
{ | |
while(stk.top() != '(') | |
{ | |
postfix = postfix + stk.top(); | |
stk.pop(); | |
} | |
stk.pop(); | |
} | |
else if(isoperand(expr[i])) | |
{ | |
postfix += expr[i]; | |
} | |
else if(isoperator(expr[i])) | |
{ | |
while(!stk.empty()&& !highprecendence(expr[i],stk.top())) | |
{ | |
postfix+= stk.top(); | |
stk.pop(); | |
} | |
stk.push(expr[i]); | |
} | |
while(!stk.empty()) | |
{ | |
postfix+= stk.top(); | |
stk.pop(); | |
} | |
return postfix; | |
} | |
bool highprecendence(char a, char b)//Check for operator precendence | |
{ | |
int weighta = operatorweight(a); | |
int weightb = operatorweight(b); | |
if(weighta >= weightb) return 1; | |
return 0; | |
} | |
bool isoperator(char arg)//Check weather the character is operator | |
{ | |
if(arg == '*' || arg == '/' || arg == '+' || arg == '-') return(1); | |
else return(0); | |
} | |
bool isoperand(char arg)//Check weather the character is operand | |
{ | |
if(arg >= '0' && arg <= '9') return 1; | |
if(arg >= 'a' && arg <= 'z') return 1; | |
if(arg >= 'A' && arg <= 'Z') return 1; | |
return 0; | |
} | |
int operatorweight(char arg)//Add weight to the operator | |
{ | |
int weight = 0; | |
switch(arg) | |
{ | |
case '*': | |
case '/': | |
weight = 2; | |
break; | |
case '+': | |
case '-': | |
weight = 1; | |
break; | |
} | |
return(weight); | |
} |
Comments
Post a Comment