实验报告第三个 中缀表达式转后缀表达式 舞会问题

第一个:中缀表达式转后缀表达式

这两个是我用STL写的,是为了方便代码的简单。

第一个的代码如下:

#include<iostream>
#include<string>  //用的是string来存放一个算式,然后再用str.at()遍历出每一个字符上的值。 所以这里面只能输入个位数的值,等空了再改
#include<stack>
using namespace std;
void main()
{
	stack<char> st;      //泛型,用的是char,因为遍历里用的是str.at(),返回的是char类型的 
	string str;
	int len,temp;
	cout<<"请输入一个算式:"<<endl;
	cin>>str;
	cout<<str<<endl;
	
	len=str.size();//获取输入算式的大小
	for(temp=0;temp<len;temp++)
		cout<<temp<<"  :"<<str.at(temp)<<endl;

	for(temp=0;temp<len;temp++)
	{
		if(str.at(temp)>='0'&&str.at(temp)<='9')//如果输入的是数字
		{
			cout<<str.at(temp)<<" ";
		}
		else 
		{
			if(str.at(temp)=='+' || str.at(temp)=='-')//如果输入的是+,-号
			{
				if(!st.empty() && (st.top()=='*' || st.top()=='/'))
				{
					while(!st.empty())
					{
						cout<<st.top()<<" ";
						st.pop();
					}
					st.push(str.at(temp));
				}
				else
					st.push(str.at(temp));
						
				
			}else if(str.at(temp)=='*' || str.at(temp)=='/')//如果输入的是*,/号
			{
				if(st.empty())
				{
					st.push(str.at(temp));
				}else
					st.push(str.at(temp));
			}
			else if(str.at(temp)=='(' )//如果输入的是(
			{
				st.push(str.at(temp));			
			}

			else if(str.at(temp)==')')如果输入的是)
			{
				while(st.top()!='(')
				{
					cout<<st.top();
					st.pop();
				}
				st.pop();
			}
		}
			
	
	}
	while(!st.empty())
	{
		cout<<st.top()<<" ";
		st.pop();
	}
		cout<<endl;
}
这个写的很不完善.


第二个比较简单。

代码如下:

#include<iostream>
#include<queue>
using namespace std;
void main()
{
	int man,women,count,less;
	cout<<"Please input the man 's number:"<<endl;
	cin>>man;
	cout<<"Please input the women 's number:"<<endl;
	cin>>women;
	
	queue<int> nan;
	queue<int> nv;
	for(int i=0;i<man;i++)
	{
		nan.push(i+1);
	}

	for(i=0;i<man;i++)
	{
		nv.push((i+1)*10);
	}

	cout<<"Please input how many dance have:"<<endl;
	cin>>count;
	
	less=man<women?man:women;
	
	for(int c=0;c<count;c++)
	{
		cout<<"The "<<c+1<<" time start"<<endl;
		for(int k=0;k<less;k++)
		{
			cout<<"nan:"<<nan.front()<<"+nv"<<nv.front()<<endl;
			nan.push(nan.front());
			nv.push(nv.front());
			nan.pop();
			nv.pop();
		}
		cout<<"The "<<c+1<<"time end"<<endl;
		cout<<endl;
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水中加点糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值