Posted By

DrPepper on 08/30/11


Tagged


Versions (?)

CISP 430 header file


 / Published in: C++
 

  1. // FILE: stats.h
  2. // CLASS PROVIDED: statistician
  3. // (a class to keep track of statistics on a sequence of real numbers)
  4. // This class is part of the namespace CISP430_A1.
  5. //
  6. // CONSTRUCTOR for the statistician class:
  7. // statistician( );
  8. // Postcondition: The object has been initialized, and is ready to accept
  9. // a sequence of numbers. Various statistics will be calculated about the
  10. // sequence.
  11. //
  12. // PUBLIC MODIFICATION member functions for the statistician class:
  13. // void next(double r) // here is next()
  14. // The number r has been given to the statistician as the next number in
  15. // its sequence of numbers.
  16. // void reset( );
  17. // Postcondition: The statistician has been cleared, as if no numbers had
  18. // yet been given to it.
  19. //
  20. // PUBLIC CONSTANT member functions for the statistician class:
  21. // int length( ) const
  22. // Postcondition: The return value is the length of the sequence that has
  23. // been given to the statistician (i.e., the number of times that the
  24. // next(r) function has been activated).
  25. // double sum( ) const
  26. // Postcondition: The return value is the sum of all the numbers in the
  27. // statistician's sequence.
  28. // double mean( ) const // here is mean ()
  29. // Precondition: length( ) > 0
  30. // Postcondition: The return value is the arithmetic mean (i.e., the
  31. // average of all the numbers in the statistician's sequence).
  32. // double minimum( ) const
  33. // Precondition: length( ) > 0
  34. // Postcondition: The return value is the tiniest number in the
  35. // statistician's sequence.
  36. // double maximum( ) const
  37. // Precondition: length( ) > 0
  38. // Postcondition: The return value is the largest number in the
  39. // statistician's sequence.
  40. //
  41. // NON-MEMBER functions for the statistician class:
  42. // statistician operator +(const statistician& s1, const statistician& s2)
  43. // Postcondition: The statistician that is returned contains all the
  44. // numbers of the sequences of s1 and s2.
  45. // statistician operator *(double scale, const statistician& s)
  46. // Postcondition: The statistician that is returned contains the same
  47. // numbers that s does, but each number has been multiplied by the
  48. // scale number.
  49. // bool operator ==(const statistician& s1, const statistician& s2)
  50. // Postcondition: The return value is true if s1 and s2 have the zero
  51. // length. Also, if the length is greater than zero, then s1 and s2 must
  52. // have the same length, the same mean, the same minimum,
  53. // the same maximum, and the same sum.
  54. //
  55. // VALUE SEMANTICS for the statistician class:
  56. // Assignments and the copy constructor may be used with statistician objects.
  57.  
  58. #ifndef STATS_H // Prevent duplicate definition
  59. #define STATS_H
  60. #include <iostream>
  61. #include <cassert> // Provides assert
  62. namespace CISP430_A1
  63. {
  64. class statistician
  65. {
  66. public:
  67. // CONSTRUCTOR
  68. statistician(){
  69. count=0; total=0;
  70. tiniest=0; largest=0;
  71. }
  72. // MODIFICATION MEMBER FUNCTIONS
  73. void next(double r);
  74. void reset();
  75. // CONSTANT MEMBER FUNCTIONS
  76. int length() const { return count; }
  77. double sum() const { return total; }
  78. double mean() const;
  79. double minimum() const;
  80. double maximum() const;
  81. // FRIEND FUNCTIONS
  82. friend statistician operator +(const statistician& s1, const statistician& s2);
  83. friend statistician operator *(double scale, const statistician& s);
  84. private:
  85. int count; // How many numbers in the sequence
  86. double total; // The sum of all the numbers in the sequence
  87. double tiniest; // The smallest number in the sequence
  88. double largest; // The largest number in the sequence
  89. };
  90.  
  91. // NON-MEMBER functions for the statistician class
  92. bool operator ==(const statistician& s1, const statistician& s2);
  93. }
  94.  
  95. #endif

Report this snippet  

You need to login to post a comment.