1

我在使用计算器类时遇到问题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;
}
4

2 回答 2

2

你拼错了-(void) setAccumulator:(double)value你的类的功能

只需更改您的拼写错误

-(void) setAccuumulator:(double)value
{
    accumulator=value;
}

-(void) setAccumulator:(double)value
{
    accumulator=value;
}

这给了你

The result is 50
于 2012-10-01T19:49:29.930 回答
-2

在 Xcode 中,打开管理器窗口并输入“计算器”。在结果列表中,在示例代码下,将是一个标记为“单元测试”的项目。下载并构建这个项目,然后看看实现。

虽然这个项目主要用于展示如何使用测试框架,但它还包括一个计算器示例应用程序,它应该可以回答您的许多问题。其他示例代码和教程也可以通过这种方式找到。

祝你好运!

于 2012-10-01T19:30:28.763 回答