Hi and thanks for reading.
consider me a total Noob to programming. ( I did some basic for a few years back in 84 )
I am going through the "Programming in Obj. C" by Kochan. in chapter 6 program 6.7 and 6.8 are calculator samples programs.
when i enter the programs into Xcode (leopard 10.5), the program will not work unless i specifically enter a value with 2 decimal places ie: 10.44 + 4.44
if i enter it the way it wants it executes correctly on all parts. xcode complies without any errors or warning.
if i don't then the program does nothing and doesn't advance at all.
i have looked at my code and the code in the book and cant figure out what i am doing wrong.
So my question is: how can i get the program to accept any number accepted regardless of the decimal places?
here is my code:
Thank you for any help you can offer,
Greg
consider me a total Noob to programming. ( I did some basic for a few years back in 84 )
I am going through the "Programming in Obj. C" by Kochan. in chapter 6 program 6.7 and 6.8 are calculator samples programs.
when i enter the programs into Xcode (leopard 10.5), the program will not work unless i specifically enter a value with 2 decimal places ie: 10.44 + 4.44
if i enter it the way it wants it executes correctly on all parts. xcode complies without any errors or warning.
if i don't then the program does nothing and doesn't advance at all.
i have looked at my code and the code in the book and cant figure out what i am doing wrong.
So my question is: how can i get the program to accept any number accepted regardless of the decimal places?
here is my code:
Code:
#import <objc/Object.h>
#import <stdio.h>
@interface Calculator: Object
{
double accumulator;
double memory;
}
// accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;
// arithmitic functions
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
-(double) changeSign;
-(double) reciprocal; r
-(double) xSquared;
// memory functions
-(double) memoryClear;
-(double) memoryStore;
-(double) memoryRecall;
-(double) memoryAdd;
-(double) memorySubtract;
-(double) memory;
@end
@implementation Calculator;
-(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) changeSign
// change sign of accumulator
{
accumulator = -accumulator;
return accumulator;
}
-(double) reciprocal
// 1/accumulator
{
accumulator = 1/accumulator;
return accumulator;
}
-(double) xSquared
{
return accumulator = accumulator * accumulator;
}
-(double) memory
{
return memory;
}
-(double) memoryClear
{
return memory = 0;
}
-(double) memoryStore
{
return memory = accumulator;
}
-(double) memoryRecall
{
return memory;
}
-(double) memoryAdd
{
return accumulator = memory + accumulator;
}
-(double) memorySubtract
{
return accumulator = memory - accumulator;
}
@end
int main(int argc, char *argv[])
{
double value1, value2;
char operator;
Calculator *deskCalc = [[ Calculator alloc] init];
printf("type in your expression:\n");
scanf("%lf %c %lf ", &value1, &operator, &value2);
[deskCalc clear];
[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)
printf("Division by 0!!\n");
else
[deskCalc divide:value2];
else
printf(" Unknown Operator!\n");
printf("%.2f\n", [deskCalc accumulator]);
[deskCalc free];
return 0;
}
Thank you for any help you can offer,
Greg