今天首先是总结之前的数据类型:
注:id数据类型是OC的一种特殊的数据类型,可用来存储任何数据类型,是多态和动态绑定的基础。
开始新的讲解--算术表达式
一段代码开始今天的学习:
1 // 2 // main.m 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014年 lee. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 int main(int argc, const char * argv[]) { 12 @autoreleasepool { 13 int a = 100; 14 int b = 2; 15 int c = 25; 16 int d = 4; 17 int result; 18 result = a-b; 19 NSLog(@"a - b = %i", result); 20 21 result = b * c; 22 NSLog(@"b * c = %i", result); 23 24 result = a / c; 25 NSLog(@"a / c = %i", result); 26 27 result = a + b * c; 28 NSLog(@"a + b * c = %i", result); 29 30 result = a * b + c * d; 31 NSLog(@"a * b + c * d = %i", result); 32 } 33 return 0; 34 } 35 输出结果: 36 2014-11-06 01:16:40.458 Demo5[651:34029] a - b = 98 37 2014-11-06 01:16:40.458 Demo5[651:34029] b * c = 50 38 2014-11-06 01:16:40.458 Demo5[651:34029] a / c = 4 39 2014-11-06 01:16:40.459 Demo5[651:34029] a + b * c = 150 40 2014-11-06 01:16:40.459 Demo5[651:34029] a * b + c * d = 300 41 Program ended with exit code: 0
注:OC中算法优先级和其他语言的优先级相同,先乘除后加减,括号能改变计算顺序
运用算数表达式的规则写出计算器类,来实现基本的算术:
首先定义接口:包含 初始化(clear),get set方法,加(add)减(subtract)乘(mutiply)除(devide)方法定义
1 // 2 // NSObject+Calculator.h 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014年 lee. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface Calculator : NSObject 12 { 13 double accumulator; 14 } 15 //accmulator 16 -(void) setAccumulator: (double) value; 17 -(void) clear; 18 -(double) getAccumulator; 19 20 //arithetic methods 21 -(void) add: (double) value; 22 -(void) subtract: (double) value; 23 -(void) multiply: (double) value; 24 -(void) devide: (double) value; 25 @end
然后定义实现类,实现接口中定义的方法
1 // 2 // NSObject+Calculator.m 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014年 lee. All rights reserved. 7 // 8 9 #import "NSObject+Calculator.h" 10 11 @implementation Calculator 12 -(void) setAccumulator:(double)value 13 { 14 accumulator = value; 15 } 16 17 -(void) clear 18 { 19 accumulator = 0; 20 } 21 22 -(double) getAccumulator 23 { 24 return accumulator; 25 } 26 27 -(void) add:(double)value 28 { 29 accumulator += value; 30 } 31 32 -(void) subtract:(double)value 33 { 34 accumulator -= value; 35 } 36 37 -(void) multiply:(double)value 38 { 39 accumulator *= value; 40 } 41 42 -(void) devide:(double)value 43 { 44 accumulator /= value; 45 } 46 47 @end
最后方法调用以及输出结果
1 // 2 // main.m 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014年 lee. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "NSObject+Calculator.h" 11 12 int main(int argc, const char * argv[]) { 13 @autoreleasepool { 14 Calculator *deskCalc; 15 deskCalc = [[Calculator alloc] init]; 16 17 [deskCalc clear]; 18 [deskCalc setAccumulator: 10]; 19 NSLog(@"The result id %g", [deskCalc getAccumulator]); 20 [deskCalc add: 200]; 21 NSLog(@"The result id %g", [deskCalc getAccumulator]); 22 [deskCalc devide: 15.0]; 23 NSLog(@"The result id %g", [deskCalc getAccumulator]); 24 [deskCalc subtract: 10.0]; 25 NSLog(@"The result id %g", [deskCalc getAccumulator]); 26 [deskCalc multiply: 5]; 27 NSLog(@"The result is %g", [deskCalc getAccumulator]); 28 } 29 return 0; 30 } 31 输出结果: 32 2014-11-06 00:50:19.874 Demo5[601:25952] The result id 10 33 2014-11-06 00:50:19.875 Demo5[601:25952] The result id 210 34 2014-11-06 00:50:19.875 Demo5[601:25952] The result id 14 35 2014-11-06 00:50:19.875 Demo5[601:25952] The result id 4 36 2014-11-06 00:50:19.875 Demo5[601:25952] The result is 20 37 Program ended with exit code: 0
类和方法定义和使用参考:http://www.cnblogs.com/likun-java/p/4058448.html
时间: 2024-10-12 11:00:24