比赛 板子大赛 评测结果 C
题目名称 表达式求值 最终得分 0
用户昵称 xxz 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2025-01-22 16:32:51
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
bool pr[128][128];
int init(){
	pr['+']['+']=0;pr['+']['-']=0;pr['+']['*']=1;pr['+']['/']=1;pr['+']['^']=1;pr['+']['(']=1;
	pr['-']['+']=0;pr['-']['-']=0;pr['-']['*']=1;pr['-']['/']=1;pr['-']['^']=1;pr['-']['(']=1;
	pr['*']['+']=0;pr['*']['-']=0;pr['*']['*']=0;pr['*']['/']=0;pr['*']['^']=1;pr['*']['(']=1;
	pr['/']['+']=0;pr['/']['-']=0;pr['/']['*']=0;pr['/']['/']=0;pr['/']['^']=1;pr['/']['(']=1;
	pr['(']['+']=1;pr['(']['-']=1;pr['(']['*']=1;pr['(']['/']=1;pr['(']['^']=1;pr['(']['(']=1;
	pr['@']['+']=1;pr['@']['-']=1;pr['@']['*']=1;pr['@']['/']=1;pr['@']['^']=1;pr['@']['(']=1;
	return 0;
}
int main(){
	freopen ("calc.in","r",stdin);freopen ("calc.out","w",stdout);
	init();
	stack<int>st;
	stack<char>fh;
	string a;
	cin>>a;
	int ans=0;
	fh.push('@');
	for (int i=0;i<a.length();i++){
		if ('0'<=a[i]&&a[i]<='9'){
			int num=0;
			while('0'<=a[i]&&a[i]<='9'){
				num*=10;num+=a[i]-'0';
				i++;
			}
			st.push(num);
		}
		if (a[i]=='('){
			fh.push(a[i]);
		}
		if (a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='^'){
			while(pr[fh.top()[a[i]]==0){
				int x=st.top]();st.pop();
				int y=st.top();st.pop();
				int z=0;char t=fh.top();fh.pop();
				if (t=='+')z=x+y;
				if (t=='-')z=y-x;
				if (t=='*')z=x*y;
				if (t=='/')z=y/x;
				if (t=='^')z=pow(y,x);
				st.push(z);
			}
			fh.push(a[i]);
		}
		if (a[i]==')'){
			char t=fh.top();fh.pop();
			while(t!='('){
				int x=st.top();st.pop();
				int y=st.top();st.pop();
				int z=0;
				if (t=='+')z=x+y;
				if (t=='-')z=y-x;
				if (t=='*')z=x*y;
				if (t=='/')z=y/x;
				if (t=='^')z=pow(y,x);
				st.push(z);
				t=fh.top();fh.pop();
			}
		}
	}
	while(fh.top()!='@'){
		int x=st.top();st.pop();
		int y=st.top();st.pop();
		int z=0;char t=fh.top();fh.pop();
		if (t=='+')z=x+y;
		if (t=='-')z=y-x;
		if (t=='*')z=x*y;
		if (t=='/')z=y/x;
		if (t=='^')z=pow(y,x);
		st.push(z);
	}
	cout<<st.top()<<endl;
	return 0;
}