/** * @file StudentStructSort.h * @author geovindu,Geovin Du,Tu Juwen ([email protected]) * ide: vscode c11,c17 Ubuntu 22.4 * @brief Structure sorting example * @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 hero * */ structHero { /** * @brief name * */ char name[20]; /** * @brief age * */ int age; /** * @brief gender * */ char sex[2]; }; /** * @brief Sort in ascending order * * @param a * @param b * @return int */ int cmp(const void *a,const void *b); /** * @brief lifting * * @param her * @param n */ void SortBubble(struct Hero her[10],int n); /** * @brief comparison * * @param px * @param py */ void TuSwap(struct Hero *px, struct Hero *py); /** * @brief ascending order * * @param her * @param n */ void SortBubbleAsc(struct Hero her[10],int n); /** * @brief descending order * * @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 Structure sorting example * @author geovindu,Geovin Du,Tu Juwen ([email protected]) * 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 Sort in ascending order * * @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; //Sort in ascending order return c.age-d.age; } /** * @brief lifting * * @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( & amp;her[j], & amp;her[j + 1]); } } } /** * @brief comparison * * @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 ascending order * * @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( & amp;her[j], & amp;her[j + 1]); //temp=her[j]; // her[j]=her[j + 1]; // her[j + 1]=temp; } } } /** * @brief descending order * * @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( & amp;her[j], & amp;her[j + 1]); } } } /** *@brief * * @param her * @param n */ void PrintList(struct Hero her[],int n) { for(int i=0;i<n;i + + ) { printf("Message: %s \t %d\t %s$\ ",her[i].name,her[i].age,her[i].sex); } }
/** * @file geovindu.h *@brief * @author geovindu,Geovin Du,Tu Juwen ([email protected]) * 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_ #defineGEOVINDU_H_ #include <stdio.h> #include <string.h> #include <stdbool.h> /** *@brief * */ void displayHero(); #endif
/** * @file geovindu.c *@brief * @author geovindu,Geovin Du,Tu Juwen ([email protected]) * 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("Enter 5 heroes:\ "); printf("Name\t Age \t Gender:\ "); int n; struct Hero sz[100]; n=5; for(int i=0;i<n;i + + ){ scanf("%s %d %s", & amp;sz[i].name, & amp;sz[i].age, & amp;sz[i].sex); } /* qsort function parameters: */ //1 //qsort(sz,n,sizeof(sz[0]),cmp); //2 //SortBubble(sz,5); //3 SortBubbleDesc(sz,5); printf("\ In descending order of age:\ \ "); printf("Name\t Age \t Gender:\ "); for(int i=0;i<n;i + + ){ printf("%s\t %d \t%s \ ",sz[i].name,sz[i].age,sz[i].sex); } //4 SortBubbleAsc(sz,5); //qsort(sz,n,sizeof(sz[0]),cmpSort); printf("\ In ascending order of age:\ \ "); printf("Name\t Age \t Gender:\ "); for(int i=0;i<n;i + + ){ printf("%s\t %d \t%s \ ",sz[i].name,sz[i].age,sz[i].sex); } }
transfer:
printf("hello c world, \ "); printf("Hello, China\ "); displayHero();
vscode adjusts resource files
Eclipse IDE for Embedded C and C++ Developers header files
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194919 people are learning the system