我在使用计算器类时遇到问题xcode
。我附上了代码以供参考。由于我是学习阶段的新手,任何帮助都将不胜感激。错误是执行不完整。
// Working with Calculator class
// What required shall be a class where we can perform 4 basic operations of +,-,/,* using accumulator which we can set to some value and perform operations on it and then also return its value, also clear it for some other operation.
#import <Foundation/Foundation.h>
@interface Calculator:NSObject
{
double accumulator;// type of variables employed
}
-(void) setAccumulator:(double)value; // Declaring the methods employed
-(void) clearAccumulator;
-(double) Accumulator;
// Method declaration for calculation - Arithematic Methods
-(void)subtract : (double) value;
-(void) add : (double) value;
-(void) multiply : (double) value;
-(void) divide : (double) value;
@end
@implementation Calculator // defining the methods declared in interface section
-(void) setAccuumulator:(double)value
{
accumulator=value;
}
-(void) clearAccumulator
{
accumulator = 0;
}
-(double) Accumulator
{
return accumulator;
}
-(void) subtract : (double)value
{
accumulator -= value;
}
-(void) add : (double) value
{
accumulator += value;
}
-(void) multiply : (double) value
{
accumulator *= value;
}
-(void) divide : (double) value
{
accumulator /= value;
}
@end
// Program section as usual begins with the main
int main(int argc, char*argv[])
{
NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];
Calculator * deskCal = [[Calculator alloc] init];
[deskCal setAccumulator: 100.0];
[deskCal add :200.0];
[deskCal divide : 15.0];
[deskCal subtract : 10.0];
[deskCal multiply : 5.0];
NSLog(@" The result is %g", [deskCal Accumulator]);
[deskCal release];
[pool drain];
return 0;
}