首页 > 代码库 > 实现一个RMB的强货币类型

实现一个RMB的强货币类型

#ifndef RMB_H_
#define RMB_H_

typedef double Yuan;
typedef unsigned long Cent;

class RMB {
public:
	RMB();
	RMB(Cent cent);
	RMB(Yuan yuan);
	RMB(RMB& that);
	~RMB();

	const char* toString();

	Yuan 	GetYuan()const;
	Cent	GetCent()const;

	RMB& 	operator=(RMB& that);
	RMB& 	operator=(Cent cent);
	RMB& 	operator=(Yuan yuan);

	bool 	operator==(RMB& that)const;
	bool 	operator> (RMB& that)const;
	bool 	operator< (RMB& that)const;
	bool 	operator>=(RMB& that)const;
	bool 	operator<=(RMB& that)const;
	bool 	operator!=(RMB& that)const;

	RMB& 	operator++();
	RMB 	operator++(int);

	RMB& 	operator--();
	RMB 	operator--(int);

	RMB&	operator+=(RMB& that);
	RMB&	operator-=(RMB& that);

private:
	Cent m_Base;
};

#endif /* RMB_H_ */


实现

#include "RMB.h"

#include <stdio.h> //for sprintf

RMB::RMB()
	:m_Base(0)
{

}

RMB::RMB(Cent cent)
{
	m_Base = cent;
}

RMB::RMB(Yuan yuan)
{
	m_Base = yuan*100;
}

RMB::RMB(RMB& that)
{
	this->m_Base = that.m_Base;
}

RMB::~RMB()
{

}

Yuan
RMB::GetYuan()const
{
	return m_Base*0.01;
}

Cent
RMB::GetCent()const
{
	return m_Base;
}


const char*
RMB::toString()
{
	static char str[256]={0};
	sprintf(str,"%f yuan(Cent:%lu)\n",m_Base*0.01,m_Base);
	return str;
}


RMB&
RMB::operator=(RMB& that)
{
	if(this != &that)
	{
		this->m_Base = that.m_Base;
	}
	return *this;
}

RMB&
RMB::operator=(Cent cent)
{
	this->m_Base = cent;
	return *this;
}

RMB&
RMB::operator=(Yuan yuan)
{
	this->m_Base = yuan*100;
	return *this;
}

bool
RMB::operator==(RMB& that)const
{
	return this->m_Base == that.m_Base;
}

bool
RMB::operator> (RMB& that)const
{
	return this->m_Base > that.m_Base;
}

bool
RMB::operator< (RMB& that)const
{
	return this->m_Base < that.m_Base;
}

bool
RMB::operator>=(RMB& that)const
{
	return *this > that || * this == that;
}

bool
RMB::operator<=(RMB& that)const
{
	return *this < that || * this == that;
}

bool
RMB::operator!=(RMB& that)const
{
	return ! (*this==that);
}

RMB&
RMB::operator++()
{
	++this->m_Base;
	return *this;
}

RMB
RMB::operator++(int)
{
	RMB tmp(*this);
	++(*this);
	return tmp;
}

RMB&
RMB::operator--()
{
	--this->m_Base;
	return *this;
}

RMB
RMB::operator--(int)
{
	RMB tmp(*this);
	--(*this);
	return tmp;
}

RMB&
RMB::operator+=(RMB& that)
{
	this->m_Base+=that.m_Base;
	return *this;
}

RMB&
RMB::operator-=(RMB& that)
{
	this->m_Base-=that.m_Base;
	return *this;
}


测试代码

#include <iostream>
#include "RMB.h"

using namespace std;

int main()
{
	RMB m;
	cout<< m.toString() <<endl;
	++m;

	RMB m1(m);//拷贝构造
	cout<< m1.toString() <<endl;

	RMB m2 = m1;//拷贝构造
	cout<< m2.toString() <<endl;

	//RMB n(10);无法编译通过 必须指定类型
	RMB n( static_cast<Yuan>(10) );
	cout<< n.toString() <<endl;

	RMB n1( static_cast<Cent>(10) );
	cout<< n1.toString() <<endl;

	n1=n;//拷贝赋值
	cout<< n1.toString() <<endl;

	if(m==n)
		cout << "m==n" <<endl;
	if(m!=n)
		cout << "m!=n" <<endl;
	if(m>n)
		cout << "m>n" <<endl;
	if(m<n)
		cout << "m<n" <<endl;
	if(m>=n)
		cout << "m>=n" <<endl;
	if(m<=n)
		cout << "m<=n" <<endl;

	//m = 200;无法编译通过 必须指定类型
	m = static_cast<Yuan>(200);
	cout<< m.toString() <<endl;

	n = static_cast<Cent>(200);
	cout<< n.toString() <<endl;

	return 0;
}


运行结果

0.000000 yuan(Cent:0)

0.010000 yuan(Cent:1)

0.010000 yuan(Cent:1)

10.000000 yuan(Cent:1000)

0.100000 yuan(Cent:10)

10.000000 yuan(Cent:1000)

m!=n

m<n

m<=n

200.000000 yuan(Cent:20000)

2.000000 yuan(Cent:200)