倒数第二个点WA其他都AC,求调
查看原帖
倒数第二个点WA其他都AC,求调
1081824
LoveFrieren楼主2024/7/8 12:36
#include<bits/stdc++.h>
using namespace std;
struct typ{
	int t,num;//t 0 num,1 char
	char c;
}in;
stack<char> sig; stack<int> calc;
queue<typ> shi;
string s;
int ans=0,pip=0;
int main(){
	getline(cin,s);
	if(s[0]=='-'){
		s.insert(0,"(0");
		int i=3;
		while(s[i]>='0'&&s[i]<='9')i++;
		s.insert(i,")");
	}
	for(int i=1;i<s.size();++i)
		if(s[i]=='-'&&(s[i-1]!=')')&&( !(s[i-1]>='0'&&s[i-1]<='9') ) ){
			s.insert(i,"(0");
			while(s[i+4]>='0'&&s[i]<='9')i++;
			s.insert(i+4,")");
		}//until here all begin
	for(int i=0;i<s.size();++i){
		if(s[i]>='0'&&s[i]<='9'){
			int nownum=s[i]-'0',ci=i,cn=s[i]-'0';
			while(i!=s.size()-1&&s[++i]>='0'&&s[i]<='9') nownum*=10,nownum+=s[i]-'0';
			in.t=0;in.num=nownum;shi.push(in);
			if(cn==nownum) i=ci;
		}
		else if(sig.empty()&&s[i]!=')'&&s[i]!='(') sig.push(s[i]);
		else if(s[i]=='^'||s[i]=='('){
			sig.push(s[i]);
			if(s[i]=='(') ++pip; 
		}
		else if(s[i]==')'){
			if(!pip) continue;
			else --pip;
			while(sig.top()!='('){
				in.t=1; in.c=sig.top();
				shi.push(in); sig.pop();
			}
			sig.pop();
		}
		else if(s[i]=='*'||s[i]=='/'){
			while(!sig.empty() && (sig.top()=='^'||sig.top()=='*'||sig.top()=='/') ){
				in.t=1;in.c=sig.top();sig.pop();
				shi.push(in);
			}
			sig.push(s[i]);
		}
		else{
			while(!sig.empty()&&sig.top()!='('){
				in.t=1;in.c=sig.top();
				shi.push(in),sig.pop();
			}	
			sig.push(s[i]);
		}
	}
	while(!sig.empty()){
		if(sig.top()=='(') {sig.pop(); continue;}
		in.t=1;in.c=sig.top();
		shi.push(in);sig.pop();
	}
//	while(!shi.empty()){
//		if(shi.front().t==0)cout<<shi.front().num<<' ';
//		else cout<<shi.front().c<<' ';
//		shi.pop();
//	}
	while(!shi.empty()){
		while(shi.front().t==0)
			calc.push(shi.front().num),shi.pop(); 
        while((!shi.empty()) && (shi.front().t==1) ){
            int a,b;
            a=calc.top();calc.pop();
            b=calc.top();calc.pop();
            switch(shi.front().c){
            	case '+': calc.push(a+b);break;
            	case '-': calc.push(b-a);break;
        		case '*': calc.push(a*b);break;
            	case '/': calc.push(b/a);break;
            	case '^': calc.push((int)pow(b,a));break;
            }
            shi.pop();
        }
	}
	cout<<calc.top();
	return 0;
}

思路是用栈转化成后缀表达式后存到队列里,然后从队列里面取,计算后缀表达式(感觉有点浪费空间,但是本蒟蒻太废了呜呜)现在剩倒数第二个点过不去

2024/7/8 12:36
加载中...