|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
#include<stdlib.h>
class Fs
{
public:
Fs(int x, int y);
Fs();
Fs operator+(Fs x);
Fs operator-(Fs x);
Fs operator*(Fs x);
Fs operator/(Fs x);
void print();
Fs normal();
protected:
int fz;
int fm;
};
Fs::Fs(int x, int y)
{
if (y > 0)
{
fz = x;
fm = y;
}
else
{
if (y < 0)
{
fz = -x;
fm = -y;
}
else
{
std::cout << "分母不能为0!" << std::endl;
}
}
}
void Fs::print()
{
std::cout << fz << "/" << fm;
}
Fs Fs::normal()
{
int a = fz;
int b = fm;
while (a > 0)
{
int t = b % a;
b = a;
a = t;
}
fz /= b;
fm /= b;
return Fs(fz,fm);
}
Fs Fs::operator+(Fs x)
{
Fs z(0,0);
z.fm = fm * x.fm;
z.fz = fz * x.fm + x.fz*fm;
return z;
}
Fs Fs::operator-(Fs x)
{
x.fz = -x.fz;
return Fs::operator+(x);
}
Fs Fs::operator*(Fs x)
{
Fs z(0, 0);
z.fz = fz * x.fz;
z.fm = fm * x.fm;
return z;
}
Fs Fs::operator/(Fs x)
{
int t = x.fz;
x.fz = x.fm;
x.fm = t;
return Fs::operator*(x);
}
int main()
{
Fs a(4, 7);
Fs b(3, 6);
a.normal();
a.print();
std::cout << " + ";
b.normal();
b.print();
std::cout << " = ";
(a + b).normal();
(a + b).print();
std::cout << "\n";
return 0;
}
为什么会输出两次分母不能为0,明明没有一个是0,求解答???
|
|