- (Fraction *)add:(Fraction *)addFra {
Fraction *numerator1 = [[Fraction alloc]initWithFraction:[self initWithNumerator:self.num]];
Fraction *numerator2 = [[Fraction alloc]initWithFraction:[self initWithNumerator:addFra.num]];
Fraction *denominator1 = [[Fraction alloc]initWithFraction:[self initWithDenominator:self.den]];
Fraction *denominator2 = [[Fraction alloc]initWithFraction:[self initWithDenominator:addFra.den]];
Fraction *result = (numerator1 * denominator2) + (numerator2 * denominator1);
return result;
}
答案 0 :(得分:0)
Objective-C没有运算符重载,所以这些数学运算必须使用方法实现:
Fraction *result = [[numerator1 multiply:denominator2] add:[numerator2 multiply:denominator1]];
我还没有展示multiply:
或add:
的实施情况,因为坦率地说,我无法理解如何正确地做到这一点。