比赛 20191211 评测结果 AAAAAAAAAA
题目名称 有括号的算术表达式运算 最终得分 100
用户昵称 Harry Potter 运行时间 0.005 s
代码语言 C++ 内存使用 13.66 MiB
提交时间 2019-12-11 21:16:17
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
char a[300],aa[300],a1[300];
int n,x[105],b[300],top,t;
int main(){
	freopen("ssexpress.in","r",stdin);
	freopen("ssexpress.out","w",stdout);
	cin>>aa;
	n=strlen(aa)-1;
	x[94]=3,x[42]=2,x[47]=2,x[43]=1,x[45]=1,x[40]=0,x[41]=0;
	for(int i=0;i<n;i++){
		if(aa[i]>='0'&&aa[i]<='9'){
			top++;
			a[top]=aa[i];
		}
		else{
			if(aa[i]=='('){
				t++;
				a1[t]=aa[i];
			}
			else{
				if(aa[i]==')'){
					while(a1[t]!='('){
						top++;
						a[top]=a1[t];
						t--;
					}
					t--;
				}
				else{
					while(x[int(a1[t])]>=x[int(aa[i])]&&t!=0){
						top++;
						a[top]=a1[t];
						if(a1[t]!='(') t--;
						else break;
					}
					t++;
					a1[t]=aa[i];
				}
			}
		}
	}
	while(t!=0){
		top++;
		a[top]=a1[t];
		t--;
	}
	n=top,top=0;
	int p,q,tt;
	for(int i=1;i<=n;i++){
		if(a[i]>='0'&&a[i]<='9'){
			top++;
			b[top]=a[i]-'0';
		}
		else{
			p=b[top],top--;
			q=b[top],top--;
			if(a[i]=='+') q=q+p;
			else if(a[i]=='-') q=q-p;
			else if(a[i]=='*') q=q*p;
			else if(a[i]=='/') q=q/p;
			else{
				tt=q;
				for(int j=2;j<=p;j++) q*=tt;
			}
			top++;
			b[top]=q;
		}
	}
	cout<<b[top];
	return 0;
}