1

嗨,大家好! 目前,我正在阅读Stephen Kochan的《Objective-C 编程》一书。其实我已经读完了。但是现在我回到了第 13 章——“底层 C 语言特性”。读完本章后,我做了所有的练习,但我坚持了其中的一个。问题是现在我在我的项目中使用 ARC,而我编写的代码给了我一个错误。当我修复它时,我得到了不正确的输出。所以这是我的Fraction ClassMain的代码。

分数.h

#import <Foundation/Foundation.h>

@interface Fraction : NSObject {
    int numerator;
    int denominator;
}

@property int numerator, denominator;

-(void) print:(BOOL)reduceCheck;
-(void) setNumerator:(int)numerator andDenominator:(int)denominator;
-(double) convertToNum;
-(Fraction *) add:(Fraction *)fraction;
-(Fraction *) subtract:(Fraction *)fraction;
-(Fraction *) multiply:(Fraction *)fraction;
-(Fraction *) divide:(Fraction *)fraction;
-(void) reduce;
-(id)initWithNumerator:(int)numeratorValue andDenominator:(int)denominatorValue;
-(void)printOut;
-(id)init;
+(Fraction *)allocFraction;
+(int)objectCounter;
+(int)methodAddInvokedCounter;

@end

分数.m

#import "Fraction.h"

static int gObjectCounter;

static int gMethodInvokedCounter;

