Amazon.com Widgets Exercise 6.2
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 07:51:16 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 6
| | | |-+  Exercise 6.2
Pages: [1]   Go Down
Print
Author Topic: Exercise 6.2  (Read 296 times)
clouded
Full Member
***
Posts: 123






« on: May 11, 2012, 07:01:02 AM »

Here's my solution, not the prettiest, but it works:

Code: (Objective-C)
// Chapter 6 Exercise 2.
// Program 6.8A displays the value in the accumulator even
// if an invalid operator is entered or division by zero is
// attempted. Fix that problem.

// Program 6.8A
// Program to evaluate simple expressions of the form
// value operator value

#import <Foundation/Foundation.h>

@interface Calculator: NSObject
-(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
{
    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
{
    accumulator /= value;
}
@end

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        
        double value1, value2;
        char operator;
        Calculator *deskCalc = [[Calculator alloc] init];
        
        NSLog (@"Type in your expression.");
        scanf ("%lf %c %lf", &value1, &operator, &value2);
        
        [deskCalc setAccumulator: value1];
        if ( operator == '+' )
            [deskCalc add: value2];
        else if ( operator == '-' )
            [deskCalc subtract: value2];
        else if ( operator == '*' )
            [deskCalc multiply: value2];
        else if ( operator == '/' )
            if ( value2 == 0 )
                NSLog (@"Division by zero.");
            else
                [deskCalc divide: value2];
            else
                NSLog (@"%c = unknown operator.", operator); // Tell user what is wrong
        
        if ( value2 != 0 )
            if ( operator == '+')
                NSLog (@"%.2f", [deskCalc accumulator]);
            else if ( operator == '-')
                NSLog (@"%.2f", [deskCalc accumulator]);
            else if ( operator == '*')
                NSLog (@"%.2f", [deskCalc accumulator]);
            else if ( operator == '/')
                NSLog (@"%.2f", [deskCalc accumulator]);
    }
    return 0;
}

Output:

Type in your expression.
1/2
0.50

Type in your expression.
1 * 2
2.00

Type in your expression.
1 + 2
3.00

Type in your expression.
1 - 2
-1.00

Type in your expression.
1 q 2
q = unknown operator.

Type in your expression.
1 x 2
x = unknown operator.

Type in your expression.
1 > 2
> = unknown operator.
« Last Edit: May 11, 2012, 08:42:23 AM by happyzhb » Logged
stuartjj
Newbie
*
Posts: 11






« Reply #1 on: June 05, 2012, 10:42:41 AM »

Clouded,
I believe that in your program if you multiply, add, or subtract by 0, the accumulator result is not printed.
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.