@@ -223,6 +223,9 @@ def __ifloordiv__(self, other):
223223 return Complex (self .re , self .im , self .restore )
224224 except (ZeroDivisionError , ValueError ) as err :
225225 self .print_err (err )
226+ # Operator overloading 17: - (Unary operator)
227+ def __neg__ (self ):
228+ return Complex (- self .re , - self .im , self .restore )
226229 ## Helper functions
227230 # Module function to calculate the modulus of a complex number
228231 def mod (self ):
@@ -237,11 +240,13 @@ def print_err(self, err):
237240 # Representation function for representing a complex number
238241 def __repr__ (self ):
239242 if (self .re != 0 and self .im > 0 ):
240- output = str (self .re ) + ' + ' + str (self .im ) + 'j'
243+ output = str (self .re ) + ' + ' + str (self .im ) + 'j' if ( self . im != 1 ) else str ( self . re ) + ' + ' + 'j'
241244 if (self .re != 0 and self .im < 0 ):
242- output = str (self .re ) + ' - ' + str (- self .im ) + 'j'
245+ output = str (self .re ) + ' - ' + str (- self .im ) + 'j' if ( self . im != - 1 ) else str ( self . re ) + ' - ' + 'j'
243246 if (self .re != 0 and self .im == 0 ):
244247 output = str (self .re )
245- if (self .re == 0 and self .im != 0 ):
246- output = str (self .im ) + 'j'
248+ if (self .re == 0 and self .im > 0 ):
249+ output = str (self .im ) + 'j' if (self .im != 1 ) else 'j'
250+ if (self .re == 0 and self .im < 0 ):
251+ output = str (self .im ) + 'j' if (self .im != - 1 ) else '-j'
247252 return output
0 commit comments