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 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Python struct.unpack() 用法及常见错误详解

《Pythonstruct.unpack()用法及常见错误详解》struct.unpack()是Python中用于将二进制数据(字节序列)解析为Python数据类型的函数,通常与struct.pa... 目录一、函数语法二、格式字符串详解三、使用示例示例 1:解析整数和浮点数示例 2:解析字符串示例 3:解

Windows 系统下 Nginx 的配置步骤详解

《Windows系统下Nginx的配置步骤详解》Nginx是一款功能强大的软件,在互联网领域有广泛应用,简单来说,它就像一个聪明的交通指挥员,能让网站运行得更高效、更稳定,:本文主要介绍W... 目录一、为什么要用 Nginx二、Windows 系统下 Nginx 的配置步骤1. 下载 Nginx2. 解压