#include
#include
class Num
{
public:
Num(int a,int b);
Num operator+( const Num& n);
Num operator-( const Num& n);
Num operator*( const Num& n);
Num operator/( const Num& n);
friend ostream&operator<<(ostream&out,Num &n);
private:
int num1;
int num2;
};
//类的实现
Num::Num(int a,int b)
{
if(b==0)
{
cout<<"ERROR"< return;
}
int min=fabs(a)
int x=1;
for(int i=1;i<=min;i++)
if(a%i==0&&b%i==0)
x=i;
a/=x;
b/=x;
if(b<0)
{
a=-a;
b=-b;
}
num1=a;
num2=b;
}
Num Num::operator +(const Num&n)
{
int a,b;
a=num1*n.num2+n.num1*num2;
b=num2*n.num2;
return Num (a,b);
}
Num Num::operator -(const Num&n)
{
int a,b;
a=num1*n.num2-n.num1*num2;
b=num2*n.num2;
return Num(a,b);
}
Num Num::operator *(const Num&n)
{
int a,b;
a=num1*n.num1;
b=num2*n.num2;
return Num(a,b);
}
Num Num::operator /(const Num&n)
{
int a,b;
a=num1*n.num2;
b=num2*n.num1;
return Num(a,b);
}
ostream&operator<<(ostream&out,Num &n)
{
out< return out;
}
//主函数
void main()
{
Num n1(3,-6),n2(8,10);
cout<<"n1="<cout<<"n2="<cout<<"n1+n2="<cout<<"n1-n2="<cout<<"n1*n2="<cout<<"n1/n2="<}
--------------------next---------------------
阅读(1682) | 评论(0) | 转发(0) |