减法中的角落测试用例场景

Corner test case scenario in subtraction

我正在 python 学习 OOP 概念。我遇到了一个挑战,我们需要创建一个对象 'comp' 来执行复数的加法和减法。但是对于我编写的代码,我得到了 2 个失败的极端案例测试场景。

class comp:

def __init__(self, Real, Imaginary=0.0):

    self.Real = Real
    self.Imaginary = Imaginary
    
def add(self,other):
    a=self.Real
    b=self.Imaginary
    c=other.Real
    d=other.Imaginary
    rsum=(a+c)
    isum=(b+d)
    print('Sum of the two Complex numbers :{}+{}i'.format(rsum,isum))
    #logging.debug('Sum of the two Complex numbers :{}+{}i'.format(rsum,isum))
    return('Sum of the two Complex numbers :{}+{}i'.format(rsum,isum))
    

def sub(self,other):
    a=self.Real
    b=self.Imaginary
    c=other.Real
    d=other.Imaginary
    rsub=(a-c)
    isub=(b-d)
    print('Subtraction of the two Complex numbers :{}+{}i'.format((rsub),(isub)))
    return('Subtraction of the two Complex numbers :{}+{}i'.format((rsub),(isub)))

2 个失败的输出是:

  1. 两个复数之和:4+6i

两个复数的减法:-2+-2i

2)两个复数之和:6+9i

两个复数的减法:-6+-9i

我无法找到如何让代码处理所有极端情况。非常感谢对此的任何帮助。

提前致谢。

通过阅读 Whosebug 中的内置类型文档和其他答案,我弄清楚了如何转换符号。 涵盖角落场景的工作代码:

class comp:

def __init__(self, Real, Imaginary=0.0):

    self.Real = Real
    self.Imaginary = Imaginary
    
def add(self,other):
    a=self.Real
    b=self.Imaginary
    c=other.Real
    d=other.Imaginary
    rsum=(a+c)
    isum=(b+d)
    
    print('Sum of the two Complex numbers :%d+%di' % (rsum,isum))
    return('Sum of the two Complex numbers :%d+%di' % (rsum,isum ))
    #return (rsum+isum)

def sub(self,other):
    a=self.Real
    b=self.Imaginary
    c=other.Real
    d=other.Imaginary
    rsub=((a)-(c))
    isub=((b)-(d))
    
    print('Subtraction of the two Complex numbers :%d%+di' % (rsub,+(isub)))
    return('Subtraction of the two Complex numbers :%d%+di' % (rsub,+(isub)))