c: struct sort descending and ascending

 写法方式之一:

/**
 * @file hello.c
 * @author your name (geovindu)
 * @brief 
 * @ide vscode c11,c17 windows 10
 * @version 0.1
 * @date 2023-11-05
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

struct num1
{
    char a; //1
    char b; //2
    double c; //8
    short d; 
};

struct num2
{
    char a; //1
    double b;//2
    char c;
    int d;

};

/**
 * @brief 学生
 * 
 */
struct Student
{
    /**
     * @brief 姓名
     * 
     */
    char name[20];
    /**
     * @brief 年龄
     * 
     */
    int age;
    /**
     * @brief 成绩
     * 
     */
    int score;
};

/**
 * @brief 英雄
 * 
 */
struct Hero
{
    /**
     * @brief 姓名
     * 
     */
    char name[20];
    /**
     * @brief 年龄
     * 
     */
    int age;
    /**
     * @brief 性别
     * 
     */
    char sex[2];
};

/**
 * @brief 升序排序
 * 
 * @param a 
 * @param b 
 * @return int 
 */
int cmp(const void *a,const void *b){

    struct Hero c=*(struct Hero*)a;
    struct Hero d=*(struct Hero*)b;
    //按升序排序
    return c.age-d.age;
}



/**
 * @brief 升降
 * 
 * @param her 
 * @param n 
 */
void SortBubble(struct Hero her[10],int n)
 {
    	
    	for(int i=0;i<n;i++)
    	{
    		for(int j=0;j<n-1;j++)
    		{
    			if(her[j].age>her[j+1].age)
    				cmp(&her[j],&her[j+1]);
    		}
    	}
 }

/**
 * @brief 比较
 * 
 * @param px 
 * @param py 
 */
void swap(struct Hero *px, struct Hero *py) // Definition of Swap function
{
  struct Hero temp;
  temp = *px;
  *px = *py;
  *py = temp;
}

/**
 * @brief 升序
 * 
 * @param her 
 * @param n 
 */
void SortBubbleAsc(struct Hero her[10],int n)
 {
    	int i,j;
        struct Hero temp;
    	for(int i=0;i<n-1;i++)
    	{
    		for(int j=0;j<n-i-1;j++)
    		{
    			if(her[j].age>her[j+1].age)
    				swap(&her[j],&her[j+1]);
                    //temp=her[j];
                   // her[j]=her[j+1];
                   // her[j+1]=temp;
    		}
    	}
 }

/**
 * @brief 降序
 * 
 * @param her 
 * @param n 
 */
void SortBubbleDesc(struct Hero her[10],int n)
 {
    	int i,j;
        struct Hero temp;
    	for(int i=0;i<n-1;i++)
    	{
    		for(int j=0;j<n-i-1;j++)
    		{
    			if(her[j].age<her[j+1].age)
    				swap(&her[j],&her[j+1]);

    		}
    	}
 }
  /**
   * @brief 
   * 
   * @param her 
   * @param n 
   */
   void PrintList(struct Hero her[],int n)
    {
    	for(int i=0;i<n;i++)
    	{
    		printf("信息:%s \t %d\t %s$\n",her[i].name,her[i].age,her[i].sex);
    	}
    }

int main()
{

    puts("hello c language world!涂聚文\t");
    printf("涂聚文");
    struct Student stu={"张三",18,100};

    struct Student *p=&stu;
    p->score=80;
    printf("姓名%s 年龄%d 成绩%d \n",p->name,p->age,p->score);
    printf("输入5位英雄:\n");
    printf("姓名\t 年龄 \t 性别:\n");
    int n;
    struct Hero sz[100];
    n=5;
    for(int i=0;i<n;i++){
        scanf("%s %d %s",&sz[i].name,&sz[i].age,&sz[i].sex);
    }

    /*
    qsort函数参数:
   
    */
    //1
    //qsort(sz,n,sizeof(sz[0]),cmp);
    //2
    //SortBubble(sz,5);
    //3 
    //SortBubbleDesc(sz,5);
    //4
    SortBubbleAsc(sz,5);
    //qsort(sz,n,sizeof(sz[0]),cmpSort);
    printf("\n按年龄升序为:\n\n");
    printf("姓名\t 年龄 \t 性别:\n");
    for(int i=0;i<n;i++){
        printf("%s\t %s \t%d \n",sz[i].name,sz[i].sex,sz[i].age);
    }


    struct num1 n1;
    struct num2 n2;
    printf("sizeof(n1)=%d,sizeof(n2)%d\n",sizeof(n1),sizeof(n2));


    system("pause");
    return 0;
}

  

 写法方式之二:

 

/**
 * @file StudentStructSort.h
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  Ubuntu 22.4
 * @brief 结构体排序示例

 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * 
 */

#ifndef STUDENTSTRUCTSORT_H_
#define STUDENTSTRUCTSORT_H_
 
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>

/**
 * @brief 英雄
 *
 */
struct Hero
{
    /**
     * @brief 姓名
     *
     */
    char name[20];
    /**
     * @brief 年龄
     *
     */
    int age;
    /**
     * @brief 性别
     *
     */
    char sex[2];
};
 

/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b);


/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n);


/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py);



/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n);



/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n);


  /**
   * @brief
   *
   * @param her
   * @param n
   */
   void PrintList(struct Hero her[],int n);


#endif

  

 

 

 

/**
 * @file StudentStructSort..c
 * @brief 结构体排序示例
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com) 
 * ide: vscode c11,c17  Ubuntu 22.4
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * 
 */

#include "include/StudentStructSort.h"




/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b){
 
    struct Hero c=*(struct Hero*)a;
    struct Hero d=*(struct Hero*)b;
    //按升序排序
    return c.age-d.age;
}
 
 
 
/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n)
 {
         
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    cmp(&her[j],&her[j+1]);
            }
        }
 }
 
/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py) // Definition of Swap function
{
  struct Hero temp;
  temp = *px;
  *px = *py;
  *py = temp;
}
 
/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
                    //temp=her[j];
                   // her[j]=her[j+1];
                   // her[j+1]=temp;
            }
        }
 }
 
/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age<her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
 
            }
        }
 }
  /**
   * @brief
   *
   * @param her
   * @param n
   */
   void PrintList(struct Hero her[],int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("信息:%s \t %d\t %s$\n",her[i].name,her[i].age,her[i].sex);
        }
    }
 

  

/**
 * @file geovindu.h
 * @brief 
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com) 
 * ide: vscode c11,c17  Ubuntu 22.4
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * 
 * @copyright Copyright (c) 2023
 * 
 */



#ifndef GEOVINDU_H_
#define GEOVINDU_H_
 
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

/**
 * @brief 
 * 
 */
void displayHero();



#endif

  

/**
 * @file geovindu.c
 * @brief 
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com) 
 * ide: vscode c11,c17  Ubuntu 22.4
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "include/StudentStructSort.h"


/**
 * @brief 
 * 
 */
void displayHero()
{
    printf("输入5位英雄:\n");
    printf("姓名\t 年龄 \t 性别:\n");
    int n;
    struct Hero sz[100];
    n=5;
    for(int i=0;i<n;i++){
        scanf("%s %d %s",&sz[i].name,&sz[i].age,&sz[i].sex);
    }
 
    /*
    qsort函数参数:
    
    */
    //1
    //qsort(sz,n,sizeof(sz[0]),cmp);
    //2
    //SortBubble(sz,5);
    //3
    SortBubbleDesc(sz,5);
    printf("\n按年龄降序为:\n\n");
    printf("姓名\t 年龄 \t 性别:\n");
    for(int i=0;i<n;i++){
        printf("%s\t %d \t%s \n",sz[i].name,sz[i].age,sz[i].sex);
    }
    //4
    SortBubbleAsc(sz,5);
    //qsort(sz,n,sizeof(sz[0]),cmpSort);
    printf("\n按年龄升序为:\n\n");
    printf("姓名\t 年龄 \t 性别:\n");
    for(int i=0;i<n;i++){
        printf("%s\t %d \t%s \n",sz[i].name,sz[i].age,sz[i].sex);
    }
}

  

调用:

    printf("hello c world, \n");
    printf("你好,中国\n");

    displayHero();

  

输出:

 

 

 

 

/**
 * @file StudentStructSort.h
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  windows 10
 * @brief 结构体排序示例

 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * 
 */

