From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter8/washing.cxx | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 chapter8/washing.cxx (limited to 'chapter8/washing.cxx') diff --git a/chapter8/washing.cxx b/chapter8/washing.cxx new file mode 100644 index 0000000..d806704 --- /dev/null +++ b/chapter8/washing.cxx @@ -0,0 +1,80 @@ +// FILE: washing.cxx +// CLASSES implemented: bool_source, averager, washer +// +// INVARIANT for the bool_source ADT: +// 1. The member variable probability is the appoximate probability that +// query( ) returns true. +// +// INVARIANT for the averager ADT: +// 1. The member variable count indicates how many numbers the averager has +// been given. +// 2. The member variable sum is the sum of all the numbers that the +// averager has been given. +// +// INVARIANT for the washer class: +// 1. The member variable seconds_for_wash is the number of seconds required +// for one wash. +// 2. The member varible wash_time_left is 0 if the washer is not busy; +// otherwise it is the number of seconds until the washer is free. + +#include // Provides assert +#include // Provides rand, RAND_MAX, size_t +#include "washing.h" // Provides bool_source, averager, washer definitions +using namespace std; + +namespace main_savitch_8A +{ + bool_source::bool_source(double p) + // Library facilities used: cassert + { + assert(p >= 0); + assert(p <= 1); + probability = p; + } + + bool bool_source::query( ) const + // Library facilities used: cstdlib + { + return (rand( ) < probability * RAND_MAX); + } + + averager::averager( ) + { + count = 0; + sum = 0; + } + + void averager::next_number(double value) + { + ++count; + sum += value; + } + + double averager::average( ) const + // Library facilities used: cassert + { + assert(how_many_numbers( ) > 0); + return sum/count; + } + + washer::washer(unsigned int s) + { + seconds_for_wash = s; + wash_time_left = 0; + } + + + void washer::one_second( ) + { + if (is_busy( )) + --wash_time_left; + } + + + void washer::start_washing( ) + // Library facilities used: cassert + { + assert(!is_busy( )); + wash_time_left = seconds_for_wash; + } +} -- cgit v1.2.3