有大佬帮忙看看吗?小弟已在崩溃的边缘试探(输入五位同学的姓名、学号、四门科目成绩、,要将他们按成绩从

2020-01-21 20:22发布

include <stdio.h>

struct Stpoint{
int code;
char name[20];
int scores[4];
int sum;
};

int main()
{
struct Stpoint students[5];
int temp;
int i,j,k,s_sum[5],temp1,temp2,sum;
char str[20]={0};
float ave;
for(i=0;i<5;i++){
printf("输入第%d个学生的姓名,学号,成绩(用空格分开):\n",i+1);
scanf("%s",&students[i].name);
scanf("%d",&students[i].code);
for(j=0;j<=3;j++){
scanf("%d",&students[i].scores[j]);
}
}

for(i=0;i<5;i++){
        students[i].sum=0;
    for(j=0;j<=3;j++){
        students[i].sum += students[i].scores[j];
    }
    ave = students[i].sum/4;
    printf("%s  总分为%d,平均分为%.1f\n",students[i].name,students[i].sum,ave);
}
for(i=0;i<4;i++){                                   /*冒泡法*/
for(j=0;j<4-i;j++){
    if(students[j].sum>students[j+1].sum){
        students[5] = students[j+1];            /*交换结构数组的成员*/
        students[j+1] = students[j];
        students[j] = students[5];
        }                                                                                                                                                                                                                                                            `
}
}

for(i=0;i<=4;i++){
    printf("%d   ",students[i].sum);
}


for(i=4;i>=0;i--){
    printf("%s   ",students[i].name);
    printf("%d   ",students[i].code);
    for(j=0;j<=3;j++){
    printf("%d   ",students[i].scores[j]);
    }
    printf("%d\n",s_sum[i]);

}



return 0;

}

标签: c语言
1条回答
Anthone
2楼-- · 2020-01-21 21:18

总的说来,你有主要有两个错误

  • 一是录入的时候,分数的录入不对
  • 二是冒泡交换代码不对

我按照你代码简单改了下,能运行。
建议你自己一步一步调试,保证每一步正确,多写点代码。

#include <stdio.h>
struct Stpoint {
    int code;
    char name[20];
    int scores[4];
    int sum;
};

int main()
{
    struct Stpoint students[5] = { 0 };
    //加了一个冒泡排序时候的临时变量,另外定义变量最好设置默认值
    struct Stpoint studentstemp;
    int temp;
    int i, j, k, s_sum[5] = { 0 }, temp1, temp2, sum;
    char str[20] = { 0 };
    float ave;
    for (i = 0; i < 5; i++) {
        printf("输入第%d个学生的姓名:", i + 1);
        scanf("%s", &students[i].name);
        printf("学号:");
        scanf("%d", &students[i].code);
        //要想一起输入需要拆分字符串并转换
        printf("科目1成绩:");
        scanf("%d", &students[i].scores[0]);
        printf("科目2成绩:");
        scanf("%d", &students[i].scores[1]);
        printf("科目3成绩:");
        scanf("%d", &students[i].scores[2]);
        printf("科目4成绩:");
        scanf("%d", &students[i].scores[3]);
    }

    for (i = 0; i < 5; i++) {
        students[i].sum = 0;
        for (j = 0; j <= 3; j++) {
            students[i].sum += students[i].scores[j];
        }
        ave = students[i].sum / 4;
        printf("%s  总分为%d,平均分为%.1f\n", students[i].name, students[i].sum, ave);
    }
    /*冒泡法*/
    for (i = 0; i < 4; i++) {                                 

        for (j = 0; j < 4 - i; j++) {
            if (students[j].sum > students[j + 1].sum) {
                /*交换结构数组的成员*/
                //之前代码有错误
                studentstemp = students[j + 1];
                students[j + 1] = students[j];
                students[j] = studentstemp;
            }
        }
    }

    for (i = 0; i <= 4; i++) {
        printf("%d   ", students[i].sum);
    }

    printf("\n");
    for (i = 4; i >= 0; i--) {
        printf("%s   ", students[i].name);
        printf("%d   ", students[i].code);
        for (j = 0; j <= 3; j++) {
            printf("%d   ", students[i].scores[j]);
        }
        printf("%d\n", s_sum[i]);

    }
    //加了一个等待查看结果
    scanf("%s", str);

    return 0;
}

查看更多
登录 后发表回答