Login
Register
Problem list
Online status
sptnk1
:
2023-12-11 00:18:19
#include
#include
#include
#include
using namespace std; int lev1(char tem) { if (tem == '(') return 1; if (tem == '*' || tem == '/') return 5; if (tem == '+' || tem == '-') return 3; if (tem == ')') return 6; return 0; } int lev1(string tem) { if (tem == "(") return 1; if (tem == "*" || tem == "/") return 5; if (tem == "+" || tem == "-") return 3; if (tem == ")") return 6; return 0; } int lev2(string tem) { if (tem == "(") return 6; if (tem == "*" || tem == "/") return 4; if (tem == "+" || tem == "-") return 2; if (tem == ")") return 1; return 0; } vector
back(vector
middle) { stack
tem; tem.push("0"); vector
bac; int i = 0; while (!tem.empty()) { if(i
lev1(te)) { tem.push(middle[i]); i++; } else if (lev2(middle[i]) < lev1(te)) { bac.push_back(te); tem.pop(); if (tem.size() == 1 && i == middle.size()) break; } else if (lev2(middle[i]) == lev1(te)) { if (te == "(") { i++; } tem.pop(); } } } else { while (tem.size() != 1) { bac.push_back(tem.top()); tem.pop(); } break; } } return bac; } vector
creat(string tem) { vector
cre; queue
one; one.push(tem[0]); int i = 1; string two = ""; while (!one.empty()) { if (i != tem.size()) { if (lev1(tem[i]) == lev1(one.front())) { one.push(tem[i]); i++; } else { two = ""; while (!one.empty()) { two += one.front(); one.pop(); } cre.push_back(two); one.push(tem[i]); i++; } } else { if(one.front()!='#') { two = ""; while (!one.empty()) { if (one.front() != '#') { two += one.front(); } one.pop(); } cre.push_back(two); } break; } } return cre; } int si(string tem) { int a = 0; for (int i = 0; i < tem.size(); i++) { a = a * 10 + tem[i] - '0'; } return a; } int calu(vector
tem) { stack
a; int i = 1; a.push(si(tem[0])); while (!a.empty()&&i!=tem.size()) { if (lev1(tem[i]) == 0) { a.push(si(tem[i])); } else if (tem[i] == "+") { int b = a.top(); a.pop(); b += a.top(); a.pop(); a.push(b); } else if (tem[i] == "-") { int b = a.top(); a.pop(); b = a.top()-b; a.pop(); a.push(b); } else if (tem[i] == "*") { int b = a.top(); a.pop(); b *= a.top(); a.pop(); a.push(b); } else if (tem[i] == "/") { int b = a.top(); a.pop(); b = a.top()/b; a.pop(); a.push(b); } i++; } return a.top(); } int main() { int n; string tem; cin >> n; for (int i = 0; i < n; i++) { cin >> tem; vector
tem1 = creat(tem); vector
tem2 = back(tem1); cout << calu(tem2) << endl; } }
61521314吴政邦
:
2022-04-28 16:26:58
#include
#include
#include
using namespace std; const int maxn = 100; int in(char a) { switch (a) { case '#':return 0; case '(':return 1; case '*':case'/': return 5; case'+':case'-':return 3; case')':return 9; } } int out(char a) { switch (a) { case '#':return 0; case '(':return 9; case '*':case'/': return 4; case'+':case'-':return 2; case')':return 1; } } int Operate(int m, int n, char x) { if (x == '+') return m + n; if (x == '-') return n - m; if (x == '*') return m * n; if (x == '/') return n / m; } int cal() { stack
OPND; stack
OPTR; OPTR.push('#'); char ss[2] = "#"; char s[maxn]; cin >> s; int len = strlen(s) + strlen(ss) + 1; strcat_s(s,len,ss); char c = s[0]; int k = 1; while (c != '#' || OPTR.top() != '#') { int y = 0; if (c >= '0' && c <= '9') { while (c >= '0' && c <= '9') { y = y * 10 + (c - '0'); c = s[k++]; } OPND.push(y); } else { if (out(c) > in(OPTR.top())) { OPTR.push(c); c = s[k++]; } if (out(c) == in(OPTR.top())) { OPTR.pop(); c = s[k++]; } if (out(c) < in(OPTR.top())){ char x = OPTR.top(); OPTR.pop(); int m = OPND.top(); OPND.pop(); int n = OPND.top(); OPND.pop(); OPND.push(Operate(m, n, x)); } } } return OPND.top(); } int main() { int T; cin >> T; int* k = new int[T]; for (int i = 0; i < T; i++) { k[i] = cal(); } for (int i = 0; i < T; i++) { cout << k[i] << endl; } return 0; }
61520218
:
2021-05-19 17:46:56
。
Post Your Comment