按照惯例,分了三个部分(头文件,头文件实现,main)来写。
只是按照他的要求粗略的写了一下,没有优化。
**仅供有需要的人以参考,如有错误请纠正我**
头文件:currency.h
#ifndef CURRENCY_H_
#define CURRENCY_H_
#include<iostream>
#include<exception>
enum signType { plu, minu };
class currency
{
private:
signType sign;
long amount;
public:
currency(signType theSign = plu, unsigned long theDollars = 0, unsigned int theCents = 0);
~currency() { }
void setValue(signType theSign, unsigned long Dollars, unsigned int Cents);
void setValue(double theAmount);
signType getSign() const;
unsigned long getDollars() const;
unsigned int getCents() const;
currency operator+(const currency & x) const;
friend std::ostream & operator<<(std::ostream & out, const currency &x);
void input();
void subtract(double x);
currency & percent(double x);
currency & multiply(double x);
currency & divide(double x);
};
#endif
头文件实现:currency.cpp
#include"currency.h"
currency::currency(signType theSign, unsigned long theDollars, unsigned int theCents)
{
setValue(theSign, theDollars, theCents);
}
void currency::setValue(signType theSign, unsigned long Dollars, unsigned int Cents)
{
using namespace std;
if (Cents > 99)
throw "Cents should be < 100";
amount = Dollars * 100 + Cents;
if (theSign == minu)
amount = -amount;
}
void currency::setValue(double theAmount)
{
if (theAmount < 0)
amount = (long)((theAmount - 0.001) * 100);
else
amount = (long)((theAmount + 0.001) * 100);
}
signType currency::getSign() const
{
if (amount < 0)
return minu;
else
return plu;
}
unsigned long currency::getDollars() const
{
if (amount < 0)
return (-amount) / 100;
else
return amount / 100;
}
unsigned int currency::getCents() const
{
if (amount < 0)
return -amount - getDollars() * 100;
else
return amount - getDollars() * 100;
}
currency currency::operator+(const currency & x) const
{
currency result;
result.amount = amount + x.amount;
return result;
}
std::ostream & operator<<(std::ostream & out, const currency & x)
{
long theAmount = x.amount;
if (theAmount < 0)
{
out << '-';
theAmount = -theAmount;
}
long dollars = theAmount / 100;
out << '$' << dollars << '.';
int cents = theAmount - dollars * 100;
if (cents < 10)
out << '0';
out << cents;
return out;
}
void currency::input()
{
using std::cin;
using std::cout;
using std::endl;
long dollars;
int cents;
cout << "Dollars: ";
cin >> dollars;
cout << "Cents: ";
cin >> cents;
if ((dollars > 0 && cents < 0) || (dollars < 0 && cents > 0))
throw "Dollars and cents symbols must be the same.";
if (dollars < 0)
setValue(minu, dollars, cents);
else
setValue(plu, dollars, cents);
}
void currency::subtract(double x)
{
amount = amount - (long)(x * 100);
std::cout << "subtract: " << *this << std::endl;
}
currency & currency::percent(double x)
{
amount = amount * (x / 100);
return *this;
}
currency & currency::multiply(double x)
{
amount = amount * x;
return *this;
}
currency & currency::divide(double x)
{
amount = amount / x;
return *this;
}
测试文件:main.cpp
#include"currency.h"
#include<iostream>
#include<cstdlib>
int main()
{
using namespace std;
currency g, i, j;
currency h(plu, 3, 50);
g.setValue(minu, 2, 25);
i.setValue(-6.45);
j = h + g;
cout << h << " + " << g << " = " << j << endl;
j = i + g + h;
cout << i << " + " << g << " + " << h << " = " << j << endl;
/*异常测试
cout << "Attempting to initialize with cents = 152" << endl;
try
{
i.setValue(plu, 3, 152);
}
catch (char * e)
{
cout << "Caught thrown exception : " << e << endl;
exit(EXIT_FAILURE);
}
*/
currency test;
try {
test.input();
}
catch (char * e)
{
cout << "test.input error: " << e << endl;
exit(EXIT_FAILURE);
}
cout << "test: " << test << endl;
test.subtract(2.5);
cout << "percent: " << test.percent(10) << endl;
cout << "multiply: " << test.multiply(5.0) << endl;
cout << "divide: " << test.divide(2.0) << endl;
return 0;
}