复合

第一题  定义分数(Fraction)类:
1、成员变量:分子、分母

2、方法:
(1)自定义初始化方法(初始分子和分母)
(2)分子的赋值、取值方法
(3)分母的赋值取值方法
(4)打印分数信息
(5)约分
(6)加、减、乘、除运算方法,返回分数对象。

3、mian.m文件中创建分数对象,测试加、减、乘、除。

提示:加减乘除运算有一个Fraction类型的参数(参与加法运算的另外一个分数对象),有一个Fraction类型的返回值(结果)。
main.m<pre name="code" class="objc">#import <Foundation/Foundation.h>

@interface Fraction : NSObject
{
    int _son;
    int _mother;
    
}
- (id)initWithSon:(int)son mother:(int)mother;
- (void)setSon:(int)son mother:(int)mother;
- (int)son;
- (int)mother;
- (void)print;
- (void)yf;
- (Fraction *)add:(Fraction *)f;
- (Fraction *)sub:(Fraction *)f;
- (Fraction *)mul:(Fraction *)f;
- (Fraction *)div:(Fraction *)f;
//- (void)addf1:(Fraction *)f1  f2:(Fraction *)f2;
@end
#import "Fraction.h"

@implementation Fraction
- (id)initWithSon:(int)son mother:(int)mother
{
    _son = son;
    _mother = mother;
    return self;
}
- (void)setSon:(int)son mother:(int)mother
{
    _son = son;
    _mother = mother;
}
- (int)son
{
    return _son;
}
- (int)mother
{
    return _mother;
}
- (void)print
{
    NSLog(@"%d/%d", _son, _mother);
}
- (void)yf
{
    int temp;
    int a = _son, b = _mother;
    while (0 != a % b) {
        temp = a % b;
        a = b;
        b = temp;
    }
    _son /= b;
    _mother /= b;
    
}
//- (void)addf1:(Fraction *)f1  f2:(Fraction *)f2
//{
//    f1->_son = f1->_son * f2->_mother + f1->_mother * f2->_son;
//    f1->_mother *= f2->_mother;
//    
//    
//}
- (Fraction *)add:(Fraction *)f
{
    Fraction *f3 = [[Fraction alloc] init];
    
    f3->_son = self->_son * f->_mother + self->_mother * f->_son;
    f3->_mother = self->_mother * f->_mother;
    return f3;
}
- (Fraction *)sub:(Fraction *)f
{
    Fraction *f3 = [[Fraction alloc] init];
    f3->_son = self->_son * f->_mother - self->_mother * f->_son;
    f3->_mother = self->_mother * f->_mother;
    
    return f3;
    
}
- (Fraction *)mul:(Fraction *)f
{
    Fraction *f3 = [[Fraction alloc] init];
    f3->_son = self->_son * f->_son;
    f3->_mother = self->_mother * f->_mother;
    
   return f3;
}
- (Fraction *)div:(Fraction *)f
{
    Fraction *f3 = [[Fraction alloc] init];
    f3->_son = self->_son * f->_mother;
    f3->_mother = self->_mother * f->_son;
   return f3;
}


@end


 

#import <Foundation/Foundation.h>
#import "Fraction.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        
        Fraction *f1 = [[Fraction alloc] initWithSon:7 mother:14];
        Fraction *f2 = [[Fraction alloc] initWithSon:6 mother:18];
        Fraction *result = [f1 add: f2];
        [result yf];
        [result print];
        result = [f1 sub: f2];
        [result yf];
        [result print];
        result = [f1 mul: f2];
        [result yf];
        [result print];
        result = [f1 div: f2];
        [result yf];
        [result print];
        
        
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值