Amazon.com Widgets Answer from Paul
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 22, 2014, 09:13:37 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Webcast Series Part I, Sept. 1 - 24
| | |-+  Chapter 6, Exercise 4
| | | |-+  Answer from Paul
Pages: [1]   Go Down
Print
Author Topic: Answer from Paul  (Read 1283 times)
zlinuk
Newbie
*
Posts: 14






« on: September 26, 2009, 09:57:18 AM »



The Code


Code: (Objective-C)
#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

Code: (Objective-C)
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.



Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.