📄 exam12-13.cpp
字号:
/*文件名:exam12_13.cpp*/
#include <stdio.h>
#define N 5
typedef struct student
{
char name[10];
int score;
} Sttype;
void bubble(Sttype a[]);
main()
{
FILE *fp;
Sttype stud[N];
int i=0;
if ((fp=fopen("stud.bin","rb"))==NULL)
{
printf("不能读取stud.bin文件\n");
return;
}
for (i=0;i<N;i++)
fread(&stud[i],sizeof(struct student),1,fp);
bubble(stud);
fclose(fp);
if ((fp=fopen("stud.bin","wb"))==NULL)
{
printf("不能建立文件stud.bin\n");
return;
}
for (i=0;i<5;i++)
fwrite(&stud[i],sizeof(struct student),1,fp);
fclose(fp);
}
void bubble(Sttype a[]) /*将结构体数组a按score成员递减排序*/
{
int i,j,exchange;
Sttype tmp;
for (i=0;i<N-1;i++)
{
exchange=0;
for (j=N-2;j>=i;j--)
if (a[j+1].score>a[j].score)
{
tmp=a[j+1];
a[j+1]=a[j];
a[j]=tmp;
exchange=1;
}
if (!exchange) /*本趟未发生交换,排序完成,退出for循环*/
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -