3. 定义一个分数类(Fraction) 实例变量:分子,分母 方法:初始化方法(2个参数),便利构造器,约分,打印,加,减,乘,除。

2023-05-16

<span style="font-size:18px;">                                  <span style="color:#FF6666;">main.m</span>                             </span>
<span style="font-size:18px;">#import <Foundation/Foundation.h>
#import "Person.h"
#import "Fraction.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {

        Fraction *num = [[Fraction alloc] init];
        [num print]; //打印初始化的值 3/5
        Fraction *num1 = [Fraction fractionWithNumerator:24 denominator:36];
        [num1 print]; //打印便利构造器值 24/36
        [num1 reductionOfFraction];
        [num1 print]; // 24/36 约分后 2/3
        
        Fraction *num2 = [Fraction fractionWithNumerator:9 denominator:12];
        Fraction *result1 = [Fraction addWitfAFraction:num1 bFraction:num2];// 加   24/36 + 2/3
        [result1 print];
        Fraction *result2 = [Fraction minusWitfAFraction:num1 bFraction:num2];// 减
        [result2 print];
        Fraction *result3 = [Fraction timeWitfAFraction:num1 bFraction:num2]; // 乘
        [result3 print];
        Fraction *result4 = [Fraction divideWitfAFraction:num1 bFraction:num2]; // 除
        [result4 print];
        
    }
    return 0;
}
</span>
<span style="color:#FF6666;">                                 Fraction.h</span>
#import <Foundation/Foundation.h>

@interface Fraction : NSObject {
    NSInteger _numerator;//分子
    NSInteger _denominator;//分母
}
//自定义初始化方法
- (id)initWithNumerator:(NSInteger)aNumerator;
- (id)initWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator;
//便利构造器
+ (Fraction *)fractionWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator;

//setter和getter
- (void)setNumerator:(NSInteger)aNumerator;
- (NSInteger)numerator;
- (void)setDenominator:(NSInteger)aDenominator;
- (NSInteger)denominator;

//最大公约数:辗转相除法
- (NSInteger)greatestCommonDivisorWithOneNumber:(NSInteger)oneNumber anotherNumber:(NSInteger)anotherNumber;
//约分
- (void)reductionOfFraction;
//打印
- (void)print;
//加
+ (Fraction *)addWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
//减
+ (Fraction *)minusWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
//乘
+ (Fraction *)timeWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
//除
+ (Fraction *)divideWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
@end

<span style="color:#FF6666;">                                  Fraction.m</span>
#import "Fraction.h"

@implementation Fraction
- (instancetype)init
{
    self = [super init];
    if (self) {
        _numerator = 3;
        _denominator = 5;
    }
    return self;
}
//自定义初始化方法
- (id)initWithNumerator:(NSInteger)aNumerator {
    self = [super init];
    if (self) {
        _numerator = aNumerator;
    }
    return self;
}
- (id)initWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator {
    self = [super init];
    if (self) {
        _numerator = aNumerator;
        _denominator = aDenominator;
    }
    return self;
}

//便利构造器
+ (Fraction *)fractionWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator {
    Fraction *fraction = [[Fraction alloc] init];
    [fraction setNumerator:aNumerator];
    [fraction setDenominator:aDenominator];
    return fraction;
}

//setter和getter
- (void)setNumerator:(NSInteger)aNumerator {
    _numerator = aNumerator;
}
- (NSInteger)numerator {
    return _numerator;
}
- (void)setDenominator:(NSInteger)aDenominator {
    _denominator = aDenominator;
}
- (NSInteger)denominator {
    return _denominator;
}

//最大公约数:辗转相除法
- (NSInteger)greatestCommonDivisorWithOneNumber:(NSInteger)oneNumber anotherNumber:(NSInteger)anotherNumber {
    while (anotherNumber != 0) {
        NSInteger temp = oneNumber % anotherNumber;
        oneNumber = anotherNumber;
        anotherNumber = temp;
    }
    return oneNumber;
}
//约分
- (void)reductionOfFraction {
    NSInteger temp = [self greatestCommonDivisorWithOneNumber:_numerator anotherNumber:_denominator];
    //self即可代指类, 也可代指对象, 谁调用就代表谁.
    _numerator /= temp;
    _denominator /= temp;
}
//打印
- (void)print {
    NSLog(@"分数: %ld/%ld",_numerator, _denominator);
}
//加
+ (Fraction *)addWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction {
    Fraction *result = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.denominator;
    result.numerator = aFraction.numerator * bFraction.denominator + bFraction.numerator * aFraction.denominator;
    [result reductionOfFraction];//加完后整体约分
    return result;
}
//减
+ (Fraction *)minusWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction {
    Fraction *result = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.denominator;
    result.numerator = aFraction.numerator * bFraction.denominator - bFraction.numerator * aFraction.denominator;
    [result reductionOfFraction];
    return result;
}
//乘
+ (Fraction *)timeWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction{
    Fraction *result  = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.denominator;
    result.numerator = aFraction.numerator * bFraction.numerator;
    [result reductionOfFraction];
    return result;
}
//除
+ (Fraction *)divideWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction {
    Fraction *result = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.numerator;
    result.numerator = aFraction.numerator * bFraction.denominator;
    [result reductionOfFraction];
    return  result;
}
@end


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

3. 定义一个分数类(Fraction) 实例变量:分子,分母 方法:初始化方法(2个参数),便利构造器,约分,打印,加,减,乘,除。 的相关文章

  • k8s 集群部署ingress-nginx

    k8s 集群部署ingress nginx 环境准备 helm方式安装ingress nginx master边缘节点 创建命名空间和部署ingress 第一个ingress 例子 查看ingress controller配置 其实就是对应
  • k8s 集群部署traefik

    k8s 集群部署traefik k8s 集群部署traefik 环境准备 下载traefik helm方式安装traefik master边缘节点 执行安装 查看安装结果 安装traefik dashboard dashboard安装和查看
  • Linux系统(Centos)安装tomcat和部署Web项目

    文章结构 1 准备工作 2 在Linux下安装Tomcat8 0 3 Linux中配置tomcat的服务器启动和关闭和配置tomcat的开机启动 4 给tomcat配置用户名和密码登录 5 使用IDEA打包Maven托管的WEB项目 6 将
  • 如何判断蓝牙设备类型

    我们在开发Android的蓝牙应用时 xff0c 可能需要知道扫描到的蓝牙设备是什么类型 xff0c 然后过滤掉不符合要求的设备 xff0c 只保留符合要求的设备 xff0c 例如我们在车载系统上开发蓝牙电话应用时 xff0c 我们希望只显
  • Aspose.Words for Java 体验

    公司中要做一些导出word的工作 xff0c 经别人推荐 xff0c 使用了Aspose Words for Java xff0c 感觉很好用 xff0c 美中不足的地方就是 xff0c 它是收费软件 原理吗 xff1f 比较常规 xff0
  • 汽车制造行业OEM Tier1 Tier2指代什么?

    OEM OEM是Original Equipment Manufacturer的缩写 xff0c 通常指设备厂商 主机厂 整车厂 例如 xff1a 宝马 奔驰 奥迪 大众 丰田 国内汽车主机厂排名前十分别是上汽集团 东风汽车 北京汽车 长安
  • Android应用系统签名方法

    1 应用配置sharedUserId 在AndroidManifest xml文件根节点中加入属性 xff1a android sharedUserId 61 34 android uid system 34 2 找到系统签名文件 plat
  • Android 读取CPU/GPU运行参数(MTK平台)

    一 使用场景 Android运行一段世时间后 xff0c 系统出现卡顿 二 分析 amp 定位问题 系统卡顿 xff0c 同时又发现芯片温度很高 xff0c 怀疑是温度过高导致CPU降频 xff0c 因此我们要将一段时间内CPU的运行信息打
  • Android签名 (二) 制作签名文件

    你可能想知道 通过这篇博客可以解决哪些问题以及学到什么 xff1a 1 公司开发一个新的app xff0c 如何创建一个应用签名 xff1f 2 为了安全性 xff0c Android系统不想使用Google给的原生签名 xff0c 如何定
  • Android签名 (一) 查看签名信息

    你可能想知道 通过这篇博客可以解决哪些问题以及学到什么 xff1a 1 如果我们有一个应用 xff0c 如何查看应用的签名信息 xff1f 2 如果我们有签名原始文件 xff0c 如何查看签名文件中的签名信息 xff1f 这篇博客介绍了如何
  • L8 U3 职业生涯

    Module 1 谈论你最近的工作 1 描述工作 描述工作的形容词 让我们来看一下可以用来描述工作 xff0c 项目和客户的形容词 用enjoyable 和 engaging来描述你觉得有意思的工作或者项目 For me sales is
  • L8 U4 商务旅行

    Module 1 计划商务旅行 1 处理信息 You mean with Lindstrom 你是说和林特罗姆 xff1f Yes with Lindstrom 是的 xff0c 和林特罗姆 Exactly 完全正确 You mean yo
  • L8 U5 产品和创新

    Module 1 产品特点 1 产品特点 询问产品特性 在询问商店中的产品时 xff0c 您可能首先想询问其质量水平 top of the range 高档的middle of the range 中档的good value for mon
  • Android打包jar的两种方法

    方法一 xff1a 使用Android Studio打包 方法二 xff1a 使用Android Build System打包 一 Android Studio打包 1 新建Android Library xff0c 取名为opensdk
  • LCS 下载插件

    难度简单2收藏分享切换为英文接收动态反馈 小扣打算给自己的 VS code 安装使用插件 xff0c 初始状态下带宽每分钟可以完成 1 个插件的下载 假定每分钟选择以下两种策略之一 使用当前带宽下载插件将带宽加倍 xff08 下载插件数量随
  • 调用webservice异常总结

    发布和调用Webservice很简单 xff0c 但小问题依然不断 xff0c 特总结如下 xff1a 一 Java调用 net的webService产生 服务器未能识别 HTTP 标头 SOAPAction 的值 错误 解决方案 xff1
  • 去除Evaluation Warning : The document was created with Spire.PDF for Java.

    去除Evaluation Warning The document was created with Spire PDF for Java 最近项目中有一个需求需要把PDF文件添加页码 xff0c 最终在网上找到了用Spire去添加的方法
  • 第二章 简单网页的爬取与Xpath、Json使用 2021-09-09

    爬虫系列总目录 本章节介绍爬虫中使用的基础库用于选择 xff0c 过滤页面信息 包括requests xff0c bs4 xff0c xpath xff0c 正则表达式re xff0c json 等内容 xff0c 能够实现对简单页面的获取
  • 解决crontab定时任务多次执行

    今天使用linux crontab定时任务时 xff0c 可能由于配置不妥 xff0c 任务多执行一次 xff0c 如下是我程序的日志记录 xff08 执行了两次 xff0c 我设置的是每分钟执行1次 xff09 解决如下 重启cronta
  • ruoyi对接CAS统一身份认证

    暂定逻辑如下 xff1a 搭建CAS服务器端 xff1a 项目地址 xff1a https gitee com weigang wu cas server webapp git 项目里有二开的说明文档 xff0c 如 xff1a 按照自定义

随机推荐