#include<stdio.h> #include<stdlib.h> #include<string.h> typedef enum gender{ MALE, FEMALE, }Gender; typedef struct score{ int c_score;//4 int java_score;//4 int python_score; }Score; typedef struct musician{ char*name;//8type Gender gender;//1 char* album;//8 Score score; }Musician; void PrintMusician(Musician *m){ printf("%s \n%c \n%s \n%d \n%d \n%d\n",m->name,m->gender,m->album,m->score.c_score,m->score.java_score,m->score.python_score); }; int Cmp(const void *p1,const void*p2){//这一函数用于按照名字拼音顺序比较结构体数组。 const Musician*left_musician=p1;/* * 由于本题比较的char*name在第一项,所以类似二维数组,可以先解一次引用得到指向字符串的指针。 *也即:const char* const *left=p1; * strcmp(*left,*right); */ const Musician*right_musician=p2; return strcmp(left_musician->name,right_musician->name); } int main(){ Musician Luo={ .name="Luo Dayou", .gender='M', .album="Zhihuzheye", .score={ .c_score=10, .java_score=100, .python_score=80 } }; Musician Zhang={ .name="Zhangchu", .gender='M', .album="hahaha", .score={ .c_score=30, .java_score=50, .python_score=90 } }; Zhang=Luo; PrintMusician(&Zhang); PrintMusician(&Luo); return 0; }
12.26学习笔记
最新推荐文章于 2024-07-27 17:39:19 发布