Objective-C分数运算

类,方法:

//

//  Operation.h

//  Day2_可见度

//

//  Created by 王佳兴 on 14-11-1.

//  Copyright (c) 2014 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Operation : NSObject

{

    int _num1;  //分母1

    int _num2;  //分母2

    int _nun1;  //分子1

    int _nun2;  //分子2

}

- (id)initWithNum1:(int)num1 andNum2:(int)num2 andNun1:(int)nun1 andNun2:(int)nun2;


- (void)setNum1:(int)num1 setNum2:(int)num2 setNun1:(int)nun1 setNun2:(int)nun2;     //给分母分子分别赋值

- (int)num1;  //取分母1

- (int)num2;  //取分母2

- (int)nun1;  //取分子1

- (int)nun2;  //取分子2


- (void)add;   //加法

- (void)subtraction;   //减法

- (void)multiplication;    //乘法

- (void)division;  //除法


@end


方法实现:


//

//  Operation.m

//  Day2_可见度

//

//  Created by 王佳兴 on 14-11-1.

//  Copyright (c) 2014 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import "Operation.h"


@implementation Operation


- (id)initWithNum1:(int)num1 andNum2:(int)num2 andNun1:(int)nun1 andNun2:(int)nun2

{

    _num1 = num1;

    _num2 = num2;

    _nun1 = nun1;

    _nun2 = nun2;

    return self;

}

- (void)setNum1:(int)num1 setNum2:(int)num2 setNun1:(int)nun1 setNun2:(int)nun2     //给分母分子分别赋值

{

    _num1 = num1;

    _num2 = num2;

    _nun1 = nun1;

    _nun2 = nun2;

}

- (int)num1  //取分母1

{

    return _num1;

}

- (int)num2  //取分母2

{

    return _num2;

}

- (int)nun1  //取分子1

{

    return _nun1;

}

- (int)nun2  //取分子2

{

    return _nun2;

}

- (void)add   //加法

{

    

    int a = _nun1 * _num2 + _nun2 * _num1; //相加后分子

    int b = _num1 * _num2; //相加后分母

    if (a > 0 && b > 0) {

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);

    }

    if (a < 0 && b < 0) {

        a = -a;

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);

        

    }

    if (a > 0 && b < 0) {

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a < 0 && b > 0) {

        a = -a;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a == 0) {

        NSLog(@"0");

    }

}

- (void)subtraction   //减法

{

    int a = _nun1 * _num2 - _nun2 * _num1; //相加后分子

    int b = _num1 * _num2; //相加后分母

    if (a > 0 && b > 0) {

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);

    }

    if (a < 0 && b < 0) {

        a = -a;

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);


    }

    if (a > 0 && b < 0) {

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a < 0 && b > 0) {

        a = -a;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a == 0) {

        NSLog(@"0");

    }

}

- (void)multiplication    //乘法

{

    int a = _nun1 * _nun2;

    int b = _num1 * _num2;

    if (a > 0 && b > 0) {

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);

    }

    if (a < 0 && b < 0) {

        a = -a;

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);

        

    }

    if (a > 0 && b < 0) {

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a < 0 && b > 0) {

        a = -a;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a == 0) {

        NSLog(@"0");

    }

}

- (void)division  //除法

{

    int a = _nun1 * _num2;

    int b = _num1 * _nun2;

    if (a > 0 && b > 0) {

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);

    }

    if (a < 0 && b < 0) {

        a = -a;

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"%d / %d", a, b);

        

    }

    if (a > 0 && b < 0) {

        b = -b;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a < 0 && b > 0) {

        a = -a;

        int c = a < b ? a : b;

        while (1) {

            if (a % c == 0 && b % c == 0) {

                a = a / c;

                b = b / c;

                break;

            }

            c--;

        }

        NSLog(@"- %d / %d", a, b);

    }

    if (a == 0) {

        NSLog(@"0");

    }

    if (b == 0) {

        NSLog(@"除数不能为0");

    }

}


@end


主函数中调用


//

//  main.m

//  Day2_可见度

//

//  Created by 王佳兴 on 14-10-31.

//  Copyright (c) 2014 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import <Foundation/Foundation.h>


#import "Student.h"

#import "Film.h"

#import "CommonZombies.h"

#import "BarricadesZombies.h"

#import "IronDrumZombies.h"

#import "Clothes.h"

#import "Ball.h"

#import "AudiCar.h"

#import "Sport.h"

#import "Vegetable.h"

#import "Fruit.h"

#import "Teacher.h"

#import "Children.h"

#import "Dog.h"

#import "Cat.h"

#import "Bird.h"

#import "Monkey.h"

#import "Pig.h"

#import "Building.h"

#import "School.h"

#import "Train.h"

