Postfix Expression Examples
Conventional logic of evaluation of post-fix expression by stack can solve numbers of only 1 digit i.e. This is a very big drawback of the logic used as well as it makes the program of no practical use. Evaluation of Infix expression. An infix expression is evaluated using two stacks, one for operator and another for operands. The infix sting is read in an array, from which symbols/characters are fetched one by one and the following checks are performed: If symbol is an operand, push it in operand’s stack. Below is the syntax highlighted version of EvaluatePostfix.java from §4.3 Stacks and Queues. /***** * Compilation: javac EvaluatePostfix.java * Execution: java EvaluatePostfix.
Program For Evaluating Postfix Expression Using Stack Stones
Here is my complete code. I have really tried everything but couldn't understand where i am going wrong. There is NO ERROR in compilation but the output does take in the elements and output is not being generated. My compiler is Borland 10.5
#include <iostream>
#include <conio>
class ListStack
{
private:
struct node
{
int num;
node *next;
}*top;
int operand_count;
public:
ListStack()
{
top=NULL;
}
int push(int);
int pop();
void display();
void get_expression();
int get_value(int);
};
int ListStack::push(int c)
{
node *temp;
temp = new node;
temp->num=c;
temp->next=top;
top=temp;
return c;
}
int ListStack::pop()
{ int c;
if(topNULL)
cout<<'Stack UnderFlow'<<endl;
else
{
node *temp;
temp=top;
cout<<'deleted Number from the stack = ';
c=top->num;
top=top->next;
//return (temp->num);
delete temp;
return (c);
}
}
void ListStack::display()
{ node*temp;
temp=top;
while(temp!=NULL)
{
cout<<'n'<<temp->num<<endl;
temp=temp->next;
}
}
int ListStack::get_value(int c)
{ int operand_count;
cout<<'how many number of operands you have?';
cin>>operand_count;
int numeric_array[5];
int i;
for (i=0;i<=operand_count;i++)
{
cout<<'Enter Value: ';
cin>>numeric_array[i];
//return numeric_array;
}
}
void ListStack::get_expression()
{
char save;
int i=0;
int first_operand, second_operand, result;
char postfix_array[50], No_operator[50];
cout<<'Enter the numeric Postfix expression: ';
cin.getline(postfix_array,50);
while (postfix_array[i]!='0')
save=postfix_array[i];
if (save!= '+' && save!= '-' && save!= '*' &&save!= '/' &&save!= '^')
{
for(i=0; i<=50; i++)
{
cout<<'n ENter operator :';
cin>>No_operator[i];
get_value( No_operator[i]);
}
while (No_operator[i]!='0')
push( No_operator[i]);
}
else
first_operand = pop();
second_operand = pop();
switch(save)
{
case '+':
result=first_operand + second_operand;
cout<<result;
push(result);
break;
case '-':
result=first_operand - second_operand;
cout<<result;
push(result);
break;
case '*':
result=first_operand * second_operand;
cout<<result;
push(result);
break;
case '/':
result=first_operand / second_operand;
cout<<result;
push(result);
break;
case '%':
result=first_operand % second_operand;
cout<<result;
push(result);
break;
}
}
void main()
{
ListStack LS;
LS.get_expression();
LS.display();
getche();
}
#include <iostream>
#include <conio>
class ListStack
{
private:
struct node
{
int num;
node *next;
}*top;
int operand_count;
public:
ListStack()
{
top=NULL;
}
int push(int);
int pop();
void display();
void get_expression();
int get_value(int);
};
int ListStack::push(int c)
{
node *temp;
temp = new node;
temp->num=c;
temp->next=top;
top=temp;
return c;
}
int ListStack::pop()
{ int c;
if(topNULL)
cout<<'Stack UnderFlow'<<endl;
else
{
node *temp;
temp=top;
cout<<'deleted Number from the stack = ';
c=top->num;
top=top->next;
//return (temp->num);
delete temp;
return (c);
}
}
void ListStack::display()
{ node*temp;
temp=top;
while(temp!=NULL)
{
cout<<'n'<<temp->num<<endl;
temp=temp->next;
}
}
int ListStack::get_value(int c)
{ int operand_count;
cout<<'how many number of operands you have?';
cin>>operand_count;
int numeric_array[5];
int i;
for (i=0;i<=operand_count;i++)
{
cout<<'Enter Value: ';
cin>>numeric_array[i];
//return numeric_array;
}
}
void ListStack::get_expression()
{
char save;
int i=0;
int first_operand, second_operand, result;
char postfix_array[50], No_operator[50];
cout<<'Enter the numeric Postfix expression: ';
cin.getline(postfix_array,50);
while (postfix_array[i]!='0')
save=postfix_array[i];
if (save!= '+' && save!= '-' && save!= '*' &&save!= '/' &&save!= '^')
{
for(i=0; i<=50; i++)
{
cout<<'n ENter operator :';
cin>>No_operator[i];
get_value( No_operator[i]);
}
while (No_operator[i]!='0')
push( No_operator[i]);
}
else
first_operand = pop();
second_operand = pop();
switch(save)
{
case '+':
result=first_operand + second_operand;
cout<<result;
push(result);
break;
case '-':
result=first_operand - second_operand;
cout<<result;
push(result);
break;
case '*':
result=first_operand * second_operand;
cout<<result;
push(result);
break;
case '/':
result=first_operand / second_operand;
cout<<result;
push(result);
break;
case '%':
result=first_operand % second_operand;
cout<<result;
push(result);
break;
}
}
void main()
{
ListStack LS;
LS.get_expression();
LS.display();
getche();
}