Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
July 23, 2017, 03:39:08 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Melinda's Answer Exercise 6.4  (Read 1481 times)
melj247
Newbie
*
Posts: 5


« on: September 10, 2009, 06:35:02 PM »

Calculator.h:
Code: (Objective-C)
@interface Calculator: NSObject
{
double accumulator;
}

// acumulator methods

-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;
-(void) S: (double) value;


// arithmetic methods

-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
-(void) E: (double) value;

@end



Calculator.m:
Code: (Objective-C)
#import "Calculator.h"


@implementation Calculator

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

-(void) clear
{
accumulator = 0;
}

-(double) accumulator
{
return accumulator;
}

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


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


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

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

-(void) divide: (double) value
{
if (value == 0)
NSLog(@"Cannot divide by 0");
else accumulator /= value;
}

-(void) E: (double) value
{
NSLog(@" = %i", accumulator);
}


@end



main:
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Calculator.h"

int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

double value1 = 0;
char operator = 0;
Calculator *deskCalc = [[Calculator alloc] init];



NSLog(@"Begin Calculations ");

while (operator != 'e')
{

NSLog(@"Enter Number and Operator:");
scanf("%lf %c", &value1, &operator);


switch (operator)
{
case 'S':
case 's':
[deskCalc setAccumulator: value1];
break;

case '+':
[deskCalc add: value1];
break;

case '-':
[deskCalc subtract: value1];
break;

case '*':
[deskCalc multiply: value1];
break;

case '/':
[deskCalc divide: value1];
break;

case 'e':
case 'E':
NSLog(@"Program End");
break;

default:
NSLog(@"That is not a valid operator, please re-enter:");
break;
}

NSLog(@" = %f", [deskCalc accumulator]);
}



[deskCalc release];

    [pool drain];

    return 0;
}
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.