gpt4 book ai didi

objective-c - Objective-C 中的 ARC

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:37 29 4
gpt4 key购买 nike

大家好!目前我正在阅读 Stephen Kochan 所著的“Programming in 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

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

问题:我做错了什么?

提前致谢!

最佳答案

reduce 方法中存在错误。你扔掉 gcd 的结果,只用分子和分母除以分子。一旦 numerator > denominator,你的分数就会变成 1/0。

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

关于objective-c - Objective-C 中的 ARC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025511/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com