forked from vaibhavpathak999/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix_Evaluationstack.cpp
More file actions
80 lines (75 loc) · 1.68 KB
/
Copy pathPostfix_Evaluationstack.cpp
File metadata and controls
80 lines (75 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define SIZE 1000
int top=-1;
int stackk[SIZE];
int pop()
{
if(top==-1)
{printf("Stack UnderFlow\n");
return -12345678;}
else
return stackk[top--];
}
void push(int data)
{
if(top==SIZE-1)
printf("Stack OverFlow!\n");
else
stackk[++top]=data;
}
int peek()
{
if(top==-1)
{printf("Stack UnderFlow!\n");
return -1;}
else
return stackk[top];
}
int main()
{
string exp;
printf("Enter the Postfix expression--\n");
cin>>exp;
int l=exp.length(),k,op1,op2;
for(int i=0;i<l;i++)
{
k=(int)exp[i];
if(k>=48&&k<=57)
push(k-48);
else
{
op1=pop();
op2=pop();
if(op1==-12345678||op2==-12345678)
{
printf("Not Enough Operands...Sorry!\n");
break;
}
char ch=exp[i];
switch(ch)
{
case '*': push(op2*op1);
break;
case '/': push(op2/op1);
break;
case '-': push(op2-op1);
break;
case '+': push(op2+op1);
break;
case '^': push(pow(op2,op1));
break;
}
}
}
if(op1!=-12345678&&op2!=-12345678)
{if(top==0){
printf("Output of %s is: %d\n\n",exp,peek());
}
else
printf("Too many Operands...Oops!\n");}
cout<<"Thank you!"<<endl;
return 0;
}