基本信息
源码名称:实现按照姓名进行排序的算法
源码大小:0.91KB
文件格式:.cpp
开发语言:C/C++
更新时间:2013-08-13
   源码介绍

该程序能够实现按照中文的姓名(字母顺序)进行排序

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME      20 //最大名字长度
#define MAX_NUM        100 //最大学生人数

void sort_bubble(char (*pc)[MAX_NAME],int n)//排序函数
{
 int i,j;
 char str[MAX_NAME];
 for(i=0;i<n-1;i )
 {
  for(j=i 1;j<n;j )
  {
   if(strcmp(pc[i],pc[j])>0)
   {
    strcpy(str,pc[i]);
    strcpy(pc[i],pc[j]);
    strcpy(pc[j],str);
   }
  }
 }
}

void display_name(char (*pc)[MAX_NAME],int n)//显示这n个姓名
{
 int i;
 for(i=0;i<n;i )
 {
  printf("%s\n",pc[i]);
 }
}
int main()
{
    int i=1,n;
 char str[MAX_NUM][MAX_NAME];
 printf("请输入学生总数:");
 scanf("%d",&n);
 while(i<=n)
 {
  printf("请输入第%d个学生姓名:",i);
  scanf("%s",str[i -1]);
 }
 printf("排序前的学生名单如下:\n");
 display_name(str,n);
 sort_bubble(str,n);
 printf("排序后的学生名单如下:\n");
 display_name(str,n);
 system("pause");
    return 0;
}