C++快速入门P26运算符重载2--有理数课后题
#include <iostream>#include <string>
#include <math.h>
class Complex
{
public:
Complex();//构造器
Complex(int a,int b);//函数重载
virtual ~Complex();
Complex operator + (Complex &d);// +重载
Complex operator - (Complex &d);// -重载
Complex operator * (Complex &d);// *重载
Complex operator / (Complex &d);// /重载
Complex Simplify(Complex &temp);//分数化简
void Print();
private:
int num;//分子
int den;//分母
};
Complex::Complex()
{
num = 0;
den = 0;
std::cout << "Complex 构造器++\n";
}
Complex::Complex(int r,int i)
{
num = r;
den = i;
}
Complex::~Complex()
{
std::cout << "Complex 析构器--\n";
}
//打印
void Complex::Print()
{
if(num == 0 || den == 0)
std::cout <<"0\n";
else if(num == den)
std::cout <<"1\n";
else
std::cout << num << "/" << den << "\n";
}
//加法
Complex Complex::operator + (Complex &d)
{
Complex c;
if(d.den < 0)
{
d.num = -d.num;
d.den = -d.den;
}
c.num = num * d.den + d.num * den;
c.den = den * d.den;
return c;
}
//减法
Complex Complex::operator - (Complex &d)
{
Complex c;
if(d.den < 0)
{
d.num = -d.num;
d.den = -d.den;
}
c.num = num * d.den - d.num * den;
c.den = den * d.den;
return c;
}
//乘法
Complex Complex::operator * (Complex &d)
{
Complex c;
c.num = num * d.num;
c.den = den * d.den;
return c;
}
//除法
Complex Complex::operator / (Complex &d)
{
Complex c;
c.num = num * d.den;
c.den = den * d.num;
return c;
}
//分数化简
Complex Complex::Simplify(Complex &temp)
{
Complex result;
if(temp.den == 0 || temp.num == 0) //分子分母为0的情况
{
result.num = 0;
result.den = 0;
return result;
}
int c = abs(temp.num);//取绝对值
int d = abs(temp.den);
int x = c < d ? c : d;//c,d的最大的公约数永远不可能是较大的那个数,只可能是较小的数
for(x; x >= 1; x--)
{
if(c % x == 0 && d % x == 0) //x正好被c,d整除,则找到了x
{
break;
}
}
result.num = temp.num / x;
result.den = temp.den / x;
if(result.den < 0)//分母带负号处理
{
result.num = -result.num;
result.den = -result.den;
}
returnresult;
}
int main()
{
Complex a(1,2), b(0,4), c, d, e, f;
//计算
c = a + b;
d = a - b;
e = a * b;
f = a / b;
//化简
c = c.Simplify(c);
d = d.Simplify(d);
e = e.Simplify(e);
f = f.Simplify(f);
//打印
std::cout << "a:";
a.Print();
std::cout << "b:";
b.Print();
std::cout << "c:";
c.Print();
std::cout << "d:";
d.Print();
std::cout << "e:";
e.Print();
std::cout << "f:";
f.Print();
return 0;
} 您的代码中有一些问题。首先,构造函数应该初始化num和den为0,而不是在构造函数体内赋值为0。其次,函数重载的构造函数应该有一个参数列表,而不是空参数列表。最后,在运算符重载函数中,参数应该是常量引用,以避免不必要的拷贝。
另外,您的代码中定义了一个Simplify函数,但是没有实现它。如果您想使用这个函数来简化有理数的分数形式,您需要补充这个函数的实现。
下面是修正后的代码:
#include <iostream>
class Complex
{
public:
Complex();
Complex(int a, int b);
virtual ~Complex();
Complex operator+(const Complex& d);
Complex operator-(const Complex& d);
Complex operator*(const Complex& d);
Complex operator/(const Complex& d);
void Print();
private:
int num;
int den;
};
Complex::Complex()
{
num = 0;
den = 0;
}
Complex::Complex(int a, int b)
{
num = a;
den = b;
}
Complex::~Complex()
{
}
Complex Complex::operator+(const Complex& d)
{
Complex temp;
temp.num = num * d.den + d.num * den;
temp.den = den * d.den;
return temp;
}
Complex Complex::operator-(const Complex& d)
{
Complex temp;
temp.num = num * d.den - d.num * den;
temp.den = den * d.den;
return temp;
}
Complex Complex::operator*(const Complex& d)
{
Complex temp;
temp.num = num * d.num;
temp.den = den * d.den;
return temp;
}
Complex Complex::operator/(const Complex& d)
{
Complex temp;
temp.num = num * d.den;
temp.den = den * d.num;
return temp;
}
void Complex::Print()
{
std::cout << num << "/" << den << std::endl;
}
这样,您就可以使用Complex类来进行有理数的运算了。例如:
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
c3.Print();// 输出:10/8
Complex c4 = c1 - c2;
c4.Print();// 输出:-2/8
Complex c5 = c1 * c2;
c5.Print();// 输出:3/8
Complex c6 = c1 / c2;
c6.Print();// 输出:4/6
return 0;
}
希望能帮助到您!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]