|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
class js
{
// friend std::ostream& operator<<(std::ostream& a,js f); //这一句注释掉和不注释掉的区别 为什么注释掉会错误
js fsjh(js &abc)//分数简化
{
int t,x,y;
if(abc.i<0)
{
x=-abc.i;
}
else
{
x=abc.i;
}
if(abc.j<0)
{
y=-abc.j;
}else
{
y=abc.j;
}
while(x>0)
{
t=y%x;
y=x;
x=t;
}
abc.i/=y;
abc.j/=y;
return abc;
};
public:
int a,b;//加减 前后
int i,j;//分子 分母
js(int i,int j)
{
if(j<0)
{
this->i=-i;
this->j=-j;
}else
{
this->i=i;
this->j=j;
}
};
js(js &abc)
{
this->i=abc.i;
this->j=abc.j;
};
js()
{
i=1;
j=1;
};
js operator*(js &abc)
{
js c;
c.i=abc.i*i;
c.j=abc.j*j;
return fsjh(c);
}
js operator/(js &abc)
{
js c;
c.i=i*abc.j;
c.j=j*abc.i;
return fsjh(c);
}
js operator-(js &b)
{
b.i=-b.i;
return operator+(b);
}
js operator+(js &b)
{
js c;
c.i=i*b.j+b.i*j;//例如 a/b+c/d (a*d+c*b)/(d*b)
c.j=j*b.j;
return fsjh(c);
};
void dy()
{
printf("%d/%d",i,j);
};
};
std::ostream& operator<<(std::ofstream& a,js f);
int main()
{
js a,b(1,-3),c(1,5);
a=b+c;
char jj='+';
if(c.i<0)
{
c.i=-c.i;
jj='-';
}
std::cout<<b<<" + "<<c<<" = "<<b+c<<"\n";
return 0;
}
std::ostream& operator<<(std::ostream& a,js f)
{
a<<f.i<<"/"<<f.j;
return a;
}
|
|