1.The complex number is widely used in circuit science. By using the sinusoidal nature of alternating current, the capacitance and reactance are given the resistance of the complex value, that is, the impedance. Then the calculation process only involves the four operations of the complex number, which greatly reduces the difficulty. Please define addition, subtraction, multiplication and division operator of complex. Write a program to realize + - * / operator overloading. The main functions are as follows: (1) Realize the addition, subtraction, multiplication and division of two complex numbers (C1 and C2). (2) Realize the addition, subtraction, multiplication and division of complex number C1 and integer num. (3) Realize the addition, subtraction, multiplication and division of integer number and complex number C1. Input format: Please write the input format here. For example: 1.0 2.0 3.0 4.0 5 Output format: Please describe the output format here. For example: 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) num+c1=(6.00,2.00i) num-c1=(4.00,-2.00i) num*c1=(5.00,10.00i) num/c1=(1.00,-2.00i)
时间: 2023-12-13 17:02:29 浏览: 63
backstepping.rar_The Show_backstepping _backstepping boost_simul
以下是C++程序,实现了复数的加减乘除运算,以及复数和整数之间的加减乘除运算:
```c++
#include<iostream>
using namespace std;
class Complex{
public:
double real, imag;
Complex(){}
Complex(double a, double b):real(a), imag(b){}
Complex operator+(const Complex &c){
return Complex(real+c.real, imag+c.imag);
}
Complex operator-(const Complex &c){
return Complex(real-c.real, imag-c.imag);
}
Complex operator*(const Complex &c){
return Complex(real*c.real-imag*c.imag, real*c.imag+imag*c.real);
}
Complex operator/(const Complex &c){
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);
}
friend Complex operator+(const Complex &c, int num){
return Complex(c.real+num, c.imag);
}
friend Complex operator-(const Complex &c, int num){
return Complex(c.real-num, c.imag);
}
friend Complex operator*(const Complex &c, int num){
return Complex(c.real*num, c.imag*num);
}
friend Complex operator/(const Complex &c, int num){
return Complex(c.real/num, c.imag/num);
}
friend Complex operator+(int num, const Complex &c){
return Complex(c.real+num, c.imag);
}
friend Complex operator-(int num, const Complex &c){
return Complex(num-c.real, -c.imag);
}
friend Complex operator*(int num, const Complex &c){
return Complex(c.real*num, c.imag*num);
}
friend Complex operator/(int num, const Complex &c){
double denominator = c.real*c.real+c.imag*c.imag;
return Complex((num*c.real)/denominator, -(num*c.imag)/denominator);
}
};
int main(){
double r1, i1, r2, i2;
int num;
cin >> r1 >> i1 >> r2 >> i2 >> num;
Complex c1(r1, i1), c2(r2, i2);
Complex res = c1+c2;
printf("c1+c2=(%.2f,%.2fi)\n", res.real, res.imag);
res = c1-c2;
printf("c1-c2=(%.2f,%.2fi)\n", res.real, res.imag);
res = c1*c2;
printf("c1*c2=(%.2f,%.2fi)\n", res.real, res.imag);
res = c1/c2;
printf("c1/c2=(%.2f,%.2fi)\n", res.real, res.imag);
res = c1+num;
printf("c1+num=(%.2f,%.2fi)\n", res.real, res.imag);
res = c1-num;
printf("c1-num=(%.2f,%.2fi)\n", res.real, res.imag);
res = c1*num;
printf("c1*num=(%.2f,%.2fi)\n", res.real, res.imag);
res = c1/num;
printf("c1/num=(%.2f,%.2fi)\n", res.real, res.imag);
res = num+c1;
printf("num+c1=(%.2f,%.2fi)\n", res.real, res.imag);
res = num-c1;
printf("num-c1=(%.2f,%.2fi)\n", res.real, res.imag);
res = num*c1;
printf("num*c1=(%.2f,%.2fi)\n", res.real, res.imag);
res = num/c1;
printf("num/c1=(%.2f,%.2fi)\n", res.real, res.imag);
return 0;
}
```
输入格式为两个复数的实部和虚部,以及一个整数,以空格分隔。例如:1.0 2.0 3.0 4.0 5
输出格式为各种运算结果,每个结果都占一行,格式为:运算符号=(实部,虚部i)。例如:c1+c2=(4.00,6.00i)
输出结果示例:
```
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)
num+c1=(6.00,2.00i)
num-c1=(4.00,-2.00i)
num*c1=(5.00,10.00i)
num/c1=(1.00,-2.00i)
```
阅读全文