Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
180468 Mino_XIE-谢文凯B班 表达式求值 C++ 解答错误 10 0 MS 264 KB 3153 2024-08-21 13:59:37

Tests(1/10):


#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int INF = 0x3f3f3f3f; #define LL long long #define quick_cin_cout ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define endl '\n' #define I return #define AK 0 #define IOI ; string s="0"; char x; stack<int>vi; stack<char>vc; int T; int cnt1,cnt2,cnt; signed main(){ while(cin>>x){ if(x==' ')continue; s+=(char)x; } for(int i = 0;i < s.size();i ++){ char c = s[i]; if(c=='(')cnt1++; if(c==')')cnt2++; } cnt=max(cnt1,cnt2),cnt++; for(int i = 0;i < cnt-cnt1;i ++) s='('+s; for(int i = 0;i < cnt-cnt2;i ++) s=s+')'; for(int i = 0;i < s.size();i ++){ char c = s[i]; int flag=1; if(c=='-'&&(s[i-1]<'0'||s[i-1]>'9')) flag = -1; if(c>='0'&&c<='9'){ int a = 0; while (i < s.size() && (s[i] >= '0' && s[i] <= '9')) { a = a * 10 + s[i] - '0'; i++; } i--; vi.push(a * flag); }else if(c=='(')vc.push(c); else if (c==')'){ while(!vc.empty() && vc.top() != '(' ){ int b=vi.top(); vi.pop(); int a=vi.top(); vi.pop(); char op=vc.top(); vc.pop(); if(op=='*')vi.push(a*b); if(op=='/')vi.push(a/b); if(op=='+')vi.push(a+b); if(op=='-')vi.push(a-b); if(op=='^')vi.push((int)(pow(a,b))); } vc.pop(); } else if(c=='+'||c=='-'||c=='*'||c=='^'||c=='/'){ while(!vc.empty() && vc.top()!='(' && ((c!='*' && vc.top()!='*' || vc.top()=='*')&&(c!='^' && vc.top()!='^' || vc.top()=='^')&&(c!='/' && vc.top()!='/' || vc.top()=='/'))){ int b=vi.top(); vi.pop(); int a=vi.top(); vi.pop(); char op=vc.top(); vc.pop(); if(op=='*')vi.push(a*b); if(op=='/')vi.push(a/b); if(op=='+')vi.push(a+b); if(op=='-')vi.push(a-b); if(op=='^')vi.push((int)(pow(a,b))); } vc.push(c); } } while(!vc.empty()){ int b=vi.top(); vi.pop(); int a=vi.top(); vi.pop(); char op=vc.top(); vc.pop(); if(op=='*')vi.push(a*b); if(op=='/')vi.push(a/b); if(op=='+')vi.push(a+b); if(op=='-')vi.push(a-b); if(op=='^')vi.push((int)(pow(a,b))); } cout<<vi.top() % 10000<<endl;//输出 } /*Note:*********************** *****************************/ //coder:Mino_XIE1212


测评信息: