| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1244 人关注过本帖
标题:求解哪里出的问题,感谢为我解答的人,我将粉粹一切自学编程路上的困难
只看楼主 加入收藏
杀戮英
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2020-5-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求解哪里出的问题,感谢为我解答的人,我将粉粹一切自学编程路上的困难
#    include <stdio.h>
#    include <malloc.h>

struct student
{
    int age;
    float score;
    char name[100];
};

int len, i, j;
struct student t; //用于排序中交换两个数字的中间值

void ShuRu (struct student** p)
{  
    printf ("请输入学生的个数:\n");
    printf ("len = ");  
    scanf ("%d", &len);
    * p = (struct student*)malloc (len * sizeof (struct student)); //动态构造一个数组

    for (i=0; i<len; ++i)
    {
        printf ("请输入第%d个学生的信息:\n", i+1);
        printf ("age = ");
        scanf ("%d", &**(p+i).age);

        printf ("score = ");
        scanf ("%f", &*p[i].score);

        printf ("name = ");
        scanf ("%s", *p[i].name);
    }

}

void PaiXiu (struct student** r)
{
    for (i=1; i<len; ++i)
    {
        for (j=0; j<len-i; ++j)
        {
            if (*r[j].score>*r[j+1].score)
            {
                t = *r[j];
                *r[j] = *r[j+1];
                *r[j+1] = t;
            }
        }
    }
}


void ShuChu (struct student** w)
{
    printf ("\n\n学生的信息是:");
   
    for (i=0; i<len; ++i)
    {
        printf ("第%d名的学生信息是:", i+1);
        printf("age = %d\n", *w[i].age);
        printf("name = %s\n", *w[i].name);
        printf("score = %f\n", *w[i].score);
    }
}

int main (void)
{
    struct student* q;

    ShuRu (&q);   //输入学生的信息

    PaiXiu (&q);   //根据成绩给学生排序,冒泡法

    ShuChu (&q);    //对处理完的结果进行输出

    return 0;
}
搜索更多相关主题的帖子: student printf struct score name 
2020-05-17 19:58
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:10 
程序代码:
#include <stdio.h>
#include <malloc.h>

struct student
{
    int age;
    float score;
    char name[100];
};

void ShuRu( struct student** p, size_t* len )
{
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%zu", len);
    *p = malloc( *len * sizeof(struct student) );

    for( size_t i=0; i!=*len; ++i )
    {
        printf ("请输入第%zu个学生的信息:\n",  i+1 );
        printf( "age = " );
        scanf( "%d", &(*p)[i].age );

        printf( "score = " );
        scanf( "%f", &(*p)[i].score );

        printf( "name = " );
        scanf( "%s", (*p)[i].name );
    }
}

void PaiXiu( struct student* p, size_t len )
{
    for( size_t i=1; i<len; ++i )
    {
        for( size_t j=0; j<len-i; ++j )
        {
            if( p[j].score > p[j+1].score )
            {
                struct student t = p[j];
                p[j] = p[j+1];
                p[j+1] = t;
            }
        }
    }
}

void ShuChu( const struct student* p, size_t len )
{
    printf ("\n\n学生的信息是:");
    for( size_t i=0; i!=len; ++i )
    {
        printf( "第%zu名的学生信息是:",  i+1 );
        printf( "age = %d\n", p[i].age );
        printf( "name = %s\n", p[i].name );
        printf( "score = %g\n", p[i].score );
    }
}

int main( void )
{
    struct student* p;
    size_t len;

    ShuRu( &p, &len );   //输入学生的信息

    PaiXiu( p, len );   //根据成绩给学生排序,冒泡法

    ShuChu( p, len );    //对处理完的结果进行输出
}

2020-05-17 20:20
fulltimelink
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:171
专家分:752
注 册:2020-4-1
收藏
得分:10 
对象点的优先级是大于解引用的优先级的
程序代码:
#define _CRT_SECURE_NO_WARNINGS

#    include <stdio.h>
#    include <malloc.h>

struct student
{
    int age;
    float score;
    char name[100];
};

int len, i, j;
struct student t; //用于排序中交换两个数字的中间值

struct student** ShuRu()
{
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%d", &len);
    struct student ** p = (struct student**)malloc(len * sizeof(struct student *)); //动态构造一个数组

    for (i = 0; i < len; ++i)
    {
        *(p + i) = (struct student*)malloc(sizeof(struct student ));
        printf("请输入第%d个学生的信息:\n", i + 1);
        printf("age = ");
        scanf("%d", &((**(p+i)).age));

        printf("score = ");
        scanf("%f", &((**(p+i)).score));

        printf("name = ");
        scanf("%s", (**(p+i)).name);
    }
    return p;

}

void PaiXiu(struct student* r[])
{
    for (i = 1; i < len; ++i)
    {
        for (j = 0; j < len - i; ++j)
        {
            if ((**(r+j)).score > (**(r+j + 1)).score)
            {
                t = **(r+j);
                **(r + j) = **(r + j+1);
                **(r + j + 1) = t;
            }
        }
    }
}


void ShuChu(struct student* w[])
{
    printf("\n\n学生的信息是:");

    for (i = 0; i < len; ++i)
    {
        printf("第%d名的学生信息是:", i + 1);
        printf("age = %d\n", (**(w+i)).age);
        printf("name = %s\n", (**(w + i)).name);
        printf("score = %f\n", (**(w + i)).score);
    }
}


void freep(struct student*p[]) {
    printf("\n\n堆释放:");

    for (i = 0; i < len; ++i)
    {
        free(*(p+i));
    }
    free(p);
}


int main(void)
{
    struct student** q = ShuRu();   //输入学生的信息

    PaiXiu(q);   //根据成绩给学生排序,冒泡法

    ShuChu(q);    //对处理完的结果进行输出

    freep(q);    // 释放内存

    return 0;
}
2020-05-18 18:58
快速回复:求解哪里出的问题,感谢为我解答的人,我将粉粹一切自学编程路上的困难 ...
数据加载中...
 
   



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

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