c: struct sort descending and ascending in windows and Ubuntu

2023-11-10 22:44

本文主要是介绍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 调头文件

这篇关于c: struct sort descending and ascending in windows and Ubuntu的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/385776

相关文章

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

新特性抢先看! Ubuntu 25.04 Beta 发布:Linux 6.14 内核

《新特性抢先看!Ubuntu25.04Beta发布:Linux6.14内核》Canonical公司近日发布了Ubuntu25.04Beta版,这一版本被赋予了一个活泼的代号——“Plu... Canonical 昨日(3 月 27 日)放出了 Beta 版 Ubuntu 25.04 系统镜像,代号“Pluc

Windows Server服务器上配置FileZilla后,FTP连接不上?

《WindowsServer服务器上配置FileZilla后,FTP连接不上?》WindowsServer服务器上配置FileZilla后,FTP连接错误和操作超时的问题,应该如何解决?首先,通过... 目录在Windohttp://www.chinasem.cnws防火墙开启的情况下,遇到的错误如下:无法与

Python解析器安装指南分享(Mac/Windows/Linux)

《Python解析器安装指南分享(Mac/Windows/Linux)》:本文主要介绍Python解析器安装指南(Mac/Windows/Linux),具有很好的参考价值,希望对大家有所帮助,如有... 目NMNkN录1js. 安装包下载1.1 python 下载官网2.核心安装方式3. MACOS 系统安

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

Windows命令之tasklist命令用法详解(Windows查看进程)

《Windows命令之tasklist命令用法详解(Windows查看进程)》tasklist命令显示本地计算机或远程计算机上当前正在运行的进程列表,命令结合筛选器一起使用,可以按照我们的需求进行过滤... 目录命令帮助1、基本使用2、执行原理2.1、tasklist命令无法使用3、筛选器3.1、根据PID

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使

Windows server服务器使用blat命令行发送邮件

《Windowsserver服务器使用blat命令行发送邮件》在linux平台的命令行下可以使用mail命令来发送邮件,windows平台没有内置的命令,但可以使用开源的blat,其官方主页为ht... 目录下载blatBAT命令行示例备注总结在linux平台的命令行下可以使用mail命令来发送邮件,Win

Windows环境下安装达梦数据库的完整步骤

《Windows环境下安装达梦数据库的完整步骤》达梦数据库的安装大致分为Windows和Linux版本,本文将以dm8企业版Windows_64位环境为例,为大家介绍一下达梦数据库的具体安装步骤吧... 目录环境介绍1 下载解压安装包2 根据安装手册安装2.1 选择语言 时区2.2 安装向导2.3 接受协议