编写一个名为“复数”的类,包含复数的实部和虚部(数据成员),以及复数之间的基本算术运算:加、减(方法成员),并要求复数加减运算,程序运行中能对给定的复数进行运算,并输出运算的结果。
时间: 2023-05-27 21:04:58 浏览: 83
以下是Python代码实现:
class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def __add__(self, other):
return Complex(self.real + other.real, self.imag + other.imag)
def __sub__(self, other):
return Complex(self.real - other.real, self.imag - other.imag)
def __str__(self):
if self.imag >= 0:
return f"{self.real}+{self.imag}i"
else:
return f"{self.real}{self.imag}i"
# 测试代码
c1 = Complex(1, 2)
c2 = Complex(3, -4)
print(c1 + c2)
print(c1 - c2)
# 输出结果
# 4-2i
# -2+6i
相关问题
编写一个名为“复数”的类,包含复数的实部和虚部(数据成员),以及复数之间的基本算术运算:加、减(方法成员),并要求复数加减运算,程序运行中能对给定的复数进行运算,并打印运算的结果。
下面是一个Java中复数类的实现,包含复数的实部和虚部,以及复数之间的基本算术运算:
```java
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber other) {
double realPart = this.real + other.real;
double imaginaryPart = this.imaginary + other.imaginary;
return new ComplexNumber(realPart, imaginaryPart);
}
public ComplexNumber subtract(ComplexNumber other) {
double realPart = this.real - other.real;
double imaginaryPart = this.imaginary - other.imaginary;
return new ComplexNumber(realPart, imaginaryPart);
}
public String toString() {
return "(" + this.real + " + " + this.imaginary + "i)";
}
public static void main(String[] args) {
ComplexNumber c1 = new ComplexNumber(2, 3);
ComplexNumber c2 = new ComplexNumber(4, -5);
ComplexNumber sum = c1.add(c2);
ComplexNumber difference = c1.subtract(c2);
System.out.println(c1 + " + " + c2 + " = " + sum);
System.out.println(c1 + " - " + c2 + " = " + difference);
}
}
```
在这个示例中,我们首先定义了一个名为ComplexNumber的类,其中包含实部和虚部作为数据成员,并且实现了add和subtract方法来实现复数的加法和减法。我们还实现了toString方法,以便在输出时能够更好地理解复数的值。
在main方法中,我们创建了两个ComplexNumber对象c1和c2,并使用它们调用add和subtract方法。然后我们打印了每个运算的结果,以便能够查看它们是否正确。
java,编写一个名为“复数”的类,包含复数的实部和虚部(数据成员),以及复数之间的基本算术运算:加、减(方法成员),并要求复数加减运算,程序运行中能对给定的复数进行运算,并输出运算的结果。
```
public class Complex {
private double realPart; //实部
private double imaginaryPart; //虚部
public Complex(double realPart, double imaginaryPart) { //构造方法
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
public Complex add(Complex other) { //加法
double r = this.realPart + other.realPart;
double i = this.imaginaryPart + other.imaginaryPart;
return new Complex(r, i);
}
public Complex subtract(Complex other) { //减法
double r = this.realPart - other.realPart;
double i = this.imaginaryPart - other.imaginaryPart;
return new Complex(r, i);
}
public String toString() { //输出结果
if (this.imaginaryPart < 0) {
return this.realPart + "-" + (-this.imaginaryPart) + "i";
} else {
return this.realPart + "+" + this.imaginaryPart + "i";
}
}
}
```
测试:
```
public class Test {
public static void main(String[] args) {
Complex a = new Complex(3, 2);
Complex b = new Complex(1, -1);
Complex c = a.add(b);
Complex d = a.subtract(b);
System.out.println(a + " + " + b + " = " + c);
System.out.println(a + " - " + b + " = " + d);
}
}
```
输出:
```
3.0+2.0i + 1.0-1.0i = 4.0+1.0i
3.0+2.0i - 1.0-1.0i = 2.0+3.0i
```
阅读全文