c: struct sort descending and ascending in windows and Ubuntu

25 篇文章 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();

vscode 调资源文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值