diff options
| author | Fuwn <[email protected]> | 2024-04-07 23:18:32 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-04-07 23:18:32 -0700 |
| commit | c1b6ffe70bd281c6c230fd63fabcaac2aff47514 (patch) | |
| tree | e8af3b1782a7cd0754590ed618fddc1bdb9b7385 /chapter8/washing.h | |
| download | dscode-main.tar.xz dscode-main.zip | |
Diffstat (limited to 'chapter8/washing.h')
| -rw-r--r-- | chapter8/washing.h | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/chapter8/washing.h b/chapter8/washing.h new file mode 100644 index 0000000..d019fba --- /dev/null +++ b/chapter8/washing.h @@ -0,0 +1,113 @@ +// FILE: washing.h (part of the namespace main_savitch_8A)
+// CLASSES PROVIDED: bool_source, averager, washer.
+//
+// CONSTRUCTOR for the bool_source class:
+// bool_source(double p = 0.5)
+// Precondition: 0 <= p <= 1.
+// Postcondition: The bool_source has been initialized so that p is the
+// approximate probability of returning true in any subsequent activation
+// of query( ).
+//
+// CONSTANT MEMBER FUNCTION for the bool_source class:
+// bool query( ) const
+// Postcondition: The return value is either true or false, with the
+// probability of a true value being approximately p (from the constructor).
+//
+// CONSTRUCTOR for the averager class:
+// averager( )
+// Postcondition: The averager has been initialized so that it
+// is ready to accept a sequence of numbers to average.
+//
+// MODIFICATION MEMBER FUNCTION for the averager class:
+// void next_number(double value)
+// Postcondition: The averager has accepted value as the next
+// number in the sequence of numbers which it is averaging.
+//
+// CONSTANT MEMBER FUNCTIONS for the averager class:
+// size_t how_many_numbers( ) const
+// Postcondition: The value returned is a count of how many
+// times next_number has been activated.
+//
+// double average( ) const
+// Precondition: how_many_numbers > 0.
+// Postcondition: The value returned is the average of all the
+// numbers which have been given to the averager.
+//
+// CONSTRUCTOR for the washer class:
+// washer(unsigned int s = 60)
+// Precondition: The value of s is the number of seconds needed for
+// the completion of one wash cycle.
+// Postcondition: The washer has been initialized so that all
+// other member functions may be used.
+//
+// MODIFICATION MEMBER FUNCTIONS for the washer class:
+// void one_second( )
+// Postcondition: The washer has recorded (and simulated) the
+// passage of one more second of time.
+//
+// void start_washing( )
+// Precondition: The washer is not busy.
+// Postcondition: The washer has started simulating one wash
+// cycle. Therefore, is_busy( ) will return true until
+// the required number of simulated seconds have occured.
+//
+// CONSTANT MEMBER FUNCTIONS for the washer class:
+// bool is_busy( ) const
+// Postcondition: Return value is true if the washer is busy
+// (in a wash cycle); otherwise the return value is false.
+//
+// VALUE SEMANTICS for the bool_source, averager, and washer classes:
+// Assignments and the copy constructor may be used with the three classes.
+//
+
+#ifndef MAIN_SAVITCH_WASHING_H
+#define MAIN_SAVITCH_WASHING_H
+#include <cstdlib> // Provides std::size_t
+
+namespace main_savitch_8A
+{
+ class bool_source
+ {
+ public:
+ // CONSTRUCTOR
+ bool_source(double p = 0.5);
+ // CONSTANT function
+ bool query( ) const;
+ private:
+ double probability; // Probability of query( ) returning true
+ };
+
+ class averager
+ {
+ public:
+ // CONSTRUCTOR
+ averager( );
+ // MODIFICATION function
+ void next_number(double value);
+ // CONSTANT functions
+ std::size_t how_many_numbers( ) const { return count; }
+ double average( ) const;
+ private:
+ std::size_t count; // How many numbers have been given to the averager
+ double sum; // Sum of all the numbers given to the averager
+ };
+
+ class washer
+ {
+ public:
+ // CONSTRUCTOR
+ washer(unsigned int s = 60);
+ // MODIFICATION functions
+ void one_second( );
+ void start_washing( );
+ // CONSTANT function
+ bool is_busy( ) const { return (wash_time_left > 0); }
+ private:
+ unsigned int seconds_for_wash; // Seconds for a single wash
+ unsigned int wash_time_left; // Seconds until washer no longer busy
+ };
+}
+
+#endif
+
+
|