| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2061 人关注过本帖
标题:结构体不能排序,找了3天没找到原因
只看楼主 加入收藏
fengyinxqy
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2020-5-2
结帖率:92.31%
收藏
已结贴  问题点数:9 回复次数:3 
结构体不能排序,找了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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:5 
提问需要提供的信息
a. 你输入什么
b. 实际输出什么
c. 你期待输出什么

先将你的代码编译,根据编译结果修改你的代码。比如
double maxavg(struct student *stu)
error C4716: “maxavg”: 必须返回一个值
这一步我就不多说了,因为编译器给出的信息你都不肯理会,那我说的更是个屁

然后调试,调试器说
int i,j;
stu[i].cavg = computerAvg(stu[i], 3);
The variable 'i' is being used without being initialized.
我也不多说了,理由同上。


2020-06-01 13:03
fengyinxqy
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2020-5-2
收藏
得分:0 
回复 2楼 rjsp
这我的编译器没报错。vscode不报错
2020-06-01 15:27
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:5 
主要错误在maxavg函数中:
1,没有返回值
2,i为初始化,vs中默认为0,其他环境就不知道计算到哪了。只计算一个学生的平均成绩
3,排序内循环条件为j<9,会产生溢出。vs环境下直接退出,不给错误提示。
将下属maxavg函数替换你的函数,应该没问题:
程序代码:
double maxavg(struct student *stu)  /* 找最大的平均分 */
{
    struct student temp;
    int index;
    int i, j;
    for(i=0;i<8;i++)
        stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */
    for (i = 0; i < 8; i++) //排序算法
    {
        index = i;
        for (j = i + 1; j < 8; 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);
    return stu[0].cavg;
}

能编个毛线衣吗?
2020-06-01 16:41
快速回复:结构体不能排序,找了3天没找到原因
数据加载中...
 
   



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

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