数据结构、算法与应用(C++语言描述)(Sartaj Sahni)第一章 1.16-1.18

1.16-1.18

Currency_.h

#ifndef Currency_
#define Currency_

#include<string>
#include<iostream>
using namespace std; // istream,ostream在std域里面

enum signType{pplus, mminus}; //枚举


class illegalParameterValue
{
public :
//	illegalParameterValue(std::string message = "Illegal parameter value") {} !有问题
	illegalParameterValue(const char* theMessage)
	{
		message = theMessage;
	}
	void outputMessage() { std::cout << message << std::endl; }

private:
	std::string message;

};

class currency {

	friend istream & operator>>(istream &, currency&);



// 友元


	//构造函数
public:
	currency
	(
		signType theSign = pplus,
		unsigned long theDollars = 0,
		unsigned int theCents = 0
	);
		//析构函数
		~currency() {}
	void setValue(signType, unsigned long, unsigned int);
	void setValue(double);
	signType getSign() const { return sign; }
	unsigned long getDollars() const { return dollars; }
	unsigned int getCents() const { return cents; }
	currency add(const currency&) const;
	currency& increment(const currency&);
	void output() const;
	void input();

	void subtract(const currency&);
	currency percent(const double x) const;

	currency multiply(const double x) const;



	currency divide(const double x) const;

	currency operator-(const currency&) const;

	currency operator*(const currency&) const;

	double operator%(const currency&) const;

	double operator/(const currency&) const;

	currency& operator=( int ) ;
	
private:
	signType sign;
	unsigned long dollars;
	unsigned int cents;
};

//class illegalParameterValue
//{
//public:
//	illegalParameterValue( std::string message = "Illegal parameter value");
//		
//	illegalParameterValue(char* theMessage)
//	{
//		message = theMessage;
//	}
//	void outputMessage() { std::cout << message << std::endl; }
//private:
//	std::string message;
//};

#endif
#include<iostream>
#include<string>
#include"Currency_.h"

currency::currency(signType theSign, unsigned long theDollars, unsigned int theCents)
{
	setValue(theSign, theDollars, theCents);
}

void currency::setValue(signType theSign, unsigned long theDollars, unsigned int theCents)
{
	if (theCents > 99)
		throw illegalParameterValue("Cents should be < 100 ");
	sign = theSign;
	dollars = theDollars;
	cents = theCents;
}

void currency::setValue(double theAmount)
{
	if (theAmount < 0) { sign = mminus; theAmount = -theAmount; }
	else sign = pplus;
	dollars = (unsigned long)theAmount;

	cents = (unsigned int)((theAmount + 0.001 - dollars) * 100);
}

currency currency::add(const currency& x) const
{
	long a1, a2, a3;
	currency result;

	a1 = dollars * 100 + cents;
	if (sign == mminus) a1 = -a1;

	a2 = x.dollars * 100 + x.cents;
	if (x.sign == mminus) a2 = -a2;

	a3 = a1 + a2;

	if (a3 < 0) { result.sign = mminus; a3 = -a3; }
	else result.sign = pplus;
	result.dollars = a3 / 100;
	result.cents = a3 - result.dollars * 100;

	return result;
}

currency& currency::increment(const currency& x)
{
	*this = add(x);
	return *this;
}

void currency::output() const
{
	if (sign == mminus) std::cout << "-";
	std::cout<< '&'<<dollars <<".";
	if (cents < 10) std::cout << '0';
	std::cout << cents;
}

void currency::input( )
{
	signType sign;
	unsigned long dollars;
	unsigned int cents;

	std::cout << "please enter the money" << std::endl;
	std::cout << "please enter the dollars" << std::endl;
	std::cin >> dollars;
	std::cout << "please enter the cents" << std::endl;
	std::cin >> cents;

	if (dollars < 0) 
		sign = mminus;
	else
		sign = pplus;

	(*this).sign = sign;
	(*this).dollars = dollars;
	(*this).cents = cents;
}

void currency::subtract(const currency& x)
{
	if (x.sign == pplus)
	{
		(*this).dollars = (*this).dollars - x.dollars;
		if((*this).cents >= x.cents) 
		(*this).cents = (*this).cents - x.cents;
		else
		{

		(*this).dollars = (*this).dollars - 1;
		(*this).cents = (*this).cents + 100 - x.cents;

		}


	}
	else
	{
		(*this).dollars = (*this).dollars + x.dollars;
		(*this).cents = (*this).cents + x.cents;
	}

}

currency currency::percent(const double x) const
{
	currency precent;

	double all = (*this).dollars * 100 + (*this).cents;

	all = all * x / 100.0;

	precent.dollars = int(all / 100.0);
	precent.cents = all - precent.dollars * 100;

	precent.sign = pplus;

	return precent;


}

currency currency::multiply(const double x) const
{
	currency mult;

	double all = (*this).dollars * 100 + (*this).cents;

	all = all * x ;

	mult.dollars = int(all / 100.0);
	mult.cents = all - mult.dollars * 100;

	mult.sign = pplus;

	return mult;
}

currency currency::divide(const double x) const
{
	currency div;

	double all = (*this).dollars * 100 + (*this).cents;

	all = all / x;

	div.dollars = int(all / 100.0);
	div.cents = all - div.dollars * 100;

	div.sign = pplus;

	return div;
}

istream & operator>>(istream & in,  currency& x)
{

	in >> x.dollars;
	in >> x.cents;

	return in;
}

currency currency::operator-(const currency& x) const
{
	currency jian;

	double all1 = (*this).dollars * 100 + (*this).cents;

	double all2 = x.dollars * 100 + x.cents;

	double all = all1 - all2;

	jian.dollars = int(all / 100.0);
	jian.cents = all - jian.dollars*100;

	return jian;
}


currency currency::operator*(const currency& x) const
{
	currency cheng;

	double all1 = (*this).dollars * 100 + (*this).cents;

	double all2 = x.dollars * 100 + x.cents;

	double all = all1 * all2;

	
	cheng.dollars = int(all / 100.0);
	cheng.cents = all - cheng.dollars * 100;


	return cheng;
}

//
double currency::operator%(const currency& x) const
{
	double baifeng;

	double all1 = (*this).dollars * 100 + (*this).cents;

	double all2 = x.dollars * 100 + x.cents;

    
	baifeng = all1 / all2;



	return baifeng;
}

double currency::operator/(const currency& x) const
{
	double chu;

	double all1 = (*this).dollars * 100 + (*this).cents;

	double all2 = x.dollars * 100 + x.cents;

	 chu = all1 / all2 ;

	return chu;
}

currency& currency::operator=(int x)
{
	

	if(x < 100)
	{
		(*this).dollars = 0;
		(*this).cents = x;
	}
	else
	{
		(*this).dollars = int(x/ 100.0);
		(*this).cents = x - (*this).dollars * 100;
	}





	return (*this);
}


/*currency operator*(const currency&) const;

currency operator%(const currency&) const;

currency operator/(const currency&) const*/;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值