如何使用C编程语言基于结构的平均运行以表格形式存储板球运动员的数据的排序顺序。
让我们尝试输入板球运动员的信息,例如姓名,年龄,比赛次数和他的平均得分。它将在运行时使用结构概念输入到控制台中。
并尝试根据每个人的平均得分,以表格形式显示信息,以便于轻松识别每个人的详细信息。
我们用来根据板球得分的平均升序对板球进行升序排列的逻辑是-
for(i=0;i<2;i++){
for(j=i+1;j<2;j++){
if(c[i].avrn > c[j].avrn){
temp1=c[i];
c[i]=c[j];
c[j]=temp1;
}
}
}#include<stdio.h>
#include<conio.h>
#include<string.h>
struct cricketer{
char name[50];
int age;
int match;
float avrn;
char temp;
};
struct cricketer c[20],temp1;
void main() {
int i,j;
for(i=0;i<2;i++){
printf("Enter data of cricketer %d\n",i+1);
//fflush(stdin);
printf("Name: ");
gets(c[i].name);
printf("\nAge: ");
scanf("%d",&c[i].age);
printf("\nMatches: ");
scanf("%d",&c[i].match);
printf("\n\nAverage runs: ");
scanf("%f",&c[i].avrn);
scanf("%c",&c[i].temp);
}
/******************/
/* sorting records */
/*******************/
for(i=0;i<2;i++) {
for(j=i+1;j<2;j++) {
if(c[i].avrn > c[j].avrn){
temp1=c[i];
c[i]=c[j];
c[j]=temp1;
}
}
}
printf("Sorted records:\n");
for(i=0;i<2;i++){
printf("%d\t%s\t%d\t%d\t%f\n\n\n",i+1,c[i].name,c[i].age,c[i].match,c[i].avrn);
}
getch();
}输出结果Enter data of cricketer 1 Name: Dhoni Age: 39 Matches: 150 Average runs: 200 Enter data of cricketer 2 Name: virat Age: 36 Matches: 135 Average runs: 190 Sorted records: 1 virat 36 135 190.000000 2 Dhoni 39 150 200.000000