Infix to Postfix Expression in C++
Enjoy the code folks.... :)
#include
#include
#include
#define MAX 500
using namespace std;
char OpStack[MAX];
int top; /*Stack top*/
void Convert(const string & Infix, string & Postfix);
bool IsOperand(char ch);
bool TakesPrecedence(char OperatorA, char OperatorB);
int Evaluate(string & Postfix);
char Pop (void );
void Initialize(void );
void Push (char);
int main()
{
char Reply;
do
{
string Infix, Postfix; // local to this loop
cout << "Enter an infix expression with no spaces[e.g. (a+b)*c-d/e ]:"
<< endl;
cin >> Infix;
//Converts the Infix to Postfix Notation
Convert(Infix, Postfix);
cout << "The equivalent postfix expression is: " << endl
<< Postfix << endl;
//Evaluates the Postfix Result
int x = Evaluate(Postfix);
cout << "The Result is: " << endl
<< x << endl;
cout << endl << "Do another (y/n)? ";
cin >> Reply;
}
while (tolower(Reply) == 'y');
return 0;
}
/* Given: ch A character.
Task: To determine whether ch represents an operand (here understood
to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false otherwise.
*/
bool IsOperand(char ch)
{
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')))
return true;
else
return false;
}
/* Given: OperatorA A character representing an operator or parenthesis.
OperatorB A character representing an operator or parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.
Return: In the function name: true, if OperatorA takes precedence over
OperatorB.
*/
bool TakesPrecedence(char OperatorA, char OperatorB)
{
if (OperatorA == '(')
return false;
else if (OperatorB == '(')
return false;
else if (OperatorB == ')')
return true;
else if ((OperatorA == '^') && (OperatorB == '^'))
return false;
else if (OperatorA == '^')
return true;
else if (OperatorB == '^')
return false;
else if ((OperatorA == '*') || (OperatorA == '/'))
return true;
else if ((OperatorB == '*') || (OperatorB == '/'))
return false;
else
return true;
}
/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/
void Convert(const string & Infix, string & Postfix)
{
stack OperatorStack;
char TopSymbol, Symbol;
int k;
for (k = 0; k < Infix.size(); k++)
{
Symbol = Infix[k];
if (IsOperand(Symbol))
Postfix = Postfix + Symbol;
else
{
while ((! OperatorStack.empty()) &&
(TakesPrecedence(
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
if ((! OperatorStack.empty()) && (Symbol == ')'))
OperatorStack.pop(); // discard matching (
else
OperatorStack.push(Symbol);
}
}
while (! OperatorStack.empty())
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
}
int Evaluate(string & Postfix)
{
int a,b,q;
Initialize();
char Symbol;
for(int k=0;k< Postfix.size();k++)
{
Symbol = Postfix[k];
if (IsOperand(Symbol)){
Push(Symbol);
}else if(Symbol == '*' || Symbol == '+' || Symbol == '-' || Symbol == '/' || Symbol == '%'){
a=Pop();
b=Pop();
/* Simple logic apply 48 only when variable is more than 48 (as its acii value) else take as it is */
if(a > 48){
a = a - 48;
}
if(b > 48){
b = b - 48;
}
switch (Symbol)
{
case '+': q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
case '%' : q=b%a; break;
}
Push(q);
}
}
return Pop();
}
/* Adds operator to the stack */
void Push ( char c )
{
if ( top == MAX-1)
cout << "\n Stack is Full " << endl;
else{
top++ ;
OpStack[top] = c ;
}
}
/* Pops an operator from the stack */
char Pop ( void )
{
if ( top == -1 ) /* Stack is empty*/
return -1;
else
{
char item = OpStack[top] ;
top-- ;
return item ;
}
}
/* Initializes top value for stack */
void Initialize (void)
{
/*Make stack empty*/
top = -1 ;
}