#import "GirlStar.h"

#import "Country.h"

#import "CheapPerson.h"

#import "GreatPerson.h"

#import "Tree.h"

#import "Flower.h"

#import "Grass.h"

#import "Cup.h"

#import "Classmate.h"

#import "martialArts.h"

#import "Face.h"

#import "Weapons.h"

#import "Operation.h"

int main(int argc, const char * argv[]) {

   

// 1 *********************************************************************

    Student *stu = [[Student alloc] initWithStudentName:@"王佳兴" StudentAge:23 StudentId:0001];

    

    [stu setStudentName:@"张三" setStudentAge:24 setStudentId:0002];

    

    NSLog(@"%@", [stu studentName]);

    NSLog(@"%d", [stu studentAge]);

    NSLog(@"%d", [stu studentId]);

    

// 2 *********************************************************************

    Film *film = [[Film alloc] initWithFilmName:@"心花路放" FilmStar:@"徐峥"];

    

    [film setFilmName:@"心花路放" setFilmStar:@"黄渤"];

    

    NSLog(@"%@", [film filmName]);

    NSLog(@"%@", [film filmStar]);

    

// 3 *********************************************************************

    Clothes *clo = [[Clothes alloc] initWithClothesType:@"西装" ClothesSize:95];

    

    [clo setClothesType:@"礼服" setClothesSize:100];

    

    NSLog(@"%@", [clo clothesType]);

    NSLog(@"%d", [clo clothesSize]);

    

// 4 *********************************************************************

    Ball *bal = [[Ball alloc] initWithType:@"足球" Number:10];

    

    [bal setType:@"篮球" setNumber:20];

    

    NSLog(@"%@", [bal type]);

    NSLog(@"%d", [bal number]);

    

// 5 *********************************************************************

    AudiCar *audi = [[AudiCar alloc] initWithAudiCarName:@"A8l" FuelComsumption:4.2];

    

    [audi setAudiCarName:@"R8" setFuelComsumption:4.2];

    

    NSLog(@"型号:%@", [audi audiCarName]);

    NSLog(@"排量:%.1fL", [audi fuelComsumption]);

// 6 *********************************************************************

    Sport *spo = [[Sport alloc] initWithSportName:@"足球" SportStar:@"罗纳尔多"];

    

    [spo setSportName:@"篮球" setSportStar:@"科比"];

    

    NSLog(@"%@", [spo sportName]);

    NSLog(@"%@", [spo sportStar]);

    

// 7 *********************************************************************

    Vegetable *veg = [[Vegetable alloc] initWithName:@"白菜" Weight:5];

    

    [veg setName:@"胡萝卜" setWeight:10];

    

    NSLog(@"%@", [veg name]);

    NSLog(@"%d", [veg weight]);

// 8 *********************************************************************

    Fruit *fru = [[Fruit alloc] initWithName:@"苹果" Weight:5];

    

    [fru setName:@"水蜜桃" setWeight:10];

    

    NSLog(@"%@", [fru name]);

    NSLog(@"%d", [fru weight]);

    

// 9 *********************************************************************

    Teacher *teach = [[Teacher alloc] initWithName:@"大仁哥" Course:@"OC"];

    

    [teach setName:@"老孟" setCourse:@"C"];

    

    NSLog(@"%@", [teach name]);

    NSLog(@"%@", [teach course]);


// 10 ********************************************************************

    Children *child = [[Children alloc] initWithChildrenName:@"小孩儿"];

    

    [child setChildrenName:@"小兔崽子"];

    

    NSLog(@"%@", [child childrenName]);

    

// 11 ********************************************************************

    Dog *dog = [[Dog alloc] initWithDogName:@"金毛"];

    

    [dog setDogName:@"高加索"];

    

    NSLog(@"%@", [dog dogName]);

    

// 12 ********************************************************************

    Cat *cat = [[Cat alloc] initWithCatName:@"龙猫"];

    

    [cat setCatName:@"加菲猫"];

    

    NSLog(@"%@", [cat catName]);

    

// 13 ********************************************************************

    Bird *bird = [[Bird alloc] initWithBirdName:@"鹦鹉"];

    

    [bird setBirdName:@"八哥"];

    

    NSLog(@"%@", [bird birdName]);


    

// 14 ********************************************************************

    Monkey *monkey = [[Monkey alloc] initWithMonkeyName:@"长尾猴"];

    

    [monkey setMonkeyName:@"金丝猴"];

    

    NSLog(@"%@", [monkey monkeyName]);

    

// 15 ********************************************************************

    Pig *pig = [[Pig alloc] initWithPigName:@"家猪"];

    

    [pig setPigName:@"野猪"];

    

    NSLog(@"%@", [pig pigName]);

    

// 16 ********************************************************************

    Building *build = [[Building alloc] initWithBuildingType:@"高楼"];

    

    [build setBuildingType:@"大厦"];

    

    NSLog(@"%@", [build buildingType]);

    

// 17 ********************************************************************

    School *sch = [[School alloc] initWithSchoolType:@"小学"];

    

    [sch setSchoolType:@"大学"];

    

    NSLog(@"%@", [sch schoolType]);

    

// 18 ********************************************************************

    Train *tra = [[Train alloc] initWithTrainType:@"普通火车"];

    

    [tra setTrainType:@"高速动车"];

    

    NSLog(@"%@", [tra trainType]);

    

// 19 ********************************************************************

    GirlStar *star = [[GirlStar alloc] initWithGirlStarName:@"范冰冰"];

    

    [star setGirlStarName:@"刘诗诗"];

    

    NSLog(@"%@", [star girlStarName]);

    

// 20 ********************************************************************

    Country *coun = [[Country alloc] initWithCountry:@"美国"];

    

    [coun setCountry:@"中国"];

    

    NSLog(@"%@", [coun country]);

    

// 作业1 ******************************************************************

// (1) -------------------------------------------------------------------

    CheapPerson *cperson = [[CheapPerson alloc] initWithCheapPersonName:@"暂时不写"];

    

    [cperson setCheapPersonName:@"小个不高事儿挺多"];

    

    NSLog(@"%@", [cperson cheapPersonName]);

    

// (2) -------------------------------------------------------------------

    GreatPerson *gperson = [[GreatPerson alloc] initWithGreatPersonName:@"蛋头"];

    

    [gperson setGreatPersonName:@"王佳兴Great"];

    

    NSLog(@"%@", [gperson greatPersonName]);

    

// (3) -------------------------------------------------------------------

    Tree *tre = [[Tree alloc] initWithTreeType:@"松树"];

    

    [tre setTreeType:@"梧桐树"];

    

    NSLog(@"%@", [tre treeType]);

    

// (4) -------------------------------------------------------------------

    Flower *flo = [[Flower alloc] initWithFlowerName:@"狗尾巴花"];

    

    [flo setFlowerName:@"水仙花"];

    

    NSLog(@"%@", [flo flowerName]);

    

// (5) -------------------------------------------------------------------

    Grass *gra = [[Grass alloc] initWithGrassName:@"小草"];

    

    [gra setGrassName:@"草泥马"];

    

    NSLog(@"%@", [gra grassName]);

    

// (6) -------------------------------------------------------------------

    Cup *cup = [[Cup alloc] initWithCupName:@"水杯"];

    

    [cup setCupName:@"茶杯"];

    

    NSLog(@"%@", [cup cupName]);

    

// (7) -------------------------------------------------------------------

    Classmate *mate = [[Classmate alloc] initWithClassmateName:@"李四"];

    

    [mate setClassmateName:@"周芷若"];

    

    NSLog(@"%@", [mate classmateName]);

    

// (8) -------------------------------------------------------------------

    martialArts *mart = [[martialArts alloc] initWithMartialArtsName:@"九阳神功"];

    

    [mart setMartialArtsName:@"九阴白骨爪"];

    

    NSLog(@"%@", [mart martialArtsNames]);

    

// (9) -------------------------------------------------------------------

    Face *fa = [[Face alloc] initWithFaceType:@"大脸"];

    

    [fa setFaceType:@"脸大"];

    

    NSLog(@"%@", [fa faceType]);


// (10) ------------------------------------------------------------------

    Weapons *weap = [[Weapons alloc] initWithWeaponsName:@""];

    

    [weap setWeaponsName:@"猪爪子"];

    

    NSLog(@"%@", [weap weaponsName]);

    

// 作业2 ******************************************************************

// (1) -------------------------------------------------------------------

    CommonZombies *cz = [[CommonZombies alloc] initWithTotalHP:50 LoseHPForOneTime:3];

    

    [cz loseHP];

    

// (2) -------------------------------------------------------------------

    BarricadesZombies *bz = [[BarricadesZombies alloc] initWithTotalHP:80 LoseHPForOneTime:2];

    

    [bz setProps:@"路障"];

    

// (3) -------------------------------------------------------------------

    IronDrumZombies *iz = [[IronDrumZombies alloc] initWithTotalHP:120 LoseHPForOneTime:1];

    

    [iz setProps:@"铁桶"];

    

// 作业3 ******************************************************************


    Operation * opera = [[Operation alloc] initWithNum1:5 andNum2:-8 andNun1:3 andNun2:5];

    [opera add]; //加法

    [opera subtraction]; //减法

    [opera multiplication];  //乘法

    [opera division];  //除法

    return 0;

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值