エクササイズ:
「複素数は、実部と虚部の 2 つの成分を含む数です。a が実部で b が虚部の場合、この表記法を使用して数を表します: a + bi 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;
}
お願いします、何が悪いのですか?