20150609类和对象_练习

//
//  BankAccount.h
//  IOS150609_OC_Exercise
//
//  Created by Peng Junlong on 15/6/10.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

//<9>.创建一个死期银行账户的类
//属性:
//账户号码
//密码(加密)
//存储金额
//存储年限
//方法:
//构造方法
//设置,账户号码,密码
//存款
//设置年限
//取款
@interface BankAccount : NSObject
{
    NSString *_account;
    NSString *_password;
    float _money;
    int _year;
}

- (id)initWithAccount:(NSString *)account
          andPassword:(NSString *)password;

//setter方法
- (void)setPassword:(NSString *)password;

- (void)setAccount:(NSString *)account;

- (void)setMoney:(float)money;

- (void)setYear:(int)year;

//getter方法
- (NSString *)account;

- (NSString *)password;

- (float)money;

- (int)year;

//存款
- (float)saveMoney:(float)aMoney;
//取款
- (float)getMoney:(float)aMoney;
@end

//==================================================
//
//  BankAccount.m
//  IOS150609_OC_Exercise
//
//  Created by qianfeng on 15/6/10.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "BankAccount.h"

//<9>.创建一个死期银行账户的类
//属性:
//账户号码
//密码(加密)
//存储金额
//存储年限
//方法:
//构造方法
//设置,账户号码,密码
//存款
//设置年限
//取款
@implementation BankAccount
- (id)initWithAccount:(NSString *)account andPassword:(NSString *)password
{
    if (self = [super init]) {
        _account = account;
        _password = password;
    }
    
    return self;
}

- (void)setPassword:(NSString *)password
{
    _password = password;
}

- (void)setAccount:(NSString *)account
{
    _account  = account;
}

- (void)setMoney:(float)money
{
    _money = money;
}

- (void)setYear:(int)year
{
    _year = year;
}

- (NSString *)account
{
    return _account;
}

- (NSString *)password
{
    return _password;
}

- (float)money
{
    return _money;
}

- (int)year
{
    return _year;
}

- (float)saveMoney:(float)aMoney
{
    [self setMoney:([self money]+aMoney)];
    return [self money];
}

- (float)getMoney:(float)aMoney
{
    [self setMoney:([self money]-aMoney)];
    return [self money];
}
@end
//============================================
//
//  Fraction.h
//  IOS150609_OC_Exercise
//
//  Created by Peng Junlong on 15/6/10.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

//创建一个分数类,
//类名: Fraction
//成员变量:  1. 分子(numerator)
//2. 分母(denominator)
//
//实例方法:
//1. 初始化方法, 带两个参数, 分别带表分子分母
//2. setter 方法, getter方法
//3. 实现两个分数的加减乘除运算
//4. 实现约分
//5. 打印分数

@interface Fraction : NSObject
{
    NSInteger _numerator;
    NSInteger _denominator;
}
- (id)initWithNumerator:(NSInteger)numerator
     andWithDenominator:(NSInteger)denominator;

- (void)setNumerator:(NSInteger)numerator;
- (void)setDenominator:(NSInteger)denominator;
- (void)setNumerator:(NSInteger)numerator andDenominator:(NSInteger)denominator;

- (NSInteger)numerator;
- (NSInteger)denominator;

- (void)addFraction:(Fraction *)aFraction;
- (void)subFraction:(Fraction *)aFraction;
- (void)multiplyFraction:(Fraction *)aFraction;
- (void)divideFraction:(Fraction *)aFraction;

- (void)reduceFraction;
- (void)printFraction;

- (NSInteger)greatCommonDivide:(NSInteger)firstNumber andSecond:(NSInteger)secondNumber;
@end
//=============================================
//
//  Fraction.m
//  IOS150609_OC_Exercise
//
//  Created by Peng Junlong on 15/6/10.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
- (id)initWithNumerator:(NSInteger)numerator
     andWithDenominator:(NSInteger)denominator
{
    if (self = [super init]) {
        _numerator = numerator;
        _denominator = denominator;
    }
    
    return self;
}

- (void)setNumerator:(NSInteger)numerator
{
    _numerator = numerator;
}
- (void)setDenominator:(NSInteger)denominator
{
    _denominator = denominator;
}
- (void)setNumerator:(NSInteger)numerator andDenominator:(NSInteger)denominator
{
    _numerator = numerator;
    _denominator = denominator;
}

- (NSInteger)numerator
{
    return _numerator;
}
- (NSInteger)denominator
{
    return _denominator;
}

- (void)addFraction:(Fraction *)aFraction
{
    NSInteger greatDived = ([self greatCommonDivide:[self denominator] andSecond:[aFraction denominator]]);
    NSInteger miniCommon = ([self denominator] * [aFraction denominator])/greatDived;
    NSInteger newNumerator = (miniCommon/[self denominator])*[self numerator] + (miniCommon/[aFraction denominator])*[aFraction numerator];
    [self setNumerator:newNumerator];
    [self setDenominator:miniCommon];
    NSLog(@"Sum = %ld/%ld",newNumerator,miniCommon);
}
- (void)subFraction:(Fraction *)aFraction
{
    NSInteger greatDived = ([self greatCommonDivide:[self denominator] andSecond:[aFraction denominator]]);
    NSInteger miniCommon = ([self denominator] * [aFraction denominator])/greatDived;
    NSInteger newNumerator = (miniCommon/[self denominator])*[self numerator] - (miniCommon/[aFraction denominator])*[aFraction numerator];
    [self setNumerator:newNumerator];
    [self setDenominator:miniCommon];
    NSLog(@"Sub = %ld/%ld",newNumerator,miniCommon);
}
- (void)multiplyFraction:(Fraction *)aFraction
{
    NSInteger newDenominator = [self denominator]*[aFraction denominator];
    NSInteger newNumerator = [self numerator] * [aFraction numerator];
    [self setNumerator:newNumerator];
    [self setDenominator:newDenominator];
    NSLog(@"Multiply = %ld/%ld",newNumerator,newDenominator);
}
- (void)divideFraction:(Fraction *)aFraction
{
    NSInteger newDenominator = [self denominator]*[aFraction numerator];
    NSInteger newNumerator = [self numerator] * [aFraction denominator];
    [self setNumerator:newNumerator];
    [self setDenominator:newDenominator];
    NSLog(@"Divided = %ld/%ld",newNumerator,newDenominator);
}

