| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 640 人关注过本帖
标题:指针问题求解
取消只看楼主 加入收藏
gaochizhen33
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:114
专家分:101
注 册:2010-11-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
指针问题求解
程序代码:
score *rank(score *head)
{
    score *p,*max;
    int i,j,x;
    float chengji;
    char t[10];
    if(head == NULL)
    {
        printf("没有任何学生资料\n");
        return(head);
    }
    max = p = head;
    printf("********************************************************************************\n");
    printf("1按学生学号排序\t2按学生姓名排序3按语文成绩排序\n");
    printf("4按英语成绩排序\t5按数学成绩排序\t\n");
    printf("********************************************************************************\n");
    printf("请选择排序类型:");
    scanf("%d",&x);
    switch(x)
    {
    case 1:
        for(i = 1;i < k; i++)
        {
            for(j = i+1; j < k; j++)
            {
                max = p;
                p = p -> next;
                if(max -> number > p -> number)
                {
                    n = max -> number;
                    max -> number = p -> number;
                    p -> number = n;
                    strcpy(t,max -> name);
                    strcpy(max -> name,p -> name);
                    strcpy(p -> name,t);
                    chengji    =    max    ->    yuwen;
                    max    ->    yuwen    =    p    ->    yuwen;
                    p    ->    yuwen    =    chengji;
                    chengji    = max -> shuxue;
                    max -> shuxue = p -> shuxue;
                    p -> shuxue = chengji;
                    chengji = max -> yingyu;
                    max -> yingyu = p -> yingyu;
                    p -> yingyu = chengji;
                }
            }
            max = head;
            p = head;
        }
        show(head);
        break;
    case 2:
        for(i = 1;i < k; i++)
        {
            for(j = i+1; j < k; j++)
            {
                max = p;
                p = p -> next;
                if(strcmp(max -> name, p -> name) > 0)
                {
                    n = max -> number;
                    max -> number = p -> number;
                    p -> number = n;
                    strcpy(t,max -> name);
                    strcpy(max -> name,p -> name);
                    strcpy(p -> name,t);
                    chengji    =    max    ->    yuwen;
                    max    ->    yuwen    =    p    ->    yuwen;
                    p    ->    yuwen    =    chengji;
                    chengji    = max -> shuxue;
                    max -> shuxue = p -> shuxue;
                    p -> shuxue = chengji;
                    chengji = max -> yingyu;
                    max -> yingyu = p -> yingyu;
                    p -> yingyu = chengji;
                }
            }
            max = head;
            p = head;
        }
        show(head);
        break;
    case 3:
            for(i = 1;i < k; i++)
        {
            for(j = i+1; j < k; j++)
            {
                max = p;
                p = p -> next;
                if(max -> yuwen > p -> yuwen)
                {
                    n = max -> number;
                    max -> number = p -> number;
                    p -> number = n;
                    strcpy(t,max -> name);
                    strcpy(max -> name,p -> name);
                    strcpy(p -> name,t);
                    chengji    =    max    ->    yuwen;
                    max    ->    yuwen    =    p    ->    yuwen;
                    p    ->    yuwen    =    chengji;
                    chengji    = max -> shuxue;
                    max -> shuxue = p -> shuxue;
                    p -> shuxue = chengji;
                    chengji = max -> yingyu;
                    max -> yingyu = p -> yingyu;
                    p -> yingyu = chengji;
                }
            }
            max = head;
            p = head;
        }
        show(head);
        break;
    case 4:
            for(i = 1;i < k; i++)
        {
            for(j = i+1; j < k; j++)
            {
                max = p;
                p = p -> next;
                if(max -> shuxue > p -> shuxue)
                {
                    n = max -> number;
                    max -> number = p -> number;
                    p -> number = n;
                    strcpy(t,max -> name);
                    strcpy(max -> name,p -> name);
                    strcpy(p -> name,t);
                    chengji    =    max    ->    yuwen;
                    max    ->    yuwen    =    p    ->    yuwen;
                    p    ->    yuwen    =    chengji;
                    chengji    = max -> shuxue;
                    max -> shuxue = p -> shuxue;
                    p -> shuxue = chengji;
                    chengji = max -> yingyu;
                    max -> yingyu = p -> yingyu;
                    p -> yingyu = chengji;
                }
            }
            max = head;
            p = head;
        }
        show(head);
        break;
    case 5:
            for(i = 1;i < k; i++)
        {
            for(j = i+1; j < k; j++)
            {
                max = p;
                p = p -> next;
                if(max -> yingyu > p -> yingyu)
                {
                    n = max -> number;
                    max -> number = p -> number;
                    p -> number = n;
                    strcpy(t,max -> name);
                    strcpy(max -> name,p -> name);
                    strcpy(p -> name,t);
                    chengji    =    max    ->    yuwen;
                    max    ->    yuwen    =    p    ->    yuwen;
                    p    ->    yuwen    =    chengji;
                    chengji    = max -> shuxue;
                    max -> shuxue = p -> shuxue;
                    p -> shuxue = chengji;
                    chengji = max -> yingyu;
                    max -> yingyu = p -> yingyu;
                    p -> yingyu = chengji;
                }
            }
            max = head;
            p = head;
        }
        show(head);
        break;
    default :
        printf("出去错误,请重试.\n");
    }
    return 0;
}
如果在main()里用
case 5:rank(head);break;
每次选择了一种排序风格后,数据还可以继续使用;但是如果用
case 5:head = rank(head);break;
的话,数据使用过一次之后,在选别的类型的排序方式时,就会提示没有任何数据,head == NULL 了,这怎么理解?

[ 本帖最后由 gaochizhen33 于 2010-11-27 21:37 编辑 ]
搜索更多相关主题的帖子: 指针 问题求解 
2010-11-27 21:35
gaochizhen33
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:114
专家分:101
注 册:2010-11-4
收藏
得分:0 
哦 谢谢了
2010-11-27 23:04
快速回复:指针问题求解
数据加载中...
 
   



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

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