1. 程式人生 > >day07 結構體 匿名結構體 結構體的初始化 訪問 結構體巢狀 結構體的儲存空間 結構體陣列

day07 結構體 匿名結構體 結構體的初始化 訪問 結構體巢狀 結構體的儲存空間 結構體陣列

//結構體的定義/*
 struct
結構體的定義{
 
資料型別成員變數1;
 
資料型別成員變數2;
 .....
 
資料型別成員變數n;
 };
注意“;”
 */
//typedef(第一種方式)//先定義結構體,在說明結構體變數//struct myPoint{//    float x;//行座標//    float y;//縱座標//};//typedef struct myPoint point;//2中方式//在定義結構體的同時,說明結構體變數/*
typedef struct myPoint{
    float x;//
行座標
    float y;//
縱座標
}point;
 */
//定義第三種方式//匿名結構體//struct{//    char name[20];
//    int age;//    char sex;//}stu1,stu2,stu3;////typedef struct student//{//    char name[20];//    int age;//    int number;//}Student;typedefstruct student1{
   
char name[20];
   
char sex;
   
int num;
   
int age;
   
float score;
}Student;


void sortStudentByScore(Student stu[],int count)
{
   
for (int i=0; i<count-
1; i++) {
       
for (int j=0; j<count-1-i; j++) {
           
if (stu[j].score>stu[j+1].score) {
               
Student tmp=stu[j];
                stu[j]=stu[j+
1];
                stu[j+
1]=tmp;
            }
        }
    }
}

//獲取學生陣列成績的最高者Student getStudentMaxByScore(Student stu[],int count)
{
   
Student maxScore={0};
   
for (int i=0; i<count; i++)
    {
       
//通過成績比較最大值
        maxScore=stu[i].
score > maxScore.score ? stu[i] : maxScore;
    }
   
return maxScore;
}

Student getStudentMaxByAge(Student stu[],int count)
{
   
Student maxAge={0};
   
for (int i=0; i<count; i++)
    {
       
//通過成績比較最大值
        maxAge=stu[i].
age > maxAge.age ? stu[i] : maxAge;
    }
   
return maxAge;
}

void printStudent(Student stu[],int count)
{
   
for (int i=0; i<count; i++) {
       
printf("name:%s,sex:%c,num:%d,age:%d,score:%.2f\n",stu[i].name,stu[i].sex,stu[i].num,stu[i].age,stu[i].score);
    }
}

int main(int argc, constchar * argv[]) {
#pragma mark-結構體//struct myPoint point={2.0,5.3};//定義一個數據型別為//struct myPoint,變數名為point,初值為(2.05.3)的一個變數。//printf("%lu %lu",sizeof(struct myPoint),sizeof(point));//前面加typedef,後面加上point,以後可以用point代替結構體變數//    Student stu1={"laowang",18,001};//    Student stu2={"laozhang",80,002};//    //訪問結構體.//    stu1.age=19;//    stu2.number=003;//    strcpy(stu1.name,"wangbadan");//    printf("name=%s,age=%d,number=%d",stu1.name,stu1.age,stu1.number);// Stu stu1={"張三",'n',18,001,59.99};// printf("name=%s,sex=%c,age=%d,number=%d,score=%.2f\n",stu1.name,stu1.sex,stu1.age,stu1.num,stu1.score);//給成員變數賦值// stu1.age=28;//Stu stu2={"王五",'f',19,002,60.0};// printf("name=%s,sex=%c,age=%d,number=%d,score=%.2f\n",stu2.name,stu2.sex,stu2.age,stu2.num,stu2.score);//Tea tea1={"張全福",'n',25,150,1200.00};//    printf("name=%s,sex=%c,age=%d,height=%d,money=%.2f\n",tea1.name,tea1.sex,tea1.age,tea1.height,tea1.money);#pragma mark-匿名結構體//    struct//    {//        char name[20];//        short num;//        char sex;//        int age;//    }stu={0};//    struct {//        float x;//        float y;//    }point1={0};//    struct myPoint point2={0};//    struct myPoint point3={0};//    struct student stu4={"張三丰",19,'m'};//    printf("%s %d %s",stu4.name,stu4.age,stu4.sex);#pragma mark-結構體的初始化//第一種初始化方法//先定義結構體,在初始化//    struct student{//        char name[20];//        int age;//        char sex;//    };//    struct student boy1={"張三",29,‘f’};//誤點//struct student boy1.name="zhangsanfeng";//錯誤//strcpy(boy1.name,"zhangsanfeng");//誤點//boy1={"laowang",89,'m'};//正確boy1=struct student{"laowang",89,'m'};//2中初始化的方法//struct student{//        char name[20];//        int age;//        char sex;//    }boy2={"wangnima",78,'m'};#pragma mark-訪問//更改姓名://strcpy(boy2.name,"你是誰");// printf("姓名是:%s 年齡是:%d 性別是:%c",boy2.name,boy2.age,boy2.sex);#pragma mark-結構體巢狀//    struct birthday{//        int year;//        int month;//        int day;//    };//    struct teacher//    {//        char name[20];//        int age;//        char sex;//        struct birthday bir;//    };//    //    struct teacher tea={"laogao",49,'m',{1970,11,11}};//    printf("姓名:%s,年齡:%d,性別:%c,生日是:(%d %d %d)\n",tea.name,tea.age,tea.sex,tea.bir.year,tea.bir.month,tea.bir.day);//第一種方式已經有了引數列表// typedef struct struct birthday Birthday;//2中方式//   typedef struct birthday{//        int year;//        int month;//        int day;//    }Birthday;#pragma mark-結構體的儲存空間//    struct teacher{//        char a;  //1//        int c;   //4//        long d;  //8//        short e;  //2//        double b;  //8//        float f;//4//    };//    printf("%lu",sizeof(struct teacher));#pragma mark-結構體陣列Student xiaoMing={"xiaoMing",'m',01,13,59.99};
   
Student xiaoHong={"xiaoHong",'f',02,12,89.9};
   
Student xiaoTang={"xiaotang",'f',03,14,100.00};
   
Student xiaoWangBa={"xiaoWangBa",'m',04,11,92.9};
   
Student xiaoDong={"xiaoDong",'m',05,15,15};
   
   
Student stu[5]={xiaoMing,xiaoHong,xiaoTang,xiaoWangBa,xiaoDong};
   
   
   
//呼叫// sortStudentByScore(stu, 5);//printStudent(stu, 5);Student maxScore=getStudentMaxByScore(stu, 5);
   
printf("name:%s,sex:%c,num:%d,age:%d,score:%.2f\n",maxScore.name,maxScore.sex,maxScore.num,maxScore.age,maxScore.score);
   
   
Student maxAge=getStudentMaxByAge(stu, 5);
   
printf("name:%s,sex:%c,num:%d,age:%d,score:%.2f\n",maxAge.name,maxAge.sex,maxAge.num,maxAge.age,maxAge.score);
   
return0;