Amazon.com Widgets Ex 6.4
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 05:26:59 PM
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
| |-+  Answers to Exercises
| | |-+  Chapter 6
| | | |-+  Ex 6.4
Pages: [1]   Go Down
Print
Author Topic: Ex 6.4  (Read 388 times)
byrsky
Newbie
*
Posts: 7


Email




« on: December 28, 2011, 07:49:47 AM »

Code: (Objective-C)

#import <Foundation/Foundation.h>

@interface Calculator : NSObject {
@private
    double accumulator;
    double memory;
}

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

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

-(double) changeSign;
-(double) reciprocal;
-(double) xSquared;   

-(void) memoryClear;                   
-(double) memoryStore;                     
-(double) memoryRecall;                   
-(double) memoryAdd: (double) value;       
-(double) memorySubtract: (double) value; 

@end

@implementation Calculator

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

-(void) clear
{
    accumulator = 0;
}

-(double) accumulator
{
    return accumulator;
}

-(double) changeSign
{
    return (unsigned) accumulator;
}

-(double)reciprocal
{
    accumulator = 1 / accumulator;
    return accumulator;
}

-(double) xSquared
{
    accumulator *= accumulator;
    return accumulator;
}

-(void)memoryClear
{
    memory = 0;
}

-(double)memoryStore
{
    memory = accumulator;
    return memory;
}

-(double)memoryRecall
{
    accumulator = memory;
    return accumulator;
}

-(double)memoryAdd:(double)value
{
    memory += value;
    return memory;
}

-(double)memorySubtract:(double)value {
    memory -= value;
    return memory;
}


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

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

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

-(double) divide:(double)value
{
    if (value != 0) {
        accumulator /= value;   
    } else {
        NSLog(@"Error: / 0");
       
    }
   
    return accumulator;
}

@end


int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        Calculator *deskCalc = [[Calculator alloc]init];
       
        double value1;
        char operator;
       
        NSLog(@"Input number & operator:");
       
        do {
           
            scanf("%lf %c", &value1, &operator);
           
            switch (operator) {
                case 'S':
                    [deskCalc setAccumulator:value1];
                    NSLog(@"= %f", [deskCalc accumulator]);
                    break;
                case '+':
                   
                    [deskCalc add:value1];
                    NSLog(@"= %f", [deskCalc accumulator]);
                    break;
                case '-':
                   
                    [deskCalc subtract:value1];
                    NSLog(@"= %f", [deskCalc accumulator]);
                    break;
                case '*':
                   
                    [deskCalc multiply:value1];
                    NSLog(@"= %f", [deskCalc accumulator]);
                    break;
                case '/':
                   
                    [deskCalc divide:value1];
                    NSLog(@"= %f", [deskCalc accumulator]);
                    break;
                case 'E':
                    NSLog(@"End");
                    break;
                default:
                    NSLog(@"Oparator Error");
                    break;
            }
           
           
        } while (operator != 'E');   
       
       
       
       
       
       
    }
    return 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.