題目內容:
利用結構體數組保存不超過10個學生的信息,每個學生的信息包括:學號、姓名和三門課(高數、物理和英語 )的成績和平均分(整型)。
編寫程序,從鍵盤輸入學生的人數,然后依次輸入每個學生的學號、姓名和3門課的成績
然后計算每個學生的平均分
最后按指定格式輸出每個學生的平均分
輸入格式:
先輸入一個整數,表示學生個數
然后每行輸入一個學生的信息:學號、姓名和高數、物理及英語成績
輸出格式:
輸出每個學生的平均分.printf中請用格式控制串"The average score of the %dth student is %d.\n"
輸入樣例:
5
1001 Zhang 100 90 80
1002 Wu 93 90 98
1003 Zhu 89 88 87
1004 Hu 90 98 98
1005 Wang 90 98 97
輸出樣例:
The average score of the 1th student is 90.
The average score of the 2th student is 93.
The average score of the 3th student is 88.
The average score of the 4th student is 95.
The average score of the 5th student is 95.
代碼如下:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <malloc.h> struct STU { char ID[20]; char Name[20]; float math; float physic; float english; }; int main() { int n,i,j,k; float res; scanf("%d", &n); if (n > 10) { return 0; } struct STU *mySTU = (struct STU*)malloc(sizeof(struct STU) * n); //動態內存分配關鍵 malloc struct STU* p = mySTU; for (i = 0; i < n; i++) { scanf("%s", &p->Name); scanf("%s", &p->ID); scanf("%f", &p->math); scanf("%f", &p->english); scanf("%f", &p->physic); *(p++); } struct STU *q = mySTU; for (j = 0; j < n; j++) { res = (q->math + q->physic + q->english)/3; k = (int)res; printf("The average score of the %dth student is %d.\n", j+1,k ); *(q++); } return 0; }