⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 currency.cpp

📁 计算器 英国一个学生写的 代码很适合学习
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
/*							  Currency.cpp							*/
/*				implementation for the Currency class				*/
/*							  written by							*/
/*							Anwar-ul-Haque							*/
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Currency Calculator.h"
#include "Currency.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Static data definiation
//////////////////////////////////////////////////////////////////////

double Currency::currency_rates[MAX_COUNTRIES][MAX_COUNTRIES];

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Currency::Currency()
{
	notes = 0;
	coins = 0;
	c_letter = "";
	operation_flag = false;
}

Currency::Currency( ULONG note )
{
	notes = note;
	coins = 0;
	set_c_letter( DEFAULT_COUNTRY );
	operation_flag = false;
}

Currency::Currency( ULONG note, WORD coin, CString letter )
{
	notes = note + coin / 100;
	coins = coin % 100;
	c_letter = letter;
	operation_flag = false;
}

Currency::Currency( double amount, CString letter )
{
	if( amount<0 )
	{
		::AfxMessageBox( "Negative currencies are not allowed..." );
		return;
	}
	notes = ULONG (amount);
	coins = WORD ((amount - notes) * 100);
	c_letter = letter;
	operation_flag = false;
}

//////////////////////////////////////////////////////////////////////
// Set & Get Functions
//////////////////////////////////////////////////////////////////////

void Currency::set_currency( ULONG note, WORD coin, CString letter)
{
	notes = note + coin / 100;
	coins = coin % 100;
	c_letter = letter;
}

void Currency::set_currency( Currency amount )
{
	notes = amount.notes;
	coins = amount.coins;
	c_letter = amount.c_letter;
}

void Currency::set_currency( double amount, CString letter )
{
	notes = ULONG (amount);
	coins = WORD ((amount-notes) * 100.0);
	c_letter = letter;
}

void Currency::set_notes( ULONG note )
{
	notes = note;
}

void Currency::set_coins( WORD coin )
{
	coins = coin;
}

void Currency::set_c_letter( CString letter )
{
	c_letter = letter;
}

void Currency::set_c_letter( cname country )
{
	BOOL found = false;
	for( int i=0; i<MAX_COUNTRIES; i++ )
		if( country==COUNTRIES[i] )
		{
			found = true;
			break;
		}
	if( found )
		c_letter = CURR_LETTERS[i];
	else
		TRACE( "Error: Undefined Currency Letter..." );
}

Currency Currency::get_currency()
{
	return Currency( notes, coins, c_letter );
}

ULONG Currency::get_notes()
{
	return notes;
}

WORD Currency::get_coins()
{
	return coins;
}

CString Currency::get_c_letter()
{
	return c_letter;
}

double Currency::get_double()
{
	double amount = double(coins)/100;
	amount +=  double(notes);
	return amount;
}

//////////////////////////////////////////////////////////////////////
// Overloaded Operators
//////////////////////////////////////////////////////////////////////

Currency Currency::operator + ( Currency amount )
{
	Currency temp;
	if( strcmp(c_letter, amount.c_letter) )
	{
		::AfxMessageBox( "Can not add amounts of two different currencies..." );
		operation_flag = false;
		return temp;
	}
	temp.coins = coins + amount.coins;
	if( temp.coins>=100 )
	{
		temp.notes++;
		temp.coins -= 100;
	}
	temp.notes += notes + amount.notes;
	temp.c_letter = c_letter;
	operation_flag = true;
	return temp;
}

Currency Currency::operator + ( double amount )
{
	Currency temp;
	ULONG note = ULONG (amount);
	WORD coin = WORD ((amount-note) * 100.0);
	temp.coins = coins + coin;
	if( temp.coins>=100 )
	{
		temp.notes++;
		temp.coins -= 100;
	}
	temp.notes += notes + note;
	temp.c_letter = c_letter;
	operation_flag = true;
	return temp;
}

void Currency::operator += ( Currency amount )
{
	if( strcmp(c_letter, amount.c_letter) )
	{
		::AfxMessageBox( "Can not add amounts of two different currencies..." );
		operation_flag = false;
		return;
	}
	coins += amount.coins;
	if( coins>=100 )
	{
		notes++;
		coins -= 100;
	}
	notes += amount.notes;
	operation_flag = true;
}

void Currency::operator += ( double amount )
{
	ULONG note = ULONG (amount);
	WORD coin = WORD ((amount-note) * 100.0);
	coins += coin;
	if( coins>=100 )
	{
		notes++;
		coins -= 100;
	}
	notes += note;
	operation_flag = true;
}

Currency Currency::operator - ( Currency amount )
{
	Currency temp;
	if( strcmp(c_letter, amount.c_letter) )
	{
		::AfxMessageBox( "Can not subtract amounts of two different currencies..." );
		operation_flag = false;
		return temp;
	}
	if( notes<amount.notes )
	{
		::AfxMessageBox( "Can not subtract higher amount from lower amount..." );
		operation_flag = false;
		return temp;
	}
	int temp_c = coins - amount.coins;
	BOOL flag = false;
	if( temp_c<0 )
	{
		flag = true;
		temp.coins = temp_c + 100;
	}
	else
		temp.coins = temp_c;
	temp.notes = notes - amount.notes;
	if( flag )
		temp.notes--;
	temp.c_letter = c_letter;
	operation_flag = true;
	return temp;
}

