#include
using namespace std;
class zrf_Ratio
{
public:
zrf_Ratio(){}
zrf_Ratio(int r1,int r2):num(r1),den(r2){}
zrf_Ratio f1(zrf_Ratio& z)
{
int r,m=z.num,n=z.den;
while(r=z.num%z.den)
{
z.num=z.den;
z.den=r;
}
r=z.den;
z.num=m/r;
z.den=n/r;
return z;
}
friend ostream& operator<<(ostream& ostr, const zrf_Ratio& r)
{ return ostr << r.num << "/" << r.den;}
zrf_Ratio& operator=(const zrf_Ratio& z1)
{
zrf_Ratio a;
return a.f1(zrf_Ratio(num+z1.num,den+z1.den));
}
zrf_Ratio& operator*=(const zrf_Ratio& z2)
{
zrf_Ratio a;
return a.f1(zrf_Ratio(z2.num*z2.den,z2.den+z2.den));
}
zrf_Ratio& operator++()
{
num++;
den++;
return *this;
}
zrf_Ratio operator++(int)
{
zrf_Ratio a;
a=*this;
++ * this;
return a;
}
private:
int num,den;
};
int main()
{
zrf_Ratio x(2,7),y(3,5),z;
z=x.operator=(y);
cout<<"x=(y)="<<z<<endl;
z=x*=(y);
cout<<"x*=(y)="<<z<<endl;
z++;
cout<<"z="<<z<<endl;
++z;
cout<<"z="<<z<<endl;
return 0;
}
運行亂碼,是因為你代碼訪問的字符等編碼是不是沒有處理好