-
Notifications
You must be signed in to change notification settings - Fork 0
/
statisticsC.cpp
33 lines (29 loc) · 965 Bytes
/
statisticsC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// Skeleton code by Phil Romig on 11/13/18.
// Solution Implemented by Nhan Tran December 2018
//
#include "packetstats.h"
statisticsC::statisticsC(std::string name) {
name_v = name;
min_v = INT_MAX;
max_v = 0;
average_v = 0.0;
count_v = 0;
}
void statisticsC::insert(unsigned int newValue) {
max_v = std::max(max_v,newValue);
min_v = std::min(min_v,newValue);
average_v = ((average_v * count_v) + newValue) / (count_v + 1);
count_v++;
}
std::ostream &operator<<(std::ostream &os, const statisticsC &c) {
os << "\tTotal " << c.name_v << " = " << c.count_v << std::endl;
if (c.min_v < c.max_v) {
os << "\tMin " << c.name_v << " = " << c.min_v << std::endl;
} else {
os << "\tMin " << c.name_v << " = " << c.max_v << std::endl;
}
os << "\tMax " << c.name_v << " = " << c.max_v << std::endl;
os << "\tAverage " << c.name_v << " = " << c.average_v << std::endl;
return os;
}