程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 編程-我這個給各位學生的總分排名的代碼錯哪了?

編程-我這個給各位學生的總分排名的代碼錯哪了?

編輯:編程綜合問答
我這個給各位學生的總分排名的代碼錯哪了?
 #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 ("num\t name\t engl\t comp\t math\t chin\t rank\n");
for (i=0;i<5;i++){
printf("%d\t",students [i].num);
printf(" %s\t",students [i].name);
printf(" %d\t",students [i].english);
printf(" %d\t",students [i].computer);
printf(" %d\t",students [i].math);
printf(" %d\t",students [i].chinese);
printf(" %d\t\n",students [i].rank);}
printf("平均分 %.2f\t%.2f\t%.2f\t%.2f\n",aen,aco,ama,ach);
printf("最高分 %d\t%d\t%d\t%d\n",maxen,maxco,maxma,maxch); /*分數的調用*/ 
printf("最低分 %d\t%d\t%d\t%d\n",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:%d\n",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("error\n");
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,以此類推
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved