标题:初始化10名学生的基本信息,包括学号、姓名、三科(高数、英语、程序设计) ...
只看楼主
fengyinxqy
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2020-5-2
结帖率:92.31%
已结贴  问题点数:10 回复次数:5 
初始化10名学生的基本信息,包括学号、姓名、三科(高数、英语、程序设计)成绩,分别计算每名学生的总分,并按个人总分对学生降序排序,输出排序后的学生信息
编写程序,初始化10名学生的基本信息,包括学号、姓名、三科(高数、英语、程序设计)成绩,分别计算每名学生的总分,并按个人总分对学生降序排序,输出排序后的学生信息。
下面是我写的代码:
程序代码:
#include <stdio.h>
struct courses
{
    char cName[20];
    double score;
};
struct student /* 学生信息结构定义 */
{
    int num;                     /* 学号 */
    char name[10];               /* 姓名 */
    double sum;              /* 平均分 */
    struct courses sc[10];
};
int main()
{
    int i=0, index, n, j;
    struct student students[10]={   {101,"xiao",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {102,"zhao",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {103,"qian",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {104,"sun",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {105,"li",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {106,"zhou",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {107,"wu",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {108,"zheng",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {109,"wang",{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {110,"jiang",{{1,"computer",89},{2,"math",78},{3,"english",76}}}
                                }, temp; /* 定义结构数据 */
    for (; i < 10; i++)
    {
        printf("num:%d\n",students[i].num);
        printf("name:%s\n",students[i].name);
        students[i].sum +=students[i].sc[10].score;
        printf("sum:%lf\n",students[i].sum);
    }
    /* 结构数组排序,选择排序法 */
    for (i = 0; i < n - 1; i++)
    {
        index = i;
        for (j = i + 1; j < n; j++)
        {
            if (students[j].sum > students[index].sum) /* 比较平均成绩 */
            {
                index = j;
            }
        }
        temp = students[index]; /* 交换数组元素 */
        students[index] = students[i];
        students[i] = temp;
    }
    /* 输出排序后的信息 */
    printf("num\tname\tsum\n");
    for (i = 0; i < n; i++)
    {
        printf("%d\t%s\t%d\n", students[i].num, students[i].name, students[i].sum);
    }
    return 0;
}


接下来就写不下去了,求和那里程序不能求和,然后下面的排序也对应 的出了问题。

求大佬帮我改正。
搜索更多相关主题的帖子: 学生 english math 排序 computer 
2020-05-25 22:11
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:3 
没看明
{101,"xiao",{{1,"computer",89},{2,"math",78},{3,"english",76}}}
其中的1,2,3是哪个结构的成员?
2020-05-26 07:01
fulltimelink
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:171
专家分:752
注 册:2020-4-1
得分:3 
sum没有初始化
courses 结构体不完整
总分计算时需要累加
n没有赋初值 10
程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct courses
{
    int index;
    char cName[20];
    double score;
};
struct student /* 学生信息结构定义 */
{
    int num;                     /* 学号 */
    char name[10];               /* 姓名 */
    double sum;              /* 平均分 */
    struct courses sc[10];
};
int main()
{
    int i = 0, index, j;
    struct student students[10] = { {101,"xiao",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {102,"zhao",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {103,"qian",0.0,{{1,"computer",89},{2,"math",78},{3,"english",77}}},
                                    {104,"sun",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {105,"li",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {106,"zhou",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {107,"wu",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {108,"zheng",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}},
                                    {109,"wang",0.0,{{1,"computer",89},{2,"math",78},{3,"english",75}}},
                                    {110,"jiang",0.0,{{1,"computer",89},{2,"math",78},{3,"english",76}}}
    }, temp; /* 定义结构数据 */
    for (; i < 10; i++)
    {
        printf("num:%d\n", students[i].num);
        printf("name:%s\n", students[i].name);
        students[i].sum += students[i].sc[0].score;
        students[i].sum += students[i].sc[1].score;
        students[i].sum += students[i].sc[2].score;
        printf("sum:%lf\n", students[i].sum);
    }
    /* 结构数组排序,选择排序法 */
    for (i = 0; i < 9; i++)
    {
        index = i;
        for (j = i + 1; j < 10; j++)
        {
            if (students[j].sum > students[index].sum) /* 比较平均成绩 */
            {
                index = j;
            }
        }
        temp = students[index]; /* 交换数组元素 */
        students[index] = students[i];
        students[i] = temp;
    }
    /* 输出排序后的信息 */
    printf("num\tname\tsum\n");
    for (i = 0; i < 10; i++)
    {
        printf("%d\t%s\t%f\n", students[i].num, students[i].name, students[i].sum);
    }
    return 0;
}
2020-05-26 08:50
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:3 
程序代码:
#include <stdio.h>

struct course
{
    char cName[20];
    double score;
};

struct student
{
    int num;
    char name[10];
    double sum;
    struct course sc[10];
};

int main( void )
{
    struct student students[] = { {101,"xiao", 0,{{"computer",89},{"math",78},{"english",90}}},
                                  {102,"zhao", 0,{{"computer",89},{"math",78},{"english",91}}},
                                  {103,"qian", 0,{{"computer",89},{"math",78},{"english",92}}},
                                  {104,"sun",  0,{{"computer",89},{"math",78},{"english",93}}},
                                  {105,"li",   0,{{"computer",89},{"math",78},{"english",94}}},
                                  {106,"zhou", 0,{{"computer",89},{"math",78},{"english",95}}},
                                  {107,"wu",   0,{{"computer",89},{"math",78},{"english",96}}},
                                  {108,"zheng",0,{{"computer",89},{"math",78},{"english",97}}},
                                  {109,"wang", 0,{{"computer",89},{"math",78},{"english",98}}},
                                  {110,"jiang",0,{{"computer",89},{"math",78},{"english",99}}} };

    const size_t n = sizeof(students)/sizeof(*students);
    const size_t m = sizeof(students[0].sc)/sizeof(*students[0].sc);
    for( size_t i=0; i!=n; ++i )
    {
        for( size_t j=0; j!=m && students[i].sc[j].cName[0]!='\0'; ++j )
            students[i].sum += students[i].sc[j].score;
    }

    printf("\nnum\tname\tsum\n");
    for( size_t i=0; i!=n; ++i )
        printf( "%d\t%s\t%g\n", students[i].num, students[i].name, students[i].sum );

    for( size_t i=0; i+1<n; ++i )
    {
        size_t index = i;
        for( size_t j=i+1; j<n; ++j )
            if( students[j].sum > students[index].sum )
                index = j;

        struct student temp = students[index];
        students[index] = students[i];
        students[i] = temp;
    }

    printf("\nnum\tname\tsum\n");
    for( size_t i=0; i!=n; ++i )
        printf( "%d\t%s\t%g\n", students[i].num, students[i].name, students[i].sum );
}
2020-05-26 08:52
forever74
Rank: 12Rank: 12Rank: 12
来 自:CC
等 级:贵宾
威 望:49
帖 子:1636
专家分:3940
注 册:2007-12-27
得分:3 
亦可调整一下顺序把sum放后边,以达到免于显式初始化但也是0的效果。

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2020-05-26 09:05
forever74
Rank: 12Rank: 12Rank: 12
来 自:CC
等 级:贵宾
威 望:49
帖 子:1636
专家分:3940
注 册:2007-12-27
得分:0 
就这个问题来说,那些科目名称过于冗余了,建议砍掉。

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2020-05-26 09:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-501792-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.013592 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved