统计学生人数和成绩(静态)

题目:

统计学生人数和成绩

Problem Description

定义一个Student类记录学生的学号和C++课程的成绩。要求使用静态成员变量和静态成员函数计算全班学生C++课程的总成绩和平均成绩。

Input

输入每个学生的学号和成绩,每个学生的信息占一行,直到文件结束。

Output

输出包括两行,第一行全班人数和总成绩,用空格隔开;
第二行平均成绩。

Sample Input

101  30
102  50
103  90
104  60
105  70

Sample Output

5 300
60

参考代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

class Student
{

private:
    int id, score;
    static int count;
    static int sum;

public:
    Student(int i, int s);
    Student(const Student&);
    ~Student();
    static void show();

};

Student::Student(int i, int s)
{
    id = i;
    score = s;
    count++;
    sum += score;
}

Student::Student(const Student& c)
{
    id = c.id;
    score = c.score;
}

Student::~Student()
{

}

void Student::show()
{
    cout << count << " " << sum << endl;
    cout << (double)(1.0 * sum / count) << endl;
}
int Student::count = 0;
int Student::sum = 0;

int main()
{
    //freopen("1.txt", "r", stdin);

    int id, score;

    while (cin >> id >> score)
        Student x(id, score);

    Student::show();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值