Amazon.com Widgets Chapter 6 Exercise 4
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 08:42:47 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 6
| | | |-+  Chapter 6 Exercise 4
Pages: [1]   Go Down
Print
Author Topic: Chapter 6 Exercise 4  (Read 1371 times)
jacqueline
Newbie
*
Posts: 7






« on: February 27, 2012, 02:03:48 AM »

Here's my answer Smiley

#import <Foundation/Foundation.h>

// Create a new Calculator class as a subclass of NSObject

@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
{
    accumulator /= value;
}

- (double) result
{
    return accumulator;
}
@end


int main (int argc, const char * argv[])
{

    @autoreleasepool {
       
        double value1;
        char operator;
       
        Calculator *deskCalc = [[Calculator alloc] init];
       
     
        NSLog(@"Begin Calculations.");
       
        do {   
           
        scanf("%lf %c", &value1, &operator);
         
        if (value1 ==0 && operator =='/')
        {
            NSLog(@"Division by 0");
            [deskCalc clear];
        }
           
        if (operator == 'S')
        {
            [deskCalc clear];
            [deskCalc add:value1];
            NSLog(@"= %.2f Contents of Accumulator", [deskCalc accumulator]);
        }
       
        if (operator == '+')
        {
            [deskCalc add:value1];
            NSLog(@"= %.2f Contents of Accumulator", [deskCalc accumulator]);
        }
       
        if (operator == '-')
        {
            [deskCalc subtract:value1];
            NSLog(@"= %.2f Contents of Accumulator", [deskCalc accumulator]);
        }
       
        if (operator == '*')
        {
            [deskCalc multiply:value1];
            NSLog(@"= %.2f Contents of Accumulator", [deskCalc accumulator]);
        }
       
        if (operator == '/')
        {
            [deskCalc divide:value1];
            NSLog(@"= %.2f Contents of Accumulator", [deskCalc accumulator]);
        }
        }
       
        while (operator !='E');
       
        NSLog(@"= %.2f Contents of Accumulator", [deskCalc accumulator]);
        NSLog(@"End of Calculations.");         
        }
   
    return 0;
        }
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: February 27, 2012, 10:12:16 AM »

Good job.  Since operator can only equal one of the tested characters, you might want to try writing your test using an if....else if chain.   Or try a switch statement.

Cheers,

Steve
Logged
neilhouselander
Newbie
*
Posts: 2


Email




« Reply #2 on: March 11, 2012, 12:17:35 PM »

completed using a switch:


int main(int argc, const char * argv[])
{

    @autoreleasepool {
       

        double value1;
        char operator;
             
   
       
        NSLog(@"START");
        NSLog(@"input number then operator");
        NSLog(@"valid operators + , - , * , / , S (SET accumulator), E (END)");
           
        PrintCalc *newCalc = [[PrintCalc alloc] init];
       
do{
              scanf("%lf %c", &value1, &operator);
   
   
       
        switch (operator)
        {
            case '+':
                [newCalc add:value1];
                break;
               
            case '-':
                [newCalc subtract:value1];
                break;
               
            case '*':
                [newCalc multiply:value1];
                break;
               
            case '/':
                   if (value1 != 0)
                   {
                       [newCalc divide:value1];
                   }
                   else
                   {
                       [newCalc getAccumulator];
                   }
               
                break;
               
            case 'S':
            case 's':
                [newCalc setAccumulator:value1];
                break;
               
                case 'e':
                case 'E':
                [newCalc getAccumulator];
                break;
         
            default:
                NSLog(@"Invalid operator");
                break;
            }
           
            NSLog(@"accumulator = %lf", [newCalc getAccumulator]);
       }
       
        while (operator != 'e' );   
       
        NSLog(@"End of calculations");
        NSLog(@"value of accumulator = %lf", [newCalc getAccumulator]);
               
           
       
       
    }
    return 0;
}
& works all OK but is there a simple way of catching the error where a non number is keyed as the first item e.g. keying E on its own to end instead of 0 E. Tried using an if e.g. if (value1 = NAN) but didn't work.

Logged
rzs
Newbie
*
Posts: 5






« Reply #3 on: March 14, 2012, 09:51:09 AM »

Here's my solution which is much similar although I don't understand why you use a "do while" statement. If the operator is 'e' from the beginning there is no reason to run through the "switch case". I just use a "while" statement which cancels right away if user types an 'e' or 'E'.