- (void)reduceFraction
{
    NSInteger greatDived = ([self greatCommonDivide:[self numerator] andSecond:[self denominator]]);
    NSInteger newDenominator = [self denominator]/greatDived;
    NSInteger newNumerator = [self numerator]/greatDived;
    [self setNumerator:newNumerator];
    [self setDenominator:newDenominator];
    NSLog(@"Reduce = %ld/%ld",newNumerator,newDenominator);

}
- (void)printFraction
{
    NSLog(@"Result = %ld/%ld",[self numerator],[self denominator]);
}

- (NSInteger)greatCommonDivide:(NSInteger)firstNumber andSecond:(NSInteger)secondNumber
{
    NSInteger temp;
    if(firstNumber<secondNumber)
    {
        //交换两个数,使大数放在a上
        temp=firstNumber;
        firstNumber=secondNumber;
        secondNumber=temp;
    }
    while(secondNumber!=0)
    {
        //利用辗除法,直到b为0为止
        temp=firstNumber%secondNumber;
        firstNumber=secondNumber;
        secondNumber=temp;
    }
    return firstNumber;
}
@end
//========================================
//
//  main.m
//  IOS150609_OC_Exercise
//
//  Created by Peng Junlong on 15/6/9.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PersonCard.h"
#import "Ex6_Dog.h"
#import "BankAccount.h"
#import "Fraction.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        PersonCard *xiaoming = [[PersonCard alloc] initWithLeft:'A' withRight:'K'];
//        NSLog(@"Left = %c ,Right = %c",[xiaoming left],[xiaoming right]);
//        
//        [xiaoming exchange];
//        NSLog(@"Left = %c ,Right = %c",[xiaoming left],[xiaoming right]);
//        
//        Ex6_Dog *dog = [[Ex6_Dog alloc] init];
//        [dog setAge:3 andWeight:30 andHeight:1];
//        [dog show];
//        BankAccount *account = [[BankAccount alloc] init];
//        [account setAccount:@"123455678890"];
//        [account setPassword:@"123456"];
//        NSLog(@"Account = %@, Password = %@",[account account],[account password]);
//        [account setMoney:1000.5];
//        [account setYear:2015];
//        NSLog(@"Money = %.2f Year = %d",[account money],[account year]);
//        [account saveMoney:999.5];
//        NSLog(@"Money = %.2f Year = %d",[account money],[account year]);
//        
//        [account getMoney:100.0];
//        NSLog(@"Money = %.2f Year = %d",[account money],[account year]);
        
        Fraction *firstFracion = [[Fraction alloc] initWithNumerator:3 andWithDenominator:4];
        Fraction *secondFracion = [[Fraction alloc] initWithNumerator:5 andWithDenominator:6];
//        [firstFracion addFraction:secondFracion];
//        [firstFracion subFraction:secondFracion];
//        [firstFracion multiplyFraction:secondFracion];
        [firstFracion divideFraction:secondFracion];
        [firstFracion reduceFraction];
        [firstFracion printFraction];
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python类和对象练习题主要是通过编写和方法来实现一些具体的功能。引用中给出了三个例子,分别是定义Point并生成对象,实现两数之和的查找,以及实现一个函数来返回一个列表中最小的k个数。这些练习题可以帮助Python初学者更好地理解类和对象的概念,并提高编程能力。 1. 定义Point并生成对象:这个练习题主要是让学生理解如何定义类和对象,并且掌握的属性和方法的使用。在这个例子中,我们定义了一个Point,它有两个属性x和y,以及一个方法saveInfo(),用于保存坐标信息。然后我们生成三个Point对象,并将它们放入一个列表中,最后打印出每个对象的属性值。 2. 实现两数之和的查找:这个练习题主要是让学生掌握字典的使用,以及如何在一个数组中查找两个数的和等于目标值。在这个例子中,我们定义了一个Solution,它有一个方法twoSum(),用于查找两个数的和等于目标值。我们首先将数组中的数字作为key,下标作为value存到一个字典中,然后遍历数组,查找另外一个数字是否在字典中,如果在就可以直接返回value了。 3. 实现一个函数来返回一个列表中最小的k个数:这个练习题主要是让学生掌握列表的使用,以及如何实现一个函数来返回一个列表中最小的k个数。在这个例子中,我们定义了一个Solution,它有一个方法mostCompetitive(),用于返回一个列表中最小的k个数。我们首先遍历列表,如果当前数字比下一个数字大,就将当前数字从列表中删除,然后继续遍历。最后返回列表中前k个数字即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值