iOS中:分数的用法

来源:互联网 发布:防网络诈骗主题班会 编辑:程序博客网 时间:2024/05/22 17:01
定义分数(Fraction)类:
1、成员变量:分子、分母
2、方法:
(1)自定义初始化方法(初始分子和分母)
(2)分子的赋值、取值方法
(3)分母的赋值取值方法
(4)打印分数信息
(5)约分
(6)加、减、乘、除运算方法,返回分数对象。
3、mian.m文件中创建分数对象,测试加、减、乘、除。

提示:加减乘除运算有一个Fraction类型的参数(参与加法运算的另外一个分数对象),有一个Fraction类型的返回值(结果)。

Fracetion .h

#import <Foundation/Foundation.h>

@interface Fracetion : NSObject

{

    @public

    NSInteger _Son;

    NSInteger _Mother;

}

// 初始化分子分母

-(id)initWithSum:(NSInteger )son Mother:(NSInteger )mother;

// 分子赋值取值

-(NSInteger )getSon;

-(void)setSon:(NSInteger )son;

// 分母赋值取值

-(NSInteger )getMother;

-(void)setMother:(NSInteger )mother;

//打印分数

-(void)printScore;

//约分

-(void)yuefen;

//加减乘除,返回分数对象

-(Fracetion *)addWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;

-(Fracetion *)devWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;

-(Fracetion *)chuWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;

-(Fracetion *)chengWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;

@end

Fracetion .m

#import "Fracetion.h"


@implementation Fracetion

//初始化

-(id)initWithSum:(NSInteger )son Mother:(NSInteger )mother

{

    _Son = son;

    _Mother = mother;

    return self;

}

//分子赋值取值

-(NSInteger )getSon

{

    return _Son;

}

-(void)setSon:(NSInteger )son;

{

    _Son = son;

}

//分母赋值取值

-(NSInteger )getMother

{

    return_Mother;

}

-(void)setMother:(NSInteger )mother

{

    _Mother = mother;

}

//打印分数

-(void)printScore

{

    NSLog(@"分数:%ld/%ld", _Son ,_Mother);

}

//约分

-(void)yuefen

{

    NSInteger min;

    min = _Son < _Mother ?_Son :_Mother;

    while (1)

    {

        if (_Son % min ==0 &&_Mother % min ==0 )

        {

            break;

        }

        min = min - 1;

    }

    NSLog(@"约分:%ld/%ld", _Son / min,_Mother / min);

}

//加减乘除,返回分数对象

-(Fracetion *)addWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;                //加法

{

    NSInteger fen1 = [f1 getSon];

    NSInteger fen2 = [f2 getSon];

    NSInteger mu1 = [f1 getMother];

    NSInteger mu2 = [f2 getMother];

    _Son = (fen1 * mu2) + (fen2 * mu1);

    _Mother = mu1 * mu2;

    Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];

    [a yuefen];

    return a;

}

-(Fracetion *)devWithF1:(Fracetion *)f1 F2:(Fracetion *)f2           //减法

{

    NSInteger fen1 = [f1 getSon];

    NSInteger fen2 = [f2 getSon];

    NSInteger mu1 = [f1 getMother];

    NSInteger mu2 = [f2 getMother];

    _Son = (fen1 * mu2) - (fen2 * mu1);

    _Mother = mu1 * mu2;

    Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];

    [a yuefen];

    return a;

}

-(Fracetion *)chuWithF1:(Fracetion *)f1 F2:(Fracetion *)f2    //除法

{

    NSInteger fen1 = [f1 getSon];

    NSInteger fen2 = [f2 getSon];

    NSInteger mu1 = [f1 getMother];

    NSInteger mu2 = [f2 getMother];

    _Son = fen1 * mu2;

    _Mother = mu1 * fen2;

    Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];

    [a yuefen];

    return a;

}

-(Fracetion *)chengWithF1:(Fracetion *)f1 F2:(Fracetion *)f2  //乘法

{

    NSInteger fen1 = [f1 getSon];

    NSInteger fen2 = [f2 getSon];

    NSInteger mu1 = [f1 getMother];

    NSInteger mu2 = [f2 getMother];

    _Son = fen1 * fen2;

    _Mother = mu1 * mu2;

    Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];

    [a yuefen];

    return a;

}

@end


main.m

    Fracetion *f1 = [[Fracetionalloc]initWithSum:0Mother:0];

    Fracetion *f2 = [[Fracetionalloc]initWithSum:5Mother:12];

    [f1 setSon:16];

    [f1 setMother:24];

    [f1 getSon];

    [f1 getMother];

    [f1 printScore];

    [f1 yuefen];

    [f2 printScore];

    [f2 yuefen];

    NSLog(@"-----------------运算区--------------------");

    NSLog(@"-------------------------------------");

    Fracetion *add = [[Fracetionalloc]addWithF1:f1F2:f2];

    [add printScore];

    NSLog(@"-------------------------------------");

    Fracetion *dev = [[Fracetionalloc]devWithF1:f1F2:f2];

    [dev printScore];

    NSLog(@"-------------------------------------");

    Fracetion *chu = [[Fracetionalloc]chuWithF1:f1F2:f2];

    [dev printScore];

    NSLog(@"-------------------------------------");

    Fracetion *cheng = [[Fracetionalloc]chengWithF1:f1F2:f2];

    [cheng printScore];


0 0
原创粉丝点击