求好人讲解C语言代码每一句话。这个简单点
#include<stdio.h>#include<stdlib.h>
#include<string.h>
struct STUDENT
{
char name [10];
char stu_no[10];
int age;
char sex;
float math, English,c_language;
float total,average_score ;
};
void display_menu()
{
printf("==================================\n");
printf(" 1:学生基本信息输入 \n");
printf(" 2:学生基本信息输出 \n");
printf(" 3:按学生年龄查找 \n");
printf(" 4:按学生年龄排序 \n");
printf(" 0:退出系统 \n");
printf("==================================\n");
}
void input_info(struct STUDENT*s,int length)
{
int i;
for (i=0;i<length;i++)
{
//while(getchar()!='\n');
//getchar()----跨过一个回车符
printf("***请输入第%d个同学的信息:\n",i+1);
printf("姓名:");
scanf("%s",&s->name);
printf("学号:");
scanf("%s",&s->stu_no);
printf("年龄:");
scanf("%d",&s->age);
printf("性别: ");
scanf("%s",&s->sex );
printf("数学:");
scanf("%f",&s->math);
printf("英语:");
scanf("%f",&s->English);
printf("C语言:");
scanf("%f",&s->c_language);
s->total=s->math+s->English+s->c_language;
s->average_score=s->total/3;
s++;
}
}
void output_info(struct STUDENT*s,int length)
{
int i;
for(i=0;i<length;i++)
{
printf("********************************************\n");
printf("|姓名|学号|年龄|性别|英语|C语言|总分|平均分 \n");
printf("********************************************\n");
printf("|%4s|%4s|%4d|%4c|%4.0f|%4.0f|%4.0f|%4.0f|%4.0f\n",s->name,s->stu_no,s->age,s->sex,s->math,s->English,s->c_language,s->total,s->average_score);
printf("********************************************\n");
s++;
}
}
//按年龄查找
void query_info(struct STUDENT*s,int length)
{
int i,result=0,s_age;
printf("请输入要查找的年龄:");
scanf("%d",&s_age);
for(i=0;i<length;i++,s++)
{
if(s->age==s_age)
{
printf("********************************************\n");
printf("|姓名|学号|年龄|性别|英语|C语言|总分|平均分 \n");
printf("********************************************\n");
printf("|%4s|%4s|%4d|%4c|%4.0f|%4.0f|%4.0f|%4.0f|%4.0f\n",s->name,s->stu_no,s->age,s->sex,s->math,s->English,s->c_language,s->total,s->average_score);
printf("********************************************\n");
result++;
}
}
if(result==0)
printf("没找到!\n");
else
printf("共找到符合条件的记录%d条\n",result);
}
//按年龄排序
void sort_info(struct STUDENT *s,int length)
{
int i,j,k;
struct STUDENT temp_stu;
printf("下面将按照各个学生的年龄排序.\n");
for(i=0;i<length-1;i++)
{
k=i;
for(j=i+1;j<length-i;j++)
{
if(s[k].age>s[j].age)k=j;
temp_stu=s[k];
s[k]=s[i];
s[i]=temp_stu;}
}
output_info(s,length);
}
main()
{
int choice;
struct STUDENT stu[100];
while(1)
{display_menu();
printf("请输入要进行的操作:");
scanf("%d",&choice);
switch(choice)
{
case 1:input_info(stu,2);break;
case 2:output_info(stu,2);break;
case 3:query_info(stu,2);break;
case 4:sort_info(stu,2);break;
case 0:exit(0);
}
}
}