From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- appendix/useful.cxx | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ appendix/useful.h | 45 ++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 appendix/useful.cxx create mode 100644 appendix/useful.h (limited to 'appendix') diff --git a/appendix/useful.cxx b/appendix/useful.cxx new file mode 100644 index 0000000..79dff57 --- /dev/null +++ b/appendix/useful.cxx @@ -0,0 +1,80 @@ +// FILE: useful.cxx +// IMPLEMENTS: A toolkit of functions (See useful.h for documentation.) + +#include // Provides assert +#include // Provides toupper +#include // Provides cout, cin, get +#include // Provides rand, RAND_MAX +#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'); +} + diff --git a/appendix/useful.h b/appendix/useful.h new file mode 100644 index 0000000..4513936 --- /dev/null +++ b/appendix/useful.h @@ -0,0 +1,45 @@ +// FILE: useful.h +// PROVIDES: A toolkit of useful functions for random numbers and displays. +// +// FUNCTIONS in the toolkit: +// double random_fraction( ) +// Postcondition: The return value is a random real number in the closed +// interval [0..1] (including the endpoints). +// +// double random_real(double low, double high) +// Precondition: low <= high. +// Postcondition: The return value is a random real number in the closed +// interval [low..high] (including the endpoints). +// +// void display(double x) +// Postcondition: The function has written one line of output to the +// standard ouput, with a vertical bar in the middle. If x is positive, +// then approximately x stars are printed to the right of the vertical +// bar. If x is negative, then approximately -x stars are printed to the +// left of the vertical bar. If the absolute value of x is more than +// 39, then only 39 stars are printed. +// Examples: +// display(8) prints: |******** +// display(-4) prints: ****| +// +// void eat_line( ) +// Postcondition: Up to next newline has been read and discarded from cin. +// +// bool inquire(const char query[ ]) +// Precondition: query is a null-terminated string of characters. +// Postcondition: query has been printed, and a one-line response read +// from the user. The function returns true if the user's response begins +// with 'Y' or 'y', and returns false if the user's response begins with +// 'N' or 'n'. (If the response begins with some other letter, then the +// query is repeated.) + +#ifndef USEFUL_H // Prevent duplicate definition +#define USEFUL_H + + double random_fraction( ); + double random_real(double low, double high); + void display(double x); + void eat_line( ); + bool inquire(const char query[ ]); + +#endif -- cgit v1.2.3