Code:
#include <iostream>
#include <string>
#include <sstream>
class usdollar
{
friend usdollar operator-(usdollar);
friend std::ostream &operator<<(std::ostream&, usdollar);
friend std::istream &operator<<(std::istream&, usdollar&);
friend bool operator>(usdollar, usdollar);
friend bool operator<(usdollar, usdollar);
friend bool operator==(usdollar, usdollar);
friend usdollar operator+(usdollar, usdollar);
friend usdollar operator-(usdollar, usdollar);
friend usdollar operator*(usdollar, double);
friend usdollar operator/(usdollar, double);
protected:
int dollaramount;
int centamount;
int sign;
public:
//variables
//constructors
usdollar(double amount)
{
this -> init(amount);
}
usdollar(int dollars = 0, int cents = 0)
{
this -> init(dollars, cents);
}
//various assignment operators
//all return itself for use in chained expressions
usdollar operator=(usdollar u1)
{
this -> dollaramount = u1.dollaramount;
this -> centamount = u1.centamount;
this -> sign = u1.sign;
return *this;
}
//dirty-work functions
void init(double amount)
{
//we take EPSILON to be 1e-9 here, should be enough
this -> init((int)amount, (int)((amount + 0.000000001 - (int)amount) * 100));
}
void init(int dollars = 0, int cents = 0)
{
this -> sign = 1;
this -> dollaramount = dollars;
this -> centamount = cents;
}
void rationalize()
{
this -> dollaramount += this -> centamount / 100;
this -> centamount %= 100;
while(this -> centamount < 0)
{
this -> dollaramount--;
this -> centamount += 100;
}
if(this -> dollaramount < 0)
{
this -> sign *= -1;
this -> dollaramount = -this -> dollaramount;
this -> centamount = -this -> centamount;
this -> rationalize();
}
}
//return double value
double getd()
{
return this -> sign * (this -> dollaramount + this -> centamount / 100.0);
}
//return sign
int gsign()
{
return this -> sign;
}
};
//unary minus
usdollar operator-(usdollar u1)
{
u1.sign *= -1;
return u1;
}
//input and output operators
std::istream &operator>>(std::istream &in, usdollar &u1)
{
double temp;
in >> temp;
u1.init(temp);
return in;
}
std::ostream &operator<<(std::ostream &out, usdollar u1)
{
if(u1.sign == -1)
{
out << "-";
}
out << "$" << u1.dollaramount << ".";
if(u1.centamount < 10)
{
out << "0";
}
out << u1.centamount;
return out;
}
//larger than?
bool operator>(usdollar u1, usdollar u2)
{
//compare dollaramounts first
if(u1.dollaramount > u2.dollaramount)
{
return true;
}
if(u1.dollaramount < u2.dollaramount)
{
return false;
}
//dollaramounts are eqaul, compare centamounts
if(u1.centamount > u2.centamount)
{
return true;
}
if(u1.centamount <= u2.centamount)
{
return false;
}
}
//smaller than?
bool operator<(usdollar u1, usdollar u2)
{
//compare dollaramounts first
if(u1.dollaramount < u2.dollaramount)
{
return true;
}
if(u1.dollaramount > u2.dollaramount)
{
return false;
}
//dollaramounts are eqaul, compare centamounts
if(u1.centamount < u2.centamount)
{
return true;
}
if(u1.centamount >= u2.centamount)
{
return false;
}
}
//equality
//unneeded (compiler provides default), but given anyway
bool operator==(usdollar u1, usdollar u2)
{
return ((u1.dollaramount == u2.dollaramount) && (u1.centamount == u2.centamount));
}
//addition
usdollar operator+(usdollar u1, usdollar u2)
{
u1.dollaramount += u2.dollaramount;
u1.centamount += u2.centamount;
//rationalize
u1.rationalize();
return u1;
}
//subtraction
usdollar operator-(usdollar u1, usdollar u2)
{
u1.dollaramount -= u2.dollaramount;
u1.centamount -= u2.centamount;
//rationalize
u1.rationalize();
return u1;
}
//and...
usdollar operator*(usdollar u1, double n)
{
//rationalize
u1.init(u1.sign * (u1.dollaramount + u1.centamount / 100.0) * n);
return u1;
}
usdollar operator/(usdollar u1, double n)
{
//rationalize
u1.init(u1.sign * (u1.dollaramount + u1.centamount / 100.0) / n);
return u1;
}
//complex operators
//reuse previous operators
bool operator>=(usdollar u1, usdollar u2)
{
return ((u1 > u2) || (u1 == u2));
}
bool operator<=(usdollar u1, usdollar u2)
{
return ((u1 < u2) || (u1 == u2));
}
usdollar &operator+=(usdollar &u1, usdollar u2)
{
u1 = u1 + u2;
return u1;
}
usdollar &operator-=(usdollar &u1, usdollar u2)
{
u1 = u1 - u2;
return u1;
}
usdollar &operator*=(usdollar &u1, double n)
{
u1 = u1 * n;
return u1;
}
usdollar &operator/=(usdollar &u1, double n)
{
u1 = u1 / n;
return u1;
}
//main()
int main()
{
using namespace std;
// self documenting code begin
cout << "This program calculates the amount of change\ngiven back after a purchase\n\n";
// self documening code end
cout << "Enter the purchase amount: $";
usdollar purchase;
cin >> purchase;
cout << "Enter the amount of money given: $";
usdollar money;
cin >> money;
cout << "\n\n";
usdollar change = money - purchase;
//if negative
if(change < 0)
{
cout << "Not enough money! Customer still owes " << change << "!\n";
}
else if(change == 0)
{
cout << "Customer has paid exact amount! No need to return change!\n";
}
//calculate change
else
{
//various denominations
usdollar denominations[] = {
1000.00,
500.00,
100.00,
50.00,
20.00,
10.00,
5.00,
1.00,
0.50,
0.25,
0.10,
0.05,
0.01
};
//temp counting array
int count[13] = {0};
//pluran and singular currencies
string namepl[] = {
"grand",
"five hundred dollars",
"hundred dollars",
"fifty dollars",
"twenty dollars",
"ten dollars",
"five dollars",
"dollars",
"half dollars",
"quarters",
"dimes",
"nickels",
"pennies"
};
string namesg[] = {
"grand",
"five hundred dollars",
"hundred dollars",
"fifty dollars",
"twenty dollars",
"ten dollars",
"five dollars",
"dollar",
"half dollar",
"quarter",
"dime",
"nickel",
"penny"
};
int length = 13;
//for each denomination
for(int i = 0; i < length; ++i)
{
//compute change
for(count[i] = 0; change >= denominations[i]; ++count[i])
{
change -= denominations[i];
}
}
// Self documenting output to screen
cout << "\nPlease give the customer " << money - purchase << " back in change:\n";
for(int i = 0; i < length; ++i)
{
//if >1 cout plural
if(count[i] > 1)
{
cout << count[i] << " " << namepl[i] << "\n";
}
//if ==1 cout singular
if(count[i] == 1)
{
cout << count[i] << " " << namesg[i] << "\n";
}
//if ==0 then skip
}
}
cout << "\n\n";
system("pause");
return 0;
}
Hmm I feel a tingling sense of accomplishment...