c: struct sort descending and ascending

发布时间 2023-11-05 15:52:05作者: ®Geovin Du Dream Park™

 

/**
 * @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("输入英雄:\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;
}