当前位置: 首页 > news >正文

c: struct sort descending and ascending in windows and Ubuntu

/*** @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);//3SortBubbleDesc(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);}//4SortBubbleAsc(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 调头文件

http://www.lryc.cn/news/226499.html

相关文章:

  • Python - 利用 OCR 技术提取视频台词、字幕
  • VUE页面导出PDF方案
  • 机器学习笔记 - WGAN生成对抗网络概述和示例
  • HoudiniVex笔记_P0_Houdini中文文档与翻译
  • 基于PowerWord的储能在主动配电网中的仿真研究
  • 并查集与最小生成树
  • 平面运动机器人的传感器外参标定
  • 【星海随笔】SDN neutron (二) Neutron-plugin(ML2)
  • 野火i.MX6ULL开发板检测按键evtest(Linux应用开发)
  • k8s存储
  • 数据分析实战 | 贝叶斯分类算法——病例自动诊断分析
  • 实用技巧:嵌入式人员使用http服务模拟工具模拟http服务器测试客户端get和post请求
  • P9836 种树
  • C# 查询腾讯云直播流是否存在的API实现
  • JAVA开源项目 于道前端项目 启动步骤参考
  • 深入理解ElasticSearch分片
  • 【Python】AppUI自动化—appium自动化元素定位、元素事件操作(17)下
  • SpringBoot使用MyBatis多数据源
  • 小程序版本审核未通过,需在开发者后台「版本管理—提交审核——小程序订单中心path」设置订单中心页path,请设置后再提交代码审核
  • Netty入门指南之NIO Selector监管
  • Spring Cloud学习(六)【统一网关 Gateway】
  • 基于单片机的空调智能控制器的设计
  • Spring Boot自动配置原理、实战、手撕自动装配源码
  • 111111111111111
  • React动态生成二维码和毫米(mm)单位转像素(px)单位
  • SpringMvc 常见面试题
  • jmeter接口自动化测试工具在企业开展实际的操作
  • 第17章 反射机制
  • 如何在在线Excel文档中对数据进行统计
  • redis配置文件详解