Amazon.com Widgets Excersise 6.2 Solution
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 02:19:01 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
| | | |-+  Excersise 6.2 Solution
Pages: [1]   Go Down
Print
Author Topic: Excersise 6.2 Solution  (Read 220 times)
Random Logic
Newbie
*
Posts: 3






« on: July 03, 2012, 07:17:00 AM »

I am looking for feedback on my solution to the invalid operator please.

Quote
#import <Foundation/Foundation.h>

@interface Calculator : NSObject   

// accumulator methods

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

// arithmetic methods

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

@end

@implementation Calculator

{
    double accumulator;
}

-(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
{
    if (value == 0){
        printf("Error - Divide by Zero ");
        accumulator = 0;
    }
    else
        accumulator /= value;
}

@end

int main (int argc, char * argv[])
{
    @autoreleasepool {
        double value1, value2;
        char operator;
        bool test = YES;
        Calculator *deskCalc = [[Calculator alloc]init];
        printf("Type in your expression: ");
        scanf("%lf  %c  %lf", &value1, &operator, &value2);
       
        [deskCalc setAccumulator: value1];
       
        switch (operator){
            case '+':
                [deskCalc add: value2];
                test = YES;
                break;
            case '-':
                [deskCalc subtract: value2];
                test = YES;
                break;
            case '*':
                [deskCalc multiply: value2];
                test = YES;
                break;
            case '/':
                [deskCalc divide: value2]; 
                test = YES;
                break;
            default:
                printf(" Unknown operator.");
                test = NO;
                break;
        }
        if (test == YES)
            printf("%.2f", [deskCalc accumulator]);
        [deskCalc release];
        if (test == NO)
        {
            printf("");
           
        }
       
       
       
    }
   
    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.