#1RE 求调
查看原帖
#1RE 求调
1183074
xzy_AK_IOI楼主2024/8/20 11:25
#include<bits/stdc++.h>
using namespace std;
#define int long long
int Fast_Pow(int a,int b){
	int sum=1;
	while (b){
		if (b&1){
			sum*=a;
		}
		a*=a;
		b>>=1;
	}
	return sum;
}
int check(char d){
	if (d=='+' || d=='-') return 0;
	if (d=='*' || d=='/') return 1;
	if (d=='^') return 2;
} 
int sum(char k,int j,int l){
	if (k=='+') return j+l;
	if (k=='-') return j-l;
	if (k=='*') return j*l;
	if (k=='/') return j/l;
	if (k=='^') return Fast_Pow(j,l);
}
int to_int(string s){
	int d=0;
	reverse(s.begin(),s.end());
	for (int i=0;i<(int)s.size();i++){
		d+=pow(10,i)*(s[i]-'0');
	}
	return d;
}
struct kuohao{
	char h;
	int wei;
};
stack <char> S;
stack <int> S2;
stack <kuohao> S3;
string s;
int leng;
string shu;
signed main()
{
	cin>>s;
	for (int i=0;i<=(int)s.size()-1;i++){
		if (s[i]=='('){
			S3.push((kuohao){'(',i});
		}
		else if (s[i]==')'){
			if (S3.size()) S3.pop();
			else{
				s.erase(i,1);
				i--;
			}
		}
	}
	while (!S3.empty()){
		s.erase(S3.top().wei,1);
		S3.pop();
	}
	leng=s.size();
	for (char i:s){
		if (i>='0' && i<='9') shu+=i;
		else{
			if (shu!=""){
				S2.push(to_int(shu));
			}
			shu="";
		}
		if (i=='(') S.push(i);
		if (i==')'){
			while (S.top()!='('){
				char k=S.top();
				int l=S2.top();
				S2.pop();
				int j=S2.top();
				S2.pop();
				S.pop();
				S2.push(sum(k,j,l));
			}
			S.pop();
		}
		if (i=='+' || i=='-' || i=='*' || i=='/' || i=='^'){
			char t=i;
			while (S.size() && S.top()!='(' && S.top()!=')' && check(t)<=check(S.top())){
				char k=S.top();
				int l=S2.top();
				S2.pop();
				int j=S2.top();
				S2.pop();
				S.pop();
				S2.push(sum(k,j,l));
			}
			S.push(i);
		}
	}
	if (shu!=""){
		S2.push(to_int(shu));
	}
	while (!S.empty()){
		char k=S.top();
		int l=S2.top();
		S2.pop();
		int j=S2.top();
		S2.pop();
		S.pop();
		S2.push(sum(k,j,l));
	}
	cout<<S2.top();
	return 0;
}
/*
1-332*0+4*5^3
*/
2024/8/20 11:25
加载中...