| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1183 人关注过本帖
标题:有关结构体数组元素的排序求Debug
只看楼主 加入收藏
qq200258
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2016-10-29
结帖率:0
收藏
 问题点数:0 回复次数:1 
有关结构体数组元素的排序求Debug

想用冒泡排序和指针给结构体内的整形进行排序 可是输出的结果不对
虚心学习
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define STD 5
struct student{
    int id;
    char name[20];
    int text_id;
    int score[3];
};
int main()
{
    int i,j;
    struct student std[STD] = {
    {1,"Mike",130210,{99,85,76}},
    {2,"John",130211,{98,83,78}},
    {3,"Tree",130212,{93,86,70}},
    {4,"Jack",130213,{91,81,72}},
    {5,"Nick",130214,{98,89,71}},
    };
    struct student * pointers[STD];
    for(i = 0;i < STD;i++)
    {
        pointers[i] = &std[i];
    }
    for(i = 0;i < STD - 1;i++)
    {
        for(j = 0;j < STD - i - 1;j++)
        {
            if(pointers[j] -> score[0] > pointers[j + 1] -> score[0])
            {
                pointers[j] = &std[j + 1];
                pointers[j + 1] = &std[j];
            }
        }
    }
    for(i = 0;i < STD;i++)
    {
        printf("%d\n",pointers[i]->score[0]);
    }
    return 0;
}
搜索更多相关主题的帖子: 结构体 元素 
2017-05-09 20:26
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define STD 5
struct student{
    int id;
    char name[20];
    int text_id;
    int score[3];
};
int main()
{
    int i,j;
    struct student std[STD] = {
    {1,"Mike",130210,{99,85,76}},
    {2,"John",130211,{98,83,78}},
    {3,"Tree",130212,{93,86,70}},
    {4,"Jack",130213,{91,81,72}},
    {5,"Nick",130214,{98,89,71}},
    };
    struct student * pointers[STD];
    for(i = 0;i < STD;i++)
    {
        pointers[i] = &std[i];
    }
    for(i = 0;i < STD ;i++)
    {
        for(j = i+1;j < STD ;j++)
        {
            if(pointers[i] -> score[1] < pointers[j] -> score[1])
            {
                struct student* tmp=pointers[i];
                pointers[i] = pointers[j];
                pointers[j] = tmp;
            }
        }
    }
    for(i = 0;i < STD;i++)
    {
        printf("%d  %s  %d %d %d %d\n",pointers[i]->id,pointers[i]->name,pointers[i]->text_id,pointers[i]->score[0],pointers[i]->score[1],pointers[i]->score[2]);
    }
    return 0;
}


DO IT YOURSELF !
2017-05-10 06:18
快速回复:有关结构体数组元素的排序求Debug
数据加载中...
 
   



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

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