iOS中:分数的用法
来源:互联网 发布:防网络诈骗主题班会 编辑:程序博客网 时间:2024/05/22 17:01
1、成员变量:分子、分母
2、方法: (1)自定义初始化方法(初始分子和分母)
(2)分子的赋值、取值方法
(3)分母的赋值取值方法
(4)打印分数信息
(5)约分
(6)加、减、乘、除运算方法,返回分数对象。
3、mian.m文件中创建分数对象,测试加、减、乘、除。
提示:加减乘除运算有一个Fraction类型的参数(参与加法运算的另外一个分数对象),有一个Fraction类型的返回值(结果)。
Fracetion .h
#import <Foundation/Foundation.h>
@interface Fracetion : NSObject
{
@public
NSInteger _Son;
NSInteger _Mother;
}
// 初始化分子分母
-(id)initWithSum:(NSInteger )son Mother:(NSInteger )mother;
// 分子赋值取值
-(NSInteger )getSon;
-(void)setSon:(NSInteger )son;
// 分母赋值取值
-(NSInteger )getMother;
-(void)setMother:(NSInteger )mother;
//打印分数
-(void)printScore;
//约分
-(void)yuefen;
//加减乘除,返回分数对象
-(Fracetion *)addWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;
-(Fracetion *)devWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;
-(Fracetion *)chuWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;
-(Fracetion *)chengWithF1:(Fracetion *)f1 F2:(Fracetion *)f2;
@end
Fracetion .m#import "Fracetion.h"
@implementation Fracetion
//初始化
-(id)initWithSum:(NSInteger )son Mother:(NSInteger )mother
{
_Son = son;
_Mother = mother;
return self;
}
//分子赋值取值
-(NSInteger )getSon
{
return _Son;
}
-(void)setSon:(NSInteger )son;
{
_Son = son;
}
//分母赋值取值
-(NSInteger )getMother
{
return_Mother;
}
-(void)setMother:(NSInteger )mother
{
_Mother = mother;
}
//打印分数
-(void)printScore
{
NSLog(@"分数:%ld/%ld", _Son ,_Mother);
}
//约分
-(void)yuefen
{
NSInteger min;
min = _Son < _Mother ?_Son :_Mother;
while (1)
{
if (_Son % min ==0 &&_Mother % min ==0 )
{
break;
}
min = min - 1;
}
NSLog(@"约分:%ld/%ld", _Son / min,_Mother / min);
}
//加减乘除,返回分数对象
-(Fracetion *)addWithF1:(Fracetion *)f1 F2:(Fracetion *)f2; //加法
{
NSInteger fen1 = [f1 getSon];
NSInteger fen2 = [f2 getSon];
NSInteger mu1 = [f1 getMother];
NSInteger mu2 = [f2 getMother];
_Son = (fen1 * mu2) + (fen2 * mu1);
_Mother = mu1 * mu2;
Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];
[a yuefen];
return a;
}
-(Fracetion *)devWithF1:(Fracetion *)f1 F2:(Fracetion *)f2 //减法
{
NSInteger fen1 = [f1 getSon];
NSInteger fen2 = [f2 getSon];
NSInteger mu1 = [f1 getMother];
NSInteger mu2 = [f2 getMother];
_Son = (fen1 * mu2) - (fen2 * mu1);
_Mother = mu1 * mu2;
Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];
[a yuefen];
return a;
}
-(Fracetion *)chuWithF1:(Fracetion *)f1 F2:(Fracetion *)f2 //除法
{
NSInteger fen1 = [f1 getSon];
NSInteger fen2 = [f2 getSon];
NSInteger mu1 = [f1 getMother];
NSInteger mu2 = [f2 getMother];
_Son = fen1 * mu2;
_Mother = mu1 * fen2;
Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];
[a yuefen];
return a;
}
-(Fracetion *)chengWithF1:(Fracetion *)f1 F2:(Fracetion *)f2 //乘法
{
NSInteger fen1 = [f1 getSon];
NSInteger fen2 = [f2 getSon];
NSInteger mu1 = [f1 getMother];
NSInteger mu2 = [f2 getMother];
_Son = fen1 * fen2;
_Mother = mu1 * mu2;
Fracetion *a = [[Fracetionalloc]initWithSum:_SonMother:_Mother];
[a yuefen];
return a;
}
@end
main.m
Fracetion *f1 = [[Fracetionalloc]initWithSum:0Mother:0];
Fracetion *f2 = [[Fracetionalloc]initWithSum:5Mother:12];
[f1 setSon:16];
[f1 setMother:24];
[f1 getSon];
[f1 getMother];
[f1 printScore];
[f1 yuefen];
[f2 printScore];
[f2 yuefen];
NSLog(@"-----------------运算区--------------------");
NSLog(@"-----------------加--------------------");
Fracetion *add = [[Fracetionalloc]addWithF1:f1F2:f2];
[add printScore];
NSLog(@"-----------------减--------------------");
Fracetion *dev = [[Fracetionalloc]devWithF1:f1F2:f2];
[dev printScore];
NSLog(@"-----------------除--------------------");
Fracetion *chu = [[Fracetionalloc]chuWithF1:f1F2:f2];
[dev printScore];
NSLog(@"-----------------乘--------------------");
Fracetion *cheng = [[Fracetionalloc]chengWithF1:f1F2:f2];
[cheng printScore];
- iOS中:分数的用法
- ios中NSPredicate的用法
- ios中self的用法
- ios中self的用法
- IOS中NSPredicate的用法
- ios中NSPredicate的用法
- iOS中copy的用法
- iOS中KVO的用法
- iOS中KVC的用法
- IOS中NSUserDefaults的用法
- iOS中copy的用法
- IOS中block的用法
- IOS中NSUserDefaults的用法
- IOS中NSUserDefaults的用法
- ios中二维码的用法
- ios中NSUserDefaults的用法
- IOS 中 hidesBottomBarWhenPushed的用法
- iOS中UITabbarController的用法
- 涣发大号放大回答哈哈
- WebKit Plugin 开发指南
- CentOS 6.3开机自动挂载磁盘和文件夹
- CCEaseElasticOut调整速度和振幅
- HDU3367 Pseudoforest 【并查集】+【贪心】
- iOS中:分数的用法
- vc6.0控制台下写日志文件
- linux下递归删除目录下所有指定类型文件
- 2014.08.09碰到问题
- IOS中定时器NSTimer的开启与关闭
- Java创建线程的常用的两种方法
- UVA 10808 - Rational Resistors(高斯消元+并查集+分数+基尔霍夫定律)
- 安卓使用JNI输出字符和LOG输出
- oracle 集群组件概念