-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfix_evaluation.cpp
More file actions
155 lines (143 loc) · 4.61 KB
/
postfix_evaluation.cpp
File metadata and controls
155 lines (143 loc) · 4.61 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
float power(float x, float y)
//power function for x^y where x is base and y is its exponent
{
float ans = 1;
if (y-int(y)==0)
{
for (int i = 0; i < y; i++)
{
ans *= x;
}
return ans;
}
ans = pow(10, (y * log10(x)));
return ans;
}
float make_number(stack<float> & s1tako, int num_count, int dot_count, bool num_is_negative)
//make_number function takes 1 stack as argument which is filled with numbers each digit in perticuler manner
//num_count for how many digits are used before dot ( ex 12.23 here num_count=2)
//dot_count for how many digits are used after dot ( ex 12.23 here dot_count=2)
//bool num_is_negative is used to identify weather number is positive or negative.
{
float total = 0;
if (dot_count)
{
for (int i = 0; i < dot_count; i++)
{
total += s1tako.top() * pow(10, -dot_count + i);
s1tako.pop();
}
}
for (int i = 0; i < num_count; i++)
{
total += power(10, i) * s1tako.top();
s1tako.pop();
}
// if bool num_is_negative is true then it returns negative value otherwise positive
if (num_is_negative)
return total * -1;
return total;
}
float postfix_evaluation(string s)
{
stack<float> stk_for_operand, stk_for_num;
//here stk_for_operand is to store operand for while
int k = s.length();
int num_count = 0, dot_count = 0;
bool dot_bool = 0, num_is_negative = 0;
for (int i = 0; i < k; i++)
{
float a, b; //for two binary operands
if (s[i] == '+')
{
a = stk_for_operand.top();
stk_for_operand.pop();
b = stk_for_operand.top();
stk_for_operand.pop();
stk_for_operand.push(a + b);
}
else if (s[i] == '-')
{
if (s[i + 1] - 48 <= 9 && s[i + 1] - 48 >= 0)
{
num_is_negative = 1;
}
else
{
a = stk_for_operand.top();
stk_for_operand.pop();
if (stk_for_operand.empty())
{
stk_for_operand.push(a * -1);
}
else
{
b = stk_for_operand.top();
stk_for_operand.pop();
stk_for_operand.push(b - a);
}
}
}
else if (s[i] == '*')
{
a = stk_for_operand.top();
stk_for_operand.pop();
b = stk_for_operand.top();
stk_for_operand.pop();
stk_for_operand.push(a * b);
}
else if (s[i] == '^')
{
a = stk_for_operand.top();
stk_for_operand.pop();
b = stk_for_operand.top();
stk_for_operand.pop();
stk_for_operand.push(power(b, a));
}
else if (s[i] == '/')
{
a = stk_for_operand.top();
stk_for_operand.pop();
b = stk_for_operand.top();
stk_for_operand.pop();
stk_for_operand.push(b / a);
}
else if (s[i] == ',')
{
if (num_count)
{
stk_for_operand.push(make_number(stk_for_num, num_count, dot_count, num_is_negative));
}
num_count = dot_count = 0;
dot_bool = 0;
num_is_negative = 0;
// cout<<"--------------------------------------------------------"<<endl;
}
else if (s[i] == '.')
{
dot_bool = 1;
}
else
{
// cout<<"in num section"<<endl;
stk_for_num.push(s[i] - 48);
if (dot_bool)
dot_count++;
else
num_count++;
}
}
return stk_for_operand.top();
}
// int main()
// {
// float ans;
// // ans=postfix_evaluation("1,2,-,"+to_string(i))+"*");
// ans=postfix_evaluation("0.001,0.5,^");
// cout<<" Answeer is "<<ans;
// return 0;
// }