#import <Foundation/Foundation.h>
@interface Calculator : NSObject
@property int numerator, denominator;
-(void) print;
-(double) convertToNum;
@end
#import "Calculator.h"
@implementation Calculator
@synthesize numerator, denominator;
-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
{
return (double) numerator / denominator;
}
else
{
return 25.68;
}
}
@end
#import <Foundation/Foundation>
#import "Calculator.h"
int main(int argc, const char * argv[])
{
Calculator *myCalc = [[Calculator alloc] init];
@autoreleasepool
{
//int numerator, denominator;
//NSLog(@"Enter two numbers...");
//scanf("%i %i", &numerator, &denominator);
[myCalc setNumerator: 8];
[myCalc setDenominator: 0];
NSLog(@"The value of myFraction is: ");
[myCalc convertToNum];
[myCalc print];
}
return 0;
}
由于某种原因,它总是将其作为分数返回。我将它设置为 8 和 0 只是为了举例,它返回 8/0 而不是我选择的随机数来检查 else 是否正常工作。任何帮助都会很棒。