static int greatestCommonDivisor(int u, int v) {
    int temp;

    while(v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    return u;
}

@implementation Fraction

@synthesize numerator, denominator;

-(void) print:(BOOL)reduceCheck {
    Fraction *resultFraction = [[Fraction alloc] init];
    [resultFraction setNumerator:numerator andDenominator:denominator];
    if(reduceCheck) {
        [resultFraction reduce];
        NSLog(@"The Fraction is %i/%i - It was reduced", resultFraction.numerator, resultFraction.denominator); 
    }
    else 
        NSLog(@"The Fraction is %i/%i - It was not reduced", resultFraction.numerator, resultFraction.denominator); 

    if(resultFraction.denominator != 1) {
        if(resultFraction.numerator > resultFraction.denominator) {
            Fraction *FractionObject = [[Fraction alloc] init];
            [FractionObject setNumerator:(resultFraction.numerator % resultFraction.denominator) andDenominator: resultFraction.denominator];
            int FractionNumber = resultFraction.numerator / resultFraction.denominator;
            NSLog(@"The Fraction is %i/%i. The whole number of fraction is %i", FractionObject.numerator, FractionObject.denominator, FractionNumber);
        }
        else 
            NSLog(@"The Fraction is %i/%i", resultFraction.numerator, resultFraction.denominator);
    }
    else
        NSLog(@"The whole number of the Fraction is %i", resultFraction.numerator);
}

-(void) setNumerator:(int)value andDenominator:(int)value2 {
    numerator = value;
    denominator = value2;
}

-(double) convertToNum {
    if(denominator != 0)
        return (double)numerator / denominator;
    else
        return NAN;
}

-(Fraction *) add:(Fraction *)fraction {
    //extern int gMethodInvokedCounter;
    Fraction *FractionResult = [[Fraction alloc] init];
    FractionResult.numerator = numerator * fraction.denominator + denominator * fraction.numerator;
    FractionResult.denominator = denominator * fraction.denominator;
    [FractionResult reduce];
    gMethodInvokedCounter++;
    return FractionResult;
}

-(Fraction *) subtract:(Fraction *)fraction {
    Fraction *FractionResult = [[Fraction alloc] init];
    FractionResult.numerator = numerator * fraction.denominator - denominator * fraction.numerator;
    FractionResult.denominator = denominator * fraction.denominator;
    [FractionResult reduce];
    return FractionResult;
}

-(Fraction *) multiply:(Fraction *)fraction {
    Fraction *FractionResult = [[Fraction alloc] init];
    FractionResult.numerator = numerator * fraction.numerator;
    FractionResult.denominator = denominator * fraction.denominator;
    [FractionResult reduce];
    return FractionResult;
}

-(Fraction *) divide:(Fraction *)fraction {
    Fraction *FractionResult = [[Fraction alloc] init];
    FractionResult.numerator = numerator * fraction.denominator;
    FractionResult.denominator = denominator * fraction.numerator;
    [FractionResult reduce];
    return FractionResult;
}

-(void) reduce {
    int u = numerator;
    int v = denominator;

    int greatestCommonDivisor(int u, int v);

    greatestCommonDivisor(u, v);

    numerator /= u;
    denominator /= u;
}

-(id)initWithNumerator:(int)numeratorValue andDenominator:(int)denominatorValue {
    self = [super init];

    if(self) 
        [self setNumerator:numeratorValue andDenominator:denominatorValue];

    return self;
}

-(void)printOut {
    printf("%i/%i", numerator, denominator);
}

-(id)init {
    return [self initWithNumerator:0 andDenominator:0];
}

+(Fraction *)allocFraction {
    //extern int gObjectCounter;
    gObjectCounter++;

    return [Fraction alloc];
}

+(int)objectCounter {
    //extern int gObjectCounter;

    return gObjectCounter;
}

+(int)methodAddInvokedCounter {
    return gMethodInvokedCounter;
}

@end

主程序

#import <Foundation/Foundation.h>
#import "Fraction.h"

Fraction *calculateFractions (Fraction  **array, int numberOfElements) {
    Fraction *resultFraction = [[Fraction alloc] initWithNumerator:array[0].numerator andDenominator:array[0].denominator];
    Fraction *holder;

    for(int i = 1; i < numberOfElements; i++) {
        holder = [resultFraction add:array[i]];
        resultFraction = holder;
    }

    return resultFraction;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        const int numberOfFractions = 5;

        Fraction *fractionArrayElementOne = [[Fraction alloc] initWithNumerator:5 andDenominator:10];
        Fraction *fractionArrayElementTwo = [[Fraction alloc] initWithNumerator:8 andDenominator:13];
        Fraction *fractionArrayElementThree = [[Fraction alloc] initWithNumerator:7 andDenominator:15];
        Fraction *fractionArrayElementFour = [[Fraction alloc] initWithNumerator:9 andDenominator:23];
        Fraction *fractionArrayElementFive = [[Fraction alloc] initWithNumerator:4 andDenominator:17];

        Fraction __autoreleasing *arrayOfFractions[numberOfFractions]= {fractionArrayElementOne, fractionArrayElementTwo, fractionArrayElementThree, fractionArrayElementFour, fractionArrayElementFive};

        printf("Fraction Expression: %i/%i + %i/%i + %i/%i + %i/%i + %i/%i = ", arrayOfFractions[0].numerator, arrayOfFractions[0].denominator, arrayOfFractions[1].numerator, arrayOfFractions[1].denominator, arrayOfFractions[2].numerator, arrayOfFractions[2].denominator, arrayOfFractions[3].numerator, arrayOfFractions[3].denominator, arrayOfFractions[4].numerator, arrayOfFractions[4].denominator);

        Fraction *calculateFractions (Fraction **array, int numberOfElements);

        Fraction *resultOfFractionArrayCalculation; 

        resultOfFractionArrayCalculation = calculateFractions(arrayOfFractions, 5);

        printf("%i/%i\n", resultOfFractionArrayCalculation.numerator, resultOfFractionArrayCalculation.denominator);

    }
    return 0;
}

输出
分数表达式:5/10 + 8/13 + 7/15 + 9/23 + 4/17 = 1/0

问题:我做错了什么?

提前致谢!

4

1 回答 1

1

reduce 方法中有一个错误。您丢弃 gcd 的结果,只需将分子和分母除以分子。一旦numerator > denominator你最终得到 1/0 作为你的分数。

顺便说一句,init应该真正将默认分母设置为 1,而不是零。

于 2012-04-05T11:01:03.240 回答