作者在 2009-07-16 14:45:43 发布以下内容
本人和寝室好友完成一个程序··但是有不完善的地方本人不能解决···希望高手们指点···问题是这样的!
如果向执行“1”然后执行“2”完全正确!但是如果向执行“2”在执行“1” “2”正确,但是“1”的学号
、班级、姓名就会混乱···不对号··高手们指点一下啊!
源代码如下:
#include <stdio.h>
#include <string.h>
#define N 3
struct students
{
char number[15];
char name[15];
char stu_class[1];
int math;
int physics;
int English;
int Chinese;
int total;
int average;
}stu[N];
int choice_students(int choice);
void total_average_students(int total,int average);
void order_students(struct students stu[N]);
void printf_students(struct students stu[N]);
void search_students(struct students stu[N],char number[15],char name[15],int choice);
void main(void)
{
int i,choice=0;
printf("\n");
printf("***************学生成绩管理与统计系统***************\n");
printf("学生信息以及成绩录入中...\n");
for (i=0;i<N;i++)
{
printf("请输入学生的学号:");
scanf("%s",stu[i].number);
printf("请输入学生的姓名:");
scanf("%s",stu[i].name);
printf("请输入学生的班级:");
scanf("%s",stu[i].stu_class);
printf("请输入学生的数学成绩:");
scanf("%d",&stu[i].math);
printf("请输入学生的物理成绩:");
scanf("%d",&stu[i].physics);
printf("请输入学生的英语成绩:");
scanf("%d",&stu[i].English);
printf("请输入学生的语文成绩:");
scanf("%d",&stu[i].Chinese);
printf("\n");
}
choice=choice_students(choice);
}
int choice_students(int choice)
{
int total=0,average=0;
char number[15],name[15];
do
{
printf("学生成绩管理与统计系统功能选项: \n");
printf("1:统计每个学生的总分和平均分\n");
printf("2:按总分由大到小排名次\n");
printf("3:打印各门课程的平均分以及平均分之上的学生名单\n");
printf("4:根据学生学号或姓名查询学生相关信息\n");
printf("0:退出\n");
printf("请选择功能:");
scanf("%d",&choice);
switch(choice)
{
case 1:
total_average_students(total,average);
break;
case 2:
order_students(stu);
break;
case 3:
printf_students(stu);
break;
case 4:
search_students(stu,number,name,choice);
case 0:
break;
}
}while(choice!=0);
printf("********************系统已退出********************\n");
printf("(*^__^*)谢谢使用学生成绩管理与统计系统功能(*^__^*)\n");
return choice;
}
void total_average_students(int total,int average)
{
int i;
printf("\n");
printf("学号 姓名 班级 数学 物理 英语 语文 总分 平均分\n");
for (i=0;i<N;i++)
{
stu[i].total=stu[i].math+stu[i].physics+stu[i].English+stu[i].Chinese;
stu[i].average=stu[i].total/4;
printf("%s %s %s %d %d %d %d %d %d\n",stu[i].number,stu[i].name,stu[i].stu_class,stu[i].math,stu[i].physics,stu[i].English,stu[i].Chinese,stu[i].total,stu[i].average);
}
printf("\n");
}
void order_students(struct students stu[N])
{
int i,j,temp1;
char temp2[15];
for (i=0;i<N;i++)
{stu[i].total=stu[i].math+stu[i].physics+stu[i].English+stu[i].Chinese;
for (j=0;j<N-i;j++)
{
if (stu[j].total<stu[j+1].total)
{
temp1=stu[j].total;
stu[j].total=stu[j+1].total;
stu[j+1].total=temp1;
strcpy(temp2,stu[j].number);
strcpy(stu[j].number,stu[j+1].number);
strcpy(stu[j+1].number,temp2);
strcpy(temp2,stu[j].name);
strcpy(stu[j].name,stu[j+1].name);
strcpy(stu[j+1].name,temp2);
strcpy(temp2,stu[j].stu_class);
strcpy(stu[j].stu_class,stu[j+1].stu_class);
strcpy(stu[j+1].stu_class,temp2);
}
}
}
printf("\n");
printf("学号 姓名 班级 总分 名次\n");
for (i=0;i<N;i++)
printf("%s %s %s %d %d\n",stu[i].number,stu[i].name,stu[i].stu_class,stu[i].total,i+1);
printf("\n");
}
void printf_students(struct students stu[N])
{
int i,subject_sum;
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].math;
printf("数学课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].math)
printf("%s ",stu[i].name);
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].physics;
printf("物理课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].physics)
printf("%s ",stu[i].name);
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].English;
printf("英语课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].English)
printf("%s ",stu[i].name);
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].Chinese;
printf("语文课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].Chinese)
printf("%s ",stu[i].name);
printf("\n");
printf("\n");
}
void search_students(struct students stu[N],char number[15],char name[15],int choice)
{
int i,flag;
printf("\n");
printf("*********欢迎使用学生成绩管理与统计系统查询功能*********\n");
do
{
flag=0;
printf("学生相关信息查询方式: 1:按学号查询 2:按姓名查询 0:退出\n");
printf("请选择查询方式:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("请输入您要查询的学生的学号:");
scanf("%s",number);
for (i=0;i<N;i++)
{
if (strcmp(number,stu[i].number)==0)
{
flag=1;
break;
}
}
break;
case 2:
printf("请输入您要查询的学生的姓名:");
scanf("%s",name);
for (i=0;i<N;i++)
{
if (strcmp(name,stu[i].name)==0)
{
flag=1;
break;
}
}
break;
case 0:
flag=2;
break;
}
printf("\n");
if(flag==1)
{
printf("学号 姓名 班级 数学 物理 英语 语文 总分 平均分\n");
printf("%s %s %s %d %d %d %d %d %d\n",stu[i].number,stu[i].name,stu[i].stu_class,stu[i].math,stu[i].physics,stu[i].English,stu[i].Chinese,stu[i].total,stu[i].average);
printf("\n");
}
else if(flag==0)
{
printf("查无此人,请重新输入!\n");
printf("\n");
}
}while(choice!=0);
printf("(*^__^*)谢谢使用学生成绩管理与统计系统查询功能(*^__^*)\n");
printf("\n");
}
#include <string.h>
#define N 3
struct students
{
char number[15];
char name[15];
char stu_class[1];
int math;
int physics;
int English;
int Chinese;
int total;
int average;
}stu[N];
int choice_students(int choice);
void total_average_students(int total,int average);
void order_students(struct students stu[N]);
void printf_students(struct students stu[N]);
void search_students(struct students stu[N],char number[15],char name[15],int choice);
void main(void)
{
int i,choice=0;
printf("\n");
printf("***************学生成绩管理与统计系统***************\n");
printf("学生信息以及成绩录入中...\n");
for (i=0;i<N;i++)
{
printf("请输入学生的学号:");
scanf("%s",stu[i].number);
printf("请输入学生的姓名:");
scanf("%s",stu[i].name);
printf("请输入学生的班级:");
scanf("%s",stu[i].stu_class);
printf("请输入学生的数学成绩:");
scanf("%d",&stu[i].math);
printf("请输入学生的物理成绩:");
scanf("%d",&stu[i].physics);
printf("请输入学生的英语成绩:");
scanf("%d",&stu[i].English);
printf("请输入学生的语文成绩:");
scanf("%d",&stu[i].Chinese);
printf("\n");
}
choice=choice_students(choice);
}
int choice_students(int choice)
{
int total=0,average=0;
char number[15],name[15];
do
{
printf("学生成绩管理与统计系统功能选项: \n");
printf("1:统计每个学生的总分和平均分\n");
printf("2:按总分由大到小排名次\n");
printf("3:打印各门课程的平均分以及平均分之上的学生名单\n");
printf("4:根据学生学号或姓名查询学生相关信息\n");
printf("0:退出\n");
printf("请选择功能:");
scanf("%d",&choice);
switch(choice)
{
case 1:
total_average_students(total,average);
break;
case 2:
order_students(stu);
break;
case 3:
printf_students(stu);
break;
case 4:
search_students(stu,number,name,choice);
case 0:
break;
}
}while(choice!=0);
printf("********************系统已退出********************\n");
printf("(*^__^*)谢谢使用学生成绩管理与统计系统功能(*^__^*)\n");
return choice;
}
void total_average_students(int total,int average)
{
int i;
printf("\n");
printf("学号 姓名 班级 数学 物理 英语 语文 总分 平均分\n");
for (i=0;i<N;i++)
{
stu[i].total=stu[i].math+stu[i].physics+stu[i].English+stu[i].Chinese;
stu[i].average=stu[i].total/4;
printf("%s %s %s %d %d %d %d %d %d\n",stu[i].number,stu[i].name,stu[i].stu_class,stu[i].math,stu[i].physics,stu[i].English,stu[i].Chinese,stu[i].total,stu[i].average);
}
printf("\n");
}
void order_students(struct students stu[N])
{
int i,j,temp1;
char temp2[15];
for (i=0;i<N;i++)
{stu[i].total=stu[i].math+stu[i].physics+stu[i].English+stu[i].Chinese;
for (j=0;j<N-i;j++)
{
if (stu[j].total<stu[j+1].total)
{
temp1=stu[j].total;
stu[j].total=stu[j+1].total;
stu[j+1].total=temp1;
strcpy(temp2,stu[j].number);
strcpy(stu[j].number,stu[j+1].number);
strcpy(stu[j+1].number,temp2);
strcpy(temp2,stu[j].name);
strcpy(stu[j].name,stu[j+1].name);
strcpy(stu[j+1].name,temp2);
strcpy(temp2,stu[j].stu_class);
strcpy(stu[j].stu_class,stu[j+1].stu_class);
strcpy(stu[j+1].stu_class,temp2);
}
}
}
printf("\n");
printf("学号 姓名 班级 总分 名次\n");
for (i=0;i<N;i++)
printf("%s %s %s %d %d\n",stu[i].number,stu[i].name,stu[i].stu_class,stu[i].total,i+1);
printf("\n");
}
void printf_students(struct students stu[N])
{
int i,subject_sum;
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].math;
printf("数学课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].math)
printf("%s ",stu[i].name);
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].physics;
printf("物理课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].physics)
printf("%s ",stu[i].name);
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].English;
printf("英语课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].English)
printf("%s ",stu[i].name);
printf("\n");
subject_sum=0;
for (i=0;i<N;i++)
subject_sum=subject_sum+stu[i].Chinese;
printf("语文课程的平均分是:%d ",subject_sum/N);
printf("在该课程平均分之上的人有:");
for (i=0;i<N;i++)
if ((subject_sum/N)<stu[i].Chinese)
printf("%s ",stu[i].name);
printf("\n");
printf("\n");
}
void search_students(struct students stu[N],char number[15],char name[15],int choice)
{
int i,flag;
printf("\n");
printf("*********欢迎使用学生成绩管理与统计系统查询功能*********\n");
do
{
flag=0;
printf("学生相关信息查询方式: 1:按学号查询 2:按姓名查询 0:退出\n");
printf("请选择查询方式:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("请输入您要查询的学生的学号:");
scanf("%s",number);
for (i=0;i<N;i++)
{
if (strcmp(number,stu[i].number)==0)
{
flag=1;
break;
}
}
break;
case 2:
printf("请输入您要查询的学生的姓名:");
scanf("%s",name);
for (i=0;i<N;i++)
{
if (strcmp(name,stu[i].name)==0)
{
flag=1;
break;
}
}
break;
case 0:
flag=2;
break;
}
printf("\n");
if(flag==1)
{
printf("学号 姓名 班级 数学 物理 英语 语文 总分 平均分\n");
printf("%s %s %s %d %d %d %d %d %d\n",stu[i].number,stu[i].name,stu[i].stu_class,stu[i].math,stu[i].physics,stu[i].English,stu[i].Chinese,stu[i].total,stu[i].average);
printf("\n");
}
else if(flag==0)
{
printf("查无此人,请重新输入!\n");
printf("\n");
}
}while(choice!=0);
printf("(*^__^*)谢谢使用学生成绩管理与统计系统查询功能(*^__^*)\n");
printf("\n");
}