#include<stdio.h>
#include<string.h>
#define N 10
struct student //定义结构体student
{
char name[20];
int score;
};
struct student stu[N]; //全局定义结构体数组变量
void Datesort()
{
int i,j,temp;
char a[20],b[20];
for(i=0;i<N-1;i++) //冒泡法排序
{
for(j=i+1;j<N;j++)
{
if(stu[i].score>stu[j].score)
{
temp=stu[i].score; //交换成绩
stu[i].score=stu[j].score;
stu[j].score=temp;
strcpy(a,stu[i].name); //交换姓名 (字符串的复制函数strcpy(),调用前提为文件头必须有string.h,用a=stu[i].name这样的形式是无法实现复制的。)
strcpy(b,stu[j].name);
strcpy(stu[j].name,a);
strcpy(stu[i].name,b);
}
}
}
}
void main()
{
int i;
printf("please enter 10 students' names and scores\n");
for(i=0;i<N;i++)
{
scanf("%s",&stu[i].name);
scanf("%d",&stu[i].score);
}
Datesort(); //调用Datesort()函数进行数据排序
printf("name score\n");
for(i=0;i<N;i++) //输出排序后的数据
printf("%s %d\n",stu[i].name,stu[i].score);
}
#include<string.h>
#define N 10
struct student //定义结构体student
{
char name[20];
int score;
};
struct student stu[N]; //全局定义结构体数组变量
void Datesort()
{
int i,j,temp;
char a[20],b[20];
for(i=0;i<N-1;i++) //冒泡法排序
{
for(j=i+1;j<N;j++)
{
if(stu[i].score>stu[j].score)
{
temp=stu[i].score; //交换成绩
stu[i].score=stu[j].score;
stu[j].score=temp;
strcpy(a,stu[i].name); //交换姓名 (字符串的复制函数strcpy(),调用前提为文件头必须有string.h,用a=stu[i].name这样的形式是无法实现复制的。)
strcpy(b,stu[j].name);
strcpy(stu[j].name,a);
strcpy(stu[i].name,b);
}
}
}
}
void main()
{
int i;
printf("please enter 10 students' names and scores\n");
for(i=0;i<N;i++)
{
scanf("%s",&stu[i].name);
scanf("%d",&stu[i].score);
}
Datesort(); //调用Datesort()函数进行数据排序
printf("name score\n");
for(i=0;i<N;i++) //输出排序后的数据
printf("%s %d\n",stu[i].name,stu[i].score);
}