📄 tvalue.h
字号:
//---------------------------------------------------------------------------
#ifndef TValueH
#define TValueH
//---------------------------------------------------------------------------
class TValue
{
public:
int mFZ;
int mFM;
protected:
void YueFen(void){
int lFZ = mFZ>0 ? mFZ: -mFZ;
int lFM = mFM;
for(int i=2 ; i<=lFM && i<=lFZ ; i++){
if( (lFM%i != 0) || (lFZ%i !=0))
continue;
mFM/=i;
mFZ/=i;
lFZ = mFZ>0 ? mFZ: -mFZ;
lFM = mFM;
i--;
}
}
public:
TValue(void){}
TValue(const int aI){
mFZ=aI;
mFM=1;
}
TValue(const AnsiString &aS){
unsigned int lPos;
lPos=aS.Pos("/");
if(lPos>0){
mFZ=StrToInt(aS.SubString(1,lPos-1));
mFM=StrToInt(aS.SubString(lPos+1,aS.Length()-lPos));
}else{
mFZ=StrToInt(aS);
mFM=1;
}
if(mFM<0){
mFZ=-mFZ;
mFM=-mFM;
}
YueFen();
}
operator AnsiString(){
AnsiString lStrResult;
if(mFZ==0){
return "0";
}else if(mFM==1){
return IntToStr(mFZ);
}else{
return IntToStr(mFZ)+"/"+IntToStr(mFM);
}
}
bool operator>(TValue aAno){
if( mFZ/(float)mFM > aAno.mFZ/(float)aAno.mFM )
return true;
return false;
}
bool operator<(TValue aAno){
if( mFZ/(float)mFM < aAno.mFZ/(float)aAno.mFM )
return true;
return false;
}
bool operator<=(TValue aAno){
return !(*this>aAno);
}
bool operator>=(TValue aAno){
return !(*this<aAno);
}
bool operator==(TValue aAno){
if(mFZ==0 && aAno.mFZ==0)
return true;
return (mFZ==aAno.mFZ && mFM==aAno.mFM);
}
bool operator!=(TValue aAno){
return !(*this==aAno);
}
TValue operator-(void){
TValue lResult;
lResult.mFZ=-mFZ;
lResult.mFM=mFM;
return lResult;
}
TValue operator+(const TValue &aAno){
TValue lResult;
lResult.mFM = mFM*aAno.mFM;
lResult.mFZ = mFZ*aAno.mFM + aAno.mFZ*mFM;
lResult.YueFen();
return lResult;
}
TValue operator-(TValue aAno){
TValue lResult;
lResult=*this+(-aAno);
return lResult;
}
TValue operator*(TValue aAno){
TValue lResult;
lResult.mFZ=mFZ*aAno.mFZ;
lResult.mFM=mFM*aAno.mFM;
lResult.YueFen();
return lResult;
}
TValue operator/(TValue aAno){
TValue lResult;
lResult.mFZ=mFZ*aAno.mFM;
lResult.mFM=mFM*aAno.mFZ;
if(lResult.mFM<0){
lResult.mFZ=-lResult.mFZ;
lResult.mFM=-lResult.mFM;
}
lResult.YueFen();
return lResult;
}
bool operator>(int aI){
return *this>TValue(aI);
}
bool operator<(int aI){
return *this<TValue(aI);
}
bool operator>=(int aI){
return *this>=TValue(aI);
}
bool operator<=(int aI){
return *this<=TValue(aI);
}
bool operator==(int aI){
return *this==TValue(aI);
}
bool operator!=(int aI){
return *this!=TValue(aI);
}
TValue operator-(int aI){
return *this-TValue(aI);
}
TValue operator+(int aI){
return *this+TValue(aI);
}
TValue operator*(int aI){
return *this+TValue(aI);
}
friend TValue operator-(int aI,TValue aMe){
return TValue(aI)-aMe;
}
friend TValue operator+(int aI,TValue aMe){
return TValue(aI)+aMe;
}
friend TValue operator*(int aI,TValue aMe){
return TValue(aI)*aMe;
}
friend TValue operator/(int aI,TValue aMe){
return TValue(aI)/aMe;
}
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -