Hướng dẫn addition and subtraction of complex numbers in python fresco play - phép cộng và phép trừ các số phức trong trò chơi python bích

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

  • Đọc
  • Bàn luận
  • 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]
            
    
    3

    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]
            
    
    4
    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]
            
    
    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]
            
    
    6
    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]
            
    
    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]
            
    
    8

    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
        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]

    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
    
    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]:
            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
    
    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
    
    222231
    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
    
    4
    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
    
    3
    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
    
    6
    O[1]

    Đầ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+5i
    0

    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]
            
    
    4
    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]
            
    
    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]
            
    
    6
    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]
            
    
    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]
            
    
    8

    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
        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
    Input: 2+3i, 4+5i
    Output: Subtraction is : -2-2i
    
    Input: 2+3i, 1+2i
    Output: Subtraction is : 1+1i
    4
    Input: 2+3i, 4+5i
    Output: Subtraction is : -2-2i
    
    Input: 2+3i, 1+2i
    Output: Subtraction is : 1+1i
    5

    Output:  

    Subtraction is :  [1+1j]

    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
    
    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]:
            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
    
    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
    
    222231
    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
    
    4
    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
    
    3
    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
    
    6
    O[1]

    Đầu ra: & nbsp;O[1]
     


    Làm thế nào để bạn thêm và trừ các số phức tạp trong Python?

    Toán Python: Tập thể dục-33 với giải pháp..
    Giải pháp mẫu:-.
    Mã Python: In ["Bổ sung hai số phức:", [4+3J]+[3-7J]] In ["Phép trừ hai số phức:", [4+3J]-[3-7J]].

    Làm thế nào để bạn thêm một số phức tạp trong Python?

    Bổ sung số phức: Trong Python, các số phức có thể được thêm vào bằng toán tử +.Ví dụ: Python3.using + operator. Example : Python3.

    Phức tạp [] làm gì trong Python?

    Hàm python Complex [] Hàm [] Hàm trả về một số phức bằng cách chỉ định một số thực và một số tưởng tượng.returns a complex number by specifying a real number and an imaginary number.

    Làm thế nào để bạn giải quyết các số phức tạp trong Python?

    Sử dụng các số phức trong biểu thức số học.Tận dụng mô-đun CMATH tích hợp.Dịch các công thức toán học trực tiếp sang mã Python ...
    Z0 = -√2/2 + 2/2 J ..
    Z1 = - √2/2 - 2/2 J ..
    Z2 = √2/2 + 2/2 J ..
    Z3 = √2/2 - 2/2 J ..

    Bài Viết Liên Quan

    Chủ Đề