2012-08-12 81 views
-2

練習:編程在目的C.科昌第三第4章練習6

「複數是含有兩種組分的數字:一個實部和 虛部如果是實分量和b是虛數。這個 表示法用於表示數字: a + bi 編寫一個Objective-C程序,該程序定義一個名爲Complex的新類。繼 爲Fraction類建立的範例,定義以下方法用於 您的新類:

-(void) setReal: (double) a; 
-(void) setImaginary: (double) b; 
-(void) print; // display as a + bi 
-(double) real; 
-(double) imaginary; 

編寫一個測試程序來測試你的新類和方法。「

這裏是我的解決方案不起作用:

#import <Foundation/Foundation.h> 

@iterface Complex:NSObject 
{ 
    double a, b; 
} 

-(void)setReal: (double) a; 
-(void)setImaginary: (double) b; 
-(void) print; 
-(double) real; 
-(double) imaginary; 

@end 

@implementation Complex 
-(void)setReal 
{ 
    scanf(@"Set real value %f",&a); 
} 
-(void)setImaginary 
{ 
    scanf(@"Set imaginary value %f", &b); 
} 
-(void) print 
{ 
    Nslog(@"Your number is %f",a+bi); 
} 
-(double)real 
{ 
    Nslog(@"Real number is %f",a); 
} 
-(double)imaginary 
{ 
    NSlog(@"Imaginary number is %f",b) 
} 

@end 



int main (int argc, char *argv[]) 
{ 
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; 
    Complex*num=[[complex alloc] init]; 
    [num setReal:3]; 
    [num setImaginary:4]; 
    Nslog(@"The complex number is %i",[num print]); 
    [num release]; 
    [pool drain]; 
    return 0; 
} 

拜託,有什麼不好?

+0

看看這個:http://en.wikipedia.org/wiki/Complex_numbers 並且還Kochan有一本關於人們有書中每一個問題的專門網站:http://classroomm.com/objective-c/index.php – TheAmateurProgrammer 2012-08-12 14:51:17

+1

「不起作用」是什麼意思? – 2012-08-12 18:14:39

+0

@theAmateurProgrammer Kochan的論壇是我的第一站。不幸的是,那裏沒有答案,只是討論。所以你不能真正檢查你的答案。 – 2012-08-12 20:15:43

回答

1

我可以看到一些明顯的缺陷。首先,(這可能是複製/粘貼錯誤),拼寫錯誤interfaceiterface

其次,您的print方法不能正確寫入NSLog。您試圖強制將表達式a+bi作爲格式說明符%f的結果。相反,你需要兩個參數,分別將ab傳遞給NSLog調用。因此,你必須:

NSLog(@"Your number is %f + %fi", a, b); 

最後,你的方法realimaginary應該是你的實例變量「干將」,而不是打印到NSLog的功能。因此,你只需要功能體分別爲return a;return b;。對於前者(全稱):

-(double)real 
    { 
     return a; 
    } 
+0

我非常感謝您的幫助!這是我的錯誤拼寫錯誤的「界面」。還有「NSlog」 - 非常明顯(一旦我看到它:)但我有一個問題:我應該如何從練習中看到「真實」和「想象」 - 應該是獲得者? – 2012-08-12 20:42:33

+0

從你的問題陳述中,「遵循爲Fraction類建立的範式...」我假設'Fraction類'實現了分別返回其分子和分母的分子和分母的方法,而不是寫它們到NSLog。你有沒有訪問'分數班'的例子? – marc 2012-08-12 22:48:49

+0

事實上,setter和getters是所有類型變量的基礎,這些變量都是公開可訪問的。這些命名約定(以及在Objective-C中對@ @ synthesize使用明確的@ @ property'關鍵字時所需的命名約定)通常是setter和getter的'setParameter'。閱讀關於這些屬性[這裏](http://en.wikipedia.org/wiki/Objective-C#Properties)。 – marc 2012-08-12 22:58:37

1

後改正的,答案是:

#import <Foundation/Foundation.h> 

@interface Complex: NSObject 
{ 
    double real, imaginary; 
} 

-(void)setReal: (double) a; 

-(void)setImaginary: (double) b; 

-(void) print; 

-(double) real; 

-(double) imaginary; 

@end 

@implementation Complex 


-(void)setReal: (double) a 
{ 
    real =a; 
} 



-(void)setImaginary: (double) b 

{ 
    imaginary = b; 
} 



-(void) print 
{ 
    NSLog(@"Your number is %.2f+%.2fi", real, imaginary); 
} 


-(double)real 
{ 
    return real; 
} 



-(double)imaginary 
{ 
    return imaginary; 
} 

@end 



int main (int argc, char *argv[]) 
{ 

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

Complex *myComplex=[[Complex alloc] init]; 

[myComplex setReal:3]; 
[myComplex setImaginary:4]; 
[myComplex print]; 

[myComplex release]; 
[pool drain]; 


    return 0; 
}