记录编号 547767 评测结果 AAAAAAAAAA
题目名称 有括号的算术表达式运算 最终得分 100
用户昵称 Gravatarfmq03 是否通过 通过
代码语言 C++ 运行时间 0.005 s
提交时间 2019-12-13 22:02:38 内存使用 13.66 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
#include<string>
using namespace std;
stack<char>ope;
stack<long>nums;
int order(char ch){
	if(ch==')')return 0;
	if(ch=='^')return 3;
	if(ch=='*'||ch=='/')return 2;
	if(ch=='+'||ch=='-')return 1;
	if(ch=='(')return 0;
}

void calcc(char ch){
	int b=nums.top();nums.pop();
	int a=nums.top();nums.pop();
	if(ch=='^')nums.push(pow(a,b));
	if(ch=='*')nums.push(a*b);
	if(ch=='/')nums.push(a/b);
	if(ch=='+')nums.push(a+b);
	if(ch=='-')nums.push(a-b);
//	cerr<<"nums.push"<<nums.top()<<endl;
	return;
}

void inope(char ch){
	while(ope.empty()==false &&order(ch)<=order(ope.top())){
		if(ope.top()=='(')break;
		calcc(ope.top());
		ope.pop();	
	}
	if(ope.empty()!=true && ch==')'){
		ope.pop();
	}else{
		ope.push(ch);
//		cerr<<"ope.push"<<ch<<endl;
	}
}
int main(){
	freopen("ssexpress.in","r",stdin);
	freopen("ssexpress.out","w",stdout);
	string str="";
	cin>>str;
//	cerr<<str;
	for(int i=0;i<str.length()-1;++i){
		char ch=str[i];	
		if(ch>='0' && ch<='9'){	
			nums.push(ch-'0');
//			cerr<<"num.push"<<ch<<endl;
		}else if(ch=='('){
			ope.push(ch);
//			cerr<<"ope.push"<<ch<<endl;
		}else{			
			inope(ch);
			
		}
	}
	while(!ope.empty()){
		//cerr<<ope.top()<<endl;
		calcc(ope.top());
		ope.pop();
	}
	cout<<nums.top()<<endl;
	return 0;
}