您的位置:首页 > 理论基础 > 数据结构算法

数据结构、算法与应用 (C++描述) 第二版 1.18

2015-11-16 15:35 225 查看
关于练习1.17,直接用这个就可以了。[数据结构、算法与应用 (C++描述) 第二版 1.16](http://blog.csdn.net/tianluoyuge/article/details/49863769)
关于1.18,重载了**+、-、*、/**和**>>**运算符,对于>>他要求不能写在**公有成员函数**里,所以用**友元**写,**关于+、-运算我写的比较啰嗦,是为了检测正负符号**,因为存储和输出的时候都是按照sign的值来显示符号,而amount里面是正值

**仅供有需要的人以参考,如有错误请纠正我**


头文件: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 operator=(int x);
void operator=(double theAmount);
signType getSign() const;
unsigned long getDollars() const;
unsigned int getCents() const;
currency operator+(const currency & x) const;
currency operator-(const currency & x) const;
currency operator*(const currency & x) const;
currency operator/(const currency & x) const;
friend std::ostream & operator<<(std::ostream & out, const currency &x);
friend std::istream & operator>>(std::istream & in, currency & x);
};

#endif


头文件实现:currency.cpp

#include"currency.h"
currency::currency(signType theSign, unsigned long theDollars, unsigned int theCents)
: sign(theSign)
{
using namespace std;
if (theCents > 99)
{
cerr << "Cents should be < 100";
exit(EXIT_FAILURE);
}
amount = theDollars * 100 + theCents;
if (theSign == minu)
amount = -amount;
}

void currency::operator=(int x)
{
if (x < 0)
{
sign = minu;
amount = -x;
}
else
{
sign = plu;
amount = x;
}
}

void currency::operator=(double theAmount)
{
if (theAmount < 0)
{
amount = (long)((theAmount - 0.001) * 100);
sign = minu;
amount = -amount;
}
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;
if (sign == plu && x.sign == plu)
result.amount = amount + x.amount;
else if (sign == plu && x.sign == minu)
{
if (amount > x.amount)
{
result.amount = amount - x.amount;
result.sign = plu;
}
else
{
result.amount = x.amount - amount;
result.sign = minu;
}
}
else if (sign == minu && x.sign == plu)
{
if (amount > x.amount)
{
result.amount = amount - x.amount;
result.sign = minu;
}
else
{
result.amount = x.amount - amount;
result.sign = plu;
}
}
else if (sign == minu && x.sign == minu)
{
result.amount = amount + x.amount;
result.sign = minu;
}
return result;
}

currency currency::operator-(const currency & x) const
{
currency temp;
if (sign == plu && x.sign == plu)
{
if (amount > x.amount)
temp.amount = amount - x.amount;
else
{
temp.amount = x.amount - amount;
temp.sign = minu;
}
}
else if (sign == plu && x.sign == minu)
{
temp.amount = amount + x.amount;
temp.sign = plu;
}
else if (sign == minu && x.sign == plu)
{
if (amount > x.amount)
{
temp.amount = amount - x.amount;
temp.sign = minu;
}
else
temp.amount = x.amount - amount;
}
else if (sign == minu && x.sign == minu)
{
temp.amount = amount + x.amount;
temp.sign = minu;
}
return temp;
}

currency currency::operator*(const currency & x) const
{
currency temp;
temp.amount = amount * x.amount;
if (sign == minu || x.sign == minu)
temp.sign = minu;
return temp;
}

currency currency::operator/(const currency & x) const
{
currency temp;
double t1 = amount;
double t2 = x.amount;
double tt = t1 / t2;
temp.amount = tt * 100;
if (sign == minu || x.sign == minu)
temp.sign = minu;
return temp;
}

std::ostream & operator<<(std::ostream & out, const currency & x)
{
if (x.sign == minu)
out << '-';
long dollars = x.amount / 100;
out << '$' << dollars << '.';
int cents = x.amount - dollars * 100;
if (cents < 10)
out << '0';
out << cents;
return out;
}

std::istream & operator>>(std::istream & is, currency & x)
{

long temp;
is >> temp;
if (temp > 0)
{
x.amount = temp;
x.sign = plu;
}
else
{
x.amount = -temp;
x.sign = minu;
}
return is;
}


测试文件:main.cpp

#include"currency.h"
#include<iostream>
#include<cstdlib>

int main()
{
using namespace std;
currency g, i, j;
currency h(plu, 3, 50);

g = 25;
i = -6.45;

cout << "g: " << g << endl;
cout << "i: " << i << endl;

j = h + g;
cout << h << " + " << g << " = " << j << endl;

j = i + g + h;
cout << i << " + " << g << " + " << h << " = " << j << endl;

currency t1;
t1 = 100;
currency t2;
t2 = -50;
currency t3;
t3 = t1 * t2;
cout << "t3: " << t3 << endl;
cout << "Enter a number: ";
cin >> t3;
cout << "t3: " << t3 << endl;

t3 = t1 / t2;
cout << t1 << " / " << t2 << " = " << t3 << endl;

t3 = t1 - t2;
cout << t1 << " - " << t2 << " = " << t3 << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: