C O D
E S A M P L E S
Feel
free to widen this frame so you see all the code that's supposed to be on each
line
Bookmarks |
// Name: Convenience
store owner
// Version: 1.0
// Purpose: Convert the price of an
item in U.S. pennies per pound to
//
Canadian dollars per kilogram.
#include <iostream.h>
//------------------------ calculator
object ---------------------------------
void main()
{
double theOutput; // Answer displayed to the user
double dollarsPerKg; // Item's cost in Canadian dollars per
kilogram
int theInput; // Input entered by the user
int penniesPerLb; // Price in U.S. pennies per pound of an
item
//-------------------- input object
---------------------------------------
//
Get the input.
cin >> theInput;
//------------------- end of input
object --------------------------------
penniesPerLb = theInput;
//-------------------- computing object
-----------------------------------
const double kgPerLb = .4536; // Number of kilograms in a pound
const double dollarsCanPerUS = 1.26; //
Number of Canadian dollars per
// U.S. dollar (exchange rate)
const int penniesPerDollar = 100; // Number of pennies per U.S. dollar
dollarsPerKg = penniesPerLb/kgPerLb * dollarsCanPerUS/penniesPerDollar;
//-------------------- end of computing
object ---------------------------
theOutput = dollarsPerKg;
//-------------------- output object
--------------------------------------
// Display the answer.
cout << theOutput << endl;
//------------------- end of output
object --------------------------------
}
//---------------------- end of
calculator object ----------------------------
//*******************************************************************************
#include <iostream.h>
//------------------------ teller object -------------------------------------
void main()
{
int countOfTwenties; // Count of $20Us to be returned
int countOfTens; // Count of $10Us to be returned
int countOfFives; // Count of $5Us to be returned
int countOfOnes; // Count of $1Us to be returned
int countOfQuarters; // Count of quarters to be returned
int countOfDimes; // Count of dimes to be returned
int countOfNickels; // Count of nickels to be returned
int countOfPennies; // Count of pennies to be returned
int theAmount; // Amount to be changed
//-------------------- input object
---------------------------------------
// Get the amount from the user.
cin >> theAmount;
//----------------- end of input object
-----------------------------------
//-------------------- computing object
-----------------------------------
//-------------------- cash drawer
object ---------------------------------
const int twentyValue = 2000; // Number of pennies in $20
const int tenValue = 1000; // Number of pennies in $10
const int fiveValue = 500; // Number of pennies in $5
const int oneValue = 100; // Number of pennies in $1
const int quarterValue = 25; // Number of pennies in a quarter
const int dimeValue = 10; // Number of pennies in a dime
const int nickelValue = 5; // Number of pennies in a nickel
const int pennyValue = 1; // Number of pennies in a penny
const char twentyName[] =
"twenties"; // Label for
$20 bills
const char tenName[] =
"tens"; // Label for
$10 bills
const char fiveName[] =
"fives"; //_Label for
$5 bills
const char oneName[] =
"ones"; // Label for
$1 bills
const char quarterName[] = "quarters"; // Label for quarter coins
consT char dimeName[] =
"dimes"; // Label for
dime coins
const char nickelName[] =
"nickels"; // Label for
nickel coins
const char pennyName[] =
"pennies"; // Label for
penny coins
//----------------- end of cash drawer
object -----------------------------
// Determine the amount of each
denomination to be returned.
countOfTwenties = theAmount /
twentyValue;
theAmount = theAmount % twentyValue;
countOfTens = theAmount / tenValue;
theAmount = theAmount % tenValue;
countOfFives = theAmount / fiveValue;
theAmount = theAmount % fiveValue;
countOfOnes = theAmount / oneValue;
theAmount = theAmount % oneValue;
countOfQuarters = theAmount /
quarterValue;
theAmount = theAmount % quarterValue;
countOfDimes = theAmount / dimeValue;
theAmount = theAmount % dimeValue;
countOfNickels = theAmount /
nickelValue;
theAmount = theAmount % nickelValue;
countOfPennies = theAmount /
pennyValue;
theAmount = theAmount % pennyValue;
//----------------- end of computing
object -------------------------------
//-------------------- output object
--------------------------------------
// Display the results.
cout << countOfTwenties <<
" " << twentyName << endl;
cout << countOfTens <<
" " << tenName << endl;
cout << countOfFives <<
" " << fiveName << endl;
cout << countOfOnes <<
" " << oneName << endl;
cout << countOfQuarters <<
" " << quarterName << endl;
cout << countOfDimes <<
" " << dimeName << endl;
cout << countOfNickels <<
" " << nickelName << endl;
cout << countOfPennies <<
" " << pennyName << endl;
//----------------- end of output
object ----------------------------------
}
//---------------------- end of teller
object --------------------------------
//*******************************************************************************
// Name: The changemaker
// Version: 2.0
// Purpose: Determine the count of each
denomination from twenty dollar bills
//
down through pennies that constitute a given monetary amount.
#include <iostream.h>
//-------------------------------
interfaces ---------------------------------
void inputInt(int& aNumber, const
char prompt[]);
// Arguments:
//
aNumber - out - Integer entered by the user
//
prompt - Message displayed to the user
// Side effect: Prompts for and reads
aNumber.
void outputLine(int aNumber, const char
aString[]);
// Arguments:
//
aNumber - Integer to be displayed
//
aString - Message displayed to identify the output
// Side effect: Display a number and
message followed by a newline.
void divMod(int numerator, int
denominator, int& quotient, int& remainder);
// Arguments:
//
numerator, denominator - two integers
//
ASSUME: numerator >= 0; denominator > 0
//
quotient - out - The quotient of numerator/denominator
//
remainder - out - The remainder of numerator/denominator
// Side effect: Find the result of
integer division of numerator/denominator.
int countForDenom1(int&
amountLeft);
int countForDenom2(int&
amountLeft);
int countForDenom3(int&
amountLeft);
int countForDenom4(int&
amountLeft);
int countForDenom5(int&
amountLeft);
int countForDenom6(int&
amountLeft);
int countForDenom7(int&
amountLeft);
int countForDenom8(int&
amountLeft);
// int countForDenomN(int&
amountLeft)
//
Argument:
//
amountLeft - in - Amount from which to give out the Nth denomination
// ASSUME: amountLeft >= 0.
// - out - Remainder of the amount after giving out the
// the Nth denomination
//
Returns: The count of the Nth denomination in theAmount
//
(Side effect: Reduce amountLeft by the amount of the Nth denomination)
//--------------------------- end of
interfaces ------------------------------
//------------------- cash drawer
object implementation
---------------------
const int denomValue1 = 2000; // Number of pennies in $20.00
const char denomName1[] =
"twenties"; // Name of
$20.00 denomination
const int denomValue2 = 1000; // Number of pennies in $10.00
const char denomName2[] =
"tens"; // Name of
$10.00 denomination
const int denomValue3 = 500; // Number of pennies in $5.00
const char denomName3[] =
"fives"; // Name of $5.00
denomination
const int denomValue4 = 100; // Number of pennies in $1.00
const char denomName4[] =
"ones"; // Name of $1.00
denomination
const int denomValue5 = 25; // Number of pennies in $.25
const char denomName5[] =
"quarters"; // Name of $.25
denomination
const int denomValue6 = 10; // Number of pennies in $.10
const char denomName6[] =
"dimes"; // Name of $.10
denomination
const int denomValue7 = 5; // Number of pennies in $.05
const char denomName7[] =
"nickels"; // Name of $.05
denomination
const int denomValue8 = 1; // Number of pennies in $.01
const char denomName8[] =
"pennies"; // Name of $.01
denomination
//--------------- end of cash drawer object implementation -----------------
//-------------------- teller object
implementation --------------------------
void main( )
{
cout << "Change Maker (version 2)\n\n";
int amountLeftToChange;
// Amount left to be changed
inputInt(amountLeftToChange, "Enter amount to be changed in
pennies: ");
outputLine(countForDenom1(amountLeftToChange), denomName1);
outputLine(countForDenom2(amountLeftToChange), denomName2);
outputLine(countForDenom3(amountLeftToChange), denomName3);
outputLine(countForDenom4(amountLeftToChange), denomName4);
outputLine(countForDenom5(amountLeftToChange), denomName5);
outputLine(countForDenom6(amountLeftToChange), denomName6);
outputLine(countForDenom7(amountLeftToChange), denomName7);
outputLine(countForDenom8(amountLeftToChange), denomName8);
}
//----------------- end of teller
object implementation ----------------------
//---------------------- input object
implementation -------------------------
void inputInt(int& aNumber, const
char prompt[])
{
cout << prompt;
cin >> aNumber;
}
//------------------- end of input
object implementation ---------------------
//--------------------- output object
implementation -------------------------
void outputLine(int aNumber, const char
aString[])
{
cout << aNumber << " " << aString <<
endl;
}
//------------------ end of output
object implementation ---------------------
//-------------------- computing object
implementation -----------------------
void divMod(int numerator, int
denominator, int& quotient, int& remainder)
{
quotient = numerator / denominator;
remainder = numerator % denominator;
}
int countForDenom1(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue1, result,amountLeft);
return result;
}
int countForDenom2(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue2, result,amountLeft);
return result;
}
int countForDeNom3(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue3, result,amountLeft);
return result;
}
int countForDenom4(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue4, result,amountLeft);
return result;
}
int countForDenom5(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue5, result,amountLeft);
return result;
}
int countForDenom6(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue6, result,amountLeft);
return result;
}
int countForDenom7(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue7, result,amountLeft);
return result;
}
int countForDenom8(int& amountLeft)
{
int result; // Value to be
returned
divMod(amountLeft,denomValue8, result,amountLeft);
return result;
}
//----------------- end of computing
object implementation -------------------
//*******************************************************************************
//FILE: date.H
// A simple date class.
class Date {
public:
Date(int d = 31, int m = 12, int y = 2000); // Constructs a date with
//
default values 31 (day), 12 (month), and
//
2000 (year).
//
ASSUME: m/d/y is a valid date.
int theDay() const; // Day
of this date.
int theMonth() const; //
Month of this date.
int theYear() const; //
Year of this date.
void advance(); //
Advances this date to the following day.
private:
int day_;
int month_;
int year_;
};
#include "date.H"
Date :: Date(int d, int m, int y) :
day_(d), month_(m), year_(y)
{ }
int Date :: theDay() const
{
return day_;
}
int Date :: theMonth() const
{
return month_;
}
int Date :: theYear() const
{
return year_;
}
void Date :: advance()
{
day_ += 1;
// advance the day
switch (month_) {
case 1: case 3: case 5: case 7: case 8: case 10:
// January, March, May, July, August, October
if (day_ == 32) {
day_ = 1;
month_ += 1;
} break;
case 12:
// December
if (day_ == 32) {
day_ = 1;
month_ = 1;
year_ += 1;
} break;
case 4: case 6: case 9: case 11:
// April, June, September, November
if (day_ == 31) {
day_ = 1;
month_ += 1;
} break;
case 2:
// February
if (day_ > 29 || day_ == 29 && ( year_ % 4 >
0 || year_ % 100 == 0)
&& year_ % 400 >
0 ) {
day_ = 1;
month_ = 3;
} break;
}
}
//*******************************************************************************
// FILE: simple.h
class SimpleList {
public:
// Constructor
SimpleList();
// Constructs an empty list.
// These methods change the list
void clear();
// Makes the list empty.
void add(int newItem);
// Adds newItem to the list.
// ASSUME: The list is not full.
// These methods tell information about the list
bool member(int anItem) const;
// True if anItem is a list member.
int maximumCapacity() const;
// Maximum possible list size.
int currentSize() const;
// Number of items currently in the list.
private:
// List data
enum {maxSize_ = 100};
// Maximum size of the list
int item_[maxSize_];
// List items
int size_;
// Number of items currently in the list
};
//FILE: SIMPLE.C
#include "simple.H"
SimpleList :: SimpleList() : size_(0)
{ }
void SimpleList :: clear()
{
size_ = 0;
}
void SimpleList :: add(int newItem)
{
item_[size_] = newItem;
size_++;
}
bool SimpleList :: member(int anItem)
const
{
for (int i = 0; i < size_; i++)
if (item_[i] == anItem)
return true;
return false;
}
int SimpleList :: maximumCaPacity()
const
{
return maxSize_;