//
// 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;
}
20150609类和对象_练习
最新推荐文章于 2023-01-07 10:28:19 发布