(5) 输入10个学生5门功课的成绩,分别用函数求:①每个学生的平均分;②每门课平均分;③找出最高的分数

2024-12-26 04:39:03
推荐回答(1个)
回答1:

//VC6.0下编译通过
//需要再进行一些简单修改,因为不完全符合你的要求
//有什么疑问,再追问吧。有了这个程序为基础,改起来就easy多了。
#include
using namespace std;
#define MaxSize 50
#define SubjectNum 2
#define MATH 0
#define ENGLISH 1

typedef struct node{
char name[20];
float score[SubjectNum];
struct node * next;
} Node;

class Students{
private:
int num;//学生数
Node student[MaxSize];//学生表
public:
Students(){ num = 0;}
bool Add();
float Average( int);
float Sum( int);
float Min( int);
float Max( int);
void Display();
};
float Students::Average( int _subject){
float sum = 0;
for( int i = 0; i < num; i++){
sum += student[i].score[_subject];
}
return sum / num;
}
float Students::Sum( int _subject){
float sum = 0;
for( int i = 0; i < num; i++){
sum += student[i].score[_subject];
}
return sum;
}
float Students::Max( int _subject){
float max = 0;
for( int i = 0; i < num; i++){
if( student[i].score[_subject] > max)
max = student[i].score[_subject];
}
return max;
}
float Students::Min( int _subject){
float min = 0;
for( int i = 0; i < num; i++){
if( student[i].score[_subject] < min)
min = student[i].score[_subject];
}
return min;
}
bool Students::Add(){
cout << "输入学生数:" ;
cin >> num;
if( num <= MaxSize){
for( int i = 0; i < num; i ++){
cout << "第" << i + 1 << "个学生的数据" << endl;
cout << "姓名:";
cin >> student[i].name;
cout << "数学:";
cin >> student[i].score[0];
cout << "英语:";
cin >> student[i].score[1];
}
cout << "完成" << endl << endl;;
return true;
}else{
cout << "失败" << endl;
return false;
}
}
void Students::Display(){
cout << "-----------------成绩单-------------" << endl << endl;
cout << "姓名\t" << "数学" << "\t英语" << endl;
for( int i = 0; i < num; i++){
cout << student[i].name << "\t" << student[i].score[MATH] << "\t" << student[i].score[ENGLISH] << endl;
}
cout << "平均\t" << Average( MATH) << "\t" << Average( ENGLISH) << endl;
cout << "总分\t" << Sum( MATH) << "\t" << Sum( ENGLISH) << endl;
cout << "最高\t" << Max( MATH) << "\t" << Max( ENGLISH) << endl;
cout << "最低\t" << Min( MATH) << "\t" << Min( ENGLISH) << endl;
cout << "-------------------------------------" << endl << endl;
}
int main(){
Students st;
st.Add();
st.Display();
return 0;
}