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

    关于练习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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值