-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluating.cpp
159 lines (144 loc) · 3.13 KB
/
evaluating.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
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
#include<iostream>
#include<fstream>
#include<vector>
#include<stack>
#include<algorithm>
int priority(char ch);
using namespace std;
struct values{
char ctr;
int x1;
int y1;
int x2;
int y2;
};
int main()
{
int count_char=0,useless,value,max=-9999,min=9999;
values temp;
char expression[20];
char postfix[20];
vector<values> data,nos,operators;
fstream fobj;
stack<char> s;
stack<int> s2;
//FILE HANDLING-storing to "data"
fobj.open("abcd.box",ios::in);
while(!fobj.eof())
{
fobj>>temp.ctr>>temp.x1>>temp.y1>>temp.x2>>temp.y2>>useless;
data.push_back(temp);
count_char++;
}
count_char--;
/*for(int j=0;j<count_char;j++)
{
cout<<data[j].ctr<<" "<<data[j].x1<<" "<<data[j].y1<<" "<<data[j].x2<<" "<<data[j].y2<<endl;
}*/
do
{
//SEGREGATION to "nos" and "operators"
for(int i=0;i<count_char;i++)
{
if(data[i].ctr>=49 && data[i].ctr<=57)
{
nos.push_back(data[i]);
}
else
{
operators.push_back(data[i]);
}
}
//MAKING of the EXPRESSION in INFIX
for(int i=0;i<count_char-1;i++)
{
expression[i]=nos[i/2].ctr;
expression[++i]=operators[i/2].ctr;
}
expression[count_char-1]=nos[(count_char-1)/2].ctr;
expression[count_char]='\0';
cout<<expression<<endl;
//CONVERTING TO POSTFIX
int j=0;
for(int i=0;i<count_char;i++)
{
if(priority(expression[i])==0)
{
postfix[j]=expression[i];
j++;
}
else
{
if(s.empty())
{
s.push(expression[i]);
}
else
{
while(!s.empty() && priority(expression[i])<=priority(s.top()))
{
postfix[j]=s.top();
s.pop();
j++;
}
s.push(expression[i]);
}
}
}
while(!s.empty())
{
postfix[j]=s.top();
s.pop();
j++;
}
postfix[j]='\0';
cout<<postfix<<endl;
//EVALUATING POSTFIX
for(int i=0;i<count_char;i++)
{
if(postfix[i]>=49 && postfix[i]<=57)
{
int num = postfix[i]-'0';
s2.push(num);
}
else
{
int a = s2.top();
s2.pop();
int b = s2.top();
s2.pop();
switch(postfix[i])
{
case '+': s2.push(a+b);
break;
case '-': s2.push(a-b);
break;
case 'x': s2.push(a*b);
break;
case '/': s2.push(a/b);
break;
}
}
}
value=s2.top();
s2.pop();
cout<<value<<endl;
if(value>max)
{
max=value;
}
if(value<min)
{
min=value;
}
}while(std::next_permutation(nos.ctr.begin(),nos.ctr.end());
}
int priority(char ch)
{
if(ch=='/'||ch=='x')
return 2;
else if(ch=='+'|| ch=='-')
return 1;
else
return 0;
}