链接
其他
JW Media Player
10 Oct.2008

输入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
版权所有©转载时必须以链接形式注明作者和原始出处及本声明!


最新日志 随机日志 综合排行
  • 2011年终总结
  • 2011新的起点
  • 清明,写给张妙两岁孩...
  • 卡扎菲爱他的人民,但...
  • 研究生上完了,终于
  • 给力的一年
  • 让子弹飞,你看懂了几...
  • MFC EDIT控件...
  • 上调个税起征点为什么...
  • 水仙花数
  • 无视中国批评 越南继...
  • 通过调用函数来求两个...
  • 美将派核动力航母参与...
  • 将n个数按输入时顺序...
  • 求a!+b!+c!的...
  • 编写函数MMM(ST...
  • 空间又被封了,国家真...
  • 开学了,空间更新的可...
  • 云南丽江环保纠纷引发...
  • 输入10个整数,将最...
  • 猴子吃桃问题
  • 使用牛顿迭代法求根 ...
  • 输入一个字符串,把其...
  • 两个乒乓球队进行比赛...
  • 中俄与美国要大摊牌了...
  • 用迭代法求a的平方根...
  • 用递归法求n阶勒让德...
  • vhdl 15位m序...
  • 通过调用函数来求两个...
  • CSMA/CA和CS...
  • 时间:12:14 评论(0) 引用(0) 阅读(929) 
    发表评论
      昵称 [注册]
      密码 (游客无需密码)
      网址
      电邮
    OpenID登入 权限选项 表情