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:

The Statistician Class

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:

The Statistician Class

An error occurred.

Reference:

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