def operate(self, num1, num2, operater): if not isinstance(num1, Fraction): num1 = Fraction(num1) if not isinstance(num2, Fraction): num2 = Fraction(num2) # 计算结果 if operater == '+': return self.fraction_add(num1, num2) if operater == '-': return self.fraction_minus(num1, num2) if operater == '×': return self.fraction_multiply(num1, num2) if operater == '÷': return self.fraction_divide(num1, num2)
def fraction_multiply(fra1, fra2): molecular = fra1.molecular * fra2.molecular denominator = fra1.denominator * fra2.denominator return Fraction(molecular, denominator)
def fraction_divide(fra1, fra2): molecular = fra1.molecular * fra2.denominator denominator = fra1.denominator * fra2.molecular return Fraction(molecular, denominator)
def fraction_minus(fra1, fra2): molecular = fra1.molecular * fra2.denominator - fra2.molecular * fra1.denominator denominator = fra1.denominator * fra2.denominator return Fraction(molecular, denominator)