【对象数组+静态数据成员+静态成员函数+...】面向对象程序设计(B)——第二次作业

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值