鱼C论坛

 找回密码
 立即注册
查看: 2249|回复: 1

小白求助下 代码比较不短额

[复制链接]
发表于 2017-12-13 22:40:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
struct Student//製作一個學生結構體
{
        char name[8];
        float w,h;
       
};
int cmpw(const void*p1,const void*p2)//比較體重
{
const Student*pf1=(const Student*)p1;
const Student*pf2=(const Student*)p2;
return (int)(pf1-> w - pf2->h);
}

int cmph(const void*p1,const void*p2)//比較身高
{
const Student*pf1= (const Student*) p1;
const Student*pf2= (const Student*) p2;
return (int)(pf1->h - pf2->h);
}
void inputStudent(Student* px)
{
        scanf("%s %f %f",px->name,px->w,px->h);//學生資料輸入函數
}
void printfstudent(const struct Student* px)
{
        printf("name:%s,w:%f\n,h:%f\n",px->name,px->w,px->h);//學生資料打印函數
}

int main()
{
        void printfstudent(const struct Student* px);
        void inputStudent( Student* px);
        int cmpw(const void* p1 ,const void* p2);
        int cmph(const void* p1 ,const void* p2);
        size_t n=0;
        int i=0;
        printf("n=");
        scanf("%u",&n);//動態的
        Student *p=(Student*) malloc(sizeof(Student)*n);//預配置空間
        for(i=0;i<n;++i)
        {
                inputStudent(p+i);
        }
        qsort(p,n,sizeof(Student),cmpw);
        for(i=0;i<n;++i)
        {
                printfstudent(p+i);
        }
        printf("\n");//稍微美觀下
        qsort(p,n,sizeof(Student),cmph);
        for(i=0;i<n;++i)
        {
                printfstudent(p+i);
        }

       
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-13 23:16:09 | 显示全部楼层
蠢了蠢了 忘记加输入符号了 此贴终结
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 22:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表