'commit'
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
/*
|
||||
中缀逻辑表达式转后缀逻辑表达式
|
||||
|
||||
测试用例:
|
||||
0&(0|1|0)
|
||||
|
||||
答案:
|
||||
001|0|&
|
||||
*/
|
||||
unordered_map<char, int> g{{'|', 1}, {'&', 2}};
|
||||
string s, t;
|
||||
stack<char> stk;
|
||||
|
||||
int main() {
|
||||
cin >> s;
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
if (isdigit(s[i]) || isalpha(s[i]))
|
||||
t += s[i];
|
||||
else if (s[i] == '(')
|
||||
stk.push(s[i]);
|
||||
else if (s[i] == ')') {
|
||||
while (stk.top() != '(') {
|
||||
t += stk.top();
|
||||
stk.pop();
|
||||
}
|
||||
stk.pop();
|
||||
} else {
|
||||
while (stk.size() && g[s[i]] <= g[stk.top()]) {
|
||||
t += stk.top();
|
||||
stk.pop();
|
||||
}
|
||||
stk.push(s[i]);
|
||||
}
|
||||
}
|
||||
while (stk.size()) {
|
||||
t += stk.top();
|
||||
stk.pop();
|
||||
}
|
||||
cout << t << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
// 中缀表达式转后缀表达式
|
||||
/*
|
||||
测试用例1:
|
||||
a+b*c+(d*e+f)*g
|
||||
|
||||
答案:
|
||||
abc*+de*f+g*+
|
||||
|
||||
|
||||
测试用例2:
|
||||
(6+3*(7-4))-8/2
|
||||
|
||||
答案:
|
||||
6 3 7 4 - * + 8 2 / -
|
||||
|
||||
测试用例3:
|
||||
(24*(9+6/3-5)+4)
|
||||
答案:
|
||||
24 9 6 3 / + 5 - * 4 +
|
||||
|
||||
测试用例4:
|
||||
2*3*2+5/3
|
||||
|
||||
*/
|
||||
|
||||
unordered_map<char, int> g{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}}; // 操作符优化级
|
||||
string t; // 结果串
|
||||
stack<char> stk; // 栈
|
||||
// 中缀转后缀
|
||||
string infixToPostfix(string s) {
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
// ①数字
|
||||
if (isdigit(s[i])) {
|
||||
int x = 0;
|
||||
while (i < s.size() && isdigit(s[i])) {
|
||||
x = x * 10 + s[i] - '0';
|
||||
i++;
|
||||
}
|
||||
i--;
|
||||
t += to_string(x), t += ' ';
|
||||
} else if (isalpha(s[i])) // ②字符,比如a,b,c
|
||||
t += s[i], t += ' ';
|
||||
else if (s[i] == '(') // ③左括号
|
||||
stk.push(s[i]); // 左括号入栈
|
||||
else if (s[i] == ')') { // ④右括号
|
||||
while (stk.top() != '(') { // 让栈中元素(也就是+-*/和左括号)一直出栈,直到匹配的左括号出栈
|
||||
t += stk.top(), t += ' ';
|
||||
stk.pop();
|
||||
}
|
||||
stk.pop(); // 左括号也需要出栈
|
||||
} else {
|
||||
// ⑤操作符 +-*/
|
||||
while (stk.size() && g[s[i]] <= g[stk.top()]) {
|
||||
t += stk.top(), t += ' ';
|
||||
stk.pop();
|
||||
}
|
||||
stk.push(s[i]); // 将自己入栈
|
||||
}
|
||||
}
|
||||
// 当栈不为空时,全部输出
|
||||
while (stk.size()) {
|
||||
t += stk.top(), t += ' ';
|
||||
stk.pop();
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string infix = "(24*(9+6/3-5)+4)";
|
||||
string postfix = infixToPostfix(infix);
|
||||
cout << "中缀表达式: " << infix << endl;
|
||||
cout << "后缀表达式: " << postfix << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
测试样例:
|
||||
+ 23 34
|
||||
|
||||
/ + 4 2 * 3 5
|
||||
*/
|
||||
|
||||
// 将前缀表达式转换为后缀表达式(带空格版本)
|
||||
|
||||
// 判断字符是否是操作符
|
||||
bool isOp(string c) {
|
||||
return c == "+" || c == "-" || c == "*" || c == "/";
|
||||
}
|
||||
|
||||
string prefixToPostfix(string s) {
|
||||
stack<string> stk;
|
||||
string c; // 子串
|
||||
vector<string> q;
|
||||
stringstream s1(s); // 构造字符串流,处理带空格的后缀表达式
|
||||
while (s1 >> c) q.push_back(c); // 将每个子串抽取出来
|
||||
|
||||
for (int i = q.size() - 1; i >= 0; i--) {
|
||||
string c = q[i];
|
||||
if (isOp(c)) {
|
||||
string a = stk.top();
|
||||
stk.pop();
|
||||
string b = stk.top();
|
||||
stk.pop();
|
||||
stk.push(a + " " + b + " " + c);
|
||||
} else
|
||||
stk.push(c); // 如果是操作数,将其转换为整数并入栈
|
||||
}
|
||||
return stk.top();
|
||||
}
|
||||
|
||||
int main() {
|
||||
string prefix = "/ + 4 2 * 3 5"; // / + 4 2 * 3 5 --> vector<>={/ + 4 2 * 3 5}
|
||||
string postfix = prefixToPostfix(prefix);
|
||||
cout << "前缀表达式: " << prefix << endl;
|
||||
cout << "后缀表达式: " << postfix << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
// 判断字符是否是操作符
|
||||
bool isOp(string c) {
|
||||
return c == "+" || c == "-" || c == "*" || c == "/";
|
||||
}
|
||||
|
||||
string prefixToPostfix(string s) {
|
||||
stack<string> stk;
|
||||
for (int i = s.size() - 1; i >= 0; i--) {
|
||||
string c = string(1, s[i]);
|
||||
if (isOp(c)) { // 操作符
|
||||
string a = stk.top();
|
||||
stk.pop();
|
||||
string b = stk.top();
|
||||
stk.pop();
|
||||
stk.push(a + b + c);
|
||||
} else
|
||||
stk.push(c); // 字母a~z
|
||||
}
|
||||
return stk.top();
|
||||
}
|
||||
|
||||
int main() {
|
||||
// * + 12 23 * 3 + 45 6
|
||||
string prefix = "*+ab*c+de";
|
||||
string postfix = prefixToPostfix(prefix);
|
||||
cout << "前缀表达式: " << prefix << endl;
|
||||
cout << "后缀表达式: " << postfix << endl;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
Prefix Expression: *+ab*c+de
|
||||
Postfix Expression: ab+cde+**
|
||||
Infix Expression: ((a+b)*(c*(d+e)))
|
||||
*/
|
||||
30
TangDou/CSP-J/J2_2020/ZhuanTi_BiaoDaShi/ToPostFix/TD.cpp
Normal file
30
TangDou/CSP-J/J2_2020/ZhuanTi_BiaoDaShi/ToPostFix/TD.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
bool isOp(string c){
|
||||
return c == "+" || c == "-" || c == "*" || c == "/";
|
||||
}
|
||||
int eval(string s){
|
||||
stack<int> stk;
|
||||
stringstream s1(s);
|
||||
string c;
|
||||
while (s1>>c){
|
||||
if(isOp(c)){
|
||||
int a = stk.top();
|
||||
stk.pop();
|
||||
int b = stk.top();
|
||||
stk.pop();
|
||||
if (c == "+") stk.push(b + a);
|
||||
if (c == "-") stk.push(b - a);
|
||||
if (c == "*") stk.push(b * a);
|
||||
if (c == "/") stk.push(b / a);
|
||||
}else
|
||||
stk.push(stoi(c));
|
||||
}
|
||||
return stk.top();
|
||||
}
|
||||
int main() {
|
||||
string s = "24 9 6 3 / + 5 - * 4 +";
|
||||
cout << "answer:" << eval(s) << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user