?? 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 + -