




已阅读5页,还剩60页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 第三章 結構化程式開發 本章內容包括 基本解決問題的技巧 透過由上而下 步步分解的過程開發演算法 用if與if else選擇指 選擇要做的動作 使用while重覆指 多次執 同一段程式碼 用計 器與結束符號控制重覆次 結構化程式設計方法 遞增 遞減與指派運算子 2 3 1 簡介簡介 3 2 演算法演算法 Algorithm 3 3 虛擬碼虛擬碼 Pseudocode 3 4 控制結構控制結構 Control Structures 3 5 if選擇命 選擇命 3 6 if else選擇命 選擇命 3 7 while重覆命 重覆命 3 8 開發演算法開發演算法 案 研 案 研 1 用計 器控制重覆次 用計 器控制重覆次 3 9 使用由上而下及步步分解方式開發演算法使用由上而下及步步分解方式開發演算法 案 研 案 研 2 用結束符號控制重覆次 用結束符號控制重覆次 3 10 使用由上而下及步步分解方式開發演算法使用由上而下及步步分解方式開發演算法 案 研 案 研 3 巢 控制結構巢 控制結構 3 11 指派運算子指派運算子 3 12 遞增與遞減運算子遞增與遞減運算子 第三章 結構化程式開發 3 3 1 簡介簡介 寫程式之前 對問題有通盤 解 小心規劃解決方法 寫程式時 知道有那些 建構方塊 可用 使用好的程式設計技巧 4 計算問題 Computing problems 可透過按照特定順序安排的一系 動作解決 的問題 演算法是一套程序 程序內含 要執 哪些動作 Actions 動作執 的順序 程式控制 Program control 指明指 執 順序 3 2 演算法演算法 Algorithm 5 3 3 虛擬碼虛擬碼 Pseudocode 虛擬碼 Pseudocode 人造的 幫助我們開發演算法的非正式語言 似日常語言 能實際在電腦上執 幫助我們在寫程式前思考程式寫法 容 轉換成相對的C程式語言 僅包含要進 的動作描述 6 3 4 控制結構控制結構 Control Structures 循序執 Sequential execution 指 一道接著一道執 如 integer1 1 integer2 2 sum integer1 integer2 printf Sum is d n sum 7 3 4 控制結構控制結構 Control Structures 控制權轉移 Transfer of control 下一道指 一定被執 如 scanf d scanf d if a b printf a is greater than b n printf b is less than a n printf End n 8 3 4 控制結構控制結構 Control Structures Bohm與Jacopini展示 任何程式 可以只用以下三種控制結構寫成 循序結構 Sequential 程式依撰寫順序一道命 接著一道命 執 撰擇結構 Selection C有三種 if if else 與switch 重覆結構 Repetition C 有三種 while do while 與for 9 3 4 控制結構控制結構 Figure 3 1Flowcharting C s sequence structure C循序結構的 程圖 10 3 4 控制結構控制結構 程圖 演算法的圖形表示法 使用某些代表特殊目的的符號 符號間使用具箭頭的線條 接 矩形符號 動作 代表任何種 的動作 隋圓形 代表程式或一段程式碼的開頭與結尾 11 3 4 控制結構控制結構 單一入口 單一出口的控制結構 方 控制結構互相 接 程式較容 建構 12 3 5 if 選擇指述選擇指述 選擇結構 選擇要 要做if程式區塊的動作 如 虛擬碼 Pseudocode If student s grade is greater than or equal to 60 Print Passed 假如條件為真 true 執 if的下一道指 印出 Passed 假如條件為假 false print Passed 動作會被忽 善用縮排可讓程式或虛擬碼較容 閱 13 3 5 if 選擇指述選擇指述 選擇結構 程圖表示方式 鑽石符號 決策符號 表示要做決策 內含一個可能產生true或false的運算式 測試條件 決定走那條 Fig 3 2 Flowcharting the single selection if statement 14 3 5 if 選擇指述選擇指述 選擇結構 C程式碼 if grade 60 printf Passed n if grade 60 printf Passed n if grade 60 printf Passed n C程式碼與pseudocode很接近 15 3 6 if else 選擇指述選擇指述 if 指 條件成 才執 指定動作 if grade 60 printf Passed n 測試條件運算結果成 true 印出Passed 測試條件運算結果 成 false 印 16 3 6 if else 選擇指述選擇指述 if else 指 條件成 true 執 一套指定動作 條件 成 false 執 另一套指定動作 範 如果學生成績 60分印出通過否則印出失敗 Pseudocode表示方式 If student s grade is greater than or equal to 60 Print Passed else Print Failed 17 3 6 if else 選擇指述選擇指述 程圖表示方式 Fig 3 3 Flowcharting the double selection if else statement 18 3 6 if else 選擇指述選擇指述 C程式碼 if grade 60 printf Passed n else printf Failed n 19 3 6 if else 選擇指述選擇指述 三運算元條件運算子 使用三個 測試條件 條件為true的值 條件為false的值 先前範 的C code可寫成 printf s n grade 60 Passed Failed 或寫成 grade 60 printf Passed n printf Failed n 20 3 6 if else 選擇指述選擇指述 巢 if else指 測試多種情況 在if else選擇指 內放if else選擇指 21 3 6 if else 選擇指述選擇指述 巢 if else的pseudocode If student s grade is greater than or equal to 90 Print A else If student s grade is greater than or equal to 80 Print B else If student s grade is greater than or equal to 70 Print C else If student s grade is greater than or equal to 60 Print D else Print F 22 3 6 if else 選擇指述選擇指述 複合指 將一組指 包在 內 包住區域稱為區塊 Block 區塊內可放多道指 也可宣稱變 如 if grade 60 printf Passed n else printf Failed n printf You must take this course again n 23 3 6 if else 選擇指述選擇指述 如果沒有 下 每次 會執 printf You must take this course again n 語法錯誤 Syntax errors 編譯器 compiler 會找到 輯錯誤 Logic errors 執 時間才會出現 非致命錯誤 Non fatal 程式可執 但結果 正確 如if grade 60 致命錯誤 Fatal 可能當機或 正常結束 除0錯誤 24 將以下虛擬碼轉換成C程式 Input the grade of a student If student s grade is greater than or equal to 90 Print A else If student s grade is greater than or equal to 80 Print B else If student s grade is greater than or equal to 70 Print C else If student s grade is greater than or equal to 60 Print D else Print F 25 3 7 while重覆指述while重覆指述 重覆指 Repetition structure 程式設計者指定一個動作可重覆多次 直到測 試條件 成 迴圈 loop 範 Pseudocode While there are more items on my shopping list Purchase next item and cross it off my list 26 3 7 while重覆指述while重覆指述 while重覆指 的 程圖表示法 Fig 3 4 Flowcharting the while repetition statement 27 3 7 while重覆指述while重覆指述 C程式碼 int product 2 while product 1000 product 2 product 28 3 8 設計演算法設計演算法 用計 器控制重覆次 用計 器控制重覆次 用計 器控制重覆次 迴圈一直重覆直到計 器達到特定值 重覆次 已知 如 有十個學生 加小考 成績範圍由0到100 計算小考平均成績 想一想Pseudocode怎麼寫 29 3 8 設計演算法設計演算法 用計 器控制重覆次 用計 器控制重覆次 Pseudocode Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average 想一想怎麼換成C語言 Outline 30 Outline fig03 06 c Part 1 of 2 1 Fig 3 6 fig03 06 c Fig 3 6 fig03 06 c 2 Class average program with counter controlled repetition Class average program with counter controlled repetition 3 include include 4 5 function main begins program execution function main begins program execution 6 intint main main 7 8 intint counter counter number of grade to be entered next number of grade to be entered next 9 intint grade grade grade value grade value 10 intint total total sum of grades input by user sum of grades input by user 11 intint average average average of grades average of grades 12 13 initialization phase initialization phase 14 total total 0 0 initialize total initialize total 15 counter counter 1 1 initialize loop counter initialize loop counter 16 17 processing phase processing phase 18 whilewhile counter counter 1010 loop 10 times loop 10 times 19 printf printf Enter grade Enter grade prompt for input prompt for input 20 scanf scanf d d read grade from user read grade from user 21 total total grade total total grade add grade to total add grade to total 22 counter counter counter counter 1 1 increment counter increment counter 23 end while end while 24 Outline 31 Outline fig03 06 c Part 2 of 2 Program Output Enter grade 98 Enter grade 76 Enter grade 71 Enter grade 87 Enter grade 83 Enter grade 90 Enter grade 57 Enter grade 79 Enter grade 82 Enter grade 94 Class average is 81 Enter grade 98 Enter grade 76 Enter grade 71 Enter grade 87 Enter grade 83 Enter grade 90 Enter grade 57 Enter grade 79 Enter grade 82 Enter grade 94 Class average is 81 25 termination phase termination phase 26 average total average total 1010 integer division integer division 27 28 printf printf Class average is d n Class average is d n average average display result display result 29 30 returnreturn 0 0 indicate program ended successfully indicate program ended successfully 31 32 end function main end function main 32 實習二實習二 33 3 9 由上而下設計演算法 用結束符號控制重覆次 問題 開發一個計算班級平均成績的程式 可處 任意 目的學生成績 學生成績未知 程式如何知道何時結束 34 3 9 由上而下設計演算法 用結束符號控制重覆次 使用結束符號 sentinel value 又稱為signal value dummy value 或flag value 當使用者輸入Sentinel value時結束迴圈 Sentinel value必須 是合法的資 以這個 子而言 1 如 100 90 85 63 74 36 86 15 1 35 3 9 由上而下設計演算法 用結束符號控制重覆次 演算法推演方式 由上而下 一步步細分 一開始只有一 pseudocode用 描述問題 Determine the class average for the quiz 36 3 9 由上而下設計演算法 用結束符號控制重覆次 許多問題 有三個過程 初始化 Initialization 初始化程式變 處 Processing 輸入資 值 調整程式變 結束 Termination 計算及 印最後結果 37 3 9 由上而下設計演算法 用結束符號控制重覆次 Determine the class average for the quiz 將上面那 細分成較小的工作並依序 出 Initialize variables Input sum and count the quiz grades Calculate and print the class average 38 3 9 由上而下設計演算法 用結束符號控制重覆次 Initialize variables 再細分 Initialize total to zero Initialize counter to zero 39 3 9 由上而下設計演算法 用結束符號控制重覆次 Input sum and count the quiz grades 再細分 Input the first grade possibly the sentinel While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade possibly the sentinel 40 3 9 由上而下設計演算法 用結束符號控制重覆次 Calculate and print the class average 再細分 If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print No grades were entered 41 Initialize total to zero Initialize counter to zero Input the first grade While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade possibly the sentinel If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print No grades were entered 想一想怎麼轉成想一想怎麼轉成C語言語言 3 9 由上而下設計演算法 用結束符號控制重覆次 完整的Pseudocode Outline 42 Outline fig03 08 c Part 1 of 2 1 Fig 3 8 fig03 08 c Fig 3 8 fig03 08 c 2 Class average program with sentinel controlled repetition Class average program with sentinel controlled repetition 3 include include 4 5 function main begins program execution function main begins program execution 6 intint main main voidvoid 7 8 intint counter counter number of grades entered number of grades entered 9 intint grade grade grade value grade value 10 intint total total sum of grades sum of grades 11 12 floatfloat average average number with decimal point for average number with decimal point for average 13 14 initialization phase initialization phase 15 total total 0 0 initialize total initialize total 16 counter counter 0 0 initialize loop counter initialize loop counter 17 18 processing phase processing phase 19 get first grade from user get first grade from user 20 printf printf Enter grade 1 to end Enter grade 1 to end prompt for input prompt for input 21 scanf scanf d d read grade from user read grade from user 22 23 loop while sentinel value not yet read from user loop while sentinel value not yet read from user 24 whilewhile grade grade 1 1 25 total total grade total total grade add grade to total add grade to total 26 counter counter counter counter 1 1 increment counter increment counter 27 Outline 43 Outline fig03 08 c Part 2 of 2 28 get next grade from user get next grade from user 29 printf printf Enter grade 1 to end Enter grade 1 to end prompt for input prompt for input 30 scanf scanf d d read next grade read next grade 31 end while end while 32 33 termination phase termination phase 34 if user entered at least one grade if user entered at least one grade 35 ifif counter counter 0 0 36 37 calculate average of all grades entered calculate average of all grades entered 38 average average floatfloat total counter total counter 39 40 display average with two digits of precision display average with two digits of precision 41 printf printf Class average is 2f n Class average is 2f n average average 42 end if end if 43 elseelse if no grades were entered output message if no grades were entered output message 44 printf printf No grades were entered n No grades were entered n 45 end else end else 46 47 returnreturn 0 0 indicate program ended successfully indicate program ended successfully 48 49 end function main end function main Outline 44 Outline Program Output Enter grade 1 to end 75 Enter grade 1 to end 94 Enter grade 1 to end 97 Enter grade 1 to end 88 Enter grade 1 to end 70 Enter grade 1 to end 64 Enter grade 1 to end 83 Enter grade 1 to end 89 Enter grade 1 to end 1 Class average is 82 50 Enter grade 1 to end 75 Enter grade 1 to end 94 Enter grade 1 to end 97 Enter grade 1 to end 88 Enter grade 1 to end 70 Enter grade 1 to end 64 Enter grade 1 to end 83 Enter grade 1 to end 89 Enter grade 1 to end 1 Class average is 82 50 Enter grade 1 to end 1 No grades were entered Enter grade 1 to end 1 No grades were entered 45 3 10 巢 控制結構巢 控制結構 問題 Problem 手上有10個學生的測驗結果 1 pass 2 fail 寫一個程式分析結果 假如多於8個學生通過 印出 Raise Tuition 46 3 10 巢 控制結構巢 控制結構 注意 此程式必須處 十個測試結果 可使用計 器控制迴圈次 使用 個計 器 一個計算通過人 一個計算沒過人 每個測驗結果 是一個 字 1或2 47 3 10 巢 控制結構巢 控制結構 Top level Analyze exam results and decide if tuition should be raised First Refinement Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised 48 3 10 巢 控制結構巢 控制結構 Refine Initialize variables to Initialize passes to zero Initialize failures to zero Initialize student counter to one 49 3 10 巢 控制結構巢 控制結構 Refine Input the ten quiz grades and count passes and failures to While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter 50 3 10 巢 控制結構巢 控制結構 Refine Print a summary of the exam results and decide if tuition should be raised to Print the number of passes Print the number of failures If more than eight students passed Print Raise tuition 51 Initialize passes to zero Initialize failures to zero Initialize student to one While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter Print the number of passes Print the number of failures If more than eight students passed Print Raise tuition 想一想怎麼轉成想一想怎麼轉成C語言語言 Outline 52 Outline fig03 10 c Part 1 of 2 1 Fig 3 10 fig03 10 c Fig 3 10 fig03 10 c 2 Analysis of examination results Analysis of examination results 3 include include 4 5 function main begins program execution function main begins program execution 6 intint main main 7 8 initialize variables in definitions initialize variables in definitions 9 intint passes passes 0 0 number of passes number of passes 10 intint failures failures 0 0 number of failures number of failures 11 intint student student 1 1 student counter student counter 12 intint result result one exam result one exam result 13 14 process 10 students using counter controlled loop process 10 students using counter controlled loop 15 while while student student passes 8 8 38 printf printf Raise tuition n Raise tuition n 39 end if end if 40 41 returnreturn 0 0 indicate program ended successfully indicate program ended successfully 42 43 end function main end function main Outline 54 Outline Program Output Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Passed 6 Failed 4 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Passed 6 Failed 4 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Passed 9 Failed 1 Raise tuition Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 2 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Enter Result 1 pass 2 fail 1 Passed 9 Failed 1 Raise tuition 55 3 11 指派運算子指派運算子 指派運算子簡化指派表示式 c c 3 可被簡化成c 3 使用加法指派運算子 底下形式的指述 variable variable operator expression 可簡化成 variableoperator expression 56 3 11 指派運算子指派運算子 如 d 4 d d 4 e 5 e e 5 f 3 f f 3 g 9 g g 9 57 3 11 指派運算子指派運算子 Assignment operator Sample expression Explanation Assigns Assume intint c c 3 3 d d 5 5 e e 4 4 f f 6 6 g g 1212 c c 7 7 C c C c 7 7 1010 to c d d 4 4 D d D d 4 4 1 1 to d e e 5 5 E e E e 5 5 2020 to e f f 3 3 F f F f 3 3 2 2 to f g g 9 9 G g G g 9 9 3 3 to g Fig 3 11 Arithmetic assignment operators 58 3 12 遞增與遞減運算子遞增與遞減運算子 遞增運算子 取代c 1 遞減運算子 取代 c 1 前置運算子 運算子放在變 之前 c或 c 變 在表示式 expression 運算前處 後置運算子 運算子放在變 之後 c 或c 變 在表示式 expression 運算後處 59 3 12 遞增與遞減運算子遞增與遞減運算子 範 int c 5 printf d c 印出6 int c 5 printf d c 印出5 個 子c的值 會變成6 60 3 12 遞增與遞減運算子遞增與遞減運算子 當變 在表示式中 前置運算 c printf d c 與後置運算效果一樣 c printf d c 61 3 12 遞增與遞減運算子遞增與遞減運算子 Operator Sample expression Explanation a a Increment a a by 1 then use the new value of a a in the expression in wh
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年网络直播行业规范化路径与多元化商业模式深度解析报告
- 制作日历活动课件
- 河南省商丘市柘城县2024-2025学年八年级下学期6月期末考试道德与法治试卷(含答案)
- 中医护理基础与实践
- 业务经理培训课件
- Excel基本操作培训
- 外科术后恶心呕吐的护理
- 暨创科培训课件
- 首饰制作基础培训课件
- 拜占庭的世俗教育
- 固废危废培训课件
- 水库安保服务方案
- 一例ANCA相关性血管炎患者的护理查房
- 《外科微创技术》课件
- 产品审核VDA6.5培训课件
- 如何建立与客户良好的关系
- 边防派出所知识讲座
- 消防安全隐患排查投标方案(技术标)
- 刑事案件模拟法庭剧本完整版五篇
- PSSE软件操作说明
- 教科版科学三年级下册实验报告单
评论
0/150
提交评论