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固定虚拟机ip地址的方法教程

《Ubuntu固定虚拟机ip地址的方法教程》本文详细介绍了如何在Ubuntu虚拟机中固定IP地址,包括检查和编辑`/etc/apt/sources.list`文件、更新网络配置文件以及使用Networ... 1、由于虚拟机网络是桥接,所以ip地址会不停地变化,接下来我们就讲述ip如何固定 2、如果apt安

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在 Windows 上安装 DeepSeek 的完整指南(最新推荐)

《在Windows上安装DeepSeek的完整指南(最新推荐)》在Windows上安装DeepSeek的完整指南,包括下载和安装Ollama、下载DeepSeekRXNUMX模型、运行Deep... 目录在www.chinasem.cn Windows 上安装 DeepSeek 的完整指南步骤 1:下载并安装

怎么关闭Ubuntu无人值守升级? Ubuntu禁止自动更新的技巧

《怎么关闭Ubuntu无人值守升级?Ubuntu禁止自动更新的技巧》UbuntuLinux系统禁止自动更新的时候,提示“无人值守升级在关机期间,请不要关闭计算机进程”,该怎么解决这个问题?详细请看... 本教程教你如何处理无人值守的升级,即 Ubuntu linux 的自动系统更新。来源:https://

Ubuntu系统怎么安装Warp? 新一代AI 终端神器安装使用方法

《Ubuntu系统怎么安装Warp?新一代AI终端神器安装使用方法》Warp是一款使用Rust开发的现代化AI终端工具,该怎么再Ubuntu系统中安装使用呢?下面我们就来看看详细教程... Warp Terminal 是一款使用 Rust 开发的现代化「AI 终端」工具。最初它只支持 MACOS,但在 20

windows系统下shutdown重启关机命令超详细教程

《windows系统下shutdown重启关机命令超详细教程》shutdown命令是一个强大的工具,允许你通过命令行快速完成关机、重启或注销操作,本文将为你详细解析shutdown命令的使用方法,并提... 目录一、shutdown 命令简介二、shutdown 命令的基本用法三、远程关机与重启四、实际应用

Windows自动化Python pyautogui RPA操作实现

《Windows自动化PythonpyautoguiRPA操作实现》本文详细介绍了使用Python的pyautogui库进行Windows自动化操作的实现方法,文中通过示例代码介绍的非常详细,对大... 目录依赖包睡眠:鼠标事件:杀死进程:获取所有窗口的名称:显示窗口:根据图片找元素:输入文字:打开应用:依

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

javafx 如何将项目打包为 Windows 的可执行文件exe

《javafx如何将项目打包为Windows的可执行文件exe》文章介绍了三种将JavaFX项目打包为.exe文件的方法:方法1使用jpackage(适用于JDK14及以上版本),方法2使用La... 目录方法 1:使用 jpackage(适用于 JDK 14 及更高版本)方法 2:使用 Launch4j(