#ifndef STUDENTSTRUCTSORT_H_
#define STUDENTSTRUCTSORT_H_
 
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>

/**
 * @brief 英雄
 *
 */
struct Hero
{
    /**
     * @brief 姓名
     *
     */
    char name[20];
    /**
     * @brief 年龄
     *
     */
    int age;
    /**
     * @brief 性别
     *
     */
    char sex[2];
};
 
/**
 * @brief 学生实体
 * 
 */
struct Student
{

    /**
     * @brief 姓名
     * 
     */
    char name[20];
    /**
     * @brief 年龄
     * 
     */
    int  age;
    /**
     * @brief 班级
     * 
     */
    char class[20];
    /**
     * @brief 成绩
     * 
     */
    double score;



};

/**
 * @brief 老师实体
 * 
 */
struct Teacher
{

    /**
     * @brief 教师编号
     * 
     */
    int id;
    /**
     * @brief 老师姓名
     * 
     */
    char *Name;
    /**
     * @brief 教师年龄
     * 
     */
    int age;
    /**
     * @brief 所教管的学生集合
     * 
     */
    //struct Student **stus;
    struct Student stus[5];
} Teacher;

/**
 * @brief 老师实体
 * 
 */
struct NewTeacher
{

    /**
     * @brief 教师编号
     * 
     */
    int id;
    /**
     * @brief 老师姓名
     * 
     */
    char *Name;
    /**
     * @brief 教师年龄
     * 
     */
    int age;
    /**
     * @brief 所教管的学生集合
     * 
     */
    //struct Student **stus;
    struct Student **stus;

} NewTeacher;

/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b);


/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n);


/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py);



/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n);



/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n);


  /**
   * @brief
   *
   * @param her
   * @param n
   */
void PrintList(struct Hero her[],int n);


    /**
     * @brief 静态编历老师
     * 
     */
void DisplayTeacher();

/**
 * @brief 遍历老师学生
 * 
 */
void DisplayTeacher1();

/**
 * @brief 
 * 
 * @param p 
 * @param n1 老师个数
 * @param n2 学生个数
 */
void initTeacher(struct NewTeacher *p, int n1, int n2);

/**
 * @brief Create a Teacher object
 * 
 * @param p 
 * @param n1 老师个数
 * @param n2  学生个数
 * @return int 
 */
int createTeacher(struct NewTeacher **p, int n1, int n2);



#endif

  

/**
 * @file StudentStructSort..c
 * @brief 结构体排序示例
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com) 
 * ide: vscode c11,c17  windows 10
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * 
 */

#include "include/StudentStructSort.h"


#define false 0
#define true 1

/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b)
{
 
    struct Hero c=*(struct Hero*)a;
    struct Hero d=*(struct Hero*)b;
    //按升序排序
    return c.age-d.age;
}
 
 
 
/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n)
 {
         
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    cmp(&her[j],&her[j+1]);
            }
        }
 }
 
/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py) // Definition of Swap function
{
  struct Hero temp;
  temp = *px;
  *px = *py;
  *py = temp;
}
 
/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
                    //temp=her[j];
                   // her[j]=her[j+1];
                   // her[j+1]=temp;
            }
        }
 }
 
