1 / 68

第二章

第二章. 流程控制. 本章投影片僅供本書上課 教師 使用 , 非經同意請勿供網路下載或拷貝. 2-1 演算法 (Algorithm). 演算法是設計程式的藍圖,規劃出解決問題 具體步驟。 演算法的定義: 為解決某特定問題,所規劃出一系列有順序 且明確步驟。 建造房子先繪藍圖再依藍圖建造符合需求房子。 利用電腦解決某問題前  先充分瞭解問題  構思出具體可行且有效率處理步驟  這些抽象思考過程需透過演算法做具體呈現, 做為撰寫程式的依據。. 好演算法必須滿足五個條件:. 1. 有 限 性:要在有限的步驟內解決問題。

lel
Télécharger la présentation

第二章

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. 第二章 流程控制 本章投影片僅供本書上課教師使用,非經同意請勿供網路下載或拷貝

  2. 2-1 演算法 (Algorithm) • 演算法是設計程式的藍圖,規劃出解決問題具體步驟。 • 演算法的定義:為解決某特定問題,所規劃出一系列有順序且明確步驟。 • 建造房子先繪藍圖再依藍圖建造符合需求房子。 • 利用電腦解決某問題前 先充分瞭解問題 構思出具體可行且有效率處理步驟 這些抽象思考過程需透過演算法做具體呈現, 做為撰寫程式的依據。

  3. 好演算法必須滿足五個條件: 1. 有 限 性:要在有限的步驟內解決問題。 2. 明 確 性:演算法中每個步驟必須清楚表達。 3. 輸入資料:應包含零個或一個以上輸入資料。 4. 輸出資料:演算法中至少應產生一個輸出。 5. 有 效 性:每個步驟須在有限時間內完成。

  4. 演算法表示方式有兩種:  使用虛擬碼(Pseudo Code)即一般描述語言  使用流程圖(Flow Chart)

  5. 一、虛擬碼 • 使用文字敘述來說明處理問題的步驟。 • 類似程式語言。 • 此種表示方式較易改寫成任何程式語言。 • 複雜演算法大都採用此方式描述。

  6. 二、流程圖 • 流程圖是利用簡明的圖形符號來表示 程式處理問題的流程和方法。 • 藉著各種不同圖形和箭頭 來表達解決問題的順序 每種圖形代表一種作業功能 箭頭代表流程方向,可協助設計出周詳程式 不致漏掉某些部份。 • 流程圖缺點 只能表示細部邏輯,對整個程式結構較難表示。 • 複雜演算法大都不採流程圖,而採虛擬碼。

  7. 【例】由鍵盤輸入密碼,若正確顯示 Pass , 若連續輸入三次都不對,顯示 Fail。 • 使用虛擬碼 Step1 令猜的次數為 0次。 Step2 輸入密碼,次數加1。 Step3 檢查密碼是否正確? 若正確,顯示 “Pass”。 跳到步驟5。 若不正確,繼續下一步驟。 Step4 檢查次數是否超過3? 若次數未超過,跳到步驟2。 若次數超過,顯示 “Fail”。跳到步驟5。 Step5 結束程式執行。

  8. 方式2 使用流程圖

  9. 2-2 結構化程式設計 結構化程式設計技巧: 1. 使用三種基本邏輯結構 循序、選擇、重覆。 2. 由上而下的設計。 3. 模組獨立性。

  10. 一、循序結構

  11. 二、選擇結構

  12. 三、重覆結構 • 亦稱迴圈 (Loop)。 • 重覆結構可分為下列兩種: 1. 前測式重覆結構 2. 後測式重覆結構

  13. 1. 前測式重覆結構

  14. 2. 後測式重覆結構

  15. 2-3 選擇敘述

  16. 一、單向選擇 if …敘述

  17. 二、雙向選擇 if …else…敘述

  18. 三、…?…:…三元運算子

  19. // FileName : ifElse1.sln 05 static void Main(string[] args) 06 { 07 int num = 0; 08 Console.Write("請輸入正整數:"); 09 num = int.Parse(Console.ReadLine()); 10 if (num % 2 == 0) 11 Console.WriteLine("{0} 是偶數!", num); 12 else 13 Console.WriteLine("{0} 是奇數!", num); 14 Console.Read(); 15 }

  20. 2-4 巢狀選擇

  21. // FileName : ifElse2.sln 01 namespace ifElse2 02 { 03 class Program 04 { 05 static void Main(string[] args) 06 { 07 int score1 = 0, score2 = 0; 08 Console.Write("請輸入操行成績:"); 09 score1 = int.Parse(Console.ReadLine()); 10 Console.Write("請輸入學科成績:"); 11 score2 = int.Parse(Console.ReadLine());

  22. 12 if (score1 >= 90) 13 if (score2 >= 95) 14 Console.WriteLine("操行{0}分, 學科{1}分,獎學金5000元", score1, score2); 15 else 16 if (score2 >= 90) 17 Console.WriteLine("操行{0}分, 學科{1}分,獎學金2000元", score1, score2); 18 else 19 Console.WriteLine("操行{0}分,學科{1}分,未達申請條件", score1, score2); 20 else 21 Console.WriteLine("操行 {0} 分,學科 {1} 分,未達申請條件", score1, score2); 22 Console.Read(); 23 } 24 } 25 }

  23. 2-5 多向選擇

  24. // FileName : switch1.sln 01 namespace switch1 02 { 03 class Program 04 { 05 static void Main(string[] args) 06 { 07 int num1 = 0, num2 = 1; 08 Console.Write("請輸入第1個數:"); 09 num1 = int.Parse(Console.ReadLine()); 10 Console.Write("請輸入第2個數(不能為0):"); 11 num2 = int.Parse(Console.ReadLine()); 12 string op; 13 Console.Write("請輸入運算子(+、-、*、/):");

  25. 14 op = Console.ReadLine(); 15 switch (op) 16 { 17 case "+": 18 Console.WriteLine("{0} + {1} = {2}", num1, num2, num1 + num2); 19 break; 20 case "-": 21 Console.WriteLine("{0} - {1} = {2}", num1, num2, num1 - num2); 22 break; 23 case "*": 24 Console.WriteLine("{0} * {1} = {2}", num1, num2, num1 * num2); 25 break; 26 case "/": 27 Console.WriteLine("{0} / {1} = {2}", num1, num2, num1 / num2); 28 break; 29 default: 30 Console.WriteLine("運算子錯誤"); 31 break; 32 } 33 Console.Read(); 34 } 35 } 36 }

  26. 2-6 計數迴圈

  27. 一. for …迴圈

  28. // FileName :forAdd1.sln 05 static void Main(string[] args) 06 { 07 int start_num, end_num, step_num, i, sum = 0; 08 Console.Write("請輸入初值(整數):"); 09 start_num = int.Parse(Console.ReadLine()); 10 Console.Write("請輸入終值(整數):"); 11 end_num = int.Parse(Console.ReadLine()); 12 Console.Write("請輸入增值(整數):"); 13 step_num = int.Parse(Console.ReadLine()); 14 for (i = start_num; i <= end_num; i += step_num) 15 sum += i; 16 Console.Write("初值 {0} 到終值 {1} 增值為 {2} 時,總和為 {3}", start_num, end_num, step_num, sum); 17 Console.Read(); 18 }

  29. 二. Foreach迴圈

  30. 簡例

More Related