开发者

BASIC Fraction prog., Can anyone tell me whats the problem with this program?

开发者 https://www.devze.com 2023-03-22 15:48 出处:网络
#import <Foundation/Foundation.h> //.........interface section......... @interface Fraction : NSObject
#import <Foundation/Foundation.h>
//.........interface section.........
@interface Fraction : NSObject 
{
    int numerator;
    int denomenator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenomenator: (int) d;
@end
//.........Implementaion Section........
@implementation Fraction
-(void) print {
    NSLog(@"Solution %i and %i is:",numerator,denomenator);
}
-(void) setNumerator:(int)n
{
    numerator = n;
}
-(void) setDenomenator:(int)d
{
    denomenator = d;
}
@end
//..........Program Section..........
int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...

    Fraction *frac1=[[Fraction alloc] init];
    Fraction *frac2=[[Fraction alloc] init];

    //....Set 1.. fraction = 2/3

    [frac1 setNumerator:2];
    [frac2 setDenomenator:3];

    //.....Set 2 .. Fraction = 3/9

    [frac1 setNumerator:3];
    [frac2 setDenomenator:9];

    //...... Display Function.....

    NSLog(@"First Fraction is:");
    [frac1 print];

    NSLog(@"Second Fraction is:");
    [frac2 print];

    [frac1 release];
    [frac2 release];

    [pool drain];
    return 0;

The output I'm getting is not displaying the "answer in fraction" i.e 2开发者_开发技巧/3 and 3/9


You have this

[frac1 setNumerator:2];
[frac2 setDenomenator:3];

instead of this

[frac1 setNumerator:2];
[frac1 setDenomenator:3];

you have done the same for frac2

0

精彩评论

暂无评论...
验证码 换一张
取 消