forked from namigoel/Hacktober-Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression_Evaluation.cpp
126 lines (108 loc) · 1.69 KB
/
Expression_Evaluation.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*============================================================================
Title: Expression Evaluuation with implementation of Stack using Array
============================================================================*/
#include<iostream>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
class ExpEva
{
int max=10;
int st[10];
int top;
public:
ExpEva()
{
top=-1;
}
int isempty()
{
if(top==-1)
{
return 1;
}
else
return 0;
}
int isfull()
{
if(top==max-1)
{
return 1;
}
else
return 0;
}
void push(int data)
{
if(isfull()==0)
{
top++;
st[top]=data;
}
else
{
cout<<"Stack is full!"<<endl;;
}
}
int pop()
{
int data;
if(isempty()==0)
{
data=st[top];
top--;
return data;
}
else
{
cout<<"Stack is empty!"<<endl;
return -1;
}
}
int calc(int s1,int s2,char ch)
{
if(ch=='*')
{
return s1*s2;
}
if(ch=='/')
{
return s1/s2;
}
if(ch=='+')
{
return s1+s2;
}
if(ch=='-')
{
return s1-s2;
}
}
};
int main()
{
ExpEva s;
int val,s1,s2,result;
char postfix[30];
cout<<"-----POSTFIX TO INFIX-----\n\n"<<endl;
cout<<"Enter postfix expression: ";
cin>>postfix;
for(int i=0;i<strlen(postfix);i++)
{
if(isdigit(postfix[i]))
{
val=postfix[i]-'0';
s.push(val);
}
else if(postfix[i]=='*'||postfix[i]=='/'||postfix[i]=='+'||postfix[i]=='-')
{
s1=s.pop();
s2=s.pop();
result=s.calc(s2,s1,postfix[i]);
s.push(result);
}
}
cout<<"Infix Expression: "<<s.pop();
return 0;
}