#include<iostream.h>
#include<iomanip.h>
class fraction
{
public:
	fraction(int x=1,int y=1);
	fraction(fraction &p);
	~fraction();
	int geta(void);
	int getb(void);
	void display(void);
    fraction& add(fraction& c);
    fraction& sub(fraction& c);
    fraction& mul(fraction& c);
    fraction& div(fraction& c);
private:
    int gcd(int a,int b);
	int a;
	int b;
    void sim(void);
};
fraction::fraction(int x,int y)
{
	if(y==0)
	 b=1;
	else
	 b=y;
	a=x;
    cout<<"构造函数被调用"<<endl;
}
fraction::fraction(fraction &p)
{
    a=p.a;
	b=p.b;
    cout<<"拷贝构造函数被调用"<<endl;
}
fraction::~fraction()
{
	cout<<"析构函数被调用"<<endl;
}
int fraction::geta(void)
{
	return(a);
}
int fraction::getb(void)
{
	return(b);
}
void fraction:: display(void)
{
	cout<<a<<"/"<<b<<endl;
}
fraction& fraction::add(fraction& c)
{
	fraction temp;
	temp.b=b*c.b;
	temp.a=a*c.b+b*c.a;
    temp.sim();
    return(temp);
}
fraction& fraction::sub(fraction& c)
{
	fraction temp;
	temp.b=b*c.b;
	temp.a=a*c.b-b*c.a;
    temp.sim();
	return(temp);
}
fraction& fraction::mul(fraction& c)
{
	fraction temp;
	temp.b=b*c.b;
	temp.a=a*c.a;
    temp.sim();
	return(temp);
}
fraction& fraction::div(fraction& c)
{
	fraction temp;
	temp.b=b*c.a;
	temp.a=a*c.b;
    temp.sim();
	return(temp);
}
void fraction::sim(void)
{
	int c;
	c=gcd(a,b);
    a=a/c;
	b=b/c;
}
int fraction::gcd(int a,int b)
{
	int c;
	if(a%b==0)
      c=b;
	else
      c=gcd(b,a%b);
	return(c);
}
int main()
{
	void set(fraction& c);
    fraction x(3,4),y(1,2),z;
	z=x.add(y);
	z.display();
	set(z);
	z=x.sub(y);
    z.display();
	set(z);
	z=x.mul(y);
    z.display();
	set(z);
	z=x.div(y);
    z.display();
	set(z);
	return(0);
}
void set(fraction& c)
{
	cout<<"result is "<<setprecision(3)<<(float)(c.geta())/c.getb()<<endl;
}
