一、问题描述
自己用c语言结构体和数组写了一个对学生简单的分类小程序,包括姓氏相同,年龄相同,身高相仿,体重相仿,运动爱好相同,喜欢的食堂相仿。自己写的比较复杂,有什么可以改善的欢迎讨论。
二、代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Num 20
typedef struct{ //生成一个学生信息结构体
char name[2][Num]; //姓,名
int old; //年龄
double height; //身高
double weight; //体重
char sport_hobby[30]; //运动爱好
char canteen[30]; //喜欢哪个食堂
}student;
void firstname_sort(student stu[]);
void Old_sort(student stu[]);
void height_sort(student stu[]);
void weight_sort(student stu[]);
void sporthooby_sort(student stu[]);
void canteen_sort(student stu[]);
int main()
{
student stu[Num]={{"chen","zhou",21,170,130,"basketball","zhongxing"},
{"jiang","yuguo",22,172.23,155.4,"badminton","qianxihe"},
{"wang","hong",23,180.5,149.3,"soccer","honggaoliang"},
{"chen","yuqi",23,167,110,"pingpong","zhongxing"},
{"xu","jing",22,158,100,"pingpong","xingyeyuan"},
{"liu","qiang",21,177.5,120.5,"basketball","qianxihe"},
{"yang","qinghua",19,173.5,119.6,"soccer","honggaoliang"},
{"jiang","feng",20,172,108,"badminton","xingyeyuan"},
{"xu","hao",19,174,119,"vollyball","qianxihe"},
{"wang","qi",20,169,110,"pingpong","zhongxing"},
{"chen","zhi",21,177,112,"vollyball","honggaoliang"},
{"liu","li",23,185,128,"basketball","daxibei"},
{"xu","chaoyang",21,177,130,"soccer","honggaoliang"},
{"jiang","ke",20,183,144,"basketball","zhongxing"},
{"wang","fan",22,172,140,"badminton","yansheng"},
{"liu","handing",21,172,138,"soccer","yansheng"},
{"yang","xiao",20,177,128,"pingpong","daxibei"},
{"xu","zhong",23,182,140,"badminton","yansheng"},
{"wang","keyi",19,168,100,"pingpong","qianxihe"},
{"liu","dexuan",23,175,130,"basketball","zhongxing"}
};
int i;
// printf("This is all of students information:\n");
// for(i=0;i<20;i++)
// {
// printf("student[%d] name:%s%s,old:%d,height:%lf,weight:%lf,sport:%s,canteen:%s\n",i+1,stu[i].name[0],stu[i].name[1],
// stu[i].old,stu[i].height,stu[i].weight,stu[i].sport_hobby,stu[i].canteen);
// }
printf("Please choose a way of sort:\n");
int choose;
scanf("%d",&choose);
switch(choose)
{
case 1:
printf("The way of sorting is students with same first name:\n");
printf("All of thse students' information of name:\n");
for(i=0;i<20;i++)
{
printf("student[%d] name:%s %s\n",i+1,stu[i].name[0],stu[i].name[1]);
}
firstname_sort(stu);
break;
case 2:
printf("The way of sorting is students with same old:\n");
printf("All of thse students' information of old:\n");
for(i=0;i<20;i++)
{
printf("student[%d] old:%d\n",i+1,stu[i].old);
}
Old_sort(stu);
break;
case 3:
printf("The way of sorting is students with same scope of height :\n");
printf("All of thse students' information of height:\n");
for(i=0;i<20;i++)
{
printf("student[%d] ,height:%lf\n",i+1,stu[i].height);
}
height_sort(stu);
break;
case 4:
printf("The way of sorting is students with same scope of weight :\n");
printf("All of thse students' information of weight:\n");
for(i=0;i<20;i++)
{
printf("student[%d] ,weight:%lf\n",i+1,stu[i].weight);
}
weight_sort(stu);
break;
case 5:
printf("The way of sorting is students with same sporthooby:\n");
printf("All of thse students' information of sport hobbies:\n");
for(i=0;i<20;i++)
{
printf("student[%d],sport:%s\n",i+1,stu[i].sport_hobby);
}
sporthooby_sort(stu);
break;
case 6:
printf("The way of sorting is students with same fond of canteen:\n");
printf("All of thse students' information of canteen:\n");
for(i=0;i<20;i++)
{
printf("student[%d] ,canteen:%s\n",i+1,stu[i].canteen);
}
canteen_sort(stu);
break;
default:
printf("Illegal input!\n");
}
return 0;
}
void firstname_sort(student stu[]) //按照姓氏分类
{
int i,j;
int len=1;
int count[Num]={0};//定义一个整数数组用来代表姓氏,存储整数的种类就是数的种类,某一个数\
的个数,就是某种姓氏的个数
for(i=0;i<20;i++)
{
if(count[i]==0)//如果count[i]=0就代表这个数还没有出现过
{
count[i]=len;
for(j=i;j<19;j++)
{
if(strcmp(stu[i].name[0],stu[j+1].name[0])==0)//连个姓氏字符串完全相同
count[j+1]=len; //这两个姓氏相同,对应count[]上面的数值改变
}
len++; //len++代表这种姓氏的已经统计完,开始统计下一个姓氏
}
}
int max=count[0]; //定义max代表这个数组存的最大的数,同时代表一共有多少种数字
for(i=1;i<20;i++)
{
if(count[i]>max)
max=count[i];
}
printf("there are %d groups\n",max);
for(i=1;i<=max;i++)
{
int flag=1;
int amount=0; //统计某种姓氏有多少个学生
printf("group %d studtents are:\n",i);
for(j=0;j<20&&(flag);j++) //找到第一个这种姓氏的学生就跳出循环
{
if(count[j]==i) //为了找到i这种姓氏的第一个学生的下标
{
printf("this group students' first name is %s\n",stu[j].name[0]);
flag=0;
}
}
for(j=0;j<20;j++)
{
if(count[j]==i)
{
printf("student[%d]\t",j+1);
amount++;
}
}
printf("\n");
printf("The student's amount of this group is %d\n",amount);
}
}
void Old_sort(student stu[])
{
int i,j;
int len=1;
int count[Num]={0};
for(i=0;i<20;i++)
{
if(count[i]==0)
{
count[i]=len;
for(j=i;j<19;j++)
{
if(stu[i].old==stu[j+1].old)
count[j+1]=len;
}
len++;
}
}
int max=count[0];
for(i=1;i<20;i++)
{
if(count[i]>max)
max=count[i];
}
printf("there are %d groups\n",max);
for(i=1;i<=max;i++)
{
int amount=0;
int flag=1;
printf("group %d studtents are:\n",i);
for(j=0;j<20&&(flag);j++)
{
if(count[j]==i)
{
printf("this group students' old is %d\n",stu[j].old);
flag=0;
}
}
for(j=0;j<20;j++)
{
if(count[j]==i)
{
printf("student[%d]\t",j+1);
amount++;
}
}
printf("\n");
printf("The student's amount of this group is %d\n",amount);
}
}
void height_sort(student stu[])
{
int count[Num];
int i;
for(i=0;i<20;i++)
{
if(stu[i].height<=170.0){
count[i]=1;
}else if(stu[i].height>170.0&&stu[i].height<=175.0){
count[i]=2;
}else if(stu[i].height>175&&stu[i].height<=180.0){
count[i]=3;
}else if(stu[i].height>=180.0){
count[i]=4;}
}
printf("there are 4 groups\n");
int j;
for(i=1;i<=4;i++)
{
int amount=0;
printf("group %d studtents are:\n",i);
if(i==1){
printf("this group students' height is <= 170:\n");
}else if(i==2){
printf("this group students' height is >170 and <=175:\n");
}else if(i==3){
printf("this group students' height is >175 and <=180:\n");
}else if(i==4){
printf("this group students' height is >180:\n");}
for(j=0;j<20;j++)
{
if(count[j]==i)
{
printf("student[%d]\t",j+1);
amount++;
}
}
printf("\n");
printf("The student's amount of this group is %d\n",amount);
}
}
void weight_sort(student stu[])
{
int count[Num];
int i;
for(i=0;i<20;i++)
{
if(stu[i].weight<=110.0)
{
count[i]=1;
}else if(stu[i].weight>110.0&&stu[i].weight<=120.0){
count[i]=2;
}else if(stu[i].weight>120&&stu[i].weight<=130.0){
count[i]=3;
}else if(stu[i].weight>130&&stu[i].weight<=140.0){
count[i]=4;
}else if(stu[i].weight>140){
count[i]=5;
}
}
printf("there are 5 groups\n");
int j;
for(i=1;i<=5;i++)
{
int amount=0;
printf("group %d studtents are:\n",i);
if(i==1){
printf("this group students' weight is <= 110:\n");
}
else if(i==2)
{
printf("this group students' weight is >110 and <=120:\n");
}
else if(i==3)
{
printf("this group students' weight is >120 and <=130:\n");
}
else if(i==4)
{
printf("this group students' weight is >130 and <=140:\n");
}
else if(i==5)
{
printf("this group students' weight is >140:\n");
}
for(j=0;j<20;j++)
{
if(count[j]==i)
{
printf("student[%d]\t",j+1);
amount++;
}
}
printf("\n");
printf("The student's amount of this group is %d\n",amount);
}
}
void sporthooby_sort(student stu[])
{
int i,j;
int len=1;
int count[Num]={0};
for(i=0;i<20;i++)
{
if(count[i]==0)
{
count[i]=len;
for(j=i;j<19;j++)
{
if(strcmp(stu[i].sport_hobby,stu[j+1].sport_hobby)==0)
count[j+1]=len;
}
len++;
}
}
int max=count[0];
for(i=1;i<20;i++)
{
if(count[i]>max)
max=count[i];
}
printf("there are %d groups\n",max);
for(i=1;i<=max;i++)
{
int amount=0;
int flag=1;
printf("group %d studtents are:\n",i);
for(j=0;j<20&&(flag);j++)
{
if(count[j]==i)
{
printf("this group students' sport_hobby is %s\n",stu[j].sport_hobby);
flag=0;
}
}
for(j=0;j<20;j++)
{
if(count[j]==i)
{
printf("student[%d]\t",j+1);
amount++;
}
}
printf("\n");
printf("The student's amount of this group is %d\n",amount);
}
}
void canteen_sort(student stu[])
{
int i,j;
int len=1;
int count[Num]={0};
for(i=0;i<20;i++)
{
if(count[i]==0)
{
count[i]=len;
for(j=i;j<19;j++)
{
if(strcmp(stu[i].canteen,stu[j+1].canteen)==0)
count[j+1]=len;
}
len++;
}
}
int max=count[0];
for(i=1;i<20;i++)
{
if(count[i]>max)
max=count[i];
}
printf("there are %d groups\n",max);
for(i=1;i<=max;i++)
{
int amount=0;
int flag=1;
printf("group %d studtents are:\n",i);
for(j=0;j<20&&(flag);j++)
{
if(count[j]==i)
{
printf("this group students' fond of canteen is %s\n",stu[j].canteen);
flag=0;
}
}
for(j=0;j<20;j++)
{
if(count[j]==i)
{
printf("student[%d]\t",j+1);
amount++;
}
}
printf("\n");
printf("The student's amount of this group is %d\n",amount);
}
}
总结
感觉还是写的比较复杂,后面有时间改简单一些。