-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·222 lines (164 loc) · 5.23 KB
/
main.cpp
File metadata and controls
executable file
·222 lines (164 loc) · 5.23 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// main.cpp
// Project 4 Spring 14
//
// Created by Ibrahima Niang on 5/7/14.
// Copyright (c) 2014 Ibrahima Niang. All rights reserved.
//
#include "stack.h"
#include "stack.cpp"
// take a character as an argument and check if it's an operator(+, -, /, *)
bool isOperators(char);
// take a character as an argument and check if it's an operands(0, 1, 2, 3 ...9)
bool isOperands(char);
// take a character as an argument and determine the order of operation using precedence of the operators
int order(char);
// take two characters as arguments and determine which one have the higher priority
bool isHigher(char, char);
// take two integer arguments as operands and one character as operator, calculate the expression and return it
int operation(int, int, char);
// take an infix expression, evaluate it and return it
int evaluateInfix(char expr);
int main()
{
char str; // variable to hold the expression characters
int result; // variable to hold the result;
cout<<"\t Enter a well-formed expression. The expression may include parenthesis or space : \n\n";
cout<<"\t Exemple: 2*6-356+2*(6/(4-2)) \n\n";
cin>>str;
result=evaluateInfix(str);
cout<<"The result is :\t"<<result<<endl;;
return 0;
}
//*********************** Method isOperators ********************
bool isOperators(char ch)
{
if(ch=='-' || ch=='+' || ch=='*' || ch=='/')
return true;
else return false;
}
//*********************** Method isOperands ********************
bool isOperands(char ch)
{
if(ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7'|| ch=='8' || ch=='9' )
return true;
else return false;
}
//*********************** Method order ********************
int order(char ch)
{
switch (ch)
{
case '+': return (1);
case '-': return (1);
case '*': return (2);
case '/': return (2);
case '(': return (0);
default: return 1 ;
}
}
//*********************** Method isHigher ********************
bool isHigher(char a, char b)
{
if(order(a)>=order(b))
return true;
else
return false;
}
//*********************** Method operation ********************
int operation(int a, int b, char ch)
{
switch (ch)
{
case '+':
return (a+b);
case '-':
return (a-b);
case '*':
return (a*b);
case '/':
return (a/b);
default: return 0;
}
}
//*********************** Method evaluateInfix ********************
int evaluateInfix(char expression)
{
char ch;
string temp;
int numb;
int op1, op2;
stack<char> stackOperators(10);
stack<int> stackOperands(10);
while(expression != '\n')
{
while(isOperands(expression) )
{
temp += expression;
cin.get(expression);
}
istringstream convert(temp);
if ( !(convert >> numb) )
numb = 0;
temp="";
if(numb!=0)
stackOperands.push(numb);
if(isOperators(expression))
{
if((!(stackOperators.isEmpty()) && isHigher(stackOperators.top(), expression)))
{
ch=stackOperators.top();
stackOperators.pop();
op1=stackOperands.top();
stackOperands.pop();
op2=stackOperands.top();
stackOperands.pop();
stackOperands.push(operation(op2, op1, ch));
if(!(stackOperators.isEmpty()) && isHigher(stackOperators.top(), expression))
{
ch=stackOperators.top();
stackOperators.pop();
stackOperators.push(expression);
op1=stackOperands.top();
stackOperands.pop();
op2=stackOperands.top();
stackOperands.pop();
stackOperands.push(operation(op2, op1, ch));
}
else
stackOperators.push(expression);
}
else
stackOperators.push(expression);
}
else if(expression=='(')
stackOperators.push(expression);
else if(expression==')')
{
while(!(stackOperators.top()=='('))
{
ch=stackOperators.top();
stackOperators.pop();
op1=stackOperands.top();
stackOperands.pop();
op2=stackOperands.top();
stackOperands.pop();
stackOperands.push(operation(op2, op1, ch));
}
stackOperators.pop();
}
if(expression!='\n')
cin.get(expression);
}
while(!(stackOperators.isEmpty()))
{
ch=stackOperators.top();
stackOperators.pop();
op1=stackOperands.top();
stackOperands.pop();
op2=stackOperands.top();
stackOperands.pop();
stackOperands.push(operation(op2, op1, ch));
}
return stackOperands.top();
}