-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpressions.cpp
More file actions
207 lines (177 loc) · 4.59 KB
/
Expressions.cpp
File metadata and controls
207 lines (177 loc) · 4.59 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<bits/stdc++.h>
#include "var.cpp"
using namespace std;
//Function to return precedence of operators
class Post// for converting a given infix expression t postfix expression
{
int prec(char ch) {
switch (ch) {
case '^': return 6;
case '/':
case '%':
case '*': return 5;
case '+':
case '-': return 4;
case '>':
case '<': return 3;
case '=':
case '~': return 2;
case '&':
case '|': return 1;
case ':': return -1;
default : return 0;
}
}
public:
void infixToPostfix(string &s)// & operator because string should be returned to main, it is not an array
{
string ns;
stack<char> st;
st.push('N');
int l = s.length();
for(int i = 0; i < l; i++)
{
// If the scanned character is an operand, add it to output string.
if(isalnum(s[i])){
int j;
for(j = 1; isalnum(s[i+j]); j++);
ns += s.substr(i,j);
i += j-1;
ns += (string)" ";
}
// If the scanned character is an �(�, push it to the stack.
else if(s[i] == '(')
st.push('(');
// If the scanned character is an �)�, pop and to output string from the stack
// until an �(� is encountered.
else if(s[i] == ')')
{
while(st.top() != 'N' && st.top() != '(')
{
char c = st.top();
st.pop();
ns += c;
ns += ' ';
}
if(st.top() == '(')
{
char c = st.top();
st.pop();
}
}
//If an operator is scanned
else{
while(st.top() != 'N' && prec(s[i]) <= prec(st.top()))
{
char c = st.top();
st.pop();
ns += c;
ns +=' ';
}
st.push(s[i]);
}
}
//Pop all the remaining elements from the stack
while(st.top() != 'N')
{
char c = st.top();
st.pop();
ns += c;
ns += ' ';
}
s = ns;
}
};// end of class post
double s[50];
int top=-1; /* Global declarations */
int flag=0;
double pop()
{ /* Function for POP operation */
return(s[top--]);
}
void push(double elem) //double
{ /* Function for PUSH operation */
if(flag==1){
double num; //double
num=pop();
s[++top]=10*num+elem;
}
else if(flag==0){
s[++top]=elem;
flag=1;
}
}
double eval(char* pofx)// double
{ /* Main Program */
char ch;
int i=0;
double op1,op2;//double
while( (ch=pofx[i++]) != 0)
{
if(isdigit(ch)) push(ch-'0'); /* Push the operand */ // double
else if(isalpha(ch)){
int j;
for(j = 1; pofx[i+j-1] != ' '; j++);
string st(pofx);
st = st.substr(i-1,j);
s[++top] = atof(prime->returnval(st).c_str());
i += j;
}
else if(ch==' ')
flag=0;
else if (ch!=0)
{
/* Operator,pop two operands */
flag=0;
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':if(op2==0)
{
cout<<"error: division by 0"<<endl;
break;
}
push(op1/op2);break;
case '%':push(fmod(op1,op2));break;
case '^':push(pow(op1,op2));break;
case '>':push(op1>op2);break;
case '<':push(op1<op2);break;
case '=':push(op1==op2);break;
case '~':push(op1!=op2);break;
case '&':push(op1&&op2);break;
case '|':push(op1||op2);break;
default:
cout<<"Input invalid ... give proper input\n";
return 0;
}
}
}
return s[top];
}
void clear_s(){
top = -1;
}
/*Driver program to test above functions
int main()
{
string infix;
Post p;
cout<<"\nEnter an expression to convert to postfix:\n";
cin>>infix;
cout<<endl;
p.infixToPostfix(infix);
cout<<"\nPostfix Expression :: "<<infix;
cout<<endl;
//int result=eval("10 12 + 3 4 - +");
double result=eval(&infix[0u]);
cout<<"result="<<result;
}
*/