</pre>1、<p></p><p></p><pre name="code" class="html">.h文件
@interface Fraction (MathOps);
-(Fraction *) add:(Fraction *)f;
-(Fraction *) sub:(Fraction *)f;
-(Fraction *) mul:(Fraction *)f;
-(Fraction *) div:(Fraction *)f;
-(Fraction *) invert;
@end
.m文件
-(Fraction *) invert
{
Fraction *result=[[Fraction alloc]init];
result.numerator=self.denominator;
result.denominator=self.numerator;
[result reduce];
return result;
}
<pre name="code" class="html">main文件下
[[result invert]print];
2、
@interface Fraction (Comparison)
-(BOOL) isEqualTo:(Fraction *)f;
-(int) compare:(Fraction *)f;
@end
@implementation Fraction (Comparison)
-(BOOL) isEqualTo:(Fraction *)f;
{
if(self.numerator*f.denominator==f.numerator*self.denominator)
return YES;
else
return NO;
}
-(int) compare:(Fraction *)f;
{
Fraction *result=[[Fraction alloc]init];
result.numerator=self.numerator;
result.denominator=self.denominator;
[result reduce];
if(result.numerator*f.denominator<f.numerator*result.denominator)
return -1;
else if(result.numerator*f.denominator==f.numerator*result.denominator)
return 0;
else
return 1;
}
@end
main文件
if([b isEqualTo:a]==YES)
NSLog(@"这两个分数相等");
else
NSLog(@"两个数不等");
if ([a compare: b] == -1)
NSLog(@"a小于b");
else if ([a compare: b] == 1)
NSLog(@"a大于b");
else
NSLog(@"a等于b");
3、
@interface Fraction(NSComparisonMethods)
-(BOOL) isEqualTo:(Fraction *)f;
-(BOOL) isLessThanOrEqualTo:(Fraction *)f;
-(BOOL) isLessThan:(Fraction *)f;
-(BOOL) isGreaterThanOrEqualTo:(Fraction *)f;
-(BOOL) isGreaterThan:(Fraction *)f;
-(BOOL) isNotEqualTo:(id)f;
@end
@implementation Fraction(NSComparisonMethods)
-(BOOL) isEqualTo:(Fraction *)f
{
if(self.numerator*f.denominator==f.numerator*self.denominator)
return YES;
else
return NO;
}
-(BOOL) isLessThanOrEqualTo:(Fraction *)f;
{
if(self.numerator*f.denominator<f.numerator*self.denominator /self.numerator*f.denominator==f.numerator*self.denominator)
return YES;
else
return NO;
}
-(BOOL) isLessThan:(Fraction *)f
{
if(self.numerator*f.denominator<f.numerator*self.denominator)
return YES;
else
return NO;
}
-(BOOL) isGreaterThanOrEqualTo:(Fraction *)f
{
if(self.numerator*f.denominator>f.numerator*self.denominator/self.numerator*f.denominator==f.numerator*self.denominator)
return YES;
else
return NO;
}
-(BOOL) isGreaterThan:(Fraction *)f
{
if(self.numerator*f.denominator>f.numerator*self.denominator)
return YES;
else
return NO;
}
-(BOOL) isNotEqualTo:(id)object
{
if((self.numerator*[object denominator])!=([object numerator]*self.denominator))
return YES;
else
return NO;
}
@end
main文件下:
if ([a isEqualTo:b]==YES) {
NSLog(@"a=b");
}
else if ([a isLessThanOrEqualTo:b]==YES) {
NSLog(@"a<=b");
}
else if ([a isLessThan:b]==YES) {
NSLog(@"a<b");
}
else if ([a isGreaterThanOrEqualTo:b]==YES) {
NSLog(@"a>=b");
}
else if ([a isGreaterThan:b]==YES)
NSLog(@"a>b");
if([a isNotEqualTo:b]==YES)
NSLog(@"a!=b");
else
NSLog(@"no");
4、
@interface Calculator(Trig)//分类
-(double) sin;
-(double) cos;
-(double) tan;
@end
@implementation Calculator(Trig)//分类
-(double) sin
{
return sin(self.accumulator);
}
-(double) cos
{
return cos(self.accumulator);
}
-(double) tan
{
return tan(self.accumulator);
}
@end
main文件
NSLog(@"Type in your degree:");
scanf("%lf",&Value3);
[deskCalc setAccumulator:Value3];
NSLog(@"%0.2f",sin(Value3));
5、