• objective-c第七章课后练习2


    题:改变第七章例子中print方法,增加bool参数,判断如果是YES则对分数进行约简

      1 @interface Fraction : NSObject
      2 {
      3     //int num,den;
      4     
      5 }
      6 @property int num,den;//定义属性
      7 
      8 - (void) print:(BOOL) yesOrNo;//打印并判断是否约简分数
      9 //- (void) setN: (int) n;//设置n的值
     10 //- (void) setD: (int) d;//设置d的值
     11 //- (int) n;//返回n的值
     12 //- (int) d;//返回d的值
     13 - (void) setN: (int) n andSetD: (int) d;//2个参数的方法分别设置n和d的值
     14 - (double) convertToNum;
     15 - (void) add: (Fraction *) f;//分数相加方法
     16 - (void) reduce;//分数约简方法
     17 - (Fraction *) addFraction: (Fraction *) f;//
     18 - (Fraction *) subFraction: (Fraction *) f;//
     19 - (Fraction *) mulFraction: (Fraction *) f;//
     20 - (Fraction *) divFraction: (Fraction *) f;//
     21 @end
     22 
     23 #import "Fraction.h"
     24 
     25 @implementation Fraction
     26 
     27 @synthesize num,den;//属性对应@property num,den
     28 
     29 - (void) print: (BOOL) yesOrNo//打印
     30 {
     31     if (yesOrNo == YES) {
     32         [self reduce];
     33     }
     34     NSLog(@"%i/%i",num,den);
     35 }
     36 //- (void) setN: (int) n//设置n的值
     37 //{
     38 //    num = n;
     39 //}
     40 //- (void) setD: (int) d//设置d的值
     41 //{
     42 //    den = d;
     43 //}
     44 //- (int) n//返回n的值
     45 //{
     46 //    return num;
     47 //}
     48 //- (int) d//返回d的值
     49 //{
     50 //    return den;
     51 //}
     52 
     53 - (void) setN: (int) n andSetD: (int) d
     54 {
     55     num = n;
     56     den = d;
     57     
     58 }
     59 - (double) convertToNum
     60 {
     61     if (den != 0)
     62     {
     63         return (double) num/den;
     64     
     65     }
     66     else
     67     {
     68         return NAN;
     69         
     70     }
     71 }
     72 
     73 - (void) add: (Fraction *) f
     74 {
     75     //分数相加:a/b + c/d = ((a*d) + (b*c)) / (b*d)
     76     
     77     num = num * f.den + den * f.num;
     78     den = den * f.den;
     79     
     80     [self reduce];
     81 }
     82 
     83 - (void) reduce
     84 {
     85     int u = num;
     86     int v = den;
     87     int temp;
     88     
     89     while (v != 0)
     90     {
     91         temp = u % v;
     92         u = v;
     93         v = temp;
     94         
     95     }
     96     num /= u;
     97     den /= u;
     98 }
     99 
    100 //加法
    101 - (Fraction *) addFraction: (Fraction *) f
    102 {
    103     Fraction *result = [[Fraction alloc]init];
    104     
    105     result.num = num * f.den + den * f.num;
    106     
    107     result.den = den * f.den;
    108 
    109     [result reduce];
    110     
    111     return result;
    112     
    113 }
    114 
    115 //减法
    116 - (Fraction *) subFraction: (Fraction *) f
    117 {
    118     Fraction *result = [[Fraction alloc]init];
    119     
    120     result.num = num * f.den - den * f.num;
    121     result.den = den * f.den;
    122     
    123     [result reduce];
    124     
    125     return result;
    126     
    127 }
    128 
    129 //乘法
    130 - (Fraction *) mulFraction: (Fraction *) f
    131 {
    132     Fraction *result = [[Fraction alloc]init];
    133     
    134     result.num = num * f.num;
    135     result.den = den * f.den;
    136     
    137     [result reduce];
    138     
    139     return result;
    140 }
    141 
    142 //除法
    143 - (Fraction *) divFraction: (Fraction *) f
    144 {
    145     //计算分数除以分数,第一个分数乘以第二个分数的倒数:1/2除以3/2 = 1/2 * 2/3
    146     Fraction *result = [[Fraction alloc]init];
    147     
    148     result.num = num * f.den;
    149     result.den = den * f.num;
    150     
    151     //[result reduce];
    152     
    153     return result;
    154 }
    155 @end
    156 
    157 #import <Foundation/Foundation.h>
    158 #import "Fraction.h"
    159 
    160 int main(int argc, const char * argv[]) {
    161     @autoreleasepool {
    162         Fraction *myF = [[Fraction alloc]init];
    163         Fraction *myF_2 = [[Fraction alloc]init];
    164         Fraction *myResult;
    165         
    166         
    167         [myF setN:1 andSetD:2];
    168         [myF_2 setN:3 andSetD:2];
    169         
    170         myResult = [myF subFraction:myF_2];
    171         //[myResult print];
    172         
    173         myResult = [myF mulFraction:myF_2];
    174         //[myResult print];
    175         
    176         myResult = [myF divFraction:myF_2];
    177         
    178         int i;//定义整型变量i记录输入值
    179         BOOL b;//定义BOOL类型通过i值传递判断yes or no
    180         NSLog(@"Need to Simple? 1 or 0");
    181         //NSLog(@"ifReadOnly value: %@" ,ifReadOnly?@"YES":@"NO");
    182         scanf("%i",&i);
    183         b = i;//将i值赋给b
    184         [myResult print:b];
    185         
    186         
    187         
    188     }
    189     return 0;
    190 }
  • 相关阅读:
    Discuz!NT 系统架构分析
    jquery pager
    Nhibernate分页方法
    Discuz nt模板机制
    WTclient创建OPC client方法
    OPC
    Regular Expressions in Java
    How to use VS to manipulate Access
    OPC客户端设计
    How to use VS to manipulate Excel使用MFC读写Excel
  • 原文地址:https://www.cnblogs.com/MrHead/p/5252262.html
Copyright © 2020-2023  润新知