第一个:中缀表达式转后缀表达式
这两个是我用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;
}
}