好吧。可能我写代码也上瘾了。
#include <stdio.h>
#define MAXSTUDENTS 200
typedef struct
{
char* name;
int year;
int month;
int day;
int birthday;
} stutype;
typedef struct
{
stutype stu[MAXSTUDENTS];
int count;
} stulist;
stutype assign(char* name, int year, int month, int day);
stulist sort(stulist list);
main()
{
stulist list;
int n = 1;
int i;
int differ = 1;
list.stu[0] = assign("XiaoZhang", 1994, 5, 1);
list.stu[1] = assign("XiaoLi", 1994, 6, 3);
list.stu[2] = assign("XiaoWang", 1994, 5, 1);
list.stu[3] = assign("XiaoChen", 1994, 6, 3);
list.stu[4] = assign("XiaoDong", 1994, 8, 1);
list.stu[5] = assign("XiaoMing", 1994, 3, 3);
list.stu[6] = assign("XiaoFang", 1994, 5, 1);
list.count = 7;
list = sort(list);
for ( i = 0; i < list.count - 1; i++)
{
if ( list.stu[i + 1].birthday == list.stu[i].birthday)
{
n += 1;
differ = 0;
}
else if ( differ == 0)
{
n +=1;
differ += 1;
}
}
if ( differ != 0) // 多加了一次,去掉
n -= 1;
for ( i = 0; i < list.count; i++)
printf("%s, %d\n", list.stu[i].name, list.stu[i].birthday);
printf("生日相同的学生总数:%d.\n", n);
}
stutype assign(char* name, int year, int month, int day)
{
stutype student;
student.name = name;
student.year = year;
student.month = month;
student.day = day;
student.birthday =
student.year * 10000 + student.month * 100 + student.day;
return student;
} // 学生结构赋值
stulist sort(stulist list)
{
stutype temp;
int i;
int j;
int finish = 0;
for ( i = 0; i < list.count - 1 && finish == 0; i++)
{
finish = 1;
for ( j = 0; j < list.count - 1; j++)
if ( list.stu[j].birthday > list.stu[j + 1].birthday)
{
temp = list.stu[j];
list.stu[j] = list.stu[j + 1];
list.stu[j + 1] = temp;
finish = 0;
}
}
return list;
} // 冒泡排序