/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age<her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
 
            }
        }
 }
  /**
   * @brief
   *
   * @param her
   * @param n
   */
   void PrintList(struct Hero her[],int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("信息:%s \t %d\t %s$\n",her[i].name,her[i].age,her[i].sex);
        }
    }



    /**
     * @brief 静态编历老师
     * 
     */
    void DisplayTeacher()
    {
        //struct Teacher teachs[3];
        struct Student stusd[5];


        struct Student **st1[5] ={ {"张一", 18, "一班",100},{"张三", 15, "一班",45},{"张三", 16, "一班",89},{"张三", 18, "一班",99},{"张三", 17, "一班",88}}; 
        struct Student **st2[5] ={ {"李一", 17, "二班",50},{"李三", 18, "二班",78},{"李三", 18, "二班",78},{"李三", 19, "二班",57},{"李三", 18, "二班",90}}; 
        struct Student **st3[5] ={ {"赵一", 19, "三班",68},{"赵三", 17, "三班",98},{"赵三", 15, "三班",97},{"赵三", 17, "三班",98},{"赵三", 16, "三班",78}}; 

        struct Teacher te1={1000,"吴丽老师",31,st1};
        struct Teacher te2={1001,"林祥生老师",29,st2};
        struct Teacher te3={1002,"何磊老师",32,st3};

        //struct Teacher te1={1000,"吴丽老师",31, {{"张一", 18, "一班",100},{"张三", 15, "一班",45},{"张三", 16, "一班",89},{"张三", 18, "一班",99},{"张三", 17, "一班",88}}};
        //struct Teacher te2={1000,"林祥生老师",29, {{"张一", 17, "二班",50},{"张三", 18, "二班",78},{"张三", 18, "二班",78},{"张三", 19, "二班",57},{"张三", 18, "二班",90}}};
        //struct Teacher te3={1000,"何磊老师",32, {{"张一", 19, "三班",68},{"张三", 17, "三班",98},{"张三", 15, "三班",97},{"张三", 17, "三班",98},{"张三", 16, "三班",78}}};

        //struct Teacher teachs[3]={te1,te2,te3};
        struct Teacher teachs[3]={{1000,"吴丽老师",31,{ {"张一", 18, "一班",100},{"张二", 15, "一班",45},{"张三", 16, "一班",89},{"张四", 18, "一班",99},{"张五", 17, "一班",88}}},
        {1001,"林祥生老师",29,{ {"李一", 17, "二班",50},{"李二", 18, "二班",78},{"李三", 18, "二班",78},{"李四", 19, "二班",57},{"李五", 18, "二班",90}}},
        {1002,"何磊老师",32,{ {"赵一", 19, "三班",68},{"赵二", 17, "三班",98},{"赵三", 15, "三班",97},{"赵四", 17, "三班",98},{"赵五", 16, "三班",78}}}
        };


        
        for(int i=0;i<3;i++)
        {
           printf("老师姓名\t编号\t年龄\n");
            printf("%s\t%d\t%d\t%s\n",teachs[i].Name,teachs[i].id,teachs[i].age,teachs[i].stus); 
        
            printf("管理学生\n");
            printf("姓名\t班级\t年龄\t分数\n");
            //stus=teachs[i].stus;
            struct Student *ppt=teachs[i].stus;
            for(int j=0;j<5;j++)
            {
                   printf("%s\t%s\t%d\t%.2f\n", teachs[i].stus[j].name,teachs[i].stus[j].class,teachs[i].stus[j].age,teachs[i].stus[j].score);
            }
            //stusd=teachs[i].stus;
            //struct Student *stus=ppt[0];
            //for (; i <sizeof(books)/sizeof(struct Book); i++) {
            //for(int j=0;j<sizeof(teachs[i].stus)/sizeof(struct Student);j++)
            //{
               //printf("%s\t%s\t%d\t%d\t%d\n",stus->name,stus->class,stus->age,stus->score);

            //}
           // for(int j=0;j<sizeof(teachs[i].stus)/sizeof(struct Student);j++)
           // {
               //printf("%s\t%s\t%d\t%d\t%d\n",ppt->name,ppt->class,ppt->age,ppt->score);

            //}
            //ppt++;
            //PrintStuList(ppt,5);

            printf("\n");
        }


   }


/**
 * @brief Create a Teacher object
 * 
 * @param p 
 * @param n1 老师个数
 * @param n2  学生个数
 * @return int 
 */
int createTeacher(struct NewTeacher **p, int n1, int n2)
{
	int len=0;
	int i,j,k;   //分配5个Student信息
	*p = (struct NewTeacher*)malloc (sizeof(struct NewTeacher)*n1);

	for (i=0;i<n1;i++)  //分配5个导师的地址空间
	{
		(*p)[i].Name=(char *)malloc (sizeof(char)*20);  
		if ((*p)[i].Name == NULL)
		{
		  return false;
		}
	} 

	//(*p)->stu=(char **)malloc (sizeof(char*)*5);
	for (j=0;j<n2;j++)
		{
		 (*p)[j].stus=(char **)malloc (sizeof(char)*n2);
		 if ((*p)[j].stus == NULL)
		 {
		   return false;
		 }
		 for (k=0;k<3;k++)
		 {
		 (*p)[j].stus[k]=(char *)malloc (sizeof(char)*20);
		 if ((*p)[j].stus[k] == NULL)
			 {
			 return false;
			 }
		 }
		}
	return true;
}
/**
 * @brief 
 * 
 * @param p 
 * @param n1 老师个数
 * @param n2 学生个数
 */
