(“玛卡巴卡”==“卡皮巴拉”)/*旁观中*/哦

程序员诶嘿害怕拍照  •  2个月前


include <bits/stdc++.h>

using namespace std; int check(char c){

int a=0; if(c=='+'||c=='-'){

a=1;

} if(c=='*'||c=='/'){

a=2;

} if(c=='^'){

a=3;

} return a; } stacknum; stackop; void deal() {

int a, b; b=num.top(); num.pop(); a=num.top(); num.pop(); char p=op.top(); op.pop(); if(p=='+'){

num.push(a+b);

} if(p=='-'){

num.push(a-b);

} if(p=='*'){

num.push(a*b);

} if(p=='/'){

num.push(a/b);

} if(p=='^'){

int d=pow(a,b);
num.push(d);

} }

void ceal(){

string s; getline(cin,s); for (int i=0;i<s.size();i++){ int d=0; if(s[i]>='0'&&s[i]<='9'){

	while(s[i]>='0'&&s[i]<='9'){
		int a=s[i]-'0';
		d=d*10+a;
		i++;
	}
	i--;
	num.push(d);
}
else if(s[i]=='('||s[i]==')'){
	if(s[i]=='('){
		op.push(s[i]);
	}
	else if(s[i]==')'){
		while(op.top()!='('){
			deal();
		}
		op.pop();
	}
}
else{
	while(op.size()>0&&check(op.top())>=check(s[i])){
		deal();
	}
	op.push(s[i]);
}

} while(!op.empty()){

deal();

} cout<<num.top(); } int main(){

ceal(); return 0; }


评论:

请先登录,才能进行评论