Posted By

DrPepper on 08/30/11


Tagged


Versions (?)

CISP 430 cpp file


 / Published in: C++
 

  1. // FILE: stattest.cxx
  2. // An interactive test program for the statistician class
  3.  
  4. #include <cctype> // Provides toupper
  5. #include <iomanip> // Provides setw to set the width of an output
  6. #include <iostream> // Provides cout, cin
  7. #include <cstdlib> // Provides EXIT_SUCCESS
  8. #include <cassert> // Provides assert();
  9. #include <cmath> // Provides fabs();
  10. #include <cstring> // Provides Null
  11. #include "stats.h"
  12.  
  13. using namespace CISP430_A1;
  14. using namespace std;
  15.  
  16. namespace CISP430_A1
  17. {
  18. statistician operator *(double scale, const statistician& s)
  19. // Postcondition: The statistician that is returned contains the same
  20. // numbers that s does, but each number has been multiplied by the
  21. // scale number.
  22. {
  23. statistician u;
  24. if(s.length() == 0 ){ return s; }
  25. u.count = s.length();
  26. u.total = s.sum() * scale;
  27. if(scale < 0 )
  28. {
  29. u.largest = s.minimum() * scale;
  30. u.tiniest = s.maximum() * scale;
  31. }
  32. else
  33. {
  34. u.tiniest = s.minimum() * scale;
  35. u.largest = s.maximum() * scale;
  36. }
  37. return u;
  38. }
  39.  
  40. statistician operator +(const statistician& s1, const statistician& s2)
  41. // Postcondition: The statistician that is returned contains all the
  42. // numbers of the sequences of s1 and s2.
  43. {
  44. statistician s3;
  45. if(s1.length() == 0){ return s2; }
  46. if(s2.length() == 0){ return s1; }
  47.  
  48. s3.count = s1.length()+s2.length();
  49. s3.total = s1.sum() + s2.sum();
  50.  
  51. if(s1.maximum() > s2.maximum())
  52. { s3.largest = s1.maximum(); }
  53. else
  54. { s3.largest = s2.maximum(); }
  55.  
  56. if(s1.minimum() <s2.minimum())
  57. { s3.tiniest = s1.minimum(); }
  58. else
  59. { s3.tiniest = s2.minimum(); }
  60.  
  61. return s3;
  62.  
  63. }
  64.  
  65. bool operator ==(const statistician& s1, const statistician& s2)
  66. //Postcondition: The return value is true if s1 and s2 have the zero
  67. // length. Also, if the length is greater than zero, then s1 and s2 must
  68. // have the same length, the same mean, the same minimum,
  69. // the same maximum, and the same sum.
  70. {
  71. if(s1.length() != s2.length()){ return false; }
  72. if((s1.length()==0)&&(s2.length()==0))
  73. { return true; }
  74. if((s1.length() > 0)&&(s2.length() > 0))
  75. {
  76. if((s1.mean()==s2.mean())&&(s1.minimum() == s2.minimum())&&(s1.maximum() == s2.maximum())){ return true; }
  77. else{ return false; }
  78. }
  79. }
  80. }
  81. double statistician::mean() const
  82. {
  83. double avarage; // The avarage of all the numbers in the sequence
  84. assert(length( ) > 0);
  85. avarage = total / count;
  86. return avarage;
  87. }
  88.  
  89. void statistician::next(double r)
  90. {
  91. total = total + r;
  92.  
  93. if(count == 0 ){ tiniest=r; largest = r; }
  94. else if(r < tiniest){ tiniest = r; }
  95. else if(r > largest){ largest = r; }
  96. count++;
  97. }
  98.  
  99. void statistician::reset()
  100. {
  101. count=0; total=0;
  102. tiniest=0; largest=0;
  103. }
  104.  
  105.  
  106. double statistician::minimum() const
  107. {
  108. assert(length() > 0);
  109. return tiniest;
  110. }
  111. double statistician::maximum() const
  112. {
  113. assert(length() > 0);
  114. return largest;
  115. }

Report this snippet  

You need to login to post a comment.