1 / 41

C++ 入門

C++ 入門. 中原大學網路策進會. 講師介紹. 鄭凱文 生日: 1985.1.21 現任中原大學網路策進會總幹事. 大綱. Why C++ 資料型態 整數 (Integer) 浮點數 (Floating Point Number) 邏輯判斷 流程控制─選擇 流程控制─迴圈 陣列 (Array) 函數. Why C++ ?. 語法簡單而具彈性

winona
Download Presentation

C++ 入門

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++入門 中原大學網路策進會

  2. 講師介紹 • 鄭凱文 • 生日:1985.1.21 • 現任中原大學網路策進會總幹事

  3. 大綱 • Why C++ • 資料型態 • 整數(Integer) • 浮點數(Floating Point Number) • 邏輯判斷 • 流程控制─選擇 • 流程控制─迴圈 • 陣列(Array) • 函數

  4. Why C++ ? • 語法簡單而具彈性 • 歷經25年考驗依然屹立不搖1.大多數資深程式設計師幾乎都會C/C++ 2.速度僅次於組合語言3. C++的物件導向支援種類,僅次於Ada 4.可以跨平台ex:Linux、Ms-dos、Windows、Unix、Freebsd

  5. 如何加強程式能力? • 寫作 & 除錯 • 善用流程圖 • 註解 • 排版 • 觀摩優良程式碼

  6. 程式碼如何產生? 原始碼 組語編譯器 程式 編譯器 目的檔 組合語言 連結器 函式庫

  7. 程式架構 /****** 標題 *******/ 資料宣告 main() { statement; return EXIT_SUCCESS; } ex1

  8. DEV-C++ 4.9.9.2

  9. 簡單的輸出 • Cout cout << “ABC” << variable << ‘G’; cout << “NewLine” << endl; cout << “跳脫字元 \n”;

  10. 跳脫字元整理 • 符號 效果 • \n 換行(newline) • \0 空格(null space) • \t 與tab鍵作用相同(tab) • \b 退後一格(backspace) • \r 回到第一格(carriage return) • \” 印出雙引號 • \’ 印出單引號 • \\ 印出斜線符號

  11. 資料型態 • 何謂變數? • 合法名稱由英文字母、_(底線)、數字組成且區分大小寫 • 宣告語法: type name; //變數功用 type neme = 初始值;

  12. 整數(Integer) • 儲存範圍-2,147,483,648至2,147,483,647 • 語法:int name; ORint name = 初始值;

  13. 浮點數(Floating Point Number) • 儲存範圍 • -3.4E+38 ~ 3.4E+38 (32bit) • 語法: float name; float name = 初始值;

  14. 運算問題 • int integer = 1 / 3; • cout << integer; • (輸出結果為0) • float fpnumber = 1.0 / 3 • cout << fpnumber ; • (輸出為0.333333) • Why ?

  15. 浮點數的精確度 • 如何觀察精確度 • cou.precision(x); // x為欲顯示位數 • 浮點數為何稱為浮點數?

  16. 關於” = “號 • 其實它是有名子的→指定敘述(Assignment statement) • 跟數學中的等號有不同嗎? • Ex: average * 3 = 1 + 2 + 3; average = (1 + 2 + 3 ) / 3;

  17. 常數(Read-Only Variable) • 為何需要? • Ex: pi、e、k…… 語法: const type name = 設定值;

  18. 基底設定 • 輸入 10進位 → 直接輸入 8進位 → 數字前加0 (數字0) 16進位 → 數字前加0x • 輸出 Cout << OutType << variable; dec、oct、hex 10 、 8 、16

  19. 運算子(operator) • + - * / %(餘數) • a += b → a = a + b; • a * = b → a = a * b; • /=、%= • ++、-- • a++、++a? • a = ++a + a++ ?

  20. 數值判斷 • <= 小於等於 • < 小於 • > 大於 • >= 大於等於 • ==等於 • != 不等於

  21. 邏輯判斷 • && 且(and) • || 或(or) • ! 非(not)

  22. 流程控制─if • 語法: If (條件判斷式) 敘述; If (條件判斷式) { 敘述一; 敘述二; . . } if 條件判斷 成立 不成立 執行敘述 繼續往下執行

  23. 流程控制─if(二) if • 語法: if (condition) 敘述一; else 敘述二; 條件判斷 成立 不成立 敘述一 敘述二 繼續往下執行

  24. 流程控制─if(三) • 語法: if (condition) 敘述一; else if (conditoin_2) 敘述二; else if (conditoin_3) 敘述三; Else 敘述四; if 條件判斷 敘述二 敘述一 敘述二 繼續往下執行

  25. 流程控制─switch • 語法: Switch (運算式) { case value 1: 敘述1_1; 敘述1_2; break; case value2: 敘述2; break; default: 敘述d; } switch 數值判斷 值1 值2 非前面兩者 敘述d 敘述1_1 敘述1_2 敘述2 繼續往下執行

  26. 範例程式─奇偶數判斷 讀入數值 • 設計一程式供使用者輸入整數,再由程式判斷其為奇數或偶數。 ex2 是否為偶數 否 是 輸出”偶數” 輸出”奇數” 結束

  27. 流程控制─while • 語法: while (條件判斷式) 敘述; 條件判斷 成立 不成立 執行敘述 往下執行

  28. 流程控制─for • 語法 For (初始敘述;條件判斷式;重複敘述) 敘述; 初始敘述 條件判斷 不成立 成立 執行敘述 繼續往下執行 重複敘述

  29. Break & Continue • Break; 功用: 1.跳出switch敘述(在switch中時) 2.跳出迴圈(在for|| while中) • Continue; 功用: 直接跳至回圈起始點(即進入點)

  30. continue示範 int i; for( i = 0;i < 100;++i) { cout << "i = " << i << endl; cout << "輸入欲設定之i值:"; cin >> i; if (i == -1) continue; } ex3

  31. 陣列(Array) • 用途? • 名詞定義 element、dimention • 宣告語法 type name[元素數量] = {………..}; 存取元素: 索引(index)由0開始計算

  32. 變數範圍 int global; main() { int local_1; { int local_2; int local_1; } } //變數僅在自己的區塊起作用

  33. 變數性質 • 永久性 ex:global variable • 暫時性 ex:local variable (利用堆疊儲存) static的效果? ex4

  34. 函數 • 函數定義 回傳型態 name(type 參數name,…..) { 敘述; return 回傳值; } • 函數宣告 why? 回傳型態 name (type p_name,……);

  35. 變數參考(別名) • 函數對於參數處理方式(pass-by-value) • 有影響嗎? ex:交換兩整數的函式 void swap(int v1,int v2) { int temp; //暫存變數 temp = v1;//交換v1,v2 v1 = v2; v2 = temp; } • 利用參考 “&” ex6

  36. 函式重載 int sum (int a,int b) { return (a + b); } float sum (float a,float b) { return (a + b); }

  37. 預設參數 float ktemp (float ctemp,float b = 273) { return (ctemp + b); }

  38. Inline函式 • 他的作用? • 語法:在一般函數前加入inline即可 Ex: inline int sum (int a,int b) { return (a + b); }

  39. 函數的遞迴 • 如何計算如下數列: F(n) = n +F(n-1), F(1)=1 利用函數可以呼叫自己的特性。 ex7

  40. 補充: • Double 雙精度浮點數 • 修飾子: Short for int Long for int double、int unsigned

  41. 謝謝各位 • 有任何疑問,聯絡請洽: http://cna.cycu.edu.tw/%7Efbiceo/cnabb/ 熊熊忘記了討論版

More Related