Bạn có thể vui lòng giúp tôi với mã dưới đây mà tôi không nhận được kết quả mong muốn:
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self,other]:
print['Sum of the two Complex numbers :{}+{}i'.format[self.real+other.real,self.imag+other.imag]]
def sub[self, other]:
print['Subtraction of the two Complex numbers :{}+{}i'.format[self.real-other.real, self.imag-other.imag]]
Mã hoạt động nhưng khi trường tưởng tượng kết quả là -2, tôi biết nó sẽ in kết quả là +-2i
Kết quả ví dụ: 1+2i -3+4i = -2-2i [nhưng vì nó được mã hóa cứng là+trong nhận xét, nó dẫn đến -2+-2i eg: 1+2i - 3+4i = -2-2i [but as it is hard coded as + in the comment it is resulting in -2+-2i
Giúp tôi hiểu làm thế nào để thoát khỏi nó?
hỏi ngày 17 tháng 2 năm 2021 lúc 21:26Feb 17, 2021 at 21:26
4
Vấn đề là bạn rõ ràng yêu cầu tách phần thực và tưởng tượng bằng một dấu cộng.
Đây là một phiên bản ngắn
class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
nếu không thì
def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
Đã trả lời ngày 17 tháng 2 năm 2021 lúc 22:55Feb 17, 2021 at 22:55
Kendfsskendfsskendfss
3963 Huy hiệu bạc9 Huy hiệu Đồng3 silver badges9 bronze badges
Tôi đề nghị thêm một câu lệnh if
trong mỗi hàm. Cụ thể if self.imag+other.imag < 0 :
cho việc bổ sung. Sau đó thêm câu lệnh
class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
0 của class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
1. Bạn cũng cần phải làm điều này với chức năng trừ. Giải pháp này không giữ chức năng ở một dòng nhưng nên giải quyết vấn đề.Đã trả lời ngày 17 tháng 2 năm 2021 lúc 21:40Feb 17, 2021 at 21:40
def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self,other]:
print['Sum of the two Complex numbers :{}{}{}i'.format[self.real+other.real,sign[self.imag+other.imag],self.imag+other.imag]]
def sub[self, other]:
print['Subtraction of the two Complex numbers :{}{}{}i'.format[self.real-other.real,sign[self.imag-other.imag],self.imag-other.imag]]
Đã trả lời ngày 25 tháng 4 lúc 7:15Apr 25 at 7:15
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Addition of complex number: In
Python, complex numbers can be added using + operator.
Examples:
Input: 2+3i, 4+5i Output: Addition is : 6+8i Input: 2+3i, 1+2i Output: Addition is : 3+5i
Bàn luận
Python3
Cho hai số phức Z1 và Z2. Nhiệm vụ là thêm và trừ đi các số phức đã cho. Đứng trước số phức: Trong Python, các số phức có thể được thêm vào bằng cách sử dụng toán tử.Examples: & nbsp; & nbsp;
& nbsp; ví dụ:
class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
2 class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
3class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
4class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
5 class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
6class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
7 class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
8class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
0 def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self,other]:
print['Sum of the two Complex numbers :{}{}{}i'.format[self.real+other.real,sign[self.imag+other.imag],self.imag+other.imag]]
def sub[self, other]:
print['Subtraction of the two Complex numbers :{}{}{}i'.format[self.real-other.real,sign[self.imag-other.imag],self.imag-other.imag]]
6 def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self,other]:
print['Sum of the two Complex numbers :{}{}{}i'.format[self.real+other.real,sign[self.imag+other.imag],self.imag+other.imag]]
def sub[self, other]:
print['Subtraction of the two Complex numbers :{}{}{}i'.format[self.real-other.real,sign[self.imag-other.imag],self.imag-other.imag]]
7 def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self,other]:
print['Sum of the two Complex numbers :{}{}{}i'.format[self.real+other.real,sign[self.imag+other.imag],self.imag+other.imag]]
def sub[self, other]:
print['Subtraction of the two Complex numbers :{}{}{}i'.format[self.real-other.real,sign[self.imag-other.imag],self.imag-other.imag]]
8‘
Addition is : [3+5j]
7def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
0 def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
1def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
222231def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
4def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
3def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
6 O[1]def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
Đầu ra: & nbsp;O[1]
Độ phức tạp về thời gian: O [1] Complex numbers in Python can be subtracted using – operator.
Examples:
Input: 2+3i, 4+5i Output: Subtraction is : -2-2i Input: 2+3i, 1+2i Output: Subtraction is : 1+1i
Không gian phụ trợ: O [1]
Python3
Phép trừ các số phức: Số phức trong Python có thể được trừ bằng cách sử dụng - toán tử.example: & nbsp; & nbsp;
Ví dụ: & nbsp;
class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
2 Input: 2+3i, 4+5i Output: Addition is : 6+8i Input: 2+3i, 1+2i Output: Addition is : 3+5i0
class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
4class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
5 class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
6class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
7 class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
8class comp[complex]:
def add[self, other]:
print[f'Sum of the two Complex numbers: {self+other}']
def sub[self, other]:
print[f'Subtraction of the two Complex numbers: {self-other}']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: [4+6j]
x.sub[y]
# Subtraction of the two Complex numbers: [-2-2j]
0 def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self,other]:
print['Sum of the two Complex numbers :{}{}{}i'.format[self.real+other.real,sign[self.imag+other.imag],self.imag+other.imag]]
def sub[self, other]:
print['Subtraction of the two Complex numbers :{}{}{}i'.format[self.real-other.real,sign[self.imag-other.imag],self.imag-other.imag]]
6Input: 2+3i, 4+5i Output: Subtraction is : -2-2i Input: 2+3i, 1+2i Output: Subtraction is : 1+1i4
Input: 2+3i, 4+5i Output: Subtraction is : -2-2i Input: 2+3i, 1+2i Output: Subtraction is : 1+1i5
Output:
Subtraction is : [1+1j]
7def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
0 def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
1def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
222231def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
4def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
3def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
6O[1]def sign[x]:
return '+' if x>=0 else ''
class comp:
def __init__[self, real, imag]:
self.real=real
self.imag=imag
def add[self, other]:
r = self.real + other.real
i = self.imag + other.imag
print[f'Sum of the two Complex numbers: {r}{sign[i]}{i}i']
def sub[self, other]:
r = self.real - other.real
i = self.imag - other.imag
print[f'Subtraction of the two Complex numbers: {r}{sign[i]}{i}i']
x = comp[1, 2]
y = comp[3, 4]
x.add[y]
# Sum of the two Complex numbers: 4+6i
x.sub[y]
# Subtraction of the two Complex numbers: -2-2i
Đầu ra: & nbsp;O[1]