| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 443 人关注过本帖
标题:结构体数组使用函数实现选择排序为何排序失败呢?
取消只看楼主 加入收藏
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
结帖率:90.24%
收藏
已结贴  问题点数:20 回复次数:0 
结构体数组使用函数实现选择排序为何排序失败呢?
//以结构体指针作为函数的参数处理

#include<stdio.h>

struct Lesson
{
    float math;
    float art;
};

struct Student
{
    char name[5];
    struct Lesson grade;  
};

int main()
{
    void convert(struct Student *p,int k);
    int i;
    struct Student stu[5];
    struct Student *p;
    p=stu;
    for(i=0;i<5;i++)
    {
        scanf("%f,%f,%s",&((*p).grade.art),&((*p).grade.math),&((*p).name));   //注意结构体数组指针的写法问题,因为涉及到符号优先级的问题!
        p++;
    }
    puts("The Information data is: ");
    p=stu;
    for(i=0;i<5;i++)
    {
        printf("%s %.1f %.1f\n",(*p).name,(*p).grade.art,(*p).grade.math);
        p++;
    }
    p=stu;
    convert(p,5);
    puts("The converted array is:");
    for(i=0;i<5;i++)
    {
        printf("%s %.1f %.1f\n",(*p).name,(*p).grade.art,(*p).grade.math);
        p++;
    }
    return 0;
}

void convert(struct Student *p,int k)                     //函数实现选择排序为何屡屡失败呢?
{
    struct Student exo;
    int i,j,m;
    for(i=0;i<k-1;i++)
    {
        m=i;
        for(j=1;j<k;j++)
        {
            if((*(p+j)).grade.math<(*(p+m)).grade.math)
            m=j;
        }
        if(m!=i)
        {
            exo=*(p+m);
            *(p+m)=*(p+i);
            *(p+i)=exo;
        }        
    }
}


//找不到原因!感觉没有错误啊~根据math成绩高低进行排序,此处使用选择排序,但是却排序失败!
搜索更多相关主题的帖子: convert include 结构体 优先级 
2015-04-09 14:32
快速回复:结构体数组使用函数实现选择排序为何排序失败呢?
数据加载中...
 
   



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

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