Anyway...like Neilhouselander I would also like to know how to test if number is NAN. My program goes apeshit if you type a char in the numbers place:)

ps. How do you insert code in the forum with number lines on the side?

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

// @Interface

@interface PostOpCalc : NSObject

//Accumulator methods
-(void) setAccumulator : (double) value;
-(double) accumulator;
-(void) clear;

//Arithmatic methods
-(void) add : (double) value;
-(void) subtract : (double) value;
-(void) divide : (double) value;
-(void) multiply : (double) value;     
@end

// @Implementation

@implementation PostOpCalc
{
    double accumulator;
}

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

-(double) accumulator
{
    return accumulator;
}

-(void) clear
{
    accumulator = 0;
}


// arithmatic methods

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

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

-(void) divide:(double)value
{
    if (value != 0)
    {
        accumulator /= value;
    }
    else
    {
        NSLog(@"Division by zero is not possible. Accumulator has not been changed!");
    }
}

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

int main (int argc, char * argv[])
{
   
    @autoreleasepool
    {
        double number;
        char operator;
       
        PostOpCalc *newCalc = [[PostOpCalc alloc]init];
       
        NSLog (@"Type the number followed by the operator");
       
        scanf (" %lf %c", &number, &operator);
       
        //only do the following if operator is 'S'

        if (operator == 's' || operator == 'S')
        {
           
            [newCalc setAccumulator: number];
            NSLog (@"= %.2f",[newCalc accumulator]);
           
           //as long as operator is not 'E' keep asking for input and doing calculations

            while (operator != 'E' && operator != 'e')
            {
                NSLog (@"Type the number you wish to add/subtract/multiply/divide and then the operator to do so");
                scanf (" %lf %c", &number, &operator);
               
               
                switch (operator)
                {
                    case 's':   
                    case 'S':
                        [newCalc setAccumulator: number];
                        NSLog(@"= %.2f", [newCalc accumulator]);
                        break;
                       
                    case '+':
                        [newCalc add: number];
                        NSLog(@"= %.2f", [newCalc accumulator]);
                        break;
                       
                    case '-':
                        [newCalc subtract: number];
                        NSLog(@"= %.2f", [newCalc accumulator]);
                        break;
                       
                    case '*':
                    case 'x':
                        [newCalc multiply: number];
                        NSLog(@"= %.2f", [newCalc accumulator]);
                        break;
                       
                    case '/':
                        [newCalc divide: number];
                        NSLog(@"= %.2f", [newCalc accumulator]);
                        break;
                       
                    default:
                        NSLog(@"Unknown operator");
                        NSLog(@"= %.2f", [newCalc accumulator]);
                        break;
                       
                }
               
            }
           
           
        }
        else
        {
            // if the accumulator was never set with S the program ends.
            NSLog(@"Operator: %c is not valid", operator);
        }
       
    }
    return 0;
}
Logged
rzs
Newbie
*
Posts: 5






« Reply #4 on: March 14, 2012, 10:00:13 AM »

It seems I managed to post the code properly...it doesn't show in the preview...just use the button with the hash tag:)
Logged
Chingon
Newbie
*
Posts: 3






« Reply #5 on: May 01, 2012, 02:23:39 PM »

Here's my solution which is much similar although I don't understand why you use a "do while" statement. If the operator is 'e' from the beginning there is no reason to run through the "switch case". I just use a "while" statement which cancels right away if user types an 'e' or 'E'.

I used a do...while loop in my solution mainly to avoid the repetitive NSLog statements in the 'case' sections. My solution:

Code: (Objective-C)
//
//  Exercise 6.4
//
//  Simple printing calculator
//

#import <Foundation/Foundation.h>

@interface Calculator: NSObject

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

// arithmatic methods
-(void) add: (double) value;
-(void) substract: (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) substract: (double) value{
    accumulator -= value;
}

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

-(void) divide: (double) value{
    if (value != 0.0)
        accumulator /= value;
    else {
        NSLog(@"Division by 0 not possible");
    }
}
@end

