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 /chapter1 | |
| download | dscode-c1b6ffe70bd281c6c230fd63fabcaac2aff47514.tar.xz dscode-c1b6ffe70bd281c6c230fd63fabcaac2aff47514.zip | |
Diffstat (limited to 'chapter1')
| -rw-r--r-- | chapter1/guess.cxx | 43 | ||||
| -rw-r--r-- | chapter1/temperature.cxx | 65 |
2 files changed, 108 insertions, 0 deletions
diff --git a/chapter1/guess.cxx b/chapter1/guess.cxx new file mode 100644 index 0000000..899c3bc --- /dev/null +++ b/chapter1/guess.cxx @@ -0,0 +1,43 @@ +// FILE: guess.cxx
+// Demostrates a guessing game function that's used as a time analysis example.
+
+#include <cassert> // Provides assert
+#include <iostream> // Provides cout and cin
+#include <cstdlib> // Provides EXIT_SUCCESS
+using namespace std; // Allows all Standard Library items to be used
+
+// Prototype for the function used in this demonstration program
+void guess_game(int n);
+// Precondition: n > 0.
+// Postcondition: The user has been asked to think of a number between 1 and n.
+// The function asks a series of questions, until the number is found.
+
+
+int main( )
+{
+ guess_game(100);
+ return EXIT_SUCCESS;
+}
+
+void guess_game(int n)
+// Library facilities used: cassert, iostream
+{
+ int guess;
+ char answer;
+
+ assert(n >= 1);
+
+ cout << "Think of a whole number from 1 to " << n << "." << endl;
+ answer = 'N';
+ for (guess = n; (guess > 0) && (answer != 'Y') && (answer != 'y'); --guess)
+ {
+ cout << "Is your number " << guess << "?" << endl;
+ cout << "Please answer Y or N, and press return: ";
+ cin >> answer;
+ }
+
+ if ((answer == 'Y') || (answer == 'y'))
+ cout << "I knew it all along." << endl;
+ else
+ cout << "I think you are cheating!" << endl;
+}
diff --git a/chapter1/temperature.cxx b/chapter1/temperature.cxx new file mode 100644 index 0000000..beeb4b7 --- /dev/null +++ b/chapter1/temperature.cxx @@ -0,0 +1,65 @@ +// File: temperature.cxx
+// This program prints a table to convert numbers from one unit to another.
+// The program illustrases some implementation techniques.
+
+#include <iostream> // Provides cout
+#include <iomanip> // Provides setw function for setting output width
+#include <cstdlib> // Provides EXIT_SUCCESS
+#include <cassert> // Provides assert function
+using namespace std; // Allows all standard library items to be used
+
+double celsius_to_fahrenheit(double c)
+// Precondition: c is a Celsius temperature no less than absolute
+// zero (-273.16).
+// Postcondition: The return value is the temperature c converted to Fahrenheit
+// degrees.
+{
+ const double MINIMUM_CELSIUS = -273.16; // Absolute zero in Celsius degrees
+ assert(c >= MINIMUM_CELSIUS);
+ return (9.0 / 5.0) * c + 32;
+}
+
+void setup_cout_fractions(int fraction_digits)
+// Precondition: fraction_digits is not negative.
+// Postcondition: All double or float numbers printed to cout will now be
+// rounded to the specified digits on the right of the decimal.
+{
+ assert(fraction_digits > 0);
+ cout.precision(fraction_digits);
+ cout.setf(ios::fixed, ios::floatfield);
+ if (fraction_digits == 0)
+ cout.unsetf(ios::showpoint);
+ else
+ cout.setf(ios::showpoint);
+}
+
+int main( )
+{
+ const char HEADING1[] = " Celsius"; // Heading for table's 1st column
+ const char HEADING2[] = "Fahrenheit"; // Heading for table's 2nd column
+ const char LABEL1 = 'C'; // Label for numbers in 1st column
+ const char LABEL2 = 'F'; // Label for numbers in 2nd column
+ const double TABLE_BEGIN = -50.0; // The table's first Celsius temp.
+ const double TABLE_END = 50.0; // The table's final Celsius temp.
+ const double TABLE_STEP = 10.0; // Increment between temperatures
+ const int WIDTH = 9; // Number chars in output numbers
+ const int DIGITS = 1; // Number digits right of decimal pt
+
+ double value1; // A value from the table's first column
+ double value2; // A value from the table's second column
+
+ // Set up the output for fractions and print the table headings.
+ setup_cout_fractions(DIGITS);
+ cout << "CONVERSIONS from " << TABLE_BEGIN << " to " << TABLE_END << endl;
+ cout << HEADING1 << " " << HEADING2 << endl;
+
+ // Each iteration of the loop prints one line of the table.
+ for (value1 = TABLE_BEGIN; value1 <= TABLE_END; value1 += TABLE_STEP)
+ {
+ value2 = celsius_to_fahrenheit(value1);
+ cout << setw(WIDTH) << value1 << LABEL1 << " ";
+ cout << setw(WIDTH) << value2 << LABEL2 << endl;
+ }
+
+ return EXIT_SUCCESS;
+}
|