實(shí)驗(yàn)9-6 按等級(jí)統(tǒng)計(jì)學(xué)生成績 (20 分)
1. 題目摘自
https://pintia.cn/problem-sets/13/problems/569
2. 題目內(nèi)容
本題要求實(shí)現(xiàn)一個(gè)根據(jù)學(xué)生成績?cè)O(shè)置其等級(jí),并統(tǒng)計(jì)不及格人數(shù)的簡(jiǎn)單函數(shù)拐纱。
函數(shù)接口定義:
int set_grade( struct student *p, int n );
其中p是指向?qū)W生信息的結(jié)構(gòu)體數(shù)組的指針濒蒋,該結(jié)構(gòu)體的定義為:
struct student{
int num;
char name[20];
int score;
char grade;
};
n是數(shù)組元素個(gè)數(shù)。學(xué)號(hào)num尼啡、姓名name和成績score均是已經(jīng)存儲(chǔ)好的习霹。set_grade函數(shù)需要根據(jù)學(xué)生的成績score設(shè)置其等級(jí)grade暇屋。等級(jí)設(shè)置:85-100為A,70-84為B攘轩,60-69為C叉存,0-59為D。同時(shí)度帮,set_grade還需要返回不及格的人數(shù)鹉胖。
輸入樣例:
10
31001 annie 85
31002 bonny 75
31003 carol 70
31004 dan 84
31005 susan 90
31006 paul 69
31007 pam 60
31008 apple 50
31009 nancy 100
31010 bob 78
輸出樣例:
The count for failed (<60): 1
The grades:
31001 annie A
31002 bonny B
31003 carol B
31004 dan B
31005 susan A
31006 paul C
31007 pam C
31008 apple D
31009 nancy A
31010 bob B
3. 源碼參考
#include <iostream>
using namespace std;
#define MAXN 10
struct student{
int num;
char name[20];
int score;
char grade;
};
int set_grade( struct student *p, int n );
int main()
{ struct student stu[MAXN], *ptr;
int n, i, count;
ptr = stu;
cin >> n;
cin.ignore();
for(i = 0; i < n; i++)
{
cin >> stu[i].num >> stu[i].name >> stu[i].score;
}
count = set_grade(ptr, n);
cout << "The count for failed (<60): " << count << endl;
cout << "The grades:" << endl;
for(i = 0; i < n; i++)
{
cout << stu[i].num << " " << stu[i].name << " " << stu[i].grade << endl;
}
return 0;
}
int set_grade( struct student *p, int n )
{
int s;
char g;
int cnt;
cnt = 0;
for(int i = 0; i < n; i++)
{
s = p[i].score;
if(s >= 85)
{
g = 'A';
}
else if(s >= 70)
{
g = 'B';
}
else if(s >= 60)
{
g = 'C';
}
else
{
g = 'D';
cnt++;
}
p[i].grade = g;
}
return cnt;
}