1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
//Program to demonstrate the class Money.
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money add(Money amount1, Money amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns the sum of the values of amount1 and amount2.
friend bool equal(Money amount1, Money amount2); //Precondition: amount1 and amount2 have been given values.
//Returns true if the amount1 and amount2 have the same value;
//otherwise, returns false.
Money(long dollars,int cents); //Initializes the object so its value represents an amount with the
//dollars and cents given by the arguments. If the amount is negative,
//then both dollars and cents must be negative.
Money(long dollars); //Initializes the object so its value represents $dollars.00.
Money( ); //Initializes the object so its value represents $0.00.
double get_value( ); //Precondition: The calling object has been given a value.
//Returns the amount of money recorded in the data of the calling object.
void input(istream& ins); //Precondition: If ins is a file input stream, then ins has already been
//connected to a file. An amount of money, including a dollar sign, has been
//entered in the input stream ins. Notation for negative amounts is -$100.00.
//Postcondition: The value of the calling object has been set to
//the amount of money read from the input stream ins.
void output(ostream& outs); //Precondition: If outs is a file output stream, then outs has already been
//connected to a file.
//Postcondition: A dollar sign and the amount of money recorded
//in the calling object have been sent to the output stream outs.
private:
long all_cents;
};
int digit_to_int(char c);
//Function declaration for function used in the definition of Money::input:
//Precondition: c is one of the digits '0' through '9'.
//Returns the integer for the digit; for example, digit_to_int ('3') returns 3.
int main( )
{
Money your_amount, my_amount(10, 9), our_amount;
cout << "Enter an amount of money: ";
your_amount.input(cin);
cout << "Your amount is ";
your_amount.output(cout);
cout << endl;
cout << "My amount is ";
my_amount.output(cout);
cout << endl;
if (equal(your_amount, my_amount))
cout << "We have the same amounts.\n";
else
cout << "One of us is richer.\n";
our_amount = add(your_amount, my_amount);
your_amount.output(cout);
cout << " + ";
my_amount.output(cout);
cout << " equals ";
our_amount.output(cout);
cout << endl;
return 0;
}
Money add(Money amount1, Money amount2)
{
Money temp;
temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}
bool equal(Money amount1, Money amount2)
{
return (amount1.all_cents == amount2.all_cents);
}
Money::Money(long dollars, int cents)
{
if (dollars * cents < 0) //If one is negative and one is positive
{
cout << "Illegal values for dollars and cents.\n";
exit(1);
}
all_cents = dollars * 100 + cents;
}
Money::Money(long dollars) : all_cents(dollars * 100)
{
//Body intentionally blank.
}
Money::Money( ) : all_cents(0)
{
//Body intentionally blank.
}
double Money::get_value( )
{
return (all_cents * 0.01);
}
//Uses iostream, cctype, cstdlib:
void Money::input(istream& ins)
{
char one_char, decimal_point, digit1, digit2;
//digits for the amount of cents
long dollars;
int cents;
bool negative;//set to true if input is negative.
ins >> one_char;
if (one_char == ' ')
{
negative = true;
ins >> one_char; //read '$'
}
else
negative = false;
//if input is legal, then one_char == '$'
ins >> dollars >> decimal_point >> digit1 >> digit2;
if (one_char != '$' || decimal_point != '.'
|| !isdigit(digit1) || !isdigit(digit2))
{
cout << "Error illegal form for money input\n";
exit(1);
}
cents = digit_to_int(digit1) * 10 + digit_to_int(digit2);
all_cents = dollars * 100 + cents;
if (negative)
all_cents = -all_cents;
}
//Uses cstdlib and iostream:
void Money::output(ostream& outs)
{
long positive_cents, dollars, cents;
positive_cents = labs(all_cents);
dollars = positive_cents / 100;
cents = positive_cents % 100;
if (all_cents < 0)
outs << "-$" << dollars << '.';
else
outs << "$" << dollars << '.';
if (cents < 10)
outs << '0';
outs << cents;
}
int digit_to_int(char c)
{
return (static_cast<int>(c) − static_cast<int>('0'));
}
|