當前位置:編程學習大全網 - 編程語言 - 有4個學生,每個學生有3門課的成績,從鍵盤上輸入以上數據,計算每個學生的平均分

有4個學生,每個學生有3門課的成績,從鍵盤上輸入以上數據,計算每個學生的平均分

#include <stdio.h>

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;

}

  • 上一篇:笑談學校 買取通關特典禮包 在哪裏買
  • 下一篇:電腦讓我歡喜讓我憂的作文50個字
  • copyright 2024編程學習大全網