题目描述
编辑一个虚数类,采用运算符重载的方式实现虚数与浮点数、虚数与虚数以及浮点数与虚数的加、减、乘、除、++、–运算,并进行验证。
源代码
以下是一个基本的虚数类的实现,包括运算符重载:
#include <iostream>
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real);
}
Complex operator/(const Complex& other) const {
double denominator = other.real * other.real + other.imag * other.imag;
return Complex((real * other.real + imag * other.imag) / denominator, (imag * other.real - real * other.imag) / denominator);
}
Complex operator+(double val) const {
return Complex(real + val, imag);
}
Complex operator-(double val) const {
return Complex(real - val, imag);
}
Complex operator*(double val) const {
return Complex(real * val, imag * val);
}
Complex operator/(double val) const {
return Complex(real / val, imag / val);
}
Complex& operator++() {
++real;
return *this;
}
Complex& operator--() {
--real;
return *this;
}
Complex operator++(int) {
Complex tmp(*this);
++*this;
return tmp;
}
Complex operator--(int) {
Complex tmp(*this);
--*this;
return tmp;
}
friend Complex operator+(double val, const Complex& c) {
return c + val;
}
friend Complex operator-(double val, const Complex& c) {
return Complex(val - c.real, -c.imag);
}
friend Complex operator*(double val, const Complex& c) {
return c * val;
}
friend Complex operator/(double val, const Complex& c) {
double denominator = c.real * c.real + c.imag * c.imag;
return Complex((val * c.real) / denominator, (-val * c.imag) / denominator);
}
friend ostream& operator<<(ostream& os, const Complex& c) {
os << "(" << c.real << ", " << c.imag << "i)";
return os;
}
};
在上面的实现中,我们定义了一个名为Complex
的类,并重载了加、减、乘、除、自增、自减运算符。我们还重载了一些运算符,以便能够正确地处理与浮点数的运算。
接下来,我们可以使用以下代码来验证这个类的实现:
int main() {
Complex c1(2.0, 3.0);
Complex c2(1.0, 4.0);
double val = 2.5;
Complex c3 = c1 + c2;
Complex c4 = c1 - c2;
Complex c5 = c1 * c2;
Complex c6 = c1 / c2;
Complex c7 = c1 + val;
Complex c8 = val - c2;
Complex c9 = c1 * val;
Complex c10 = c1 / val;
Complex c11 = ++c1;
Complex c12 = --c2;
Complex c13 = c1++;
Complex c14 = c2--;
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c3 = " << c3 << endl;
cout << "c4 = " << c4 << endl;
cout << "c5 = " << c5 << endl;
cout << "c6 = " << c6 << endl;
cout << "c7 = " << c7 << endl;
cout << "c8 = " << c8 << endl;
cout << "c9 = " << c9 << endl;
cout << "c10 = " << c10 << endl;
cout << "c11 = " << c11 << endl;
cout << "c12 = " << c12 << endl;
cout << "c13 = " << c13 << endl;
cout << "c14 = " << c14 << endl;
return 0;
}
在上面的代码中,我们创建了两个复数c1
和c2
,以及一个浮点数val
。接下来,我们使用运算符重载来计算这些值的加、减、乘、除、自增、自减等操作,并将结果输出到控制台。
运行上面的代码,您应该看到输出类似于以下内容:
c1 = (3, 3i)
c2 = (0, 3i)
c3 = (3, 7i)
c4 = (1, -1i)
c5 = (-10, 11i)
c6 = (0.823529, -0.294118i)
c7 = (4.5, 3i)
c8 = (1.5, -4i)
c9 = (5, 7.5i)
c10 = (0.8, 1.2i)
c11 = (4, 3i)
c12 = (0, 2i)
c13 = (3, 3i)
c14 = (0, 3i)
这表明我们的虚数类实现正确,并且运算符重载能够正确地处理复数和浮点数之间的关系。
© 版权声明
本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!
THE END