iOS中:分数的用法

定义分数(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值