?? longcal.cpp
字號:
// LongCal.cpp: implementation of the CLongCal class.
//
// created by darnshong in ioe cas.
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "afx.h"
#include "LongCal.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLongCal::CLongCal()
{
this->m_str="0";
this->m_sign='+';
}
CLongCal::CLongCal(CString str)
{
if(str.IsEmpty())
{
this->m_str="0";
this->m_sign='+';
return;
}
else
{
if(str[0]=='+')
{
this->m_sign='+';
this->m_str=str.Right(str.GetLength()-1);
}
else if(str[0]=='-')
{
this->m_sign='-';
this->m_str=str.Right(str.GetLength()-1);
}
else if(isNum(str[0]))
{
this->m_sign='+';
this->m_str=str;
}
else
{
this->m_sign='+';
m_str="0";
return;
}
trimNoNum(m_str);
trimZero(m_str);
m_str.MakeReverse();
}
}
CLongCal::~CLongCal()
{
}
bool CLongCal::isNum(char a)
{
if(a>=48 && a<=57)
return true;
else
return false;
}
void CLongCal::Add(CLongCal c)
{
if(this->m_sign=='+' && c.m_sign=='+')
{
this->m_str=this->aADDb(this->m_str,c.m_str);
this->m_sign='+';
}
else if(this->m_sign=='-' && c.m_sign=='-')
{
this->m_str=this->aADDb(this->m_str,c.m_str);
this->m_sign='-';
}
else if(this->m_sign=='+' &&c.m_sign=='-')
{
int iComp=strValueCompare(this->m_str,c.m_str);
if(iComp==0)
{
this->m_sign='+';
this->m_str="0";
}
else if(iComp==1)
{
this->m_str=aSUBb(this->m_str,c.m_str);
this->m_sign='+';
}
else //iComp=-1
{
this->m_str=aSUBb(c.m_str,this->m_str);
this->m_sign='-';
}
}
else // this->m_sign=='-' &&c.m_sign=='+'
{
int iComp=strValueCompare(this->m_str,c.m_str);
if(iComp==0)
{
this->m_sign='+';
this->m_str="0";
}
else if(iComp==-1)
{
this->m_str=aSUBb(c.m_str,this->m_str);
this->m_sign='+';
}
else //iComp=1
{
this->m_str=aSUBb(this->m_str,c.m_str);
this->m_sign='-';
}
}
}
void CLongCal::swapString(CString &a, CString &b)
{
CString str;
str=a;
a=b;b=str;
}
CString CLongCal::aADDb(CString a, CString b)
{
int tLen=a.GetLength()>=b.GetLength()?a.GetLength():b.GetLength();
int carry=0;
CString temp('0',tLen);
int ia,ib;
for(int i=0;i<tLen;i++)
{
ia=strGetChar(a,i);
ib=strGetChar(b,i);
ia+=ib;
ia+=carry;
if(ia>=10)
{
ia-=10;
carry=1;
}
else
{
carry=0;
}
temp.SetAt(i,ia+'0');
}
if(carry==1)
temp+="1";
return temp;
}
void CLongCal::trimZero(CString &str)
{
int iCount=0;
while( iCount<(str.GetLength()) && str[iCount]=='0' )
iCount++;
str=str.Right(str.GetLength()-iCount);
if(str.IsEmpty())
str="0";
}
void CLongCal::trimNoNum(CString &str)
{
int iCount=0;
while(iCount<str.GetLength() && isNum(str[iCount]))
iCount++;
str=str.Left(iCount);
if(str.IsEmpty())
str="0";
}
CString CLongCal::aSUBb(CString a, CString b)
{
int tLen=a.GetLength()>=b.GetLength()?a.GetLength():b.GetLength();
int carry=0;
CString temp('0',tLen);
int ia,ib;
for(int i=0;i<tLen;i++)
{
ia=strGetChar(a,i);
ib=strGetChar(b,i);
ia-=ib;
ia+=carry;
if(ia<0)
{
ia+=10;
carry=-1;
}
else
{
carry=0;
}
temp.SetAt(i,ia+'0');
}
temp.MakeReverse();
trimZero(temp);
temp.MakeReverse();
return temp;
}
int CLongCal::strValueCompare(CString a, CString b)
{
if(a.Compare(b)==0) return 0;
a.MakeReverse();
b.MakeReverse();
if(a.GetLength()<b.GetLength() || (a.GetLength()==b.GetLength() &&a.Compare(b)==-1))
{
return -1;
}
else
{
return 1;
}
}
void CLongCal::iniUseString(CString str)
{
if(str.IsEmpty())
{
this->m_str="0";
this->m_sign='+';
return;
}
else
{
if(str[0]=='+')
{
this->m_sign='+';
m_str=str.Right(str.GetLength()-1);
}
else if(str[0]=='-')
{
this->m_sign='-';
m_str=str.Right(str.GetLength()-1);
}
else if(isNum(str[0]))
{
this->m_sign='+';
this->m_str=str;
}
else
{
this->m_sign='+';
m_str="0";
return;
}
trimNoNum(m_str);
trimZero(m_str);
m_str.MakeReverse();
}
}
bool CLongCal::isZero()
{
if(this->m_str.Compare("0")==0)
return true;
else
return false;
}
void CLongCal::toString()
{
CString temp=this->m_str;
temp.MakeReverse();
trimZero(temp);
temp.TrimLeft();
if(this->m_sign=='-')
temp="-"+temp;
printf("\n計算結果為:%s",temp.LockBuffer());
temp.ReleaseBuffer();
}
int CLongCal::strGetChar(CString a ,int index)
{
int nLen=a.GetLength();
if(index>nLen-1)
{
return 0;
}
else
{
return a[index]-'0';
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -