| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1420 人关注过本帖
标题:帮我看下这个姓名排序对不对!选择不会就用了冒泡法!
只看楼主 加入收藏
iqyuankong
Rank: 2
等 级:论坛游民
帖 子:68
专家分:10
注 册:2009-10-23
结帖率:92.86%
收藏
已结贴  问题点数:20 回复次数:5 
帮我看下这个姓名排序对不对!选择不会就用了冒泡法!
#include<stdio.h>
#include<string.h>
int main()
{
    char name[5][10],buff[100],t[10];
    int i,j;
    for(i=0;i<5;i++)
    {
        printf("请输入第%d个学生名字:\n",i+1);
        gets(buff);
        if(strlen(buff)>10)
        {printf("the length of the name is large than 10\n");
        continue;}
        strcpy(name[i],buff);
    }
    for(i=0;i<4;i++)
        for(j=0;j<4-i;j++)
            if(strcmp(name[j],name[j+1])<0)
            {
                strcpy(t,name[j]);
                strcpy(name[j],name[j+1]);
                strcpy(name[j+1],t);
            }
    printf("排完顺序是:\n");
    for(i=0;i<5;i++)
        printf("%d:%s\n",i+1,name[i]);
    return 0;
}
搜索更多相关主题的帖子: 选择 冒泡 姓名 
2009-12-27 14:28
hnliji1107
Rank: 4
等 级:业余侠客
帖 子:53
专家分:216
注 册:2009-10-23
收藏
得分:20 
你的程序基本没问题,只是在处理超过10个字符的字符串时,if用的不合适,应修改为:
程序代码:
while(strlen(buff)>10)
  {
      printf("the length of the name is large than 10,input again:\n");
      gets(buff);
  }

2009-12-27 16:12
hnliji1107
Rank: 4
等 级:业余侠客
帖 子:53
专家分:216
注 册:2009-10-23
收藏
得分:0 
另外,本题的选择排序做法:

程序代码:
#include<stdio.h>
#include<string.h>

