专栏文章
封装分数类
个人记录参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mioxq3oi
- 此快照首次捕获于
- 2025/12/03 02:52 3 个月前
- 此快照最后确认于
- 2025/12/03 02:52 3 个月前
CPP
namespace std{
template<class T=int>class frac{
private:
T Numerator=0,Denominator=1;
T gcd(T a,T b){return (!b)?a:gcd(b,a%b);}
void simplify(){
if(Denominator==0)
throw std::runtime_error("Denominator cannot be zero");
if(Numerator==0){
Denominator=1;
return;
}
if(Denominator<0){
Numerator=-Numerator;
Denominator=-Denominator;
}
int common_divisor=gcd(std::abs(Numerator),Denominator);
Numerator/=common_divisor;
Denominator/=common_divisor;
}
public:
frac(int num=0,int denom=1):Numerator(num),
Denominator(denom){simplify();}
T numerator(){return Numerator;}
T denominator(){return Denominator;}
frac operator+(const frac&other)const{
T new_num=Numerator*other.denominator()+other.numerator()*Denominator;
T new_denom=Denominator*other.denominator();
return frac(new_num,new_denom);
}
frac operator-(const frac&other)const{
T new_num=Numerator*other.denominator()-other.numerator()*Denominator;
T new_denom=Denominator*other.denominator();
return frac(new_num,new_denom);
}
frac operator*(const frac&other)const{
int new_num=Numerator*other.numerator();
int new_denom=Denominator*other.denominator();
return frac(new_num,new_denom);
}
frac operator/(const frac&other)const{
if(other.numerator()==0)
throw std::runtime_error("Cannot divide by zero");
int new_num=Numerator*other.denominator();
int new_denom=Denominator*other.numerator();
return frac(new_num,new_denom);
}
frac operator+=(const frac&other){return *this=*this+other;}
frac operator-=(const frac&other){return *this=*this-other;}
frac operator*=(const frac&other){return *this=*this*other;}
frac operator/=(const frac&other){return *this=*this/other;}
frac abs()const{return frac(std::abs(Numerator),Denominator);}
frac operator-()const{return frac(-Numerator,Denominator);}
bool operator==(const frac&other)const{return Numerator==other.numerator()&&Denominator==other.denominator();}
bool operator!=(const frac&other)const{return !(*this==other);}
bool operator< (const frac&other)const{return Numerator*other.denominator()<other.numerator()*Denominator;}
bool operator> (const frac&other)const{return Numerator*other.denominator()>other.numerator()*Denominator;}
bool operator<=(const frac&other)const{return !(*this>other);}
bool operator>=(const frac&other)const{return !(*this<other);}
double toDouble()const{return static_cast<double>(Numerator)/Denominator;}
T toInt()const{return Numerator/Denominator;}
friend ostream& operator<<(ostream&os,const frac&fraction){
if(fraction.Denominator==1)os<<fraction.Numerator;
else os<<fraction.Numerator<<"/"<<fraction.Denominator;
return os;
}
friend istream& operator>>(istream&is,frac&fraction){
char slash;
is>>fraction.Numerator>>slash>>fraction.Denominator;
if(slash!='/'){
is.setstate(ios::failbit);
fraction=frac();
}
else fraction.simplify();
return is;
}
};
}
可搭配
__intinf 使用相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...