蒟蒻10分求助,求dalao指点
  • 板块题目总版
  • 楼主AKakA
  • 当前回复1
  • 已保存回复1
  • 发布时间2020/5/10 19:13
  • 上次更新2023/11/7 02:42:04
查看原帖
蒟蒻10分求助,求dalao指点
308587
AKakA楼主2020/5/10 19:13

->题目<-

#include<bits/stdc++.h>
using namespace std;

char a[110],b[110];
int l,t=0,k=1;
stack<char> s;
stack<int> f;
int lev(char x)
{
	if(x=='('||x==')') return 0;
	if(x=='^') return 3;
	if(x=='*'||x=='/') return 2;
	if(x=='-'||x=='+') return 1;
}

void js(char x)
{
	if(x=='+')
	{
		int p=f.top();
		f.pop();
		int q=f.top();
		f.pop();
		f.push(q+p);
	}
	if(x=='-')
	{
		int p=f.top();
		f.pop();
		int q=f.top();
		f.pop();
		f.push(q-p);
	}
	if(x=='*')
	{
		int p=f.top();
		f.pop();
		int q=f.top();
		f.pop();
		f.push(q*p);
	}
	if(x=='/')
	{
		int p=f.top();
		f.pop();
		int q=f.top();
		f.pop();
		f.push(q/p);
	}
	if(x=='^')
	{
		int p=f.top();
		f.pop();
		int q=f.top();
		f.pop();
		f.push(round(pow(q,p)));
	}
	int c[f.size()];
	int m=0;
	while(!f.empty())
	{
		m++;
		c[m]=f.top();
		f.pop();
	}
	while(m!=0)
	{
		cout<<c[m]<<" ";
		f.push(c[m]);
		m--;
	}
	for(int i=k+1;i<=t;i++)
	{
		cout<<b[i]<<" ";
	}
	cout<<endl;
}

int main()
{
	gets(a);
	l=strlen(a);
	for(int i=0;i<l;i++)
	{
		if(a[i]>='1'&&a[i]<='9')
		{
			t++;
			b[t]=a[i];
			cout<<a[i]<<" ";
		}
		if(a[i]=='(')
		{
			s.push(a[i]);
		}
		if(a[i]==')')
		{
			while(s.top()!='(')
			{
				t++;
				b[t]=s.top();
				cout<<s.top()<<" ";
				s.pop();
			}
			s.pop();
		}
		if(a[i]=='-'||a[i]=='+'||a[i]=='*'||a[i]=='/'||a[i]=='^')
		{
			while(!s.empty()&&lev(a[i])<=lev(s.top()))
			{
				t++;
				b[t]=s.top();
				cout<<s.top();
				s.pop();
			}
			s.push(a[i]);
		}
	}
	while(!s.empty())
	{
		t++;
		b[t]=s.top();
		cout<<s.top();
		s.pop();
	}
	cout<<endl;
	f.push(b[k]-'0');
	k++;
	while(k<=t)
	{
		if(b[k]>='0'&&b[k]<='9')
		{
			f.push(b[k]-'0');
		}
		else
		{
			js(b[k]);
		}
		k++;
	}
	return 0;
}
2020/5/10 19:13
加载中...