標籤:ios object-c
部落格地址 : http://blog.csdn.net/shulianghan/article/details/41624613
1. 算術運算子
算術運算子 : 加(+), 減(-), 乘(*), 除(/), 模(%), 自增(++);
-- 其它運算 : 如果需要平方開放運算, 使用 math.h 中得方法即可;
源碼樣本 :
/************************************************************************* > File Name: 10-arithmetic.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 日 11/30 17:54:01 2014算數運算子樣本 : 加減乘除 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {// 加法樣本double a = 3.8;double b = 3.8;double sum = a + b;NSLog(@"加法 : %g", sum);//減法樣本double sub = a - b;NSLog(@"減法 : %g", sub);//乘法運算double multi = a * b;NSLog(@"乘法 : %g", multi);//除法運算double divde = a / b;NSLog(@"除法 : divide = %g", divde);NSLog(@"除法 : 3 / 0.0 = %g", 3/0.0);NSLog(@"除法 : -3/0.0 = %g", -3/0.0);//取餘運算int c = 5;int d = 2;int mod = c % d;NSLog(@"取餘 : mod = %d", mod);//自增運算int e = c++ + 6;NSLog(@"自增 : c = %d , e = %d", c, e);//次方計算, 調用 math.h 中得 pow() 方法進行次方計算;double f = pow(a, 2);NSLog(@"次方 : f = %g",f);//計算平方根int g = sqrt(4);NSLog(@"平方根 : g = %d", g);//擷取隨機數 5以內的int h = arc4random() % 5;NSLog(@"隨機數 : h = %d", h);}}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-arithmetic.m octopus-2:oc octopus$ ./a.out 2014-11-30 18:22:24.786 a.out[3198:507] 加法 : 7.62014-11-30 18:22:24.788 a.out[3198:507] 減法 : 02014-11-30 18:22:24.789 a.out[3198:507] 乘法 : 14.442014-11-30 18:22:24.790 a.out[3198:507] 除法 : divide = 12014-11-30 18:22:24.790 a.out[3198:507] 除法 : 3 / 0.0 = inf2014-11-30 18:22:24.790 a.out[3198:507] 除法 : -3/0.0 = -inf2014-11-30 18:22:24.791 a.out[3198:507] 取餘 : mod = 12014-11-30 18:22:24.792 a.out[3198:507] 自增 : c = 6 , e = 112014-11-30 18:22:24.792 a.out[3198:507] 次方 : f = 14.442014-11-30 18:22:24.793 a.out[3198:507] 平方根 : g = 22014-11-30 18:22:24.793 a.out[3198:507] 隨機數 : h = 4
2. 賦值運算子
賦值分析 :
-- OC 字串賦值 : 將 指標 賦值給 OC 字串指標, 詳情見下面的樣本;
-- C 語言字串賦值 : 將 C 字串指標, 賦值給 C 字串指標;
-- 連續賦值 : 多個變數之間連續賦值 a = b = c = 38;
源碼樣本 :
/************************************************************************* > File Name: 10-assignment.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 日 11/30 21:32:18 2014賦值運算子樣本 = ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {//將 str 字串變數 賦值給 str2 字串變數NSString *str = @"octopus";//OC 字串賦值, 直接將指標賦值給 str2 指標NSString *str2 = str;char * str3 = "han";//C 字串指標賦值 直接將指標賦值給 str4char * str4 = str3;//列印 Object-C 字串, 列印時 不帶 * 指標符號, 列印 OC 類型使用 %@ 預留位置NSLog(@"str2 = %@", str2);NSLog(@"str4 = %s", str4);//連續賦值int a, b, c;a = b = c = 38;NSLog(@"a = %d, b = %d, c = %d", a, b, c);}}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-assignment.m octopus-2:oc octopus$ octopus-2:oc octopus$ ./a.out 2014-12-01 00:44:46.793 a.out[3695:507] str2 = octopus2014-12-01 00:44:46.795 a.out[3695:507] str4 = han2014-12-01 00:44:46.795 a.out[3695:507] a = 38, b = 38, c = 38
3. 位元運算符
資料轉二進位 :
-- 正數 : 如果數字是正數, 按照正常編碼;
-- 負數 : 數字是負數, 除了符號位 1, 最高位不變, 剩下的按位取反 加 一;
-- 負數轉成位元 : 補碼形式 -1, 之後除了最高位之外按位取反;
按位 與 (&) : 有一位是 0, 就是 0;
按位 或 (|) : 有一位是 1, 就是 1;
按位 非 (~) : 所有的值 取 反;
按位 異或 (^) : 不同的取 1, 相同的取 0;
左移 (<<) : 左移, 最低位 補 0, 相當於 乘以 2 ^n;
右移 (>>) : 右移, 最高位 補 符號位, 相當於 除以 2 ^ n;
程式碼範例 :
/************************************************************************* > File Name: 10-bitOperation.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 一 12/ 1 00:52:53 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {// 按位與 01 & 10 = 00NSLog(@"1 & 2 = %d", 1 & 2);//按位或 01 & 10 = 11NSLog(@"1 | 2 = %d", 1 | 2);/* 按位非 ~ -5 源碼 10000000 00000000 00000000 00000101 反碼 11111111 11111111 11111111 11111010 (源碼按位取反, 最高位不變) 補碼 11111111 11111111 11111111 11111011 (補碼是反碼加一) 按位取反結果 100 十進位 4 */NSLog(@"~ -5 = %d", ~-5);//按位異或(相同 0, 不同 1) 011 ^ 110 = 101NSLog(@"3 ^ 6 = %d", 3 ^ 6);/* 5 左移 2 位 101 << 2 結果 10100 16 + 4 = 20 */ NSLog(@"5 << 2 = %d", 5 << 2); /* -5 左移 2 位 -5 補碼是 11111111 11111111 11111111 11111011 -5 左移後 11111111 11111111 11111111 11101100 補碼 減一 11111111 11111111 11111111 11101011 取反 10000000 00000000 00000000 00010100 結果是 -20 */ NSLog(@"-5 << 2 = %d", -5 << 2); /* 右移, 最高位補符號位-5 右移 2 位 -5 補碼 11111111 11111111 11111111 11111011右移2位 11111111 11111111 11111111 11111110減一 11111111 11111111 11111111 11111101取反 10000000 00000000 00000000 00000010 結果是 -2 */ NSLog(@"-5 >> 2 = %d", -5 >> 2);}}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-bitOperation.m octopus-2:oc octopus$ octopus-2:oc octopus$ ./a.out 2014-12-01 01:31:47.055 a.out[3813:507] 1 & 2 = 02014-12-01 01:31:47.057 a.out[3813:507] 1 | 2 = 32014-12-01 01:31:47.057 a.out[3813:507] ~ -5 = 42014-12-01 01:31:47.058 a.out[3813:507] 3 ^ 6 = 52014-12-01 01:31:47.058 a.out[3813:507] 5 << 2 = 202014-12-01 01:31:47.059 a.out[3813:507] -5 << 2 = -202014-12-01 01:31:47.059 a.out[3813:507] -5 >> 2 = -2
4. 比較子
比較運算 : 結果是正數, 1 是 真, 0 是 假;
-- == 運算 : 比較數實值型別, 即使 類型不同, 值相等, 那麼返回 true, 5.0 == 5 結果是 1;
== != 運算 : 比較數實值型別, 不管類型, 值不等, 返回 false 0;
源碼樣本 :
/************************************************************************* > File Name: 10-compare.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 一 12/ 1 01:35:57 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {//只能進行數值比較, 只進行值的比較NSLog(@"3 > 1 = %d", 3 > 1);//資料類型不同, 但是如果值 相等, 那麼返回 1NSLog(@"3 == 3.0 = %d", 3 == 3.0);NSLog(@"97 == ‘a‘ = %d", 97 == ‘a‘);}}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-compare.m octopus-2:oc octopus$ ./a.out 2014-12-01 01:44:56.099 a.out[3844:507] 3 > 1 = 12014-12-01 01:44:56.101 a.out[3844:507] 3 == 3.0 = 12014-12-01 01:44:56.101 a.out[3844:507] 97 == ‘a‘ = 1
5. 邏輯運算子
邏輯運算 : 邏輯運算只能是 BOOL 變數或者 常量才能使用該運算子;
-- 與 操作 (&&) : 都為 TRUE 結果才為 TRUE;
-- 或 操作 (||) : 只要有一個是 TRUE, 結果就是 TRUE;
-- 非 操作 (!) : 運算元有一個, !TRUE = FALSE;
-- 異或 操作 (^) : 運算元相同 返回 FALSE, 不用 返回 TRUE;
程式碼範例 :
/************************************************************************* > File Name: 10-logic.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 一 12/ 1 01:49:28 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {//非 0 是 TRUE, 非 其它數 是 FALSENSLog(@"!3 = %d", !3);NSLog(@"3 > 2 && ‘1‘ > 10 = %d", 3 > 2 && ‘1‘ > 10);NSLog(@"3 > 2 || 2 > 3 = %d", 3 > 2 || 2 > 3);NSLog(@"3 > 2 ^ 3 > 2 = %d", 3 > 2 ^ 3 > 2);}}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-logic.m octopus-2:oc octopus$ ./a.out 2014-12-01 01:54:00.282 a.out[3898:507] !3 = 02014-12-01 01:54:00.284 a.out[3898:507] 3 > 2 && ‘1‘ > 10 = 12014-12-01 01:54:00.285 a.out[3898:507] 3 > 2 || 2 > 3 = 12014-12-01 01:54:00.286 a.out[3898:507] 3 > 2 ^ 3 > 2 = 0
6. 逗號運算子
逗號運算式 : "運算式1, 運算式2 ... 運算式n", 結果返回的時 運算式n 的值;
程式碼範例 :
/************************************************************************* > File Name: 10-comma.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 一 12/ 1 01:57:31 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {int a = (3, 3 > 2);NSLog(@"a = %d", a);int b = (a = 3, a = 4, a = 5, a = 6);NSLog(@"b = %d", b);}}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-comma.m 10-comma.m:13:12: warning: expression result unused [-Wunused-value] int a = (3, 3 > 2); ^1 warning generated.octopus-2:oc octopus$ ./a.out 2014-12-01 02:00:06.466 a.out[3919:507] a = 12014-12-01 02:00:06.469 a.out[3919:507] b = 6
7. 三目運算子
三目運算子格式 : "運算式1 : 運算式2 : 運算式3", 如果運算式1 為 true, 返回 運算式2 的值, 運算式1 為 false, 返回 運算式3 的值;
樣本源碼 :
/************************************************************************* > File Name: 10-three.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 一 12/ 1 02:03:40 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {int a = 0;NSString *str = a ? @"a是TRUE" : @"a是FALSE" ;NSLog(@"%@", str);}}
執行效果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-three.m octopus-2:oc octopus$ ./a.out 2014-12-01 02:05:42.389 a.out[3930:507] a是FALSE
部落格地址 : http://blog.csdn.net/shulianghan/article/details/41624613
【IOS 開發】Object-C 運算子