2011-07-23 73 views
0
#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; 

我得到 「的部分答案」,即不顯示輸出2/3和3/9BASIC Fraction prog。,有誰能告訴我這個程序有什麼問題嗎?

+0

輸出是你傳遞給NSLog在'print'方法...什麼你期望? – Saphrosit

回答

0

你有這樣的

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

,而不是這個

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

你已經做了相同的壓裂2

+0

我試過這個並得到了:[切換到進程469線程0x0] 2011-07-23 18:50:25.159 3 [469:707]第一個分數是: 2011-07-23 18:50:25.162 3 [469 :707]解決方案2和3是:{//我想要「2/3」在這裏..} 2011-07-23 18:50:25.162 3 [469:707]第二個分數是: 2011-07-23 18:50:25.163 3 [469:707]解決方案3和9是:{//我希望這裏的「3/9」..} 程序以退出碼結束:0 – Pranay

+0

您只需要更改Fraction打印方法功能來自NSLog(@「解決方案%i和%i是:」,分子,分母);到printf(「%d /%d」,分子,分母);使用c的printf而不是NSLog將刪除所有附加的調試信息,只記得printf不支持Objective-C「%@」來打印對象,所以如果你想用printf打印出一個字符串對象,你將不得不做一些事情像[aStr UTF8String],然後用%s輸出。 –

相關問題