/*******************************************************
APPLICATION : Evaluation of Postfix Expression
CODED BY : Ankit Pokhrel
COMPILED ON : Borland C++ Ver 5.02
DATE : 2010 - June - 10
********************************************************/
#include "iostream.h"
#include "conio.h"
#include "stdio.h"
#include "math.h"
/*********** Class to Represent Stack ************/
template "class T"
class stack
{
private:
T *arr;
int TOP,MAXSTK;
public:
stack()
{
MAXSTK = 31;
arr = new T[MAXSTK];
TOP = 0;
}
stack(int n)
{
MAXSTK = n + 1;
arr = new T[MAXSTK];
TOP = 0;
}
friend void push(stack &,T); //Push an Item to Stack
friend T pop(stack &); //Pop an Item From Stack
friend int empty(stack); //To Check wheter the stack is empty or not
~stack()
{
delete arr; //Destroy arr
}
};
template "class T"
void push(stack "T" &s, T item)
{
if(s.TOP != s.MAXSTK)
{
s.TOP += 1;
s.arr[s.TOP] = item;
}
else //Overflow Condition
cout << "\nStack Overflow.";
}
template "class T"
T pop(stack "T" &s)
{
T temp;
if(s.TOP != 0)
{
temp = s.arr[s.TOP];
s.TOP -= 1;
return temp;
}
else //Underflow Condition
{
temp = -999;
return temp;
}
}
template "class T"
int empty(stack "T" s)
{
if(s.TOP == 0) //Stack is empty
return 1;
else
return 0;
}
int main()
{
char postfix[30],temp[8];
int i = 0;
cout << "Enter a Valid Postfix Expression,\n";
cout << "Write Expression in One Line Seperated by Space,\n";
cout << "Hit Enter when Finished\n\n";
postfix[i - 1] = '\0';
while(postfix[i-1] != '\n') //Until a user hit Enter key
postfix[i++] = getchar(); //Get a Character
stack "float" stk(50); //Float Stack
float value,operand1,operand2,item;
i = 0;
int j,pos = 0,k;
while(postfix[i-1] != '\n')
{
if(postfix[i] >= '0' && postfix[i] <= '9') //If Number is found
{
temp[0] = postfix[i]; // Save Postfix[i] to temp
j = i+1;
k = 1;
if(postfix[i+1] != ' ') //Check for next character, If not Space
{
while(postfix[j] != ' ') //While space is not found
temp[k++] = postfix[j++]; //Save Postfix[j] to temp
}
temp[k] = '\0'; //End String temp
pos = j + 1; //Update position
item = atof(temp); //Convert string temp to float and store to item
push(stk,item); //Push to stack
}
else if(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '$' || postfix[i] == ' ') //If Operator is found
{
operand2 = pop(stk); //Pop operand2 from Stack
if(operand2 == -999) //if Underflow Condition
break; //break from loop
operand1 = pop(stk); //Pop operand1 from Stack
if(operand1 == -999)
break;
if(postfix[i] == '+')
value = operand1 + operand2;
if(postfix[i] == '-')
value = operand1 - operand2;
if(postfix[i] == '*')
value = operand1 * operand2;
if(postfix[i] == '/')
if(operand2 != 0)
value = operand1 / float(operand2);
else
{
cout << "\nError ! Cannot Divide by Zero";
getch();
return 0;
}
if(postfix[i] == '$') //Power
value = pow(operand1,operand2);
push(stk,value); //Push result to stack
pos += 2; //Update position
}
else
{
cout << "\nError : Illegal Character";
getch();
return 0;
}
i = pos; //Update i
}
float Result = pop(stk); //Pop Result
if(Result == -999 || !empty(stk))//If Underflow Condition or stack is not empty, Print Error message and exit
{
cout << "\nError : The Given Postfix Expression is Not Valid.";
getch();
return 0;
}
else //Print Result
cout << "\nResult = " << Result;
getch();
return 0;
}
......................
Labels:
Stack
No comments:
Post a Comment