9-6 按等級統計學生成績
阿新 • • 發佈:2018-12-01
// 按等級統計學生成績 #include <stdio.h> #define N 10 struct Student { char num[15]; char name[15]; int score; char grade; }; void set_grade (struct Student *p, int n, int *f); int main(void) { int i, n; int fail = 0; struct Student s[N]; printf("請輸入學生人數:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("學生%d資訊\n",i+1); printf("學號:"); scanf("%s",s[i].num); printf("姓名:"); scanf("%s",s[i].name); printf("成績:"); scanf("%d",&s[i].score); } set_grade (s,n,&fail); printf("學生成績資訊\n"); printf("姓名\t等級\n"); for (i=0;i<n;i++) { printf("%s\t",s[i].name); printf("%c\n",s[i].grade); } printf("不及格人數:%d\n",fail); return 0; } void set_grade (struct Student *p, int n, int *f) { int i; *f = 0; for (i=0;i<n;i++,p++) { if (p->score>=85&&p->score<=100) { p->grade = 'A'; } else if (p->score>=70&&p->score<=84) { p->grade = 'B'; } else if (p->score>=60&&p->score<=69) { p->grade = 'C'; } else { p->grade = 'D'; (*f)++; } } }