输入n个字符串,将它们按字母按由大到小的顺序排列并输出
#include<iostream>
#include<string>
using namespace std;
int main()
{
const int max=5; //修改max值可以改变排序的字符串数目
string buf[max],tmp;
int n=max-1;
string input(); //定义返回值为string型的子函数
cout<<"pls input the string,and must end with ~!"<<endl;
for(int i=0;i<=max-1;i++)//输入个字符串,必须以~为结束标志
{
cout<<"pls input the "<<i+1<<"th string!"<<endl;
buf[i]=input();
}
for(int i=0;i<=max-2;i++) //开始排序,为从大到小的顺序,冒泡法
{
for(int j=1;j<=n;j++)
{
if(buf[i]<buf[i+j])
{
tmp=buf[i];
buf[i]=buf[i+j];
buf[i+j]=tmp;
}
else ;
}
n--;
}
cout<<"the string with order are:"<<endl;
for(int i=0;i<=max-1;i++) //按顺序输出
cout<<buf[i]<<" ";
cout<<endl;
getchar();
return 0;
}
string input()
{
char cache[100],tmp;
int i=0;
do
{
tmp=getchar();
if(tmp!='~')
{
*(cache+i)=tmp;
i++;
}
else
{
*(cache+i)='\0';
break;
}
}while(1);
getchar();
return cache;
}
作者:菜鸟学编程@Bo-Blog
地址:http://www.node-net.org/read.php?109
版权所有©转载时必须以链接形式注明作者和原始出处及本声明!














编写一程序,将两个字符串
输入n个字符串,把其中以


