首页 新闻 会员 周边 捐助

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

0
悬赏园豆:50 [已解决问题] 解决于 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];
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;

}

Hoxie的主页 Hoxie | 初学一级 | 园豆:53
提问于:2019-04-27 23:13

大佬救救我吧!呜呜呜

Hoxie 5年前
< >
分享
最佳答案
1

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

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

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

#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;
}

收获园豆:50
皓月空 | 小虾三级 |园豆:726 | 2019-04-29 11:09

谢谢大佬,谢谢!豆子全给你吧,哈哈哈

Hoxie | 园豆:53 (初学一级) | 2019-04-29 11:12

@Hoxie: 应该标注一下问题已解决

皓月空 | 园豆:726 (小虾三级) | 2019-04-29 15:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册