What is wrong with this Obj-C code?
It's an example program in a textbook. It does not run as expected. It's supposed to simulate a sigma calculation and add all the fractions in a series. but it just gives me 0.000000 when it's done the loop. Any ideas? Thanks in advance:
#import <Foundation/Foundation.h>
// The Fraction class
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
@property int numerator, denominator;
-(void) print;
-(void) set: (int) n: (int) d;
-(double) convertToNum;
-(void) reduce;
-(Fraction *) add: (Fraction *) f;
@end
-------------------------------------
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
return (int) numerator / denominator;
else
return 1.0;
}
-(void) set:(int)n:(int)d
{
numerator = n;
denominator = d;
}
-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;
while (v != 0) {
temp = v % u;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;
resultNum = numerator * f.denominator +
denominator * f.nume开发者_运维技巧rator;
resultDenom = denominator * f.denominator;
[result set: resultNum: resultDenom];
[result reduce];
return result;
}
@end
--------------------------------------------
#import "Fraction.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init], *sum2;
int i, pow2, n;
// set 1stfraction to 0
[sum set: 0.0 : 1.0];
NSLog (@"Enter your value for n:");
scanf ("%i", &n);
pow2 = 2;
for (i = 1; i <= n; ++i) {
[aFraction set: 1 : pow2];
sum2 = [sum add: aFraction];
[sum release]; // release previous sum
sum = sum2;
pow2 *= 2;
}
NSLog (@"After %i iterations, the sum is %f", n, [sum convertToNum]);
[aFraction release];
[sum release];
[pool drain];
return 0;
}
Your convertToNum
method is declared as returning a double, but the actual value you return is an int (the result of dividing two ints is always an int), so in practice any values less than 1 will be truncated to 0.
Also, as a side note: That coding style, where you don't give names to arguments, is really hard to read. - (void)set:(int)n :(int)d
is not a good method declaration, and if that's actually used in a book, it should be pulled from print. There's no clue what n
and d
are, and the second argument doesn't have anything but a colon before it. It should be something like - (void)setNumerator:(int)aNumerator denominator:(int)aDenominator
.
Your convertToNum function will return 0 when the numerator is smaller than the denominator. Try changing it to this:
-(double) convertToNum
{
if (denominator != 0)
return (double)numerator / (double)denominator;
else
return 1.0;
}
Reduce seems incorrect. I don't know how to make that logic work, but
i = 2;
lLimit = ((lNumerator > lDenominator) ? lNumerator : lDenominator)/2;
while (i < lLimit) {
if ((lNumerator % i == 0) &&(lDenominator % i == 0)) {
lNumerator /= i;
lDenominator /= i;
i = 1;
}
i++;
}
should be closer. sorry for the name changes, but i don't work too well with lots of single letter vars
精彩评论