作者:夏晓林
时间:2016.12
问题描述:
定义存放一个学生信息的结构体类型,学生信息包括:姓名,学号,性别,院系,要求在随意输入5个学生的相关信息后,按照学号从小到大顺序输出这些学生的信息。
输入:先输入5名学生的信息,按学号顺序排序输出后在输入要统计的院系名称
输出:先按学号从小到大的顺序输出学生信息
问题及代码:
#include <stdio.h>
#include <string.h>
#define n 5
struct Student
{
char name[20];
char num[20];
char sex;
char college[20];
};
int main()
{
int i,j;
struct Student str[n],temp;
for(i=0; i<5; i++)
scanf("%s %s %c %s",str[i].name,str[i].num,&str[i].sex,str[i].college);
for (j = 0; j < n - 1; j++)
for (i = 0; i < n - 1 - j; i++)
{
if(strcmp(str[i].num,str[i+1].num)>0)
{
temp = str[i];
str[i] = str[i + 1];
str[i + 1] = temp;
}
}
for(i=0; i<n; i++)
printf("%s %s %c %s\n",str[i].name,str[i].num,str[i].sex,str[i].college);
return 0;
}
运行结果:
学习心得:
在输入性别的时候是一个字符,用符号c表示,运用了冒泡排序的思想。字符串比较函数必须有头文件。