Figure 4.1 |
/* f2c.cpp converts a temperature from Fahrenheit to Celsius,
* using the standard Fahrenheit-to-Celsius conversion formula.
*
* Input: tempFahrenheit
* Output: tempCelsius
********************************************************************/
#include <iostream>
using namespace std;
int main()
{
cout << "This program converts a temperature\n"
<<"from Fahrenheit to Celsius.\n";
cout <<"\nEnter a Fahrenheit temperature: ";
double tempFahrenheit;
cin >> tempFahrenheit;
double tempCelsius = (tempFahrenheit - 32.0) / 1.8;
cout << tempFahrenheit << " degrees Fahrenheit is equivalent to "
<< tempCelsius << " degrees Celsius\n";
}
|
Sample Runs for Figure 4.1 |
This program converts a temperature from Fahrenheit to Celsius. Enter a Fahrenheit temperature: 212 212 degrees Fahrenheit is equivalent to 100 degrees Celsius This program converts a temperature from Fahrenheit to Celsius. Please enter a Fahrenheit temperature: 32 32 degrees Fahrenheit is equivalent to 0 degrees Celsius
|
Figure 4.2 |
/* f2c.cpp converts a temperature from Fahrenheit to Celsius,
* using a conversion function named fahrToCelsius().
*
* Input: tempFahrenheit
* Output: tempCelsius
*******************************************************************/
#include <iostream>
using namespace std;
double fahrToCelsius(double tempFahr); // function prototype
int main()
{
cout << "This program converts a temperature\n"
<< "from Fahrenheit to Celsius.\n";
cout << "\nEnter a Fahrenheit temperature: ";
double tempFahrenheit;
cin >> tempFahrenheit;
double tempCelsius = fahrToCelsius(tempFahrenheit);
cout << tempFahrenheit << " degrees Fahrenheit is equivalent to "
<< tempCelsius << " degrees Celsius\n";
}
/* fahrToCelsius converts a temperature from Fahrenheit to Celsius.
*
* Receive: tempFahr, a (double) Fahrenheit temperature
* Return: the equivalent Celsius temperature
*******************************************************************/
double fahrToCelsius(double tempFahr) // function definition
{
return (tempFahr - 32.0) / 1.8;
}
|
Figure 4.3 |
#include <iostream> // cin, cout, <<, >>
using namespace std;
double fahrToCelsius(double tempFahr); // function prototype
int main()
{
cout << "212F => " << fahrToCelsius(212)
<< "C\n 32F => " << fahrToCelsius(32)
<< "C\n";
}
double fahrToCelsius(double tempFahr) // function definition
{
return (tempFahr - 32.0) / 1.8;
}
|
Sample Run for Figure 4.3 |
212F => 100c 32F => 0c |
Figure 4.5 |
/* transaction.cpp computes the amount to be returned for a purchase.
*
* Input: purchase, payment
* Output: amount returned to customer (via printAsMoney())
*****************************************************************/
#include <iostream> // cin, cout, <<, >>, ...
#include <iomanip> // setprecision, ...
using namespace std;
void printAsMoney(double dollars); // prototype
int main()
{
double purchase, // amount of purchase
payment; // amount paid
cout << "Enter amount of purchase: ";
cin >> purchase;
cout << "Enter amount paid (>= purchase): ";
cin >> payment;
cout << "Amount to return is: ";
printAsMoney(payment - purchase);
cout << endl;
}
/* printAsMoney displays an amount in monetary format.
*
* Receive: dollars, the double value to be displayed
* Output: dollars in monetary format
**************************************************************/
void printAsMoney(double dollars)
{
cout << fixed << showpoint
<< setprecision(2)
<< '$' << dollars;
}
|
Sample Runs for Figure 4.5 |
Enter amount of purchase: 4.01 Enter amount paid (>= purchase): 5.00 Amount to return is: $0.99 Enter amount of purchase: 9.00 Enter amount paid (>= purchase): 20.00 Amount to return is: $11.00 |
Figures 4.6 & 4.7 |
/* driver.cpp is a driver program to test the minimum function.
****************************************************************/
#include <iostream> // cin, cout, <<, >>
using namespace std;
double minimum(double first, double second);
int main()
{
double num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Minimum is " << minimum(num1, num2) << endl;
}
/* minimum finds the minimum of two doubles. * * Receive: first and second * Return: the smaller of first and second ************************************************/ double minimum(double first, double second)
{
if (first < second)
return first;
else
return second;
}
|
Sample Run for Figure 4.7 |
Enter two numbers: -2 -5 Minimum is -5 Enter two numbers: -2 3 Minimum is -2 |
Figures 4.8 & 4.9 |
/* factDriver.cpp is a driver program to test the factorial function * *******************************************************************/ #include <iostream> using namespace std; int factorial(int n); int main()
{
cout << "To compute n!, enter n: ";
int theNumber;
cin >> theNumber;
cout << theNumber << "! = "
<< factorial(theNumber) << endl;
}
/* factorial computes the factorial of a nonnegative integer. * * Receive: n, an integer * Precondition: n is nonnegative * Return: n! *************************************************************/ #include <cassert> // assert() using namespace std; int factorial(int n)
{
assert(n >= 0);
int product = 1;
for (int count = 2; count <= n; count++)
product *= count;
return product;
}
|
Sample Runs for Figure 4.9 |
To compute n!, enter n: 1 1! = 1 To compute n!, enter n: 2 2! = 2 To compute n!, enter n: 5 5! = 120 To compute n!, enter n: -1 factorial9.cpp:22: failed assertion 'n >= 0' -1! = Abort |
Figure 4.10 |
/* factorials.cpp is a driver program to test the factorial
* function. It computes any number of factorials.
*
************************************************************/
#include <iostream> // cin, cout, <<, >>
using namespace std;
int factorial(int n);
int main()
{
int aNumber;
cout << "To compute n!, enter n (a negative number to quit): ";
cin >> aNumber;
while (aNumber >= 0)
{
cout << theNumber << "! = "
<< factorial(aNumber) << "\n\n";
cout << "Enter next value of n (negative to quit): ";
cin >> aNumber;
}
}
|
Figure 4.12 |
/* Heat.h provides an interface for a library of * heat-related constants and functions. * * Created by: Jane Roe, August, 2001, at Dooflingy Industries. * Modification History: Kelvin functions added January 2002 -- JR. *******************************************************************/ const double HEAT_OF_FUSION = 79.71; // calories per gram const double HEAT_OF_VAPORIZATION = 539.55; // calories per gram double fahrToCelsius(double tempFahr); // degrees Celsius double celsiusToFahr(double tempCels); // degrees Fahrenheit double fahrToKelvin(double tempFahr); // degrees Kelvin double kelvinToFahr(double tempKelv); // degrees Fahrenheit double celsiusToKelvin(double tempCels); // degrees Kelvin double kelvinToCelsius(double tempKelv); // degrees Celsius |
Figure 4.13 |
/* Heat.cpp provides the function implementations for Heat,
* a library of heat-related constants and functions.
*
* Created by: Jane Roe, August 2001, at Dooflingy Industries.
* Modification History: Kelvin functions added January 2002 -- JR.
*******************************************************************/
#include "Heat.h"
//---------------------------------------------
double fahrToCelsius(double tempFahr)
{
return (tempFahr - 32.0) / 1.8;
}
//---------------------------------------------
double celsiusToFahr(double tempCels)
{
return tempCels * 1.8 + 32.0;
}
// . . . Definitions of other functions omitted to save space . . .
|
Figure 4.15 |
/* f2c.cpp converts a temperature from Fahrenheit to Celsius,
* using function fahrToCelsius() that is stored in library Heat.
*
* Input: tempFahrenheit
* Output: tempCelsius
*******************************************************************/
#include <iostream> // cin, cout, <<, >>
using namespace std;
#include "Heat.h" // our library's header file
int main()
{
cout << "This program converts a temperature\n"
<<"from Fahrenheit to Celsius.\n";
cout << "\nEnter a Fahrenheit temperature: ";
double tempFahrenheit;
cin >> tempFahrenheit;
double tempCelsius = fahrToCelsius(tempFahrenheit);
cout << tempFahrenheit << " degrees Fahrenheit is equivalent to "
<< tempCelsius << " degrees Celsius\n";
}
|
Figure 4.17 |
/* Temperature.h provides a Temperature class that (so far)
* provides class methods for temperature conversions.
* ...
*******************************************************************/
#include <cassert> // assert()
using namespace std;
const double MIN_FAHRENHEIT = -459.67,
MIN_CELSIUS = -273.15,
MIN_KELVIN = 0;
class Temperature
{
public:
static double fahrToCels(double tempFahr);
static double fahrToKelv(double tempFahr);
static double celsToFahr(double tempCels);
static double celsToKelv(double tempCels);
static double kelvToFahr(double tempKelv);
static double kelvToCels(double tempKelv);
// ... other Temperature class method prototypes ...
};
inline double Temperature::fahrToCels(double tempFahr)
{
assert(tempFahr >= MIN_FAHRENHEIT)
return (tempFahr - 32.0) / 1.8;
}
inline double Temperature::celsToFahr(double tempCels)
{
assert(tempCels >= MIN_CELSIUS);
return tempCels * 1.8 + 32.0;
}
// ... Definitions of other Temperature class methods ...
|