-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfixToPrefix.cpp
147 lines (134 loc) · 4.44 KB
/
InfixToPrefix.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
// InfixToPrefix.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//De convert tu infix sang prefix ta thuc hien cac steps sau:
//step 1: dao nguoc infix expression
//step 2: thuc hien convert infix expression da reversed sang postfix expression
//Qua trinh thuc hien giong nhu convert infix sang postfix thong thuong chi la: can custom 1 vai cho sau day:
//2.1 Khi gap toan tu co precedence thap hon top cua stack thi ta ta chi pop cac toan tu co precedence
// cao hon no trong stack, nghia la khi gap top co precedence tuong duong/ nho hon ta deu dung lai
// va push toan tu dang duyet vao
//2.2 co 1 ngoai le voi toan tu ^ co right-associative nghia la tinh tu phai sang trai, neu gap toan tu nay
// va top bang nhau(top cung la '^') thi ta phai pop ca top do ra roi push toan tu nay vao
#include <iostream>
#include <stack>
#include<string>
#include <cmath>
using namespace std;
bool IsOperand(char op) {
if (op >= 'a' && op <= 'z' || op >= 'A' && op <= 'Z' || op >= '0' && op <= '9')
return true;
return false;
}
int Pre(char op) {
if (op == '^')
return 3;
else if (op == '/' || op == '*')
return 2;
else if (op == '+' || op == '-')
return 1;
else
return -1;
}
string Reverse(string infix) {
string reverse = "";
for (int i = infix.length()-1; i >= 0;i--) {
if (infix[i] == ')') {
reverse += '(';
}
else if (infix[i] == '(') {
reverse += ')';
}
else {
reverse += infix[i];
}
}
return reverse;
}
//sau khi reverse infix expression thi thuc hien shunting yard algorithm chuyen chuoi reversed do
// sang postfix expression, sau do ta lai reverse ket qua(dang la chuoi postfix expression)
// de co duoc prefix expression
string InfixToPrefix(string infix) {
infix = Reverse(infix);
stack<char> operators;
string res = "";
for (int i = 0; i < infix.length();i++) {
if (IsOperand(infix[i])) {
res += infix[i];
}
else if (operators.empty() || operators.top() == '(' || infix[i] == '('
|| Pre(infix[i]) > Pre(operators.top())) {
operators.push(infix[i]);
}
else if (infix[i] == ')') {
while (operators.top()!= '(') {
res += operators.top();
operators.pop();
}
operators.pop();
}
else if(infix[i] == '^') {
while (!operators.empty() && Pre(operators.top()) >= Pre(infix[i])) {
res += operators.top();
operators.pop();
}
operators.push(infix[i]);
}
else {
while (!operators.empty() && Pre(operators.top()) > Pre(infix[i]) && operators.top()!= '(') {
res += operators.top();
operators.pop();
}
operators.push(infix[i]);
}
}
while (!operators.empty()) {
res += operators.top();
operators.pop();
}
return Reverse(res);
}
int Calculator(int top1, int top2, char op) {
switch (op) {
case '+':
return top1 + top2;
case '-':
return top1 - top2;
case '*':
return top1 * top2;
case '/':
return top1 / top2;
case '^':
return (int)pow(top1, top2);
}
}
int PrefixCalculator(string prefix) {
stack<int> res;
for (int i = prefix.length()-1;i >= 0;i--) {
if (IsOperand(prefix[i])) {
res.push(prefix[i]-'0');
}
else {
int top1 = res.top();
res.pop();
int top2 = res.top();
res.pop();
res.push(Calculator(top1, top2, prefix[i]));
}
}
return res.top();
}
int main()
{
string s;
getline(cin, s);
cout << PrefixCalculator(InfixToPrefix(s));
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file