#include<iostream>
using namespace std;
class F{
int n;
int d;
private:
void reduce()
{
int mcd = maxcd(n<0?-n:n,d);
if(mcd != 1) n/=mcd,d/=mcd;
}
public:
static int maxcd(int a,int b)//静态成员函数 工具函数 不依赖于某个对象
{//没有当前对象 没有this
if(0 == a) return b;
else{
return maxcd(b%a,a);
}
}
F(int n = 0,int d = 1):n(n),d(d){}
friend ostream& operator<<(ostream& o,const F& f)
{
o<<f.n<<'/'<<f.d;
return o;
}
F& operator++(){n+=d;return *this;}//前++
F operator++(int){F old(*this);n += d;return old;}//后++带int参数 为哑元函数
friend F& operator--(F& f){f.n -= f.d;return f;}
friend F operator--(F& f,int){F old(f);f.n -= f.d;return old;}
operator double(){return 1.0*n/d;}
operator bool(){return n != 0;}
};
void fun(F a){cout<<a<<endl;}
int main()
{
F f1(2,5),f2(4,9),f3(17,3);
cout<<++f1<<endl;//f1.operator++();
cout<<f2++<<endl;//f2.operator++(0);
cout<<"f1 = "<<f1<<",f2 = "<<f2<<endl;
cout<<--f3<<endl;//operator--(f3)
cout<<f3--<<endl;//operator--(f3,0)
cout<<"f3 = "<<f3<<endl;
cout<<double(f1)<<endl;//f1.operator double();
cout<<boolalpha<<(bool)f3<<endl;//f3.operator bool();
cout<<F(5)<<endl;
cout<<(F)3<<endl;
fun(7);
}
using namespace std;
class F{
int n;
int d;
private:
void reduce()
{
int mcd = maxcd(n<0?-n:n,d);
if(mcd != 1) n/=mcd,d/=mcd;
}
public:
static int maxcd(int a,int b)//静态成员函数 工具函数 不依赖于某个对象
{//没有当前对象 没有this
if(0 == a) return b;
else{
return maxcd(b%a,a);
}
}
F(int n = 0,int d = 1):n(n),d(d){}
friend ostream& operator<<(ostream& o,const F& f)
{
o<<f.n<<'/'<<f.d;
return o;
}
F& operator++(){n+=d;return *this;}//前++
F operator++(int){F old(*this);n += d;return old;}//后++带int参数 为哑元函数
friend F& operator--(F& f){f.n -= f.d;return f;}
friend F operator--(F& f,int){F old(f);f.n -= f.d;return old;}
operator double(){return 1.0*n/d;}
operator bool(){return n != 0;}
};
void fun(F a){cout<<a<<endl;}
int main()
{
F f1(2,5),f2(4,9),f3(17,3);
cout<<++f1<<endl;//f1.operator++();
cout<<f2++<<endl;//f2.operator++(0);
cout<<"f1 = "<<f1<<",f2 = "<<f2<<endl;
cout<<--f3<<endl;//operator--(f3)
cout<<f3--<<endl;//operator--(f3,0)
cout<<"f3 = "<<f3<<endl;
cout<<double(f1)<<endl;//f1.operator double();
cout<<boolalpha<<(bool)f3<<endl;//f3.operator bool();
cout<<F(5)<<endl;
cout<<(F)3<<endl;
fun(7);
}