锻炼:
“复数是包含两个分量的数字:实部和虚部。如果 a 是实部,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;
}
请问,有什么问题吗?