-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
92 lines (92 loc) · 1.66 KB
/
source.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
void convertInfixToPostfix(char* P, char* Q)//((6+4)/2+7)/3-(1+2)*2)
{ //1*2+3*((4-5)+6)/7
//3*(2+6*2/3–1)–2*3/2+1
Stack<char> s;
char tmp;
int j = 0;
initStack(s);
push(s, '(');//Them ( vao stack
showStack(s);
for (int i = 0; i < strlen(P); i++)
{
//p = createNode(P[i]);
if (P[i] >= '0' && P[i] <= '9')//Neu P la toan hang
{
Q[j++] = P[i];
}
else
{
if (P[i] == '(')//Neu P la dau ( thi day vao stack
{
push(s, P[i]);
showStack(s);
}
else
{
if (P[i] == '^' || P[i] == '*' || P[i] == '/' || P[i] == '+' || P[i] == '-')//Neu P la toan tu
{
while (s.top != NULL && checkPriority(P[i]) <= checkPriority(s.top->info) && s.top->info != '(')//Kiem tra do uu tien
{
Q[j++] = pop(s);
}
push(s, P[i]);
showStack(s);
}
else
{
if (P[i] == ')')
{
while (s.top != NULL && s.top->info != '(')
{
Q[j++] = pop(s);
showStack(s);
}
pop(s);
showStack(s);
}
}
}
}
}
while (s.top != NULL && s.top->info != '(')
{
Q[j++] = pop(s);
showStack(s);
}
showStack(s);
Q[j] = '\0';
}
int calc(char op, int x, int y)
{
switch (op)
{
case '+': return x + y;
case '-': return x - y;
case '*': return x * y;
case '/': return x / y;
}
}
int calculateInfix(char* Q)
{
int x, y, result;
Stack<int> s;
initStack(s);
for (int i = 0; i < strlen(Q); i++)
{
if (Q[i] >= '0' && Q[i] <= '9')
{
push(s, Q[i] - 48);
}
else
{
if (Q[i] == '^' || Q[i] == '*' || Q[i] == '/' || Q[i] == '+' || Q[i] == '-')
{
x = pop(s);
y = pop(s);
result = calc(Q[i], y, x);
push(s, result);
}
}
}
return pop(s);
}