标题:结构体不能排序,找了3天没找到原因
取消只看楼主
fengyinxqy
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2020-5-2
结帖率:92.31%
已结贴  问题点数:9 回复次数:1 
结构体不能排序,找了3天没找到原因
下面是全部代码:
程序代码:
#include <stdio.h>
#include <string.h>
struct birthday
{
    int year;
    int month;
    int day;
};
struct course
{
    unsigned int cid; /* 科目编号 */
    char cname[20];   /* 科目名字*/
    double score;     /* 分数 */
};
struct student
{
    char sno[9];   /* 身份编号 */
    char name[10]; /* 姓名 */
    char sex;
    struct birthday birth;
    struct course sc[8]; /* 人数  */
    double cavg, cmax, cmin,sum;
};

void find(struct student stu[], int n);
double computerAvg(struct student stu, int n);
void printinfos(struct student stu);
void test(struct student stu[], int n, unsigned int cid, char cname[20]);
double maxavg(struct student *stu);
int main()
{
    int i = 0, num = 0;
    char sno[9];
    double max = {0.0}, avg[8] = {0.0};
    struct student stu[8] =
        {
            {"9527001", "张无忌", 1, {2000, 8, 18}, {{101, "Kung fu", 89}, {102, "EQ", 78}, {103, "Face Score", 76}}},
            {"9527002", "韦小宝", 1, {2001, 6, 16}, {{101, "Kung fu", 64}, {102, "EQ", 73}, {103, "Face Score", 91}}},
            {"9527003", "李寻欢", 1, {2002, 5, 20}, {{101, "Kung fu", 75}, {102, "EQ", 34}, {103, "Face Score", 61}}},
            {"9527004", "李小龙", 1, {2003, 5, 21}, {{101, "Kung fu", 98}, {102, "EQ", 79}, {103, "Face Score", 95}}},
            {"9527005", "小龙女", 0, {2000, 5, 19}, {{101, "Kung fu", 64}, {102, "EQ", 35}, {103, "Face Score", 100}}},
            {"9527006", "乔峰", 1, {2008, 3, 27}, {{101, "Kung fu", 85}, {102, "EQ", 15}, {103, "Face Score", 35}}},
            {"9527007", "欧阳锋", 1, {2004, 5, 13}, {{101, "Kung fu", 76}, {102, "EQ", 46}, {103, "Face Score", 84}}},
            {"9527008", "郭靖", 1, {2001, 7, 30}, {{101, "Kung fu", 67}, {102, "EQ", 35}, {103, "Face Score", 61}}},
        };
    // 菜单
    printf("a.查看学生全部信息    b.查看平均分最高的学生信息\n");
    printf("c.查看课程挂科的学生  d.查看课程平均成绩\n");
    printf("e.查看成绩前三和后五的学生信息\n");

    char ch = getchar();        // 走一步就行了 
    switch (ch)
    {
    case 'a':
        find(stu, 8);
        break;
    case 'b':
        maxavg(stu);
        break;
    default:
        break;
    }
    return 0;
}
void test(struct student stu[], int n, unsigned int cid, char cname[20])
{

}
void find(struct student stu[], int n)
{
    char sno[9];
    printf("please input the sno you want to check:");
    scanf("%s", sno);
    int i = 0, j;
    while (i < n)
    {
        if (strcmp(stu[i].sno, sno) == 0)
        {
            printf("\n===================================\n");
            printf("sno:    %s\n", stu[i].sno);
            printf("name:    %s\n", stu[i].name);

            if (1 == stu[i].sex)
                printf("sex:     男\n");
            else
                printf("sex:     女\n");
            printf("bir:    %d-%d-%d\n", stu[i].birth.year,
                    stu[i].birth.month, stu[i].birth.day);
            printf("score;");
            for (j = 0; j < 3; j++)
                printf("%-4.0f", stu[i].sc[j].score);
            printf("\n==================================\n");
            break;
        }
        else
        {
            i++;
        }
    }
    if (i >= n)
        printf("it can't find this item.\n");
}
double computerAvg(struct student stu, int n)
{
    int i;
    double sum = 0.0;
    for (i = 0; i < n; i++)
    {
        sum += stu.sc[i].score;
        
    }
    return sum / n;
}
double maxavg(struct student *stu)  /* 找最大的平均分 */
{
    struct student temp;
    int index;
    int i,j;
    stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */
    for (i = 0; i < 8; i++) //排序算法
    {
        index=i;
        for (j = i+1; j < 9; j++)
        {
            
            if (stu[j].cavg>stu[index].cavg)
            {
                index=j;
            }
        }
        temp=stu[index];
        stu[index]=stu[i];
        stu[i]=temp;
    }
    printf("The Avg max is ");
    printinfos(stu[0]);
    printf("cavg:%lf",stu[0].cavg);
}
void printinfos(struct student stu)
{
    printf("sno:%s", stu.sno);
    printf("name:%s", stu.name);
}


其中排序算法在
程序代码:
double computerAvg(struct student stu, int n)
{
    int i;
    double sum = 0.0;
    for (i = 0; i < n; i++)
    {
        sum += stu.sc[i].score;
        
    }
    return sum / n;
}
double maxavg(struct student *stu)  /* 找最大的平均分 */
{
    struct student temp;
    int index;
    int i=0,j;
    stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */
    for (i = 0; i < 8; i++) //冒泡排序算法
    {
        index=i;
        for (j = i+1; j < 9; j++)
        {
            
            if (stu[j].cavg>stu[index].cavg)
            {
                index=j;
            }
        }
        temp=stu[index];
        stu[index]=stu[i];
        stu[i]=temp;
    }
    printf("The Avg max is ");
    printinfos(stu[0]);
    printf("cavg:%lf",stu[0].cavg);
}
void printinfos(struct student stu)
{
    printf("sno:%s", stu.sno);
    printf("name:%s", stu.name);
}

找了好久还是找不到问题,也不知道怎么修改。求大佬帮忙修改使输入b能输出平均分最高的学生信息。
搜索更多相关主题的帖子: student int struct printf stu 
2020-06-01 12:27
fengyinxqy
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2020-5-2
得分:0 
回复 2楼 rjsp
这我的编译器没报错。vscode不报错
2020-06-01 15:27



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




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

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