分别输入2个复数的实部与虚部,用函数实现计算2个复数之和与之积。 若2个复数分别为:c1=x1+(y1)i, c2=x2+(y2)i, 则: c1+c2 = (x1+x2) + (y1+y2)i c1*c2 = (x1*x2-y1*y2) + (x1*y2+x2*y1)i 输入格式: 输入在一行中给出4个实数,分别代表c1和c2的实部和虚部。 输出格式: 在两行中分别输出c1+c2和c1*c2的计算结果。 输入样例: 1 1 -2 3 输出样例: addition of complex is -1.000000+4.000000i product of complex is -5.000000+1.000000i
时间: 2024-02-09 18:08:03 浏览: 180
这是1.定义一个复数类 complex ,它的内部具有两个实例变量:realPart和imagPart,分别代表复数的实部和虚
5星 · 资源好评率100%
以下是实现该功能的 Python 代码:
```python
def complex_addition(x1, y1, x2, y2):
# 计算两个复数的和
real_part = x1 + x2
imaginary_part = y1 + y2
return (real_part, imaginary_part)
def complex_multiplication(x1, y1, x2, y2):
# 计算两个复数的积
real_part = x1 * x2 - y1 * y2
imaginary_part = x1 * y2 + x2 * y1
return (real_part, imaginary_part)
# 输入两个复数的实部和虚部
x1, y1, x2, y2 = map(float, input().split())
# 计算两个复数的和
addition_real, addition_imaginary = complex_addition(x1, y1, x2, y2)
print("addition of complex is {:.6f}+{:.6f}i".format(addition_real, addition_imaginary))
# 计算两个复数的积
multiplication_real, multiplication_imaginary = complex_multiplication(x1, y1, x2, y2)
print("product of complex is {:.6f}+{:.6f}i".format(multiplication_real, multiplication_imaginary))
```
输入样例:
```
1 1 -2 3
```
输出样例:
```
addition of complex is -1.000000+4.000000i
product of complex is -5.000000+1.000000i
```
阅读全文