📄 currency calculatordlg.cpp
字号:
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '7';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum8()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '8';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '8';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum9()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '9';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '9';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnPoint()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = "0.";
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount.Find('.')!=-1 )
return;
m_amount += '.';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnBackspace()
{ // remove currency letter if it is currency
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( m_amount=="0" )
return; // return if already 0
if( m_amount.GetLength()==1 )
m_amount = "0"; // make result 0, if only one number exists
else // delete last number
m_amount.Delete( m_amount.GetLength()-1, 1 );
if( m_curr_or_num==IDC_CURRENCY )
{ // add currency letter if it is currency
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount ); // up-date result field
}
void CCurrencyCalculatorDlg::OnClear()
{ // remove currency letter if it is currency
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( m_amount=="0" )
return; // return if already 0
m_amount = "0"; // make result 0
if( m_curr_or_num==IDC_CURRENCY )
{ // add currency letter if it is currency
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount ); // up-date result field
}
void CCurrencyCalculatorDlg::OnClearall()
{ // remove currency letter if it is currency
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
m_amount = "0"; // make result 0
if( m_curr_or_num==IDC_CURRENCY )
{ // add currency letter if it is currency
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount ); // up-date result field
last_op = none; // make last operation null
new_num_flag = false; // turn new number flag OFF
if( first_num.curr_flag ) // make first number 0
first_num.curr_num.set_currency( 0.0f, Currency::get_c_letter( COUNTRIES[m_cur_sel] ) );
else
first_num.doub_num = 0.0f;
intermediate_num.make_null(); // make intermediate_num null
}
void CCurrencyCalculatorDlg::OnAdd()
{ // convert m_amount into double after removing currency letter if it is currency
CString str;
if( m_amount.Find( ' ' )!=-1 )
str = remove_c_letter();
else
str = m_amount;
double num = atof( str );
if( num==0 )
return; // return if 0
last_op = addition; // make addition last operation
new_num_flag = true; // turn new number flag ON
if( m_curr_or_num==IDC_CURRENCY )
{ // save it as first number
first_num.curr_flag = true;
first_num.curr_num.set_currency( num, Currency::get_c_letter( COUNTRIES[m_cur_sel] ) );
}
else
{
first_num.curr_flag = false;
first_num.doub_num = num;
}
intermediate_num.make_null(); // make intermediate_num null to indicate fresh operation
}
void CCurrencyCalculatorDlg::OnSubtract()
{
CString str;
if( m_amount.Find( ' ' )!=-1 )
str = remove_c_letter();
else
str = m_amount;
double num = atof( str );
if( num==0 )
return;
last_op = subtraction;
new_num_flag = true;
if( m_curr_or_num==IDC_CURRENCY )
{
first_num.curr_flag = true;
first_num.curr_num.set_currency( num, Currency::get_c_letter( COUNTRIES[m_cur_sel] ) );
}
else
{
first_num.curr_flag = false;
first_num.doub_num = num;
}
intermediate_num.make_null();
}
void CCurrencyCalculatorDlg::OnMultiply()
{
CString str;
if( m_amount.Find( ' ' )!=-1 )
str = remove_c_letter();
else
str = m_amount;
double num = atof( str );
if( num==0 )
return;
last_op = multiplication;
new_num_flag = true;
if( m_curr_or_num==IDC_CURRENCY )
{
first_num.curr_flag = true;
first_num.curr_num.set_currency( num, Currency::get_c_letter( COUNTRIES[m_cur_sel] ) );
CWnd::CheckRadioButton( IDC_CURRENCY, IDC_NUMBER, IDC_NUMBER );
OnNumber(); // check Number, as one operand of multiply should be a Number
}
else
{
first_num.curr_flag = false;
first_num.doub_num = num;
}
intermediate_num.make_null();
}
void CCurrencyCalculatorDlg::OnDivide()
{
CString str;
if( m_amount.Find( ' ' )!=-1 )
str = remove_c_letter();
else
str = m_amount;
double num = atof( str );
if( num==0 )
return;
last_op = division;
new_num_flag = true;
if( m_curr_or_num==IDC_CURRENCY )
{
first_num.curr_flag = true;
first_num.curr_num.set_currency( num, Currency::get_c_letter( COUNTRIES[m_cur_sel] ) );
CWnd::CheckRadioButton( IDC_CURRENCY, IDC_NUMBER, IDC_NUMBER );
OnNumber(); // check Number, as one operand of divide should be a Number
}
else
{
first_num.curr_flag = false;
first_num.doub_num = num;
}
intermediate_num.make_null();
}
void CCurrencyCalculatorDlg::OnEqual()
{
if( last_op==none )
return; // return, if no last operation exists
// check intermediate_num. If it is null, take 2nd operand from result field
if( intermediate_num.is_null() )
{ // convert m_amount into double after removing currency letter if it is currency
CString str;
if( m_amount.Find( ' ' )!=-1 )
str = remove_c_letter();
else
str = m_amount;
double num = atof( str );
if( num==0 )
return; // return, if number is 0
// save it as second number
num_currency second_num;
if( m_curr_or_num==IDC_CURRENCY )
{
second_num.curr_num.set_currency( num,
Currency::get_c_letter( COUNTRIES[m_cur_sel] ) );
second_num.curr_flag = true;
}
else
{
second_num.doub_num = num;
second_num.curr_flag = false;
}
intermediate_num = second_num; // set this to intermediate_num
}
BOOL flag = do_operation( intermediate_num ); // perform the required operation
if( !flag )
{ // if last operation was unsuccessful (due to two multiplication & division of two
// currency amounts, add currency letter as it would be not added by make string()
m_amount = "0 ";
if( m_curr_or_num==IDC_CURRENCY )
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
OnClearall(); // clear all inputs
}
// otherwise, set amount from result and up-date result field
else if( result.curr_flag )
m_amount = result.curr_num.make_string();
else
dbl_to_text( result.doub_num, m_amount );
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
if( result.curr_flag && m_curr_or_num==IDC_NUMBER )
{ // if result is in currency & number radio button is selected, (it will happen during * & / operations)
CWnd::CheckRadioButton( IDC_CURRENCY, IDC_NUMBER, IDC_CURRENCY );
OnCurrency(); // check Currency radio button
}
first_num = result; // make result now the first_num for next equal operation
new_num_flag = true; // turn new number flag ON
}
void CCurrencyCalculatorDlg::OnAbout()
{
CAboutDlg dialog;
dialog.DoModal();
}
void CCurrencyCalculatorDlg::OnExit()
{
CCurrencyCalculatorDlg::OnOK();
}
/////////////////////////////////////////////////////////////////////////////
// CCurrencyCalculatorDlg member functions
CString CCurrencyCalculatorDlg::remove_c_letter()
{ // remove currency letter from m_amount and return new string
CString temp_string = m_amount; // m_amount will remain unchanged
int length = temp_string.GetLength();
int index = temp_string.Find( ' ' );
if( index!=-1 )
temp_string.Delete( index, length-index );
return temp_string;
}
void CCurrencyCalculatorDlg::dbl_to_text( double value, CString &str )
{
int decimal, sign;
str = fcvt( value, PRECISION, &decimal, &sign );
// handle invalid precision
if( value<1.0 && str.GetLength()!=PRECISION )
{
int diff = PRECISION - str.GetLength();
for( int j=0; j<diff; j++ )
str.Insert(0, "0" );
}
// insert "0", "." & "-" at appropriate position
if( str=="" )
str = "0.0";
else
{
str.Insert(decimal, ".");
if( value<1.0 )
str.Insert(0, "0");
if( sign )
str.Insert(0, "-");
}
}
BOOL CCurrencyCalculatorDlg::do_operation( num_currency second_num )
{ // if currencies are different, convert currency of 1st number into currency of 2nd
if( first_num.curr_flag && second_num.curr_flag )
if( first_num.curr_num.get_c_letter()!=second_num.curr_num.get_c_letter() )
first_num.curr_num = Currency::convert_currency( first_num.curr_num,
first_num.curr_num.get_c_letter(), second_num.curr_num.get_c_letter() );
BOOL flag = true; // true flag for operations by default
switch( last_op )
{
case addition:
if( !first_num.curr_flag && !second_num.curr_flag )
{ // both numbers are double (simple double arithematic is used)
result.curr_flag = false;
result.doub_num = first_num.doub_num + second_num.doub_num;
}
else if( !first_num.curr_flag && second_num.curr_flag )
{ // 1st number is double & 2nd is currency (overloaded + operator with
result.curr_flag = true; // double number is used)
result.curr_num = second_num.curr_num + first_num.doub_num;
}
else if( first_num.curr_flag && !second_num.curr_flag )
{ // 1st number is currency & 2nd is double (overloaded + operator with
result.curr_flag = true; // double number is used)
result.curr_num = first_num.curr_num + second_num.doub_num;
}
else
{ // both numbers are currency (overloaded + operator with Currency is used)
result.curr_flag = true;
result.curr_num = first_num.curr_num + second_num.curr_num;
}
break;
case subtraction:
if( !first_num.curr_flag && !second_num.curr_flag )
{
result.curr_flag = false;
result.doub_num = first_num.doub_num - second_num.doub_num;
}
else if( !first_num.curr_flag && second_num.curr_flag )
{
result.curr_flag = true;
double num = first_num.doub_num - second_num.curr_num.get_double();
if( num<0 ) // if result is negative
{
::AfxMessageBox( "Cannot subtract highr amount from lower amount..." );
flag = false; // show error message and set flag false
}
else
result.curr_num.set_currency( num, second_num.curr_num.get_c_letter() );
}
else if( first_num.curr_flag && !second_num.curr_flag )
{
result.curr_flag = true;
result.curr_num = first_num.curr_num - second_num.doub_num;
if( !first_num.curr_num.get_op_flag() ) // if subtraction was not successful
flag = false; // set flag false (overloaded - operator will show error message
}
else
{
result.curr_flag = true;
result.curr_num = first_num.curr_num - second_num.curr_num;
if( !first_num.curr_num.get_op_flag() )
flag = false;
}
break;
case multiplication:
if( !first_num.curr_flag && !second_num.curr_flag )
{
result.curr_flag = false;
result.doub_num = first_num.doub_num * second_num.doub_num;
}
else if( !first_num.curr_flag && second_num.curr_flag )
{
result.curr_flag = true;
result.curr_num = second_num.curr_num * first_num.doub_num;
}
else if( first_num.curr_flag && !second_num.curr_flag )
{
result.curr_flag = true;
result.curr_num = first_num.curr_num * second_num.doub_num;
}
else
{ // multiplication not allowed for two currency amounts
::AfxMessageBox( "Can not multiply two Currency Amounts. One operand should be a number..." );
flag = false;
}
break;
case division:
if( !first_num.curr_flag && !second_num.curr_flag )
{
result.curr_flag = false;
result.doub_num = first_num.doub_num / second_num.doub_num;
}
else if( !first_num.curr_flag && second_num.curr_flag )
{
result.curr_flag = true;
double num = first_num.doub_num / second_num.curr_num.get_double();
result.curr_num.set_currency( num, second_num.curr_num.get_c_letter() );
}
else if( first_num.curr_flag && !second_num.curr_flag )
{
result.curr_flag = true;
result.curr_num = first_num.curr_num / second_num.doub_num;
}
else
{ // division not allowed for two currency amounts
::AfxMessageBox( "Can not divide two Currency Amounts. Divisor should be a number..." );
flag = false;
}
break;
}
return flag;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -