0% found this document useful (0 votes)
7 views2 pages

Postfix Expression Evaluator in C++

The document contains a C++ program that implements a stack data structure to evaluate postfix expressions. It defines a class 'stack' with methods for pushing, popping, and displaying elements, as well as a function to evaluate basic arithmetic operations. The main function reads a postfix expression from the user, processes it using the stack, and outputs the final result.

Uploaded by

yashkhirode3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Postfix Expression Evaluator in C++

The document contains a C++ program that implements a stack data structure to evaluate postfix expressions. It defines a class 'stack' with methods for pushing, popping, and displaying elements, as well as a function to evaluate basic arithmetic operations. The main function reads a postfix expression from the user, processes it using the stack, and outputs the final result.

Uploaded by

yashkhirode3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include<iostream>

#include<math.h>
using namespace std;
const int Max=5;
class stack
{
private:
int a[Max];
int top;
public:
stack()
{
top=-1;
}
int full(void)
{
if(top==Max-1)
{
cout<<"stack is full";
return(1);
}
else
return(0);
}
int empty(void)
{
if(top==-1)
return(1);
else
return(0);
}
int peek(void)
{
return(a[top]);
}
void push(int x)
{
top++;
a[top]=x;
}
int pop(void)
{
int x=a[top];
top--;
return(x);
}
void display(void)
{
for(int i=top;i>=0;i--)
{
cout<<endl<<a[i];
}
}
};
int evaluate(char x,int op1,int op2)
{
if(x=='+') return(op1+op2);
if(x=='-') return(op1-op2);
if(x=='*') return(op1*op2);
if(x=='/') return(op1/op2);
if(x=='%') return(op1%op2);
if(x=='$') return(pow(op1,op2));
}
int main()
{
stack s;
char x;
int op1,op2,val;
cout<<"\n Enter Postfix Expression:";
while((x=getchar())!='\n')
{
if(isdigit(x))
[Link](x-48);
else
{
op2=[Link]();
op1=[Link]();
val=evaluate(x,op1,op2);
[Link](val);
}
}
val=[Link]();
cout<<"\n Answer="<<val;
return(0);
}
/*
OUTPUT:-
PS D:\Chaitanya programs> cd "d:\Chaitanya programs\" ; if ($?) { g++
arrray_evaluation.cpp -o arrray_evaluation } ; if ($?) { .\arrray_evaluation }

Enter Postfix Expression:623+-382/+*2$3+

Answer=52
PS D:\Chaitanya programs>
*/

You might also like