第一个点WA剩下都AC 求大佬调!!!
查看原帖
第一个点WA剩下都AC 求大佬调!!!
765239
sakura_21楼主2024/7/18 16:27
#include<bits/stdc++.h>//p10473
#define int long long
using namespace std;
string s;
stack<char>k;
stack<int>a;
stack<string>f;
queue<string>q;
string ans;
int qpow(int x,int y)
{
	int ans=1,res=x;
	while(y>0)
	{
		if (y&1)
		{
			ans*=res;
		}
		res*=res;
		y=y>>1;
	}
	return ans;
}
signed main()
{
	cin>>s;
	int len=s.size();
	if (s[0]==')')s.erase(0,1);
	if (s[len-1]=='(')s.erase(len-1,1);
	int l=0;
	for (int i=0;i<len;i++)
	{
		if(s[i]=='(')k.push('(');
		else if (s[i]==')'&&!k.empty())
		{
			k.pop();
		}
		else if (s[i]==')'&&k.empty())
		{
			l++;//cout<<i<<endl;
		}
	}
	if (!k.empty())
	{
		while(!k.empty())
		{
			s+=')';k.pop();len++;
		}
	}
	if (l!=0)
	{
		while(l!=0)
		{
			s.insert(0,"(");len++;
			l--;
		}
	}
	//cout<<1;
//cout<<s<<endl;
	int num=0,ff=0;
	for(int i=0;i<len;i++)
	{
		if(s[i]>='0'&&s[i]<='9')
		{
			num=num*10+s[i]-'0'; ff=1;
		}
		else
		{//cout<<ff<<" "<<num<<endl<<endl;
		//cout<<num<<" "; 
			if (ff==1)	q.push(std::to_string(num));
			num=0;ff=0;
		}
		
		if (s[i]<'0'||s[i]>'9')
		{ff=0;
			string y;y=s[i];
			if (s[i]=='(')f.push("(");
			else if (s[i]==')')
			{
				while(!f.empty()&&f.top()!="(")
				{
					q.push(f.top());//cout<<i<<" "<<f.top()<<endl;
					f.pop();
				}
				f.pop();
			}
			else if (f.empty())f.push(y);
			else if (f.top()=="(")f.push(y);
			else if ((f.top()=="+"||f.top()=="-")&&(s[i]=='*'||s[i]=='/'))f.push(y);
			else if (s[i]=='^')f.push(y);
	
			else if (s[i]=='+'||s[i]=='-')
			{
				while(!f.empty()&&f.top()!="(")
				{
					q.push(f.top());
					f.pop();
				}
			//	f.pop();
				f.push(y);
			}
			else if (s[i]=='*'||s[i]=='/')
			{
				//cout<<1<<s[i]<<endl;
				while(!f.empty()&&f.top()!="("&&f.top()!="+"&&f.top()!="-")
				{
					
					q.push(f.top());
					f.pop();
				}
			//	f.pop();
				f.push(y);
			}
			
		}
	}
//	cout<<ff<<" "<<num<<endl<<endl; 
	if (ff==1)q.push(std::to_string(num));
	ff=0;
	while(!f.empty())
	{
		q.push(f.top());
		f.pop();
	}

	for (int i=0;i<q.size();i++)
	{
	//	if (q.front()=="(")q.pop();
	//	if (q.front()==")")q.pop();
		
		q.push(q.front());q.pop();
	}
	if (q.front()[0]=='-')
	{
	/*	string xx=q.front();q.pop();
		string yy=q.front();q.pop();
		q.push(yy);q.push(xx);
		for (int i=0;i<q.size();i++)
		{//cout<<q.front()<<endl;
			q.push(q.front());q.pop();
		}*/
		cout<<'-';
		q.pop();
		cout<<q.front();
		return 0;
	}
	





	stack<int>um;
	int lq=q.size();
	for (int i=0;i<lq;i++)
	{
		if (q.front()[0]>='0'&&q.front()[0]<='9')
		{
			int sz=stoll(q.front());
			um.push(sz);
			q.pop();
		}
		else if (q.front()=="+"&&um.size()>1)
		{
			int xx=um.top();um.pop();
			int yy=um.top();um.pop();
			um.push(yy+xx);//cout<<xx<<"+"<<yy<<"="<<um.top()<<endl;
			q.pop();
		}
		else if (q.front()=="-"&&um.size()>1)
		{
			int xx=um.top();um.pop();
			um.push(-xx);q.pop();
			//int yy=um.top();um.pop();
		//	um.push(yy-xx);cout<<xx<<"-"<<yy<<"="<<um.top()<<endl;
			//q.pop();
		}
		else if (q.front()=="*"&&um.size()>1)
		{
			int xx=um.top();um.pop();
			int yy=um.top();um.pop();
			um.push(yy*xx);//cout<<xx<<"*"<<yy<<"="<<um.top()<<endl;
			q.pop();
		}
		else if (q.front()=="/"&&um.size()>1)
		{
			int xx=um.top();um.pop();
			int yy=um.top();um.pop();
			um.push(yy/xx);//cout<<xx<<"/"<<yy<<"="<<um.top()<<endl;
			q.pop();
		}
		else if (q.front()=="^"&&um.size()>1)
		{
			int xx=um.top();um.pop();
			int yy=um.top();um.pop();
			um.push(qpow(yy,xx));//cout<<xx<<"^"<<"="<<um.top()<<yy<<endl;
			q.pop();
		}
	}
	int ans=0;
	while(!um.empty())
	{
		ans+=um.top();
		um.pop();
	}
	cout<<ans;
	return 0;
	

	 
	return 0;
}
2024/7/18 16:27
加载中...