Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2529334
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-22 13:24:10

    在主函数中输入5个等长的字符串。用另一个函数对它们进行排序。然后在主函数输出这5个已排好序的字符串。
    根据书中的对整数的选择排序法,修改为对字符串的选择排序法。即可完成。代码如下:
 

#include <stdio.h>

void sort(char (*)[6],int);
int main(int argc,char *argv[])
{
    char str[5][6];
    char (*p)[6] = str;
    int i;
    printf("please input 5 strings(5 character):\n");
    for (i = 0; i < 5; i++)
    {
        scanf("%s",*p++);
    }
    p = str;
    sort(p,5);
    printf("now,the sequence is :\n");
    for (i = 0; i < 5 ;i++)
    {
        printf("%s\n",str[i]);
    }
    
    system("pause");
    return 0;
}

void sort(char (*string)[6],int n)
{
     int i,j,k;
     char temp[6];
     for (i = 0; i < n - 1; i++)
     {
         k = i;
         for(j = i + 1;j < n; j++)
         {
               if (strcmp(string[k],string[j]) > 0)
               {
                  k = j;
               }
         }
         if (k != i)
         {
            strcpy(temp,string[i]);
            strcpy(string[i],string[k]);
            strcpy(string[k],temp);
         }
     }
}


阅读(1301) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~