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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
class Money
{
public:
friend Money operator +(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount);
friend bool operator ==(const Money& amount1, const Money& amount2);
Money(long dollars, short cents);
Money(long dollars);
Money();
double get_value() const;
friend istream& operator >>(istream& ins, Money& amount);
friend ostream& operator <<(ostream& outs, const Money& amount);
private:
long all_cents;
};
int main()
{
char loop = 'y';
do
{
bool more = true;
while(more)
{
cout << "What would you like to do?\n"
<< "1) negate"
<< "2) add"
<< "3) subtract"
<< "4) check =="
<< "5) check <"
<< "6) check >"
<< "7) check =<"
<< "8) check >=";
}
cout << "Would you like to run the program again? (y/n)? ";
cin >> loop;
loop = tolower(loop);
while(loop != 'y' && loop != 'n')
{
cout << "Incorrect output. Would you like to run the program again? ";
cin >> loop;
}
}while(loop == 'y');
cout << "Press a key to exit: ";
char exit;
cin >> exit;
return 0;
}
istream& operator >>(istream& ins, Money& amount)
{
char one_char, decimal_point;
long dollars;
short cents;
bool negative;
ins >> one_char;
if(one_char == '-')
{
negative = true;
ins >> one_char;
}
else
{
negative = false;
ins >> dollars >> decimal_point >> cents;
if(one_char != '$' || decimal_point != '.')
{
cout << "Error, illegal input\n";
exit(1);
}
amount.all_cents = dollars*100 + cents;
if(negative)
{
amount.all_cents = -amount.all_cents;
}
}
return ins;
}
ostream& operator <<(ostream& outs, const Money& amount)
{
long positive_cents, dollars, cents;
positive_cents = labs(amount.all_cents);
dollars = positive_cents/100;
cents = positive_cents%100;
if(amount.all_cents < 0)
{
outs << "-";
}
outs << '$' << dollars << '.';
if(cents < 10)
{
outs << '0';
}
outs << cents;
return outs;
}
Money operator +(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}
bool operator ==(const Money& amount1, const Money& amount2)
{
return (amount1.all_cents == amount2.all_cents);
}
Money operator -(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents - amount2.all_cents;
return temp;
}
Money operator -(const Money& amount)
{
Money temp;
temp.all_cents = -amount.all_cents;
return temp;
}
|