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 /chapter9/useful.cxx | |
| download | dscode-main.tar.xz dscode-main.zip | |
Diffstat (limited to 'chapter9/useful.cxx')
| -rw-r--r-- | chapter9/useful.cxx | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/chapter9/useful.cxx b/chapter9/useful.cxx new file mode 100644 index 0000000..694016b --- /dev/null +++ b/chapter9/useful.cxx @@ -0,0 +1,80 @@ +// FILE: useful.cxx
+// IMPLEMENTS: A toolkit of functions (See useful.h for documentation.)
+
+#include <cassert> // Provides assert
+#include <cctype> // Provides toupper
+#include <cstdlib> // Provides rand, RAND_MAX
+#include <iostream.h> // Provides cout, cin, get
+#include "useful.h"
+
+void display(double x)
+// Library facilities used: iostream.h, stdlib.h
+{
+ const char STAR = '*';
+ const char BLANK = ' ';
+ const char VERTICAL_BAR = '|';
+ const int LIMIT = 39;
+ int i;
+
+ if (x < -LIMIT)
+ x = -LIMIT;
+ else if (x > LIMIT)
+ x = LIMIT;
+
+ for (i = -LIMIT; i < 0; i++)
+ {
+ if (i >= x)
+ cout << STAR;
+ else
+ cout << BLANK;
+ }
+ cout << VERTICAL_BAR;
+ for (i = 1; i <= LIMIT; i++)
+ {
+ if (i <= x)
+ cout << STAR;
+ else
+ cout << BLANK;
+ }
+ cout << endl;
+}
+
+double random_fraction( )
+// Library facilities used: stdlib.h
+{
+ return rand( ) / double(RAND_MAX);
+}
+
+double random_real(double low, double high)
+// Library facilities used: assert.h
+{
+ assert(low <= high);
+ return low + random_fraction( ) * (high - low);
+}
+
+void eat_line( )
+// Library facilities used: iostream.h
+//
+{
+ char next;
+
+ do
+ cin.get(next);
+ while (next != '\n');
+}
+
+bool inquire(const char query[ ])
+// Library facilities used: ctype.h, iostream.h
+{
+ char answer;
+ do
+ {
+ cout << query << " [Yes or No]" << endl;
+ cin >> answer;
+ answer = toupper(answer);
+ eat_line( );
+ }
+ while ((answer != 'Y') && (answer != 'N'));
+ return (answer == 'Y');
+}
+
|