-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject.cpp
267 lines (258 loc) · 7.8 KB
/
Project.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: EL-Torky
*
* Created on April 20, 2017, 4:48 PM
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
using namespace std;
// operators stack
struct stackOfChars{
char data;
stackOfChars *next;
stackOfChars(char d){
data = d;
next = NULL;
}
};
// numbers stack
struct stackOfNumbers{
int num;
stackOfNumbers *next;
stackOfNumbers(int no){
num = no;
next = NULL;
}
};
// push operators to the char stack
void push(stackOfChars *&top,char data){
stackOfChars *temp = new stackOfChars(data);
temp->next = top;
top = temp;
}
// push opernads to number stack
void pushNumbers(stackOfNumbers *&top,int data){
stackOfNumbers *temp = new stackOfNumbers(data);
temp->next = top;
top = temp;
}
// pop operators from the char stack
char pop(stackOfChars *&top){
if(top == NULL){return 0;}
char data;
stackOfChars *temp = top;
top = top->next;
data = temp->data;
delete(temp);
return data;
}
// pop operands from the number stack
int popNumbers(stackOfNumbers *&top){
if(top == NULL ){ return 0;}
int data;
stackOfNumbers *temp = top;
top = top->next;
data = temp->num;
delete(temp);
return data;
}
// peek first element of char stack without removing it
char peek(stackOfChars *&top){
if (top == NULL){return 0;}
return top->data;
}
// check the precedence of the operator
int pirotity(char token){
if (token == '^'){
return 3;
}
else if (token == '*' || token == '/'){
return 2;
}
else if (token == '+' || token == '-'){
return 1;
}
}
/* display all elemtns in the stack by poping them all and printing them
Implemented for test purpouse only
not used */
void displayStack(stackOfChars *&top){
if (top == NULL){printf("Empty Stack\n");return;}
while(top != NULL){
printf("%c",pop(top));
}
printf("\n");
}
/* method that covert the infex to postfix and evaluate it then print the results
it takes array of char of size 50 that contains infex forumla*/
void infixToPostFix(char infex[50]){
// intialize the char array to save postfix notation in
char postFix[50];
// for operators and converting infex to postfix only
stackOfChars *operators = NULL;
// for operands and evaluate postfix
stackOfNumbers *evaluation = NULL;
int result = 0;
// for numbers of more than one digit only
int bigNumbers = 0;
// counters to move the index of postfix and infex array
int i,z,y,n;
n = y = i = z = 0;
char token;
bool check = true;
while(infex[i] != '\0'){
if(infex[i] == '(' || infex[i] == ')'
|| infex[i] == '^' || infex[i] == '*'
|| infex[i] == '/' || infex[i] == '+'
|| infex[i] == '-' ){
postFix[z] = ' ';
z++;
if ( infex[i] == ')'){
while(operators != NULL){
char token2 = pop(operators);
if ( token2 == '('){
break;
}
else{
postFix[z] = token2;
z++;
postFix[z] = ' ';
z++;
}
}
}
else if ( (peek(operators) != '^' && infex[i] != '^') &&
pirotity(peek(operators)) >= pirotity(infex[i])
&& peek(operators) != '('){
postFix[z] = pop(operators);
push(operators,infex[i]);
z++;
}
else{
push(operators,infex[i]);
}
token = pop(operators);
while(check){
if ( (token != '^' || peek(operators) != '^' )
&& pirotity(token) <= pirotity(peek(operators))
&& peek(operators) != 0 && peek(operators) != '('
){
postFix[z] = pop(operators);
push(operators,token);
z++;
token = pop(operators);
}
else{
push(operators,token);
check = false;
}
}
check = true;
}
else{
postFix[z] = infex[i];
z++;
}
i++;
}
postFix[z] = ' ';
z++;
// if the stack is not empty we pop them all and put them into the postfix array
while(operators != NULL){
char token2 = pop(operators);
postFix[z] = token2;
z++;
postFix[z] = ' ';
z++;
}
printf("PostFix: \n");
while(postFix[y] != '\0'){
if(postFix[y] == '+' || postFix[y] == '-' || postFix[y] == '/' || postFix[y] == '*'
|| postFix[y] == '^' || postFix[y] == ' ' ||(postFix[y] >= 48 && postFix[y] <= 57) )
{
printf("%c",postFix[y]);
y++;
}
else{
y++;
}
}
printf("\n");
y = 0;
////////////////////////////////////////////////////////////////////////////
// PostFix Evaluation //
////////////////////////////////////////////////////////////////////////////
printf("Evaluation: \n");
while(postFix[y] != '\0'){
if( isdigit( postFix[y] ) ){
n = postFix[y] - '0';
pushNumbers(evaluation,n);
}
else if ( postFix[y] == '+'){
int topOfStack,afterTopOfStack;
topOfStack = popNumbers(evaluation);
afterTopOfStack = popNumbers(evaluation);
result = afterTopOfStack + topOfStack;
pushNumbers(evaluation,result);
}
else if ( postFix[y] == '-'){
int topOfStack,afterTopOfStack;
topOfStack = popNumbers(evaluation);
afterTopOfStack = popNumbers(evaluation);
result = afterTopOfStack - topOfStack;
pushNumbers(evaluation,result);
}
else if ( postFix[y] == '*'){
int topOfStack,afterTopOfStack;
topOfStack = popNumbers(evaluation);
afterTopOfStack = popNumbers(evaluation);
result = afterTopOfStack * topOfStack;
pushNumbers(evaluation,result);
}
else if ( postFix[y] == '^'){
int topOfStack,afterTopOfStack;
topOfStack = popNumbers(evaluation);
afterTopOfStack = popNumbers(evaluation);
result = pow(afterTopOfStack , topOfStack);
pushNumbers(evaluation,result);
}
else if ( postFix[y] == '/'){
int topOfStack,afterTopOfStack;
topOfStack = popNumbers(evaluation);
afterTopOfStack = popNumbers(evaluation);
if(topOfStack == 0){
printf("Cannot divide by zero");
result = 0;
break;
}
result = afterTopOfStack / topOfStack;
pushNumbers(evaluation,result);
}
if (isdigit( postFix[y] ) && isdigit( postFix[y-1] )){
n = popNumbers(evaluation);
bigNumbers = popNumbers(evaluation) * 10;
bigNumbers += n;
pushNumbers(evaluation,bigNumbers);
}
y++;
result = 0;
}
printf("%d",popNumbers(evaluation));
}
int main() {
char tester[50];
printf("Please Enter a mathematical to evaluate it and convert it to postfix\n");
scanf("%s",&tester);
infixToPostFix(tester);
getch();
return 0;
}