Name: Anonymous 2006-01-11 12:42
#import <stdio.h>
#import <objc/Object.h>
@interface Calc: Object
{
double total;
}
//Total methods
-(void) setTotal: (double) value;
-(void) clear;
-(double) total;
//Arithmatic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end
@implementation Calc;
-(void) setTotal: (double) value
{
total = value;
}
-(void) clear
{
total = 0;
}
-(double) total
{
return total;
}
-(void) add: (double) value
{
total += value;
}
-(void) subtract: (double) value
{
total -= value;
}
-(void) multiply: (double) value
{
total *= value;
}
-(void) divide: (double) value
{
total /= value;
}
@end
int main (int argc, char *argv[])
{
double value1, value2;
char operator;
Calc *myCalc = [[Calc alloc] init];
printf ("Type in your expression\n");
scanf ("%1f %c %1f", &value1, &operator, &value2);
[myCalc setTotal: value1];
if ( operator == '+' )
[myCalc add: value2];
else if ( operator == '-' )
[myCalc subtract: value2];
else if ( operator == '*' )
[myCalc multiply: value2];
else if ( operator == '/' )
[myCalc divide: value2];
printf ("%.2f\n", [myCalc total]);
[myCalc free];
return 0;
}
I've been trying to figure out why it keeps return nonsensical results for the past two days, and I'm sure its something really stupid which I missed. Just learning object based stuff, its pretty cool. Please help :(
#import <objc/Object.h>
@interface Calc: Object
{
double total;
}
//Total methods
-(void) setTotal: (double) value;
-(void) clear;
-(double) total;
//Arithmatic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end
@implementation Calc;
-(void) setTotal: (double) value
{
total = value;
}
-(void) clear
{
total = 0;
}
-(double) total
{
return total;
}
-(void) add: (double) value
{
total += value;
}
-(void) subtract: (double) value
{
total -= value;
}
-(void) multiply: (double) value
{
total *= value;
}
-(void) divide: (double) value
{
total /= value;
}
@end
int main (int argc, char *argv[])
{
double value1, value2;
char operator;
Calc *myCalc = [[Calc alloc] init];
printf ("Type in your expression\n");
scanf ("%1f %c %1f", &value1, &operator, &value2);
[myCalc setTotal: value1];
if ( operator == '+' )
[myCalc add: value2];
else if ( operator == '-' )
[myCalc subtract: value2];
else if ( operator == '*' )
[myCalc multiply: value2];
else if ( operator == '/' )
[myCalc divide: value2];
printf ("%.2f\n", [myCalc total]);
[myCalc free];
return 0;
}
I've been trying to figure out why it keeps return nonsensical results for the past two days, and I'm sure its something really stupid which I missed. Just learning object based stuff, its pretty cool. Please help :(