用c++写代码复数广泛用于电路科学。利用交流电的正弦性质,赋予电容和电抗复数值的电阻,即阻抗。那么计算过程只涉及复数的四次运算,大大降低了难度。请定义复数的加法、减法、乘法和除法运算符。 编写一个程序来实现 + - * / 运算符重载。主要功能如下: (1)实现两个复数(C1和C2)的加减乘除法。 (2)实现复数C1和整数的加减乘除法。 (3)实现整数和复数C1的加减乘除法。 (4)可以输入和输出 输入格式: 请在此处输入输入格式。例如: 1.0 2.0 3.0 4.0 5 输出格式: 请在此处描述输出格式。例如: c1+c2=(4.00,6.00i) c1-c2=(-2.00,-2.00i) c1*c2=(-5.00,10.00i) c1/c2=(0.44,0.08i) c1+num=(6.00,2.00i) c1-num=(-4.00,2.00i) c1*num=(5.00,10.00i) c1/num=(0.20,0.40i) 数字+C1=(6.00,2.00i) 数字-c1=(4.00,-2.00i) 数字*c1=(5.00,10.00i) 数字/c1=(1.00,-2.00i)
时间: 2023-11-22 08:54:48 浏览: 92
以下是代码实现:
```c++
#include <iostream>
using namespace std;
class Complex {
private:
double real; // 实部
double imag; // 虚部
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex& c) const { // 加法重载
return Complex(real + c.real, imag + c.imag);
}
Complex operator-(const Complex& c) const { // 减法重载
return Complex(real - c.real, imag - c.imag);
}
Complex operator*(const Complex& c) const { // 乘法重载
return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
Complex operator/(const Complex& c) const { // 除法重载
double denominator = c.real * c.real + c.imag * c.imag;
return Complex((real * c.real + imag * c.imag) / denominator, (imag * c.real - real * c.imag) / denominator);
}
Complex operator+(const double num) const { // 复数加整数
return Complex(real + num, imag);
}
Complex operator-(const double num) const { // 复数减整数
return Complex(real - num, imag);
}
Complex operator*(const double num) const { // 复数乘整数
return Complex(real * num, imag * num);
}
Complex operator/(const double num) const { // 复数除整数
return Complex(real / num, imag / num);
}
friend Complex operator+(const double num, const Complex& c) { // 整数加复数
return Complex(c.real + num, c.imag);
}
friend Complex operator-(const double num, const Complex& c) { // 整数减复数
return Complex(num - c.real, -c.imag);
}
friend Complex operator*(const double num, const Complex& c) { // 整数乘复数
return Complex(c.real * num, c.imag * num);
}
friend Complex operator/(const double num, const Complex& c) { // 整数除复数
double denominator = c.real * c.real + c.imag * c.imag;
return Complex(num * c.real / denominator, -num * c.imag / denominator);
}
friend ostream& operator<<(ostream& os, const Complex& c) { // 输出重载
os << "(" << c.real << "," << c.imag << "i)";
return os;
}
friend istream& operator>>(istream& is, Complex& c) { // 输入重载
is >> c.real >> c.imag;
return is;
}
};
int main() {
Complex c1, c2;
double num;
cin >> c1 >> c2 >> num;
Complex c_add = c1 + c2;
Complex c_sub = c1 - c2;
Complex c_mul = c1 * c2;
Complex c_div = c1 / c2;
Complex c_num_add = c1 + num;
Complex c_num_sub = c1 - num;
Complex c_num_mul = c1 * num;
Complex c_num_div = c1 / num;
Complex num_c_add = num + c1;
Complex num_c_sub = num - c1;
Complex num_c_mul = num * c1;
Complex num_c_div = num / c1;
cout << "c1+c2=" << c_add << endl;
cout << "c1-c2=" << c_sub << endl;
cout << "c1*c2=" << c_mul << endl;
cout << "c1/c2=" << c_div << endl;
cout << "c1+num=" << c_num_add << endl;
cout << "c1-num=" << c_num_sub << endl;
cout << "c1*num=" << c_num_mul << endl;
cout << "c1/num=" << c_num_div << endl;
cout << "num+c1=" << num_c_add << endl;
cout << "num-c1=" << num_c_sub << endl;
cout << "num*c1=" << num_c_mul << endl;
cout << "num/c1=" << num_c_div << endl;
return 0;
}
```
输入格式为:两个复数和一个整数,每个数字之间用空格隔开。
输出格式为:每行输出一个运算结果,格式为“运算符左操作数 运算符 右操作数=运算结果”。其中,复数的实部和虚部均保留两位小数,虚部以“i”结尾,实部和虚部之间用逗号隔开。
阅读全文