-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.cpp
More file actions
90 lines (81 loc) · 2.65 KB
/
Copy pathconverter.cpp
File metadata and controls
90 lines (81 loc) · 2.65 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
#include "converter.h"
#include <algorithm>
int Converter::precedence(char op) {
if(op == '+' || op == '-') return 1;
if(op == '*' || op == '/') return 2;
if(op == '^') return 3;
return 0;
}
bool Converter::isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}
std::string Converter::infixToPostfix(const std::string& infix) {
std::stack<char> st;
std::string postfix;
for(char c : infix) {
if(isalnum(c)) {
postfix += c;
} else if(c == '(') {
st.push(c);
} else if(c == ')') {
while(!st.empty() && st.top() != '(') {
postfix += st.top();
st.pop();
}
if(!st.empty()) st.pop(); // pop '('
} else if(isOperator(c)) {
while(!st.empty() && precedence(st.top()) >= precedence(c)) {
postfix += st.top();
st.pop();
}
st.push(c);
}
}
while(!st.empty()) {
postfix += st.top();
st.pop();
}
return postfix;
}
std::string Converter::infixToPrefix(const std::string& infix) {
std::string reversedInfix = infix;
std::reverse(reversedInfix.begin(), reversedInfix.end());
for(auto& c : reversedInfix) {
if(c == '(') c = ')';
else if(c == ')') c = '(';
}
std::string reversedPostfix = infixToPostfix(reversedInfix);
std::reverse(reversedPostfix.begin(), reversedPostfix.end());
return reversedPostfix;
}
std::string Converter::postfixToInfix(const std::string& postfix) {
std::stack<std::string> st;
for(char c : postfix) {
if(isalnum(c)) {
st.push(std::string(1, c));
} else if(isOperator(c)) {
if(st.size() < 2) throw std::runtime_error("Malformed expression");
std::string b = st.top(); st.pop();
std::string a = st.top(); st.pop();
st.push("(" + a + c + b + ")");
}
}
if(st.size() != 1) throw std::runtime_error("Malformed expression");
return st.top();
}
std::string Converter::prefixToInfix(const std::string& prefix) {
std::stack<std::string> st;
for(auto it = prefix.rbegin(); it != prefix.rend(); ++it) {
char c = *it;
if(isalnum(c)) {
st.push(std::string(1, c));
} else if(isOperator(c)) {
if(st.size() < 2) throw std::runtime_error("Malformed expression");
std::string a = st.top(); st.pop();
std::string b = st.top(); st.pop();
st.push("(" + a + c + b + ")");
}
}
if(st.size() != 1) throw std::runtime_error("Malformed expression");
return st.top();
}