//
// Fraction.h
// OC2_分数类
//
// Created by zhangxueming on 15/6/10.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
{
NSInteger _numerator;
NSInteger _denominator;
}
-
(id)initWithNumerator:(NSInteger)aNumerator andDenominator:(NSInteger)aDenominator;
-
(NSInteger)numerator;
-
(NSInteger)denominator;
- (
void)setNumerator:(NSInteger)aNumerator;
- (
void)setDenominator:(NSInteger)aDenominator;
- (
void)setNumerator:(NSInteger)aNumerator andDenominator:(NSInteger)aDenominator;
- (
void)addFraction:(Fraction *
)aFraction;
- (
void)subFraction:(Fraction *
)aFraction;
- (
void)mulFraction:(Fraction *
)aFraction;
- (
void)divFraction:(Fraction *
)aFraction;
- (
void)reduceFraction;
- (
void)printFraction;
+ (
void)testFraction;
@end
//
// Fraction.m
// OC2_分数类
//
// Created by zhangxueming on 15/6/10.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import "Fraction.h"
@implementation Fraction
-
(id)initWithNumerator:(NSInteger)aNumerator andDenominator:(NSInteger)aDenominator
{
if (self =
[super init]) {
_numerator =
aNumerator;
_denominator =
aDenominator;
}
return self;
}
-
(NSInteger)numerator
{
return self->
_numerator;
}
-
(NSInteger)denominator
{
return self->
_denominator;
}
- (
void)setNumerator:(NSInteger)aNumerator
{
self->_numerator =
aNumerator;
}
- (
void)setDenominator:(NSInteger)aDenominator
{
self->_denominator =
aDenominator;
}
- (
void)setNumerator:(NSInteger)aNumerator andDenominator:(NSInteger)aDenominator
{
self->_numerator =
aNumerator;
self->_denominator =
aDenominator;
}
- (
void)addFraction:(Fraction *
)aFraction
{
self->_numerator = [self numerator] * [aFraction denominator] + [self denominator] *
[aFraction numerator];
self->_denominator = [self denominator] *
[aFraction denominator];
}
- (
void)subFraction:(Fraction *
)aFraction
{
self->_numerator = [self numerator] * [aFraction denominator] - [self denominator] *
[aFraction numerator];
self->_denominator = [self denominator] *
[aFraction denominator];
}
- (
void)mulFraction:(Fraction *
)aFraction
{
_numerator = [self numerator] *
[aFraction numerator];
_denominator = [self denominator] *
[aFraction denominator];
}
- (
void)divFraction:(Fraction *
)aFraction
{
_numerator = [self numerator] *
[aFraction denominator];
_denominator = [self denominator] *
[aFraction numerator];
}
- (
void)reduceFraction
{
NSInteger n =
_numerator;
NSInteger d =
_denominator;
while (d) {
NSInteger temp = n%
d;
n =
d;
d =
temp;
}
_numerator /=
n;
_denominator /=
n;
}
- (
void)printFraction
{
NSLog(@"%ld/%ld", [self numerator], [self denominator]);
}
+ (
void)testFraction
{
Fraction *f1 = [[Fraction alloc] initWithNumerator:
1 andDenominator:
4];
Fraction *f2 =
[[Fraction alloc] init];
[f2 setNumerator:1 andDenominator:
4];
[f1 addFraction:f2];
[f1 reduceFraction];
[f1 printFraction];
}
@end
//
// main.m
// OC2_分数类
//
// Created by zhangxueming on 15/6/10.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Fraction.h"
int main(
int argc,
const char *
argv[]) {
@autoreleasepool {
[Fraction testFraction];
}
return 0;
}
转载于:https://www.cnblogs.com/0515offer/p/4570073.html