Polymorphism
class ComplexNumber:def __init__(self, real, imag):
self.real = real
self.imag = imag
def __add__(self, other):
return ComplexNumber(self.real + other.real, self.imag + other.imag)
def __sub__(self, other):
return ComplexNumber(self.real - other.real, self.imag - other.imag)
def __mul__(self, other):
real_part = self.real * other.real - self.imag * other.imag
imag_part = self.real * other.imag + self.imag * other.real
return ComplexNumber(real_part, imag_part)
def __truediv__(self, other):
denominator = other.real**2 + other.imag**2
real_part = (self.real * other.real + self.imag * other.imag) / denominator
imag_part = (self.imag * other.real - self.real * other.imag) / denominator
return ComplexNumber(real_part, imag_part)
def __str__(self):
return f'{self.real:.2f} + {self.imag:.2f}i'
# Example usage
c1 = ComplexNumber(1, 2)
c2 = ComplexNumber(3, -4)
print(c1 + c2)# Output: '4.00 - 2.00i'
print(c1 - c2)# Output: '-2.00 + 6.00i'
print(c1 * c2)# Output: '11.00 + 2.00i'
print(c1 / c2)# Output: '-0.20 + 0.40i'
以上是我的代码,得到的以下结果:
4.00 + -2.00i {:10_266:} 但这里我要怎么变成 4.00 - 2.00i ,这里不是我想要的
-2.00 + 6.00i
11.00 + 2.00i
-0.20 + 0.40i
在你的代码中,你在 __str__ 方法中使用了 f-string 来格式化复数的字符串表示。你可以在格式化字符串中使用 - 符号来显示负数的负号。你可以在 __str__ 方法中进行修改,如下所示:
def __str__(self):
return f'{self.real:.2f} - {abs(self.imag):.2f}i' if self.imag < 0 else f'{self.real:.2f} + {self.imag:.2f}i'
这样修改后,当虚部为负数时,会显示为 - 符号。重新运行你的代码,应该会得到你期望的输出:
4.00 - 2.00i
·2.00 + 6.00i
11.00 + 2.00i
·0.20 + 0.40i
要将结果从 "4.00 + -2.00i" 改为 "4.00 - 2.00i",你可以在 __str__ 方法中对负数部分进行特殊处理。你可以使用条件语句来检查 imag 的符号,并根据符号决定是否添加一个负号。以下是修改后的代码:
def __str__(self):
if self.imag >= 0:
return f'{self.real:.2f} + {self.imag:.2f}i'
else:
return f'{self.real:.2f} - {abs(self.imag):.2f}i'
这样修改后,运行结果将会变为所期望的输出:
4.00 - 2.00i
-2.00 + 6.00i
11.00 + 2.00i
-0.20 + 0.40i
希望能帮助到你!
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:} 为了实现你想要的输出格式,你可以在 __str__ 方法中添加一个条件判断,以便在虚部为负数时显示减号。以下是修改后的 __str__ 方法:
def __str__(self):
sign = '+' if self.imag >= 0 else '-'
return f'{self.real:.2f} {sign} {abs(self.imag):.2f}i'
现在,当虚部为负数时,输出将显示减号。这是修改后的完整代码:
class ComplexNumber:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def __add__(self, other):
return ComplexNumber(self.real + other.real, self.imag + other.imag)
def __sub__(self, other):
return ComplexNumber(self.real - other.real, self.imag - other.imag)
def __mul__(self, other):
real_part = self.real * other.real - self.imag * other.imag
imag_part = self.real * other.imag + self.imag * other.real
return ComplexNumber(real_part, imag_part)
def __truediv__(self, other):
denominator = other.real2 + other.imag2
real_part = (self.real * other.real + self.imag * other.imag) / denominator
imag_part = (self.imag * other.real - self.real * other.imag) / denominator
return ComplexNumber(real_part, imag_part)
def __str__(self):
sign = '+' if self.imag >= 0 else '-'
return f'{self.real:.2f} {sign} {abs(self.imag):.2f}i'
# Example usage
c1 = ComplexNumber(1, 2)
c2 = ComplexNumber(3, -4)
print(c1 + c2)# Output: '4.00 - 2.00i'
print(c1 - c2)# Output: '-2.00 + 6.00i'
print(c1 * c2)# Output: '11.00 + 2.00i'
print(c1 / c2)# Output: '-0.20 + 0.40i'
现在,你应该得到了期望的输出结果:
4.00 - 2.00i
·2.00 + 6.00i
11.00 + 2.00i
·0.20 + 0.40i
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案 def __str__(self):
if self.imag >= 0:
return f'{self.real:.2f} {self.imag:.2f}i'
else:
return f'{self.real:.2f} - {abs(self.imag):.2f}i'
# Example usage
c1 = ComplexNumber(1, 2)
c2 = ComplexNumber(3, -4)
print(c1 + c2)# Output: '4.00 2.00i'
print(c1 - c2)# Output: '-2.00 6.00i'
print(c1 * c2)# Output: '11.00 2.00i'
print(c1 / c2)# Output: '-0.20 0.40i'
输出结果为你想要的。
4.00 - 2.00i
-2.00 6.00i
11.00 2.00i
-0.20 0.40i
页:
[1]