## Posted By

maggie_guo on 11/10/10

# Objective -c 2.0 Chapter 6.5 Exercise

/ Published in: Objective C

Begins Calculations 10 S Set Accumulator to 10 = 10.000000 contens of Accumulator 2 / Divide by 2 = 5.000000 contents of Accumulator 55 - Substract 55 = - 50.000000 100.25 S Set Accumulator to 100.25 = 100.25 4 * Multiply by 4 = 401.000000 0 E End of program = 401.000000 End of Calculations

`//Program to implement the following://Enter the expression:  number operator// + - * / S E// S: scanf mode// E: end of program/*Begins Calculations 10 S				Set Accumulator to 10 = 10.000000		contens of Accumulator 2 /				Divide by 2 = 5.000000			contents of Accumulator 55 -				Substract 55 = - 50.000000 100.25 S			Set Accumulator to 100.25 = 100.25			 4 *				Multiply by 4 = 401.000000		 0 E				End of program = 401.000000  End of Calculations */ #import <Foundation/Foundation.h>@interface Calculator : NSObject{	double accumulator;} -(void) setAccumulator :(double) value;-(void) clear;-(double) accumulator; //arithmetic methods-(double) add:(double) value;-(double) substract:(double) value;-(double) multiply:(double) value;-(double) divide :(double) value; @end@implementation Calculator -(void) setAccumulator:(double) value{	accumulator = value;}-(void) clear {	accumulator = 0.0;}-(double) accumulator {	return accumulator;} -(double) add: (double) value {	accumulator += value;	return accumulator;} -(double) substract:(double) value {	accumulator -= value;	return accumulator;}-(double) multiply:(double) value {	accumulator *= value;	return accumulator;}-(double) divide :(double) value {	if( value == 0){		NSLog(@"Division by 0");		return 99999.9;	}else {		accumulator /= value;		return accumulator;	}} @end int main (int argc, const char * argv[]) {    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];	double value1,value2;	char operator;	Calculator *deskCal = [[Calculator alloc] init];	NSLog(@"Begin Calculations");	//set Accumulator to 10	NSLog(@"Please Enter you expression");	scanf("%lf %c", &value1, &operator);	[deskCal setAccumulator:value1];	NSLog(@"=%f", [deskCal accumulator]);	if(operator =='S'){			do{			scanf("%lf %c", &value2, &operator);			if(operator =='+')				[deskCal add:value2];							else if( operator =='-')				[deskCal substract:value2];			else if(operator == '*')				[deskCal multiply:value2];			else if(operator =='/')				[deskCal divide:value2];			else if(operator =='S')				[deskCal setAccumulator:value2];				else if (operator =='E'){				NSLog(@"=%f", [deskCal accumulator]);						NSLog(@"End of Calculations");				break;			}			else 				NSLog(@"Unknown Operator");			NSLog(@"=%f", [deskCal accumulator]);				}while(operator != 'E');	}     [pool drain];    return 0;}`