こんにちは、みんな! 現在、Stephen Kochan著の「Programming in Objective-C」という本を読んでいます。実はもう読み終わりました。しかし、ここで第 13 章「C 言語の基礎となる機能」に戻ります。章を読んだ後、私はすべての演習を行いましたが、そのうちの 1 つに固執しました。問題は、プロジェクトで ARC を使用しているのに、書いたコードでエラーが発生することです。修正すると、間違った出力が得られました。これが私のFraction ClassとMainのコードです。
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
質問:私が間違っているのは何ですか?
前もって感謝します!