int main(int argc, const char * argv[])
{
   
    @autoreleasepool {
        double    number;
        char      operator;
       
        Calculator *deskCalc = [[Calculator alloc] init];
        [deskCalc clear];
       
        NSLog(@"Enter a number and operator (+,-,*,/,S,E).");
        NSLog(@"Use S to set the accumulator to a specific value.");
        NSLog(@"Use 0 E to end the calculation.");
                     
        do {
            scanf("%lf %c", &number, &operator);
           
            switch ( operator ) {
                case '+':
                    [deskCalc add: number];
                    break;
                case '-':
                    [deskCalc substract: number];
                    break;
                case '*':
                    [deskCalc multiply: number];
                    break;
                case '/':
                    [deskCalc divide:number];
                    break;
                case 'S':
                    [deskCalc setAccumulator: number];
                    break;
                case 'E':
                    break;
                default:
                    NSLog (@"Unknown operator.");
                    break;
            }
            NSLog (@"= %.2f", [deskCalc accumulator]);
        } while ( operator != 'E' );
                   
        NSLog (@"End of Calculations.");
    }
    return 0;
}
Logged
nknighth
Newbie
*
Posts: 1


Email




« Reply #6 on: May 06, 2012, 09:52:05 PM »

I also did a switch although it is a little shorter then the one above.



int main (int argc, const char * argv[])
{

    @autoreleasepool
    {
        double n, counter;
        char operator;
        Calculator *myCalc = [[Calculator alloc] init];
        
        counter = 1;
        
        [myCalc setAccumulator: 0];
        
        while (counter != 0)
        {
            
            NSLog(@"Please type in your expression");
            scanf("%lf %c", &n, &operator);
            
            
            
            switch (operator)
            {
                case '+':
                    [myCalc add: n];
                    NSLog(@"%lf", accumulator);
                    break;
                case '-':
                    [myCalc subtract: n];
                    NSLog(@"%lf", accumulator);
                    break;
                case '*':
                case 'x':
                case 'X':
                    [myCalc multiply: n];
                    NSLog(@"%lf", accumulator);
                    break;
                case '/':
                    if ( n == 0)
                        NSLog(@"not a number");
                    else
                        [myCalc divide: n];
                    NSLog(@"%lf", accumulator);
                    break;
                case 'S':
                case 's':
                    [myCalc setAccumulator: n];
                    NSLog(@"%lf", accumulator);
                    break;
                case 'E':
                case 'e':
                    counter = 0;
                    NSLog(@"end of calculations");
                    break;
                default:
                    NSLog(@"unknown operator");
            }
        }
        
        
        
    }
    return 0;
}
Logged
clouded
Full Member
***
Posts: 123






« Reply #7 on: May 11, 2012, 12:32:50 PM »

Watch how you type on this one:

Code: (Objective-C)
// Chapter 6 Exercise 4.
// Write a program that acts as a simple printing calculator.
// The program should allow the user to type in expressions
// of the following form:
//      number     operator
// The program should recognize the following operators:
//      +   -   *   /   S   E
// The S operator tells the program to set the accumulator to
// the typed-in number, and the E operator tells the program
// that execution is to end.The arithmetic operations are
// performed on the contents of the accumulator, with the
// number that was keyed in acting as the second operand. The
// following is a sample run showing how the program should
// operate:
//
//  Begin Calculations
//  10 S                //Set Acumulator to 10
//  = 10.000000         //Contents of Accumulator
//  2/                  //Divide by 2
//  = 5.000000          //Contents of Accumulator
//  55 -                //Subtract 55
//  = -50.000000
//  100.25 S            //Set Acumulator to 100.25
//  = 100.250000
//  4*                  //Multiply by 4
//  = 401.000000
//  0E                  //End of program
//  = 401.000000
//  End of Calculations.
//
// Make sure that the program detects division by 0 and also
// checks for unknown operators. Use the Calculator class
// developed in Program 6.8 for performing your calculations.
// Note: Remember to use a space character in your scanf format
// string (e.g.,"%f %c") to skip whitespace characters in the input.

// Implement a Calculator class
#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
{
    accumulator /= value;
}
@end

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        
        double value;
        char operator;
        Calculator *deskCalc = [[Calculator alloc] init];
        
        NSLog (@"Begin Calculations: Valid Types are: +   -   *   /   S   E");
        NSLog (@"S stands for Set Value & E stands for Exit. \n\ne.g.,\t\t\"0 E\"\t\t\"1 S\"\t\tor \"5 *\"");
        
        do {
            NSLog (@"Type in your expression.");
            scanf ("%lf %c", &value, &operator);
            
            
            if ( operator == 'E' )
                ;
            else if ( operator == 'S' )
                [deskCalc setAccumulator: value];
            else if ( operator == '+' )
                [deskCalc add: value];
            else if ( operator == '-' )
                [deskCalc subtract: value];
            else if ( operator == '*' )
                [deskCalc multiply: value];
            else if ( operator == '/' )
                [deskCalc divide: value];
            NSLog (@"%.6f", [deskCalc accumulator]);
        } while ( operator != 'E' );
        NSLog (@"End of Calculations.");
        
    }
    return 0;
}
Output:

Begin Calculations: Valid Types are: +   -   *   /   S   E
S stands for Set Value & E stands for Exit.

e.g.,      "0 E"      "1 S"      or "5 *"
Type in your expression.
0 S
0.000000
Type in your expression.
6 +
6.000000
Type in your expression.
-15 -
21.000000
Type in your expression.
25 *
525.000000
Type in your expression.
-32 /
-16.406250
Type in your expression.
0 S
0.000000
Type in your expression.
-1 *
-0.000000                          <------------------------- funny to me
Type in your expression.
-3.42 /
0.000000
Type in your expression.
8.329604 +
8.329604
Type in your expression.
-.29383 -
8.623434
Type in your expression.
0 E
8.623434
End of Calculations.
« Last Edit: May 11, 2012, 05:25:15 PM by clouded » Logged
john67
Newbie
*
Posts: 32






« Reply #8 on: June 17, 2012, 03:14:13 PM »

code seems okay, just be careful when entering that you have the spaces i.e. for 12+12 it is

12_+_12 ('_' = space)

and to exit it is 12_E_23 for example.

I have reproduced my listing below, which seems to run okay, I have commented out a line because I have not implemented that function

Code: (Objective-C)


int main (int argc, const char * argv[]) {
   
    @autoreleasepool {
   
    double value1, value2; 
    char operator; 
   
    Calculator *deskcalc; 
   
    deskcalc = [Calculator alloc]; 
    deskcalc = [deskcalc init]; 
   
    [deskcalc clear]; 
   
   
    do { 
       
        NSLog (@"Enter your expression:"); 
        scanf("%lf %c %lf", &value1, &operator, &value2); 
       
        [deskcalc setAccumulator: value1]; 
       
        switch (operator)
        { 
               
            case '+': 
                [deskcalc add: value2]; 
                break; 
               
            case '-': 
                [deskcalc subtract: value2]; 
                break; 
               
            case '*': 
            case 'x': 
                [deskcalc multiply: value2]; 
                break; 
               
            case '/': 
                [deskcalc divide: value2]; 
                break; 
               
            default: 
                NSLog(@"Unknown Operator."); 
                break; 
        }         
       
        //if ([deskcalc divide_by_0] == FALSE){ 
            NSLog(@"The result is %.2f and operator is %c", [deskcalc accumulator], operator); 
        //} 
       
    } while(operator != 'E'); 
   
   
    }
 
    return 0; 
}
 
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #9 on: July 24, 2012, 05:14:52 PM »

here's mine:

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        double     value;
        char       operator;
        Calculator *deskCalc = [[Calculator alloc] init];
       
        NSLog(@"Begin Calculations");
       
           
        while (operator != 'E'){
           
            scanf(" %lf %c", &value, &operator);   
           
               
            switch (operator) {
                case 'S':
                case 's':   
                    [deskCalc setAccumulator:value];
                    break;
                case '+':
                    [deskCalc add:value];
                    break;
                case '-':   
                    [deskCalc subtract:value];
                    break;
                case '*':
                case 'X':
                case 'x':   
                    [deskCalc multiply:value];
                    break;
                case '/':
                    [deskCalc divide:value];
                    break;
                case 'E':
                case 'e':
                    operator = 'E';
                    break;
                default:   
                    NSLog(@"Unknown operator.");
                    break;
            }
            NSLog(@"= %.2f", [deskCalc accumulator]);
        }   
    }
    return 0;
}

output:

2012-07-25 01:10:13.429 prog4[5644:403] Begin Calculations
10S
2012-07-25 01:10:18.627 prog4[5644:403] = 10.00
10+
2012-07-25 01:10:24.681 prog4[5644:403] = 20.00
5-
2012-07-25 01:10:32.351 prog4[5644:403] = 15.00
3/
2012-07-25 01:10:58.562 prog4[5644:403] = 5.00
2*
2012-07-25 01:11:07.014 prog4[5644:403] = 10.00
0/
2012-07-25 01:11:20.680 prog4[5644:403] Division by zero not possible.
2012-07-25 01:11:20.680 prog4[5644:403] = 10.00
1?
2012-07-25 01:11:32.556 prog4[5644:403] Unknown operator.
2012-07-25 01:11:32.557 prog4[5644:403] = 10.00
0E
2012-07-25 01:11:41.237 prog4[5644:403] = 10.00
Logged
the_raz
Newbie
*
Posts: 3


Email




« Reply #10 on: January 15, 2013, 06:52:45 AM »

This is my try. One thing I wonder is why the while loop doesn't exit when case 'E' or 'e' is fulfilled?

Code: (Objective-C)
//
//  main.m
//  exersice 6.4 simple calculator
//
//  Created by * on 2013-01-15.
//  Copyright (c) *. All rights reserved.
//
@interface Calculator : NSObject

//accumulator methods
-(void) setAccumulator: (int) acc;
-(double) getAccumulator;

// arithmetic operators
-(void) add: (int) value;
-(void) sub: (int) value;
-(void) multiply: (int) value;
-(void) divide: (int) value;

@end

@implementation Calculator
double accumulator;

-(void) setAccumulator:(int)acc
{
    accumulator = acc;   
}
-(void) add:(int)value
{
    accumulator += value;   
}
-(void) sub:(int)value
{
    accumulator -= value;   
}
-(void) multiply:(int)value
{
    accumulator *= value;   
}
-(void) divide:(int)value
{
    if (value != 0){
        accumulator /= value;
    }
    else{
        NSLog(@"Division by zero is not allowed, accumulator was not changed");
    }
   
}
-(double) getAccumulator
{
    return accumulator;
}

@end

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        char C = 0;
        int value;
       
        Calculator *deskCalc = [Calculator new];
       
        NSLog(@"Begin calculations");
        NSLog(@"Enter like this 'number operator'");
        NSLog(@"E.g. : 4 S to start setting the accumulator to 4");
       
       
        while(C != 'E' || C != 'e'){
           
            scanf("%i %c",&value, &C);
                       
            switch (C) {
                case 's':
                case 'S':
                    [deskCalc setAccumulator:value];
                    NSLog(@"= %f",[deskCalc getAccumulator]);
                    break;
                case '+':
                    [deskCalc add:value];
                    NSLog(@"= %f",[deskCalc getAccumulator]);
                    break;
                case '-':
                    [deskCalc sub:value];
                    NSLog(@"= %f",[deskCalc getAccumulator]);
                    break;
                case 'x':
                case '*':
                    [deskCalc multiply:value];
                    NSLog(@"= %f",[deskCalc getAccumulator]);
                    break;
                case '/':
                    [deskCalc divide:value];
                    NSLog(@"= %f",[deskCalc getAccumulator]);
                    break;
                case 'e':
                case 'E':
                    NSLog(@"Calculator terminated");
                    C = 'E';
                    break;
                   
                default:
                    NSLog(@"Operator not valid");
                    break;
            }           
                       
        }                     
       
    }
    return 0;
}
Logged
afterDark
Global Moderator
Full Member
*****
Posts: 126






« Reply #11 on: January 23, 2013, 12:43:02 PM »

This condition:
Code: (Objective-C)
C != 'E' || C != 'e'
will always result in TRUE.

because it says: if either one of the two conditions is true, the result will be true.
C != 'E' is always true (so the result is TRUE) except when C has the value 'E'. But.... if C has the value 'E', the second condition C != 'e' is true!

maybe you want this
Code: (Objective-C)
C != 'E' && C != 'e'
if C is not 'E' and it is not 'e', we continue... so if it is either 'E' or 'e' we stop.

by the way... since your code sets C to be 'E' (and not 'e') in line 105 why don't you just check for C being 'E' in line 75, without bothering about 'e'?
Logged

I am just an amateur with Objective-C, don't let the moderator label fool you. Working my way through the book slowly.
JeroenJK
Newbie
*
Posts: 38







« Reply #12 on: January 27, 2013, 11:54:15 AM »

Okay, here is my version.

It's till now the longest and most complicated program I made, but after thinking good about it I managed to do it. Smiley
I have a few questions about it which will be at the bottom of the post...

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

@interface Calculator : NSObject

-(void) askUserNumberOfCalcs;
-(void) askUserTypesOfCalcs;

-(int) numberOfCalcs;

-(double) returnAccumulator;
-(void) accumulator;
-(void) clearAccumulator;
-(void) setAccumulator;

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

-(char) typeOfCalc;
-(double) value2;

//-(void) endCalc;

@end

@implementation Calculator
{
    double accumulator, value2;
    char typeOfCalc;
    int numberOfCalcs;
}

-(void) askUserNumberOfCalcs
{
    NSLog(@"How many calculations do you want to make?");
    scanf(" %i", &numberOfCalcs);
}

-(void) askUserTypesOfCalcs
{
    NSLog(@"Which calculation do you want to make now?");
    scanf(" %c %lf",&typeOfCalc, &value2);
}

-(int) numberOfCalcs
{
    return numberOfCalcs;
}

-(double) returnAccumulator
{
    return accumulator;
}

-(void) accumulator
{
    NSLog(@"The accumulator is now %.3lf", accumulator);
}

-(void) clearAccumulator
{
    accumulator = 0;
    NSLog(@"The accumulator is now %.3lf", accumulator);
}

-(void) setAccumulator
{
    NSLog(@"What accumulator do you want to start with?");
    scanf(" %lf", &accumulator);
    NSLog(@"The accumulator is now %.3lf", accumulator);
}

-(void) add: (double) value
{
    accumulator += value;
    NSLog(@"The accumulator is now %lf", accumulator);
}

-(void) substract: (double) value
{
    accumulator -= value;
    NSLog(@"The accumulator is now %.3lf", accumulator);
}

-(void) multiply: (double) value
{
    accumulator *= value;
    NSLog(@"The accumulator is now %.3lf", accumulator);
}

-(void) divide: (double) value
{
    if ( value == 0 ) {
        NSLog(@"Please don't divide by 0!");
    }
    else {
        accumulator /= value;
    NSLog(@"The accumulator is now %.3lf", accumulator);
    }
}

-(char) typeOfCalc
{
    return typeOfCalc;
}

-(double) value2
{
    return value2;
}

@end

int main(int argc, char * argv []) {
    @autoreleasepool {
        Calculator *deskCalc = [[Calculator alloc] init];
       
        int n;
        BOOL yesNo;
       
        [deskCalc setAccumulator];
        [deskCalc askUserNumberOfCalcs];
       
        if ( [deskCalc numberOfCalcs] >= 1 )
            yesNo = YES;
        else
            yesNo = NO;
       
       
        for ( n = 0; n < [deskCalc numberOfCalcs]; ++n ) {
       
            [deskCalc askUserTypesOfCalcs];
           
            switch ( [deskCalc typeOfCalc] ) {
                case '+':
                    [deskCalc add: [deskCalc value2]];
                    break;
                   
                case '-':
                    [deskCalc substract: [deskCalc value2]];
                    break;
                   
                case '*':
                    [deskCalc multiply: [deskCalc value2]];
                    break;
                   
                case '/':
                    [deskCalc divide: [deskCalc value2]];
                    break;
                   
                case 'E' || 'e':
                    n = [deskCalc numberOfCalcs];
                    NSLog(@"You ended the program!");
                   
                default:
                    NSLog(@"Invalid operator!");
                    n -= 1;
                    break;
            }
        }
       
        NSLog(@"Done calculating.\nThe final result is %.3lf.", [deskCalc returnAccumulator]);
       
    }
   
    return 0;
}

Question 1: Is it possible to decrease n by 1 (n -= 1) when divided by 0? Since I put the divide-by-zero-checker in the divide method I can't increase the n from there...

Question 2: Since I have this "scanf(" %c %lf",&typeOfCalc, &value2);" in the method askUserTypesOfCalcs the user can't just enter one character. To end the program you need to enter an 'e' or 'E', but you still need also to enter a number because scanf asks for 2 variables...

Question 3: When I type an 'e' or 'E' (with any number) I get "Invalid operator!". It does that because 'e' or 'E' isn't a valid operator, but how can I exit the whole switch?

Thank you!
Have any other tips? Just post them! Smiley

Jeroen
Logged

Just started learning Objective-C (fourth edition), so don't expect too much from me. Wink
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.