Currency Currency::operator - ( double amount )
{
	Currency temp;
	ULONG note = ULONG (amount);
	WORD coin = WORD ((amount-note) * 100.0);
	if( notes<note )
	{
		::AfxMessageBox( "Can not subtract higher amount from lower amount..." );
		operation_flag = false;
		return temp;
	}
	int temp_c = coins - coin;
	BOOL flag = false;
	if( temp_c<0 )
	{
		flag = true;
		temp.coins = temp_c + 100;
	}
	else
		temp.coins = temp_c;
	temp.notes = notes - note;
	if( flag )
		temp.notes--;
	temp.c_letter = c_letter;
	operation_flag = true;
	return temp;
}

void Currency::operator -= ( Currency amount )
{
	if( strcmp(c_letter, amount.c_letter) )
	{
		::AfxMessageBox( "Can not subtract amounts of two different currencies..." );
		operation_flag = false;
		return;
	}
	if( notes<amount.notes )
	{
		::AfxMessageBox( "Can not subtract higher amount from lower amount..." );
		operation_flag = false;
		return;
	}
	int temp = coins-amount.coins;
	coins = temp;
	if( temp<0 )
	{
		notes--;
		coins += 100;
	}
	notes -= amount.notes;
	operation_flag = true;
}

void Currency::operator -= ( double amount )
{
	ULONG note = ULONG (amount);
	WORD coin = WORD ((amount-note) * 100.0);
	if( notes<note )
	{
		::AfxMessageBox( "Can not subtract higher amount from lower amount..." );
		operation_flag = false;
		return;
	}
	int temp = coins-coin;
	coins = temp;
	if( temp<0 )
	{
		notes--;
		coins += 100;
	}
	notes -= note;
	operation_flag = true;
}

Currency Currency::operator * ( double number )
{
	Currency temp;
	if( number<0 )
	{
		::AfxMessageBox( "Multiplication by negative number is not allowed..." );
		operation_flag = false;
		return temp;
	}
	double amount = double (notes + double (coins) / 100);
	amount *= number;
	temp.notes = ULONG (amount);
	temp.coins = WORD ((amount-temp.notes) * 100.0);
	temp.c_letter = c_letter;
	operation_flag = true;
	return temp;
}

void Currency::operator *= ( double number )
{
	if( number<0 )
	{
		::AfxMessageBox( "Multiplication by negative number is not allowed..." );
		operation_flag = false;
		return;
	}
	double amount = double (notes + double (coins) / 100);
	amount *= number;
	notes = ULONG (amount);
	coins = WORD ((amount-notes) * 100.0);
	operation_flag = true;
}

Currency Currency::operator / ( double number )
{
	Currency temp;
	if( number==0 )
	{
		::AfxMessageBox( "Can not divide by zero..." );
		operation_flag = false;
		return temp;
	}
	if( number<0 )
	{
		::AfxMessageBox( "Division by negative number is not allowed..." );
		operation_flag = false;
		return temp;
	}
	double amount = double (notes + double (coins) / 100);
	amount /= number;
	temp.notes = ULONG (amount);
	temp.coins = WORD ((amount-temp.notes) * 100.0);
	temp.c_letter = c_letter;
	operation_flag = true;
	return temp;
}

void Currency::operator /= ( double number )
{
	if( number==0 )
	{
		::AfxMessageBox( "Can not divide by zero..." );
		operation_flag = false;
		return;
	}
	if( number<0 )
	{
		::AfxMessageBox( "Division by negative number is not allowed..." );
		operation_flag = false;
		return;
	}
	double amount = double (notes + double (coins) / 100);
	amount /= number;
	notes = ULONG (amount);
	coins = WORD ((amount-notes) * 100.0);
	operation_flag = true;
}

Currency Currency::operator % ( double per )
{
	Currency temp;
	if( per<0 )
	{
		::AfxMessageBox( "Percentage on the base of negative values is not allowed..." );
		operation_flag = false;
		return temp;
	}
	double amount = double (notes + double (coins) / 100);
	amount *= (per / 100.0);
	temp.notes = ULONG (amount);
	temp.coins = WORD ((amount-temp.notes) * 100.0);
	temp.c_letter = c_letter;
	operation_flag = true;
	return temp;
}

void Currency::operator %= ( double per )
{
	if( per<0 )
	{
		::AfxMessageBox( "Percentage on the base of negative values is not allowed..." );
		operation_flag = false;
		return;
	}
	double amount = double (notes + double (coins) / 100);
	amount *= (per / 100.0);
	notes = ULONG (amount);
	coins = WORD ((amount-notes) * 100.0);
	operation_flag = true;
}

//////////////////////////////////////////////////////////////////////
// Overloaded Comparison Operators
// Note: Deviation of 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -