问题描述
- 我这个给各位学生的总分排名的代码错哪了?
-
#include<stdio.h> #include<stdlib.h> #include<string.h> #define maxsize 5 struct student { int num; char name[30]; int english,computer,math,chinese; int rank; }; double aen,aco,ama,ach;int maxch,maxma,maxen,maxco,minch,minma,minen,minco,sum[5],k; //void menu (struct student students []); void input_student (struct student students []); void change_student (struct student *p); void search_student (struct student *p); void cal (struct student s[]); void max (struct student s[]); void min (struct student s[]); void sort (struct student students[]); void output_student (struct student students []); void input_student (struct student students []) { int i; for (i=0;i<5;i++){ printf("********************************n"); printf("请输入:n学号:"); scanf_s("%d",&students [i].num);getchar(); printf("姓名:"); scanf_s("%s",students [i].name,30);getchar(); printf("英语:"); scanf_s("%d",&students [i].english);getchar(); printf("计算机:"); scanf_s("%d",&students [i].computer);getchar(); printf("数学:"); scanf_s("%d",&students [i].math);getchar(); printf("语文:"); scanf_s("%d",&students [i].chinese);getchar(); } max (students); min (students); cal (students); sort(students); } void output_student (struct student students [ ]) { int i; printf ("---------------------------------------------------------------------------n"); printf ("numt namet englt compt matht chint rankn"); for (i=0;i<5;i++){ printf("%dt",students [i].num); printf(" %st",students [i].name); printf(" %dt",students [i].english); printf(" %dt",students [i].computer); printf(" %dt",students [i].math); printf(" %dt",students [i].chinese); printf(" %dtn",students [i].rank);} printf("平均分 %.2ft%.2ft%.2ft%.2fn",aen,aco,ama,ach); printf("最高分 %dt%dt%dt%dn",maxen,maxco,maxma,maxch); /*分数的调用*/ printf("最低分 %dt%dt%dt%dn",minen,minco,minma,minch); printf ("---------------------------------------------------------------------------"); } void search_student (struct student students [ ] ) { int i,way,j; char a[20],cour; printf("choice the way:1.姓名2.课程3.总分名次"); scanf_s("%d",&way);getchar(); switch(way){ case 1: printf("姓名:"); scanf_s("%s",a,20);getchar(); for (i=0;i<5;i++){ if (strcmp(students[i].name,a)==0) { /*逐个比较相等为0 ,不等返回不相等的差值 !strcmp(students[i].name, a)*/ printf(" num:%d",students [i].num); printf(" english:%d",students [i].english); printf(" computer:%d",students [i].computer); printf(" math:%d",students [i].math); printf("chinese:%dn",students [i].chinese);} } case 2:printf("课程:"); printf("a-英语,b-计算机,c-数学,d-语文:"); scanf_s("%c",&cour); if(cour=='a') { printf(" 英语:"); for (i=0;i<5;i++) printf(" %d",students [i].english); printf("平均分: %f",aen); printf(" 最高分:%d",maxen); printf("最低分: %d",minen); } if(cour=='b') { printf(" 计算机:"); for (i=0;i<5;i++) printf(" %d",students [i].computer); printf("平均分: %f",aco); printf(" 最高分:%d",maxco); printf("最低分: %d",minco); } if(cour=='c') { printf(" 数学:"); for (i=0;i<5;i++) printf(" %d",students [i].math); printf("平均分: %f",ama); printf(" 最高分:%d",maxma); printf("最低分: %d",minma); } if(cour=='d') { printf("语文:"); for (i=0;i<5;i++) printf(" %d",students [i].chinese); printf("平均分: %f",ach); printf(" 最高分:%d",maxch); printf("最低分: %d",minch); } case 3: for (j=1;j<6;j++) for (i=0;i<5;i++) if(students [i].rank==j) printf(" name:%s",students [i].name); return; } } void sort (struct student students[]){ int i,j,x,maxsum;x=1; for(i=0;i<5;i++) sum[i]=students[i].chinese+students[i].math+ students[i].computer+students[i].english; for(i=0;i<5;i++) students[i].rank=0; for(i=0;i<4;i++) { if(students[i].rank==0) { maxsum=sum[i]; for(j=i+1;j<5;j++) { if(students[j].rank==0) if(sum[j]>maxsum) { maxsum=sum[j]; k=j; } } students[k].rank=x; x++; } } for(i=0;i<5;i++) if(students[i].rank==0) { k=i; students[k].rank=x; } } void max (struct student students []){ int i; maxch=students [0].chinese; maxma=students [0].math; maxen=students [0].english; maxco=students [0].computer; for (i=1;i<5;i++) { if (students [i].chinese>maxch){ maxch=students [i].chinese;} if (students [i].math>maxma){ maxma=students [i].math;} if (students [i].english>maxen){ maxen=students [i].english;} if (students [i].computer>maxco){ maxco=students [i+1].computer;} } } void min (struct student students []){ int i; minch=students [0].chinese; minma=students [0].math; minen=students [0].english; minco=students [0].computer; /*各科最低*/ for (i=1;i<5;i++) { if(students [i].chinese<minch) minch=students [i].chinese; if (students [i].math<minma) minma=students [i].math; if (students [i].english<minen) minen=students [i].english; if (students [i].computer<minco) minco=students [i].computer;} } void cal (struct student students[]) { int i;double tach=0,tama=0,taen=0,taco=0; for(i=0;i<5;i++) { tach+=students[i].chinese; tama+=students[i].math; taen+=students[i].english; taco+=students[i].computer; } ach=tach/5; ama=tama/5; aen=taen/5; aco=taco/5; } void change_student (struct student *p) { int num,course,score,i; printf("输入学号:"); scanf_s("%d",&num); printf("choice the course:1.english2.computer3.math4.chinese:"); scanf_s("%d",&course); printf("输入新成绩:"); scanf_s("%d",&score); for(i=0;i<5;i++,p++){ if(p->num==num) switch(course) { case 1:p->english=score;break; case 2:p->computer=score;break; case 3:p->math=score;break; case 4:p->chinese=score;break; } } } int main (void){ struct student students[maxsize]; int c; while (1){ printf("********************************n"); printf(" 学生成绩管理系统n"); printf("********************************n"); printf(" (1)输入学生成绩n"); printf(" (2)查询学生成绩n"); printf(" (3)修改学生成绩n"); printf(" (4)输出学生成绩n"); printf(" (5) 退出系统n"); printf("请选择(1、2、3、4、5)______n"); scanf_s("%d",&c);getchar(); if (c>5||c<1) printf("errorn"); else switch(c) { case 1:input_student (students);break; case 2:search_student (students);break; case 3:change_student (students);break; case 4:output_student (students) ;break; case 5:break; } if(c==5) break; } system("pause"); return 0; }
解决方案
for(i=0;i
{
maxsum=sum[0];//从第一个值比较开始
for(j=0;j
{
if(students[j].rank==0)//
if(sum[j]>=maxsum)
{
maxsum=sum[j]; k=j;//冒泡maxsum取与sum[0]比较的较大值,for(j=0;j<5;j++)经过五次比较maxsum为最值,
//如果每次每一个值都参与比较最值唯一,if(students[j].rank==0)进行筛选循环一次产生的最大值,保证下次循环依旧有最值。
}else
continue;
else
continue;
}
students[k].rank=x;
x++;
}//此循环结束取到五个最值第一次取值rank为1,以此类推
}
解决方案二:
for(j=i+1;j<5;j++)
这里的j=i+1有问题,直接写j=1即可
解决方案三:
不是太懂,不懂帮顶...
解决方案四:
你的sort函数有问题,这个函数就是对sum和进行排名对rank赋值,你的逻辑结构太乱给你一个你参考一下
for(i=0;i
{
maxsum=sum[0];
for(j=0;j
{
if(students[j].rank==0)
if(sum[j]>=maxsum)
{
maxsum=sum[j]; k=j;
}else
continue;
else
continue;
}
students[k].rank=x;
x++;
}
```这个实际上就是一个冒泡排序,外循环为循环五次保证每个rank都有赋值,内循环为将每一个没有rank的sum和与sum[0]比较采用最大值然就找到最大sum和。你把你那两个没用的循环删掉试试这个。
解决方案五:
你的sort函数有问题,这个函数就是对sum和进行排名对rank赋值,你的逻辑结构太乱给你一个你参考一下
for(i=0;i
{
maxsum=sum[0];
for(j=0;j
{
if(students[j].rank==0)
if(sum[j]>=maxsum)
{
maxsum=sum[j]; k=j;
}else
continue;
else
continue;
}
students[k].rank=x;
x++;
}
```这个实际上就是一个冒泡排序,外循环为循环五次保证每个rank都有赋值,内循环为将每一个没有rank的sum和与sum[0]比较采用最大值然就找到最大sum和。你把你那两个没用的循环删掉试试这个。
解决方案八:
void sort (struct student students[]){
int i,j,x,maxsum;x=1;
for(i=0;i<5;i++)
sum[i]=students[i].chinese+students[i].math+
students[i].computer+students[i].english;
for(i=0;i<5;i++)
students[i].rank=0;
for(i=0;i<5;i++)
{
maxsum=sum[0];//从第一个值比较开始
for(j=0;j<5;j++)//每个sum[]与第一个值比较
{
if(students[j].rank==0)//
if(sum[j]>=maxsum)
{
maxsum=sum[j]; k=j;//冒泡maxsum取与sum[0]比较的较大值,for(j=0;j<5;j++)经过五次比较maxsum为最值,
//如果每次每一个值都参与比较最值唯一,if(students[j].rank==0)进行筛选循环一次产生的最大值,保证下次循环依旧有最值。
}else
continue;
else
continue;
}
students[k].rank=x;
x++;
}//此循环结束取到五个最值第一次取值rank为1,以此类推
}