The Statistician Class

Question:

    Specify, design, and implement a class called statistician. After a statistician is initialized, it can be given a sequence of double members. Each number in the sequence is given to the statistician by activating a member function called next_number. For example, we can declare a statistician called s, and then give it the sequence of numbers 1.1, -2.4, 0.8 as shown here:

statistician s;
s.next_number(1.1);
s.next_number(-2.4);
s.next_number(0.8);

    After a sequence has been given to a statistician, there are various member functions to obtain information about the sequence. Include member functions that will provide the length of the sequence, the last number of the sequence, the sum of all the numbers in the sequence, the arithmetic mean of the numbers (i.e, the sum of the numbers divided by the length of the sequence) , the smallest number in the sequence, and  the largest number in the sequence. Notice that the length and sum functions can be called at any time, even if there are no numbers in the sequence. In the case of an "empty" sequence, both length and sum will be zero. But the other member functions all have a precondition requiring that the sequence is non-empty.

    You should also provide a member function that erases the sequence (so that the statistician can start afresh with a new sequence).

    Notes: Do not try to store the entire sequence (because you don't know how long this sequence will be). Instead, just store the necessary information about the sequence: What is the sequence length? What is the sum of the numbers in the sequence? What are the last, smallest, and largest numbers? Each of these pieces of information can be stored in a private member variable that is updated whenever next_number is acticated.

My answer:

main.h

#ifndef MY_MAIN_H
#define MY_MATH_H

class statistician{
public:
    statistician(){
        length = min = max = sum = last_num = 0;
    };
    void next_number(double num);
    int sequence_length();
    double sequence_sum();
    double sequence_last();
    double sequence_mean();
    double sequence_min();
    double sequence_max();
    void erase();
private:
    int length;
    double min, max, sum, last_num;
};

#endif

main.cpp

#include "main.h"
#include <iostream>
#include <assert.h>

using namespace std;

int main(){
    statistician s;
    cout << "before insert" << endl;
    cout << "length: " << s.sequence_length() << endl;
    cout << "sum: " << s.sequence_sum() << endl;
    s.next_number(1.1);
    s.next_number(-2.4);
    s.next_number(0.8);
    s.next_number(2.1);
    s.next_number(-9);
    cout << "********************************" << endl;
    cout << "length: " << s.sequence_length() << endl;
    cout << "sum: " << s.sequence_sum() << endl;
    cout << "last number: " << s.sequence_last() << endl;
    cout << "mean: " << s.sequence_mean() << endl;
    cout << "min: " << s.sequence_min() << endl;
    cout << "max: " << s.sequence_max() << endl;
    s.erase();
    cout << "********************************" << endl;
    cout << "after erase" << endl;
    cout << "length: " << s.sequence_length() << endl;
    cout << "sum: " << s.sequence_sum() << endl;
    return 0;
}

void statistician::next_number(double num){
    length++;
    sum += num;
    if(min > num)
        min = num;
    if(max < num)
        max = num;
    last_num = num;
}

int statistician::sequence_length(){
    return length;
}

double statistician::sequence_sum(){
    return sum;
}

double statistician::sequence_last(){
    assert(length > 0);
    return last_num;
}

double statistician::sequence_mean(){
    assert(length > 0);
    return sum/length;
}

double statistician::sequence_min(){
    assert(length > 0);
    return min;
}

double statistician::sequence_max(){
    assert(length > 0);
    return max;
}

void statistician::erase(){
    length = min = max = sum = last_num = 0;
}

 result:

another test case:

int main(){
    statistician s;
    cout << "before insert" << endl;
    cout << "length: " << s.sequence_length() << endl;
    cout << "sum: " << s.sequence_sum() << endl;
    cout << "last number: " << s.sequence_last() << endl;
    s.next_number(1.1);
    s.next_number(-2.4);
    s.next_number(0.8);
    s.next_number(2.1);
    s.next_number(-9);
    cout << "********************************" << endl;
    cout << "length: " << s.sequence_length() << endl;
    cout << "sum: " << s.sequence_sum() << endl;
    cout << "last number: " << s.sequence_last() << endl;
    cout << "mean: " << s.sequence_mean() << endl;
    cout << "min: " << s.sequence_min() << endl;
    cout << "max: " << s.sequence_max() << endl;
    s.erase();
    cout << "********************************" << endl;
    cout << "after erase" << endl;
    cout << "length: " << s.sequence_length() << endl;
    cout << "sum: " << s.sequence_sum() << endl;
    return 0;
}

 result:

An error occurred.

Reference:

整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p120

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值