/ Published in: C++
Expand |
Embed | Plain Text
// FILE: stattest.cxx // An interactive test program for the statistician class #include <cctype> // Provides toupper #include <iomanip> // Provides setw to set the width of an output #include <iostream> // Provides cout, cin #include <cstdlib> // Provides EXIT_SUCCESS #include <cassert> // Provides assert(); #include <cmath> // Provides fabs(); #include <cstring> // Provides Null #include "stats.h" using namespace CISP430_A1; using namespace std; namespace CISP430_A1 { statistician operator *(double scale, const statistician& s) // Postcondition: The statistician that is returned contains the same // numbers that s does, but each number has been multiplied by the // scale number. { statistician u; if(s.length() == 0 ){ return s; } u.count = s.length(); u.total = s.sum() * scale; if(scale < 0 ) { u.largest = s.minimum() * scale; u.tiniest = s.maximum() * scale; } else { u.tiniest = s.minimum() * scale; u.largest = s.maximum() * scale; } return u; } statistician operator +(const statistician& s1, const statistician& s2) // Postcondition: The statistician that is returned contains all the // numbers of the sequences of s1 and s2. { statistician s3; if(s1.length() == 0){ return s2; } if(s2.length() == 0){ return s1; } s3.count = s1.length()+s2.length(); s3.total = s1.sum() + s2.sum(); if(s1.maximum() > s2.maximum()) { s3.largest = s1.maximum(); } else { s3.largest = s2.maximum(); } if(s1.minimum() <s2.minimum()) { s3.tiniest = s1.minimum(); } else { s3.tiniest = s2.minimum(); } return s3; } bool operator ==(const statistician& s1, const statistician& s2) //Postcondition: The return value is true if s1 and s2 have the zero // length. Also, if the length is greater than zero, then s1 and s2 must // have the same length, the same mean, the same minimum, // the same maximum, and the same sum. { if(s1.length() != s2.length()){ return false; } if((s1.length()==0)&&(s2.length()==0)) { return true; } if((s1.length() > 0)&&(s2.length() > 0)) { if((s1.mean()==s2.mean())&&(s1.minimum() == s2.minimum())&&(s1.maximum() == s2.maximum())){ return true; } else{ return false; } } } } double statistician::mean() const { double avarage; // The avarage of all the numbers in the sequence assert(length( ) > 0); avarage = total / count; return avarage; } void statistician::next(double r) { total = total + r; if(count == 0 ){ tiniest=r; largest = r; } else if(r < tiniest){ tiniest = r; } else if(r > largest){ largest = r; } count++; } void statistician::reset() { count=0; total=0; tiniest=0; largest=0; } double statistician::minimum() const { assert(length() > 0); return tiniest; } double statistician::maximum() const { assert(length() > 0); return largest; }
You need to login to post a comment.
