YXTS122
YXTS122
采纳率100%
2015-12-31 05:02

我这代码的computer的最高分和最低分老不对,怎么办?

已采纳
 #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("平均分    \t%.1f\t%.1f\t%.1f\t%.1f\n",aen,aco,ama,ach);
printf("最高分    \t%d\t%d\t%d\t%d\n",maxen,maxco,maxma,maxch); /*分数的调用*/ 
printf("最低分    \t%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]; k=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;
}

图片说明
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • herozhangbz herozhangbz 6年前
     void maxa (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].computer;}
    }
    }
    

    你按我这个修改一下最大值最小值都有处理异常,而且你的computer[i+1]加1干什么

    点赞 评论 复制链接分享
  • YXTS122 YXTS122 6年前
     #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("平均分    \t%.1f\t%.1f\t%.1f\t%.1f\n",aen,aco,ama,ach);
    printf("最高分    \t%d\t%d\t%d\t%d\n",maxen,maxco,maxma,maxch); /*分数的调用*/ 
    printf("最低分    \t%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<5;i++)
    {
        if(students[i].rank==0)
        {
            maxsum=sum[i]; k=i;
            for(j=0;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].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;
    }
    
    点赞 评论 复制链接分享
  • herozhangbz herozhangbz 6年前
     void mina (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;}
    }
    ```你的computer数据有两个数据一样你的处理有异常,你之前的问题我也给你了解决方法,但是输入你的数据就出异常,我发现你的sum和有两个相同的sum和,我的方法处理也有异常输入其他数据是没有问题的你自己调试修改一下就行了
    
    
    点赞 评论 复制链接分享

相关推荐