Think:
1知识点:对象数组+静态数据成员+静态成员函数+构造函数+复制构造函数+析构函数
题意:
编程实现求一定数量的学生的平均分。用类实现,Student类属性有学号、姓名、成绩、总分、数量,成员函数有总分及平均分。
输入:
多组测试数据,每组测试数据先输入学生数量n(n <= 4000),之后输入n行,每行输入第i个学生的学号,成绩,姓名,以空格分开
输出:
输出n个学生的总分和平均分,每组测试样咧以空格分开,详细格式参照样例输出
样例输入:
4
1 100 a
2 98 bb
3 99 ccc
4 99 dddd
1
1234 100 abcd
样例输出:
sum = 396
ave = 99
sum = 100
ave = 100
以下为Accepted代码
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int test_size = 4014;
class Student{
private:
int id, score;
string name;
public:
static int sum, num;
Student(){};
Student(int new_id, int new_score, string new_name);
Student(Student &t);
~Student(){};
void cin_data(int new_id, int new_score, string new_name);
static void pri_sum();
static void pri_ave();
};
int Student::sum = 0;
int Student::num = 0;
Student::Student(int new_id, int new_score, string new_name){
id = new_id;
score = new_score;
name = new_name;
sum += score;
num += 1;
}
Student::Student(Student &t){
id = t.id;
score = t.score;
name = t.name;
sum += score;
num += 1;
}
void Student::cin_data(int new_id, int new_score, string new_name){
id = new_id;
score = new_score;
name = new_name;
sum += score;
num += 1;
}
void Student::pri_sum(){
cout << "sum = " << sum << endl;
}
void Student::pri_ave(){
double ave = (double)sum / (double)num;
cout << "ave = " << ave << endl;
}
int main(){
int n, i, id, score;
string name;
Student test[test_size];
while(cin >> n){
Student::sum = 0;
Student::num = 0;
for(i = 0; i < n; i++){
cin >> id >> score >> name;
test[i].cin_data(id, score, name);
}
Student::pri_sum();
Student::pri_ave();
cout << endl;
}
return 0;
}