The Code
#import <Foundation/Foundation.h>
@interface Calculator : NSObject
{
double accumulator;
}
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end
@implementation Calculator
-(void) setAccumulator: (double) value
{
accumulator = value;
}
-(void) clear
{
accumulator = 0;
}
-(double) accumulator
{
return accumulator;
}
-(void) add: (double) value
{
accumulator += value;
}
-(void) subtract: (double) value
{
accumulator -= value;
}
-(void) multiply: (double) value
{
accumulator *= value;
}
-(void) divide: (double) value
{
accumulator /= value;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Calculator *deskCalc = [[Calculator alloc] init];
double number;
char operator;
while (operator != 'E' ) {
NSLog(@"Please enter a number and an operator then press return");
scanf("%lf %c", &number, &operator);
if (operator == 'E')
break;
else if (operator == 'S'){
[deskCalc setAccumulator: number];
NSLog(@"%g",[deskCalc accumulator]);
}
else {
switch (operator) {
case '+':
[deskCalc add:number];
NSLog(@"%g",[deskCalc accumulator]);
break;
case '-':
[deskCalc subtract:number];
NSLog(@"%g",[deskCalc accumulator]);
break;
case '*':
[deskCalc multiply:number];
NSLog(@"%g",[deskCalc accumulator]);
break;
case '/':
if (number == 0)
NSLog(@"Divide by zero; accumulator still = %g", [deskCalc accumulator]);
else
[deskCalc divide:number];
NSLog(@"%g",[deskCalc accumulator]);
break;
default:
NSLog(@"Unknown Operator; accumulator still = %g", [deskCalc accumulator]);
break;
}
}
}
NSLog(@"%g", [deskCalc accumulator]);
[deskCalc release];
[pool drain];
return 0;
}
The Output
Loading program into debugger…
Program loaded.
run
[Switching to process 14655]
Running…
2009-09-26 17:51:32.303 Calculator[14655:a0f] Please enter a number and an operator then press return
10 S
2009-09-26 17:51:40.113 Calculator[14655:a0f] 10
2009-09-26 17:51:40.114 Calculator[14655:a0f] Please enter a number and an operator then press return
2 /
2009-09-26 17:51:49.665 Calculator[14655:a0f] 5
2009-09-26 17:51:49.666 Calculator[14655:a0f] Please enter a number and an operator then press return
55 -
2009-09-26 17:51:59.265 Calculator[14655:a0f] -50
2009-09-26 17:51:59.266 Calculator[14655:a0f] Please enter a number and an operator then press return
100.25 S
2009-09-26 17:52:13.209 Calculator[14655:a0f] 100.25
2009-09-26 17:52:13.210 Calculator[14655:a0f] Please enter a number and an operator then press return
4 *
2009-09-26 17:52:24.691 Calculator[14655:a0f] 401
2009-09-26 17:52:24.692 Calculator[14655:a0f] Please enter a number and an operator then press return
0 /
2009-09-26 17:52:39.627 Calculator[14655:a0f] Divide by zero; accumulator still = 401
2009-09-26 17:52:39.628 Calculator[14655:a0f] 401
2009-09-26 17:52:39.629 Calculator[14655:a0f] Please enter a number and an operator then press return
10 F
2009-09-26 17:52:49.340 Calculator[14655:a0f] Unknown Operator; accumulator still = 401
2009-09-26 17:52:49.341 Calculator[14655:a0f] Please enter a number and an operator then press return
0 E
2009-09-26 17:53:03.295 Calculator[14655:a0f] 401
Debugger stopped.
Program exited with status value:0.