c: struct sort descending and ascending in windows and Ubuntu

/**
 * @file StudentStructSort.h
 * @author       geovindu,Geovin Du,涂聚文 ([email protected])
 * 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 
#include 
#include 
#include 
#include 
 
/**
 * @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,涂聚文 ([email protected])
 * 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;iher[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;iher[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
/**
 * @file geovindu.h
 * @brief
 * @author       geovindu,Geovin Du,涂聚文 ([email protected])
 * 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 
#include 
#include 
 
/**
 * @brief
 *
 */
void displayHero();
 
 
 
#endif
/**
 * @file geovindu.c
 * @brief
 * @author       geovindu,Geovin Du,涂聚文 ([email protected])
 * 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

调用:

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

c: struct sort descending and ascending in windows and Ubuntu_第1张图片c: struct sort descending and ascending in windows and Ubuntu_第2张图片

vscode 调资源文件

c: struct sort descending and ascending in windows and Ubuntu_第3张图片

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

c: struct sort descending and ascending in windows and Ubuntu_第4张图片

你可能感兴趣的:(C,C++,c语言,开发语言)