提交时间:2026-06-12 15:37:04

运行 ID: 91367

#include <bits/stdc++.h> using namespace std ; const int N = 110 ; int cnt = 2 ; int num[N] , top1 = 0 , top2 = 0 ; char op[N] ; string s ; void solve() { int y = num[top1--] , x = num[top1--] ; char ch = op[top2--] ; if(ch == '+') num[++top1] = x + y ; if(ch == '-') num[++top1] = x - y ; if(ch == '*') num[++top1] = x * y ; if(ch == '/') num[++top1] = x / y ; if(ch == '^') num[++top1] = int(pow(x , y)) ; } int id(char x) { if(x == '(') return 0 ; if(x == '+' || x == '-') return 1 ; if(x == '*' || x == '/') return 2 ; return ++cnt ; } int main() { cin >> s ; s = "(" + s + ")" ; for(int i = 0 ; i < s.size() ; i++ ) { if(s[i] == '@') continue ; if(id(s[i]) <= 2 && id(s[i]) >= 1 && id(s[i + 1]) <= 2 && id(s[i + 1]) >= 1) { cout << "NO\n" ; return 0 ; } if(s[i] >= '0' && s[i] <= '9') { string tmp = "" ; while(s[i] >= '0' && s[i] <= '9') tmp += s[i] , i++ ; num[++top1] = stoi(tmp) ; i-- ; } else if(s[i] == '(') op[++top2] = s[i] ; else if(s[i] == ')') { while(op[top2] != '(') solve() ; top2-- ; } else { while(top2 > 0 && id(op[top2]) >= id(s[i])) solve() ; op[++top2] = s[i] ; } } cout << num[top1] << endl ; return 0 ; }