int main()
{
    char name[5][10],buff[100],t[10];
    int i,j,k;
    for(i=0;i<5;i++)
    {
        printf("请输入第%d个学生名字:\n",i+1);
        gets(buff);
        while(strlen(buff)>10)
        {
            printf("the length of the name is large than 10,input again:\n");
            gets(buff);
        }
        strcpy(name[i],buff);
    }

    for(i=0;i<4;i++)//选择排序
    {
        k=i;
        for(j=i+1;j<5;j++)
            if(strcmp(name[k],name[j])<0) k=j;
        if(k!=i)
        {
            strcpy(t,name[i]);
            strcpy(name[i],name[k]);
            strcpy(name[k],t);
        }
    }

    printf("排完顺序是:\n");
    for(i=0;i<5;i++)
        printf("%d:%s\n",i+1,name[i]);

    return 0;
} 
2009-12-27 16:20
zhengzeqinc
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-12-27
收藏
得分:0 
/***bubble.c 冒泡排序 */
#include<graphics.h>
#include<dos.h>
#define N 10/*定义排序数据为10个*/
#include <math.h>
#include   <time.h>/*显示程序运行时间*/
#include <stdlib.h>
void Init (int a[]);/*初始状态*/
void Close (void);/*图形模式结束*/
void Pr(int a[], int n);/*输出数组*/
void DrawChange(int i,int j);/*画交换箭头*/
void Bubble_sort(int a[],int n);/*冒泡排序*/
void main (void)/*主函数*/
{
    int i,a[N],q,z;
    printf("0 = random\n");/*按0随机产生10个数*/
    printf("1 =enter number\n");/*按1键入10个数*/
    scanf("%d",&q);
                  printf("0 = ascending\n");/*按0升序*/
    printf("1 =descending\n");/*按1降序*/
                 scanf("%d",&z);
    if((q<0)|(q>1))/*判断随机产生数或用户键入*/
         {
          printf("error!then num id outside.\n");/*输出错误*/
         }
                  else
                      {
        if(q<=0)/*随机产生10个数*/
             {
                    printf("%d\t",rand());/*产生随机数*/
                    Init(a);
                                                 Bubble_sort(a,10);
                                  Close();
                                   exit(0);   
               }
                      else/*用户键入10个数*/
                                          {   
                              int i,a[N];
                                            printf("please enter 10 int number\n");/*输出
10个整数*/
                              for (i=0;i<N;i++)
                              scanf("%d",&a[i]);
                              Init(a);
                              Bubble_sort(a,10);/*具体排序*/
                              Close();/*图形模式结束*/
                              exit(0);
                                           }
                      }
}
/*初始*/
void Init (int a[])
{
    int gd=DETECT,gm,i;
    initgraph(&gd,&gm,"");/*初始化图形系统*/
    cleardevice();/*清屏*/
    setcolor(YELLOW);/*设置颜色*/
    outtextxy(210,10,"any key to continue");/*主界面输出的字符
串*/
    Pr(a,40);/*第一次输出数组*/
    getch();/*等待按任意键*/
}
void Close (void)/*图形模式结束*/
{
    getch();/*等待按任意键*/
    closegraph();/*关闭图形模式*/
}
void Pr(int a[],int n)/*输出数组*/
{
    int i;
    char num[5];
    settextstyle(1,0,2);/*设置输出样式*/
    setcolor(GREEN);/*设置输出颜色*/
    for(i=100;i<600;i+=50)/*i控制显示位置和计算数组*/
    {
        sprintf(num,"%d",a[(i-100)/50]);/*将数值转化成
字符串*/
        outtextxy(i,n,num);/*输出字符串*/
    }
}
/*画交换箭头,画五根线组成一双向箭头线*/
void DrawChange(int i,int j)
{
    setcolor(6);
    line(j*50+135,i+8,j*50+130,i+4);/*按给出的坐标位置画直
线*/
    line(j*50+135,i+4,j*50+135+1,i+2);
    line(j*50+135,i+4,j*50+135+1,i+6);
    line(j*50+145,i+4,j*50+145-1,i+2);
    line(j*50+145,i+4,j*50+13450-1,i+6);
}
/*冒泡排序*/
void Bubble_sort(int a[], int n)
{
    int i,j,t,flag,m,x,z;
    char num1[5], num2[5];
                  m=0;
                  x=0;
    for(i=0;i<n-1;i++)/*冒泡排序*/
    {
                                   x=x+1;
        flag=0;/*设置数据交换标志*/
        for(j=0;j<n-1-i;j++)
        {           
            Pr(a,i*40+80);/*输出数*/
            setcolor(BLUE);/*输出两个要比较的数
*/
            sprintf(num1,"%d",a[j]);/*将两个数转换
成字符串输出*/
            outtextxy(100+j*50,i*40+80,num1);
            sprintf(num2,"%d",a[j+1]);
            outtextxy(100+(j+1)*50,i*40+80,num2);
            sleep(1);/*暂停运行一秒*/
                                                       if(z=0)
                                                             {
                        if(a[j]>a[j+1])
                           {
                                                                        m=m+1;
                flag=1;/*置交换标志*/
                DrawChange(i*40+80,j);/*画
交换箭头*/
                setcolor(RED);
                outtextxy
(100+j*50,i*40+80,num1);
                outtextxy(100+(j+1)
*50,i*40+80,num2);
                t=a[j];/*交换*/
                a[j]=a[j+1];
                a[j+1]=t;
                sleep(1);
                setfillstyle
(SOLID_FILL,BLACK);/*黑矩形的方式删除行*/
                bar
(0,i*40+60,640,i*40+100);
                           }
                                     }
                                                     else
                                                                {
                                                                   if(a[j]<a[j+1])
                           {
                                                                        m=m+1;
                flag=1;/*置交换标志*/
                DrawChange(i*40+80,j);/*画
交换箭头*/
                setcolor(RED);
                outtextxy
(100+j*50,i*40+80,num1);
                outtextxy(100+(j+1)
*50,i*40+80,num2);
                t=a[j];/*交换*/
                a[j]=a[j+1];
                a[j+1]=t;
                sleep(1);
                setfillstyle
(SOLID_FILL,BLACK);/*黑矩形的方式删除行*/
                bar
(0,i*40+60,640,i*40+100);
                           }
                                                                 }
                                         }
        Pr(a,i*40+80);
        sleep(1);
        if(flag==0) break;/*如果本次比较没有发生交换则
跳出循环*/
    }
                 printf("degree=%d\n",m);
                 printf("ranks=%d\n",x);
}
2009-12-27 16:21
风之上位
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:55
专家分:156
注 册:2009-12-20
收藏
得分:0 
if(strcmp(name[j],name[j+1])<0)
            {
                strcpy(t,name[j]);
                strcpy(name[j],name[j+1]);
                strcpy(name[j+1],t);
            }
问题可能出在这段  另外的我都测试过了  都没发现什么BUG
2009-12-27 16:37
jiangwu10057
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:341
专家分:667
注 册:2009-3-25
收藏
得分:0 
回复 4楼 zhengzeqinc
好帅的程序啊··
2009-12-29 11:20
快速回复:帮我看下这个姓名排序对不对!选择不会就用了冒泡法!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031730 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved