第4章_程序的控制結(jié)構(gòu)-1.ppt_第1頁(yè)
第4章_程序的控制結(jié)構(gòu)-1.ppt_第2頁(yè)
第4章_程序的控制結(jié)構(gòu)-1.ppt_第3頁(yè)
第4章_程序的控制結(jié)構(gòu)-1.ppt_第4頁(yè)
第4章_程序的控制結(jié)構(gòu)-1.ppt_第5頁(yè)
已閱讀5頁(yè),還剩62頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第4章 程序的控制結(jié)構(gòu),C語(yǔ)言大學(xué)實(shí)用教程,哈爾濱工業(yè)大學(xué) 計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院 蘇小紅,本章學(xué)習(xí)內(nèi)容,算法的描述方法 常用算法(累加累乘、統(tǒng)計(jì)、遞推迭代、窮舉) 選擇結(jié)構(gòu)及相關(guān)控制語(yǔ)句 循環(huán)結(jié)構(gòu)及相關(guān)控制語(yǔ)句 結(jié)構(gòu)化程序設(shè)計(jì)的基本思想 Skill: Map problem to solution in flowchart and pseudocode forms Be able to develop a program containing selection and loop control structure,Consider the following . Problem: 烤蛋糕(B

2、aking a Cake) How to solve: Start 將烤箱預(yù)熱 準(zhǔn)備一個(gè)盤子 在盤子上抹上一些黃油 將面粉、雞蛋、糖和香精混合在一起攪拌均勻 將攪拌好的面粉團(tuán)放在盤子上 將盤子放到烤箱內(nèi) End,實(shí)際生活中的算法Algorithm in Real Life,Divide and Conquer Strategy (分治策略)in Algorithm,Problem: 準(zhǔn)備早餐( Prepare a Breakfast),1.Start 2.準(zhǔn)備早餐 3.End,1. Start 2. 準(zhǔn)備早餐 2.1 準(zhǔn)備一個(gè)金槍魚三明治 2.2 準(zhǔn)備一些薯?xiàng)l 2.3 沖一杯咖啡 3. End

3、,Divide and Conquer Strategy (分治策略)in Algorithm,1. Start 2.準(zhǔn)備早餐 2.1 準(zhǔn)備一個(gè)金槍魚三明治 2.1.1 拿來(lái)兩片面包 2.1.2 準(zhǔn)備一些金槍魚醬 2.2 準(zhǔn)備一些薯片 2.3 沖一杯咖啡 3. End,Divide and Conquer Strategy (分治策略)in Algorithm,1. Start 2.準(zhǔn)備早餐 2.1 準(zhǔn)備一個(gè)金槍魚三明治 2.1.1 拿來(lái)兩片面包 2.1.2 準(zhǔn)備一些金槍魚醬 2.2 準(zhǔn)備一些薯片 2.2.1 將土豆切成片 2.2.2 油炸這些土豆片 2.3 沖一杯咖啡 3. End,Divi

4、de and Conquer Strategy (分治策略)in Algorithm,Divide and Conquer Strategy (分治策略)in Algorithm,1. Start 2.準(zhǔn)備早餐 2.1 準(zhǔn)備一個(gè)金槍魚三明治 2.1.1 拿來(lái)兩片面包 2.1.2 準(zhǔn)備一些金槍魚醬 2.2 準(zhǔn)備一些薯片 2.2.1 將土豆切成片 2.2.2 油炸這些土豆片 2.3 沖一杯咖啡 2.3.1 燒些開水放入杯中 2.3.2 在水杯中加入一些咖啡和糖 3. End,What is the connection between these real life processes and a

5、lgorithm?,Something to ponder ,算法( Algorithm )的概念,數(shù)據(jù)結(jié)構(gòu) + 算法 = 程序 只對(duì)面向過(guò)程的語(yǔ)言(C)成立 面向?qū)ο蟪绦?= 對(duì)象 + 消息 算法: 為解決一個(gè)具體問(wèn)題而采取的確定的有限的操作步驟,僅指計(jì)算機(jī)能執(zhí)行的算法 A specific and step-by-step set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at som

6、e point,算法的特性,有窮性 在合理的時(shí)間內(nèi)完成 確定性,無(wú)歧義 如果x0,則輸出Yes 如果x0,則輸出No 有效性 能有效執(zhí)行 負(fù)數(shù)開平方 沒(méi)有輸入或有多個(gè)輸入 有一個(gè)或多個(gè)輸出,算法的表示方法,自然語(yǔ)言描述 傳統(tǒng)流程圖(Flowchart) 在1966年,Bohra 與 Jacopini 提出 N-S結(jié)構(gòu)化流程圖 1973年,美國(guó)學(xué)者I.Nassi 和 B.Shneiderman 提出 偽碼(Pseudocode)表示,流程圖(Flowchart),Flowchart represents algorithm graphically.,Are the steps in the al

7、gorithm discussed earlier specific enough to be executed by computer?,Something to ponder ,問(wèn)題求解步驟(Problem Solving Process),Input,Process,Output,First identify the input and output of the problem.,Example 1:買蘋果,計(jì)算價(jià)錢,Calculate and display the price of a number of apples if the quantity in kg and price

8、 per kg are given.,quantity pricePerkg,price,price = quantity * pricePerkg,Input,Process,Output,流程圖(Flowchart): Calculate Price of Apples,Input quantity,Start,price quantity * pricePperkg,Input pricePerkg,Output price,End,If necessary, use Divide scanf(%d, ,C Program: Calculate Price of Apples,main(

9、) int quantity,price_per_kg,price; scanf(%d, ,C Program: Calculate Price of Apples,An outline of a program, written in a form that can easily be converted into real programming statements. It resembles the actual program that will be implemented later. However, it cannot be compiled nor executed.,偽碼

10、(Pseudocode),1. Start 2. Read quantity 3. Read price_per_kg 4. price quantity * price_per_kg 5. Print price 6. End,Pseudocode normally codes the following actions: Initialisation of variables Assignment of values to the variables Arithmetic operations Relational operations,順序( Sequence )結(jié)構(gòu)的NS圖,給變量賦值

11、 賦值表達(dá)式語(yǔ)句 賦值表達(dá)式 ; price = quantity*pricePerkg; 輸入輸出數(shù)據(jù) 標(biāo)準(zhǔn)庫(kù)函數(shù)調(diào)用語(yǔ)句 scanf(%d, ,NS圖,B,A,Example 2: Calculate the Minimum,計(jì)算兩個(gè)數(shù)中的最小者.,num1 num2,min,?,Input,Process,Output,B,N,A,Y,條 件P,選擇結(jié)構(gòu)(分支結(jié)構(gòu)) (Selection Structure),NS圖,傳統(tǒng)流程圖,Flowchart: Calculate the Minimum,Input num1 Input num2,Output min,num1num2?,min

12、num2,min num1,Start,End,scanf(%d%d,if (num1 num2) min = num1; else min = num2; printf(%d, min);,C Program: Calculate the Minimum,main() int num1, num2, min; scanf(%d%d, ,C Program: Calculate the Minimum,選擇結(jié)構(gòu)(分支結(jié)構(gòu)) (Selection Structure),單分支選擇結(jié)構(gòu)(Single Selection),step a,condition,step m,step n,step b,

13、true,false,Pseudocode Structure step a if start step m step n end_if step b,if Statement,The structure is similar to single selection (flowchart),Syntax: if (expression) statement; or if (expression) statement1; statement2; ,if Statement,The structure is similar to single selection (flowchart),Synta

14、x: if (expression) statement; or if (expression) statement1; statement2; ,if Statement,The similarity between single selection structure and if statement:,Single Selection Pseudocode : if start step 1 step 2 step k end_if,if Statement: if (expression) statement 1 statement 2 statement k ,if Statemen

15、t,main() int num1, num2, min; printf(“Input 2 numbers: “); scanf(“%d%d”, ,Input 2 numbers: _,20,15,Input 2 numbers: 20 15 Smallest: 15 _,if Statement,Example: main() int mark; scanf(“%d”, ,What will the output be if the mark is 65?,if Statement,Example: main() int mark; scanf(“%d”, ,What will the ou

16、tput be if the mark is 35?,雙分支選擇結(jié)構(gòu)( Double Selection),Pseudocode Structure Step a if start Step m Step n end_if else start Step x Step y end_else Step z,Step a,condition,Step m,Step n,Step b,true,false,Step x,Step y,if - else Statement,The structure is similar to double selection (flowchart),Syntax:

17、 if (expression) statement; else statement; or if (expression) statement1; statement2; else statement3;,or if (expression) statement1; statement2; else statement3; statement4; ,if - else Statement,The similarity between double selection structure and if - else statement:,Double Selection Pseudocode:

18、 if start step 1 step k end_if else start step 1 step n end_else,if Statement: if (expression) statement 1 statement k else statement 1 statement n ,if - else Statement,Example: if (num1 num2) min = num1; else min = num2; printf(“Smallest: %dn”, min);,_,10,Smallest: 10 _,if - else Statement,Example:

19、 if (num1 num2) min = num1; else min = num2; printf(“Smallest: %dn”, min);,_,15,Smallest: 15 _,if - else Statement,if (num1 num2) min = num1; max = num2; else min = num2; max = num1; printf(“Min = %d, Max = %dn”, min, max);,_,Min = 125, Max = 700 _,125,700,條件運(yùn)算符,計(jì)算最小值,if (num1 num2) min = num1; else

20、 min = num2; printf(“Smallest: %dn”, min);,min = num1 num2 ? num1 : num2; printf(“Smallest: %dn”, min);,Lets recap ,Single Selection Statement step a if (expression) step m step n step b,Single Selection,Lets recap ,Double Selection Statement step a if (expression) step m step n else step x step y s

21、tep b,Double Selection,Guesshow does multiple selection look like ?,多分支選擇結(jié)構(gòu)(Multiple Selection),Multi-way if Step a if (expression1) Step m if (expression2) Step n Step z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,多分支選擇結(jié)構(gòu)(Multiple Selection),Cascaded if Step a if (expr

22、ession1) Step m else if (expression2) Step n else Step x Step z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,Step x,例4.5 :體型判斷,按“體指數(shù)”對(duì)肥胖程度進(jìn)行劃分: 體指數(shù)t = w / h2 (體重w單位為公斤,身高h(yuǎn)單位為米) 當(dāng)t 18時(shí),為低體重; 當(dāng)18 t 25時(shí),為正常體重; 當(dāng)25 t 27時(shí),為超重體重; 當(dāng)t 27時(shí),為肥胖。 編程從鍵盤輸入你的身高h(yuǎn)和體重w,判斷你的體重屬于何種類型。,例4.5

23、,#include main() float h, w, t; printf(Please enter h,w:); scanf(%f, %f, ,當(dāng)t 18時(shí),為低體重; 當(dāng)18 t 25時(shí),為正常體重; 當(dāng)25 t 27時(shí),為超重體重; 當(dāng)t 27時(shí),為肥胖。,#include main() float h, w, t; printf(Please enter h,w:); scanf(%f, %f, ,18 25 27,例4.5,當(dāng)t 18時(shí),為低體重; 當(dāng)18 t 25時(shí),為正常體重; 當(dāng)25 t 27時(shí),為超重體重; 當(dāng)t 27時(shí),為肥胖。,多路選擇(switch) and brea

24、k,The structure is similar to multiple selection (flowchart),switch (expression) case value1 : statement1; break; case value2 : statement2; break; default : statementX; break; ,Important Rule !,switch (expression) case value1 : statement1; break; case value2 : statement2; break; default : statementX

25、; break; ,多路選擇(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,January _,January End _,多路選擇(switch) and break,Example: switch (month) case 1: printf(“Ja

26、nuaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,March _,March End _,多路選擇(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”);

27、break; default: printf(“Othersn”); break; printf(“End”);,多路選擇(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,多路選擇(switch) and break,Example: switch (month) ca

28、se 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,February _,March _,End _,多路選擇(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: p

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論