2012-04-05 16 views
1

こんにちは、みんな! 現在、私は「Programming in Objective-C」をStephen Kochanが読んでいます。実際に私はすでにそれを読み終えています。 しかし、今度はChapter#13 - 「基礎となるC言語の機能」に戻りました。章を読んだ後、私はすべての練習をしましたが、私はそれらのうちの1つについたままです。問題は今私のプロジェクトでARCを使用していて、私が書いたコードが私にエラーを与えることです。 私はそれを修正するとき、私は間違った出力を持っています。だから私の分数クラスのコードはメインです。Objective-CのARC

Fraction.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 

Fraction.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 

Main.m

#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

質問:私が間違ってやっている何

ありがとうございます!

+0

あなたが書いたコードを提供することもできますか?そして、ARCのためにそれをどのように修正しましたか? – Novarg

+0

@Novarg関数の引数を "Fraction * array"から "Fraction ** array"に設定し、このステートメントに__autoreleasingフラグを追加します "Fraction __autoreleasing * arrayOfFractions [numberOfFractions] = {fractionArrayElementOne、fractionArrayElementTwo、fractionArrayElementThree、fractionArrayElementFour、 fractionArrayElementFive}; " –

+0

@ノヴァールそう、助けてくれますか? –

答えて

1

reduceメソッドにバグがあります。 gcdの結果を捨て、分子と分母を分子で除算するだけです。 numerator > denominatorとすぐにあなたの分数として1/0で終わるでしょう。

脇に、initは、実際にはデフォルトの分母を0ではなく1に設定する必要があります。

+0

ありがとう、私は前にこのコードに問題はありません。 –

+0

わかりました。関数を呼び出すためですが、返された値はどこにも使われませんでした。ありがとうございました。本当にnoobの間違い。 –