实施分数抽象类

2024-10-01 07:21:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试使is0,isNegative,互惠和negate工作时遇到了困难。当我测试它们时,我得到“内置属性错误:“tuple”对象没有属性“intermediate”。“
这四个功能是否正确实现?在

我的代码以及每个部分应该做什么:

class Fraction:

    # Creates a new rational number from the supplied values. The denominator must be greater than zero
    def __init__( self, numerator = 0, demoninator = 1 ) :
        self._numerator = numerator
        self._demoninator = demoninator
        # Demoninator cannot be zero
        if demoninator == 0 :
            raise ZeroDivisionError("The demoninator cannot be zero.")

        # Rational number must be stored in its smallest reduced form 
        if numerator == 0 :
            self._numerator = 0
            self._demoninator = 1
        else :
            if (numerator < 0 and demoninator >= 0 or numerator >= 0 and demoninator < 0) :
                sign = -1
            else :
                sign = 1


    # Returns a Boolean indicating if the rational number is zero
    def isZero( self ) :
        if self != 0:
            return True
        else:
            return False        

    #  Returns a Boolean indicating if the rational number is negative 
    def isNegative( self ) :
        if self < 0:
            return True
        else:
            return False

    #  Returns a new rational number that is the reciprocal of this rational number
    def reciprocal( self ) :
        reciprocal = 1 / self
        return reciprocal



    # Returns the floating-point representation of the rational number.
    # This operation is performed on the Fraction x by typecasting with the
    # float(x) function
    def __float__( self) :
        return self._numerator / self._denominator 

    # Compares this rational number to the rational number stored in
    # rhsFraction to determine their logical ordering
    def __eq__( self, rhsFraction ) :
        if self._numerator == rhsFraction._numerator and self._demonaitor ==    rhs.Frction._denominator :
            return True
        else:
            return False

    def __lt__( self, rhsFraction ) :
        if self._numerator * rhsFraction._denominator < self._demoninator * rhsFraction._numerator :
            return True
        else:
            return False

    def __le__( self, rhsFraction ) :
        return not rhsFraction < self 



    # Returns a new rational number that is the negative (-x) of this
    # rational number
    def negate( self ) :
        self = -self
        return self


    # Returns a new rational number that is the absolute version of this rational number.
    # Performed by applying the abs( x ) function to the Fraction x
    def __abs__( self ) :
        a = abs(numerator)           
        b = abs(denominator)         
        while a % b != 0 :             
            tempA = a              
            tempB = b             
            a = tempB             
            b = tempA % tempB           
        self._numerator = abs(numerator) // b * sign          
        self._denominator = abs(denominator) // b 

    # Creates and returns a new rational number that is the result of adding this rational
    # number to the given rhsFraction
    def __add__( self, rhsFraction) :
        num = (self._numerator * rhsFraction._denominator + self._denominator * rhsFraction._numerator)  
        den = self._denominator * rhsFraction._denominator       
        return Fraction(num, den) 

    # The same as the add() operation but subtracts the two rational numbers
    def __sub__( self, rhsFraction ) :
        num = (self._numerator * rhsFraction._denominator - self._denominator * rhsFraction._numerator)        
        den = self._denominator * rhsFraction._denominator       
        return Fraction(num, den) 

    # Creates and returns a new rational number that is the result of multiplying this
    # rational number to the given rhsFraction
    def __mul__( self, rhsFraction ) :
        n = self.__numerator * rhsFraction._numerator
        d = self.__denominator * rhsFraction._demoninator
        return Fraction(n, d)        

    # Creates and returns a new rational number that is the result of dividing this
    # rational number by the given rhsFraction. The rhsFraction can not be zero
    def __truediv__( self, rhsFraction ) :
        n = self._numerator / rhsFraction
        d = self._demoninator / rhsFraction
        return Fraction(n, d)

    # Returns a string representation of the rational number in the format #/#
    def __str__( self ) :
        return str(self._numerator) + "/" + str(self._demoninator)

我的代码测试:

^{pr2}$

错误: 3.4.1(v3.4.1:c0e311e010fc,2014年5月18日,10:38:22)[MSC v.1600 32位(Intel)] Python键入“help”、“copyright”、“credits”或“license”以获取更多信息。 [评估测试分形.py] -7/4-1/4 3/4 4/3-1-3/4 假 是的 是的 1 3/4页 回溯(最近一次呼叫):

在内置属性错误:“Fraction”对象没有属性“referential”


Tags: oftheselfnumbernewreturnifis
1条回答
网友
1楼 · 发布于 2024-10-01 07:21:29

fractions也是python内置库的名称,另请参见the documentation。所以你实际上不是在测试分数类,而是测试内置函数。但这不提供您尝试调用的reciprocal。{然后按不同的文件名导入

from myfractions import Fractions

再做一次测试。在

相关问题 更多 >