c: struct sort descending and ascending in windows and Ubuntu

编程入门 行业动态 更新时间:2024-10-14 10:43:06

c: struct sort <a href=https://www.elefans.com/category/jswz/34/1332780.html style=descending and ascending in windows and Ubuntu"/>

c: struct sort descending and ascending in windows and Ubuntu

/*** @file StudentStructSort.h* @author       geovindu,Geovin Du,涂聚文 (geovindu@163)* 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)* 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)* 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)* 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

本文发布于:2023-11-15 12:47:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:descending   sort   struct   Ubuntu   windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!