void initTeacher(struct NewTeacher *p, int n1, int n2)
{
	  int i,j,k=0;  //
	  if (p == NULL)
	  {
	    printf ("error\n");
	  }
	  //n1 =3; //导师个数
	  //n2 = 5 //学生
         struct Student *st1[5] ={ {"张一", 18, "一班",100},{"张三", 15, "一班",45},{"张三", 16, "一班",89},{"张三", 18, "一班",99},{"张三", 17, "一班",88}}; 
        struct Student *st2[5] ={ {"张一", 17, "二班",50},{"张三", 18, "二班",78},{"张三", 18, "二班",78},{"张三", 19, "二班",57},{"张三", 18, "二班",90}}; 
        struct Student *st3[5] ={ {"张一", 19, "三班",68},{"张三", 17, "三班",98},{"张三", 15, "三班",97},{"张三", 17, "三班",98},{"张三", 16, "三班",78}}; 

	 puts ("-----导师赋值------");
	 for (i=0;i<n1;i++)
	 {
	    char *buf[3]={"吴丽老师","林祥生老师","何磊老师"};
        int *sage[3]={31,29,32};
        int *sid[3]={10,11,12};
		//p[i].age=22+i;
		//printf ("%d",p[i].age);
        p[i].id=sid[i];
        p[i].Name=buf[i];
        p[i].age=sage[i];
		//strcpy (p[i].id,sid[i]);
        //strcpy (p[i].Name,buf[i]);
        //strcpy (p[i].age,sage[i]);

	    //	printf ("%s",p[i].tName);
		for (j=0;j<n2;j++)
		{
		  
		  //struct Student *arr[]={"小黎","小田","小张","小王","小胡","小范",
			 // "小杨","小石","小柯"};
            //p[i].stus[j]->name=st1[j]->name;
            //p[i].stus[j]->class=st1[j]->class;
            //p[i].stus[j]->age=st1[j]->age;
           // p[i].stus[j]->score=st1[j]->score;
		  //strcpy (p[i].stus[j],st1[k]);
		  ++k;
		//  printf ("%s\n",p[i].stu[j]);
		}
	 }
}

/**
 * @brief 打印结构体成员信息
 * 
 * @param p 
 * @param n1 老师个数
 * @param n2 学生个数
 */
void printTeacher(struct NewTeacher *p, int n1, int n2)
{
	  int i,j;
	  if (p == NULL)
	  {
	   printf ("error\n");
	  }

	  for (i=0;i<n1;i++)
	  {
	    printf ("\t\t%s\t%d\t%d\n",p[i].Name,p[i].id,p[i].age);  //导师
		for (j=0;j<n2;j++)
		{
		   //printf ("\t%s",p[i].stus[j]);
		}
		putchar('\n');
		printf ("\t\t%d\n\n",p[i].age);
	  }
}

/**
 * @brief 根据导师名字排序, 降序
 * 
 * @param p 
 * @param n 老师个数
 */
void sortTeacher(struct NewTeacher *p, int n)
{
	  struct NewTeacher t1;
	  int i,j;
	   if (p==NULL)
	   {
	     printf ("error\n");
	   }
	   for (i=0;i<n-1;i++)
	   {
	     for (j=i+1;j<n;j++)
	     {
		   if ((strcmp (p[i].Name,p[j].Name))>0)
		   {
		     t1=p[i];
			 p[i]=p[j];
			 p[j]=t1;
		   }
	     }
	   }

}
/**
 * @brief 释放空间,在函数内部把p赋值为NULL
 * 
 * @param p 
 * @param n1 老师个数
 * @param n2 学生个数
 */
void freeTeacher(struct NewTeacher **p, int n1, int n2)
{
	   int i,j;
	   if (p == NULL)
	   {
	     printf ("Empty\n");
	   }
	   else
		   {
		      for (i=0;i<n1;i++)
		      {
			     for (j=0;j<n2;j++)
			     {
				   if (p[i]->stus[j]!=NULL)
				   {
				     free (p[i]->stus[i]);
					 p[i]->stus[j]=NULL;
				   }
			     }
				 if (p[i]->stus!= NULL)
				 {
				   free (p[i]->stus);
				   p[i]->stus=NULL;
				 }
		      }
		   }
}

/**
 * @brief 遍历老师学生
 * 
 */
void DisplayTeacher1()
{

        int ret = 0;
        int n1 = 3; //导师个数
        int n2 = 5; //学生
        struct Teacher *p = NULL;
        ret = createTeacher(&p, n1, n2);
        if (ret == false)
            {
            printf("createTeacher err:%d\n", ret);
            exit (EXIT_FAILURE);
            }

        initTeacher(p, n1, n2); //给成员赋值
        //打印成员,排序前
        printf("排序前:\n");
        printTeacher(p, n1, n2);
        //根据导师名字排序, 降序
        sortTeacher(p, n1);
        //打印成员,排序后
        printf("\n排序后:\n");
        printTeacher(p, n1, n2);
}


   
    /**
     * @brief 
     * 
     * @param stusd 
     * @param n 
     */
    void PrintStuList(struct Student *stusd,int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("%s\t%s\t%d\t%d\t%d\n",stusd[i].name,stusd[i].class,stusd[i].age,stusd[i].score);
        }
    }
 

  

 

꧁꫞꯭我加油꫞꧂🐬🌹:
/**
 * @brief       学生
 * 
 */
struct GaoStudent
{
 char *name;  // 或用 char *name
 float score; 
 int teachid;
};

/**
 * @brief       老师
 * 
 */
struct GaoTeacher
{

    int id;
    char *name;//1名老师,管理3名学生
    //struct GaoStudent stu[130]; // 或用: 这里什么类型,对应的
     struct GaoStudent *stu   

};

/**
 * @brief       
 * 
 */
void teststu()
{
    //学生
    struct GaoStudent stu[9] = {
    {"Tom", 89.00,3 },
    { "Marry", 79.00,1},
    { "Jerry", 86.00,2 },
    { "张三", 88.00,3 },
    { "李四", 82.00,1 },
    { "王五", 80.00,2},
    { "Kate", 79.60 ,3},
    { "Bob", 89.78,1 },
    { "Lily", 90.36 ,2}
    }; 

   struct GaoStudent studata3[3] ={    {"Tom", 89.00,3 },
    { "Marry", 79.00,1},
    { "Jerry", 86.00,2 }};
   struct GaoStudent *stu3=studata3;
    struct GaoStudent studata4[3] ={
        { "张三", 88.00,3 },
        { "李四", 82.00,1 },
        { "王五", 80.00,2}
    };
    struct GaoStudent *stu4=studata4;
   struct GaoStudent studata5[3] =
   {
    { "Kate", 79.60 ,3},
    { "Bob", 89.78,1 },
    { "Lily", 90.36 ,2}
   };
   struct GaoStudent *stu5=studata5;
    // 老师
    struct GaoTeacher t[3] = {
    { 1, "刘杰",stu3},
    { 2, "王大", stu4},
    { 3, "陈小", stu5}
    };

    for(int i = 0; i < 3; i++)
    {
    printf("编号%d 老师%s管理的学生有: \n", t[i].id,t[i].name ); //, t[i].stu->name
    //数组时用
    //struct GaoStudent *pt=&t[i].stu;
    struct GaoStudent *pt=t[i].stu;
    printf("姓名\t 分数\n");
    for(int j=0;j<3;j++)
    {
            printf("%s\t%.2f\n",(*pt++).name,pt->score);
    }
    printf("\n");
    };
    
}

  

vscode 调资源文件

 

Eclipse IDE for Embedded C and C++ Developers 调头文件

 

 

        //vscode 两个写法都可以。
        //printf("%s\t%.2f\t%d\n",(*pt++).name,(*pt).score,(*pt).teachid);
        printf("%s\t%.2f\t%d\n",(*pt++).name,pt->score,pt->teachid);

  

CLin 2023.1 结构也是调头文件

 

posted @ 2023-11-05 15:49  ®Geovin Du Dream Park™  阅读(46)  评论(0)    收藏  举报