typedef struct student_type_tag{
char num[11];
char name[9];
char classname[20];
float score[3];
float ave;
}student_type;
student_type stud[4];
void save()
{
FILE *fp = fopen("score.txt", "wb");
if (fp)
{
for (int i = 0; i < 4; i++)
{
fwrite(&stud[i], sizeof(student_type), 1, fp);
}
fclose(fp);
}
}
void display()
{
FILE *fp = fopen("score.txt", "rb");
if (fp)
{
printf("\n------------成績---------------");
for (int i = 0; i < 4; i++)
{
student_type astud;
fread(&astud, sizeof(student_type), 1, fp);
printf("\n%11s | %9s | %20s | %3.1f | %3.1f | %3.1f | %3.1f",
astud.name, astud.name, astud.classname,
astud.score[0], astud.score[1], astud.score[2], astud.ave);
}
fclose(fp);
}
}
int main(int argc, char* argv[])
{
for (int i = 0; i < 1; i++)
{
printf("\n--輸入成績[%i/4]--\n", i+1);
printf("請輸入學號(最長11位):");scanf("%s", &stud[i].num);
printf("請輸入姓名(最長9位):");scanf("%s", &stud[i].name);
printf("請輸入班級(最長20位):");scanf("%s", &stud[i].classname);
printf("請輸入成績1(xx.x):");scanf("%f", &stud[i].score[0]);
printf("請輸入成績2(xx.x):");scanf("%f", &stud[i].score[1]);
printf("請輸入成績3(xx.x):");scanf("%f", &stud[i].score[2]);
stud[i].ave = (stud[i].score[0] + stud[i].score[1] + stud[i].score[2]) / 3;
}
printf("\n正在保存...");
save();
printf("ok");
display();
getchar();
getchar();
return 0;
}