




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、計算概論A課程 程序設(shè)計部分C 語言的基本成分 控制成分李戈北京大學(xué) 信息科學(xué)技術(shù)學(xué)院 軟件研究所計算機程序的基本結(jié)構(gòu)什么樣的結(jié)構(gòu)才能支持程序運行的邏輯?1966年,G. Jacopini 和 C. Bohm在“Communications of the ACM”上發(fā)表論論文“Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules” 。從理論上證明了:任何具有單入口單出口的程序都可以用三種基本結(jié)構(gòu)表達(dá):順序結(jié)構(gòu)分支結(jié)構(gòu)循環(huán)結(jié)構(gòu)C. Bohm & G. Jacopini, Flow Diagrams,
2、Turing Machines and Languages with Only Two Formation Rules, Communications of the ACM, vol9(5) May 1966, pp 366-371.分支語句#include using namespace std; int main()float weight = 0, height = 0, healthRate = 0;cin weightheight;healthRate = weight / (height*height);if (18 = healthRate)&(healthRate = 25)
3、cout 體重適中! endl;else if (25 healthRate)&(healthRate = 30)cout 超重!注意控制! endl; else if ( 30 healthRate)&(healthRate = 35)cout 肥胖!減肥吧! endl;else if (35 healthRate)&(healthRate = 40)cout 重度肥胖!別吃了! endl;elsecout 請直接撥打120! endl;return 0;分支語句#include using namespace std; int main()int year = 0;cin year;if
4、(year % 4 = 0)if (year % 100 = 0)if (year % 400 = 0)cout Y;elsecout N;elsecout Y;elsecout N;return 0;if 語句使用須知在執(zhí)行 if 語句前先對表達(dá)式求解if ( )內(nèi)可以是任意的數(shù)值類型(包括整型、實型、字符型、指針型數(shù)據(jù))if (a)coutaendl;if (3)cout“OK”endl;若表達(dá)式的值為0,按“假”處理若表達(dá)式的值為 非0,按“真”處理多分支語句Switch語句的一般形式如下:switch(表達(dá)式)case常量表達(dá)式1:語句1; case常量表達(dá)式2:語句2;case常量表
5、達(dá)式n:語句n;default: 語句n1;當(dāng)表達(dá)式的值與某一個case后面的常量表達(dá)式的值相等時,就執(zhí)行此case后面的語句,若所有的case中的常量表達(dá)式的值都沒有與表達(dá)式的值匹配的,就執(zhí)行default后面的語句。#include using namespace std; int main()char grade = a; cingrade; switch (grade)case a: cout 85100 endl; case b: cout 7084 endl; case c: cout 6069 endl; case d: cout 60 endl; default: cout e
6、rror endl;return 0;#include using namespace std; int main()char grade = a; cin grade; switch (grade)case a: cout 85100 endl; break; case b: cout 7084 endl; break; case c: cout 6069 endl; break; case d: cout 60 endl; break; default: cout error endl;return 0;#include using namespace std; int main()cha
7、r grade = a; cin grade; switch (grade)case a: cout 85100 endl; case b: cout 7084 endl; case c:case d:case e:case f: cout 6069 endl; case g: cout 60 endl; default: cout error endl;return 0;#include using namespace std; int main()char grade = a; cin grade; switch (grade)case a: cout 85100 endl; case b
8、: cout 7084 endl; case c:case d:case e:case f: cout 6069 endl; case g: cout 60 endl; break;default: cout error endl;return 0;#include using namespace std; int main()char grade = a; cin grade; switch (grade)case a: cout 85100 endl; default: cout error endl; case b: cout 7084 endl; case c:case d:case
9、e:case f: cout 6069 endl; case g: cout 60 endl;return 0;#include using namespace std; int main()char grade = a; cin grade; switch (grade)case a: cout 85100 endl; default: cout error endl; case b: cout 7084 endl; case c:case d:case e:case f: cout 6069 endl; case g: cout 60 endl;return 0;思考與練習(xí)問題:學(xué)校要求實
10、行成績等級制度?,F(xiàn)在已經(jīng)有同學(xué)們的百分制成績,要求按照百分制成績輸出相應(yīng)的等級成績. 90- 100為A, 8090分為B,7080分為C,6070分為D,60分以下為E。最直接的辦法:輸入成績;判定成績是否:90=成績=100判定成績是否:80=成績90如果用switch語句,如何解決?#include using namespace std; int main()int score, num;cout please give the score score; num = score / 10; switch (num)case 10: cout A endl; case 9: cout A
11、 endl; case 8: cout B endl; case 7: cout C endl; case 6: cout D endl; default: cout E endl;return 0;#include using namespace std; int main()int score, num;cout please give the score score; num = score / 10; switch (num)case 10: cout A endl; break; case 9: cout A endl; break; case 8: cout B endl; break; case 7: cout C endl; break; case 6: cout D endl; break; default: cout E endl;return 0;#include using namespace std; int main()int score, num;cout please give t
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)員工合伙合同范本
- 個人英文傭金合同范本
- 亮化購貨合同范本
- 代理續(xù)約合同范本
- 魚池出租合同范本
- 公司裝飾勞務(wù)合同范例
- 兼職工作合同范本
- 停止合作合同范本
- 水上安全合同范本
- 做綠化合同范本
- 部編版小學(xué)一年級語文下冊《春夏秋冬》課件
- 中國煙草總公司鄭州煙草研究院筆試試題2023
- 建設(shè)法規(guī)(全套課件)
- 心衰患者的容量管理中國專家共識-共識解讀
- 外用激素合理使用
- 個人投資收款收據(jù)
- H3C全系列產(chǎn)品visio圖標(biāo)庫
- 新生兒常見儀器的使用與維護 課件
- 魯棒控制理論基礎(chǔ)1-2章
- 工藝能力分析報告
- 《給校園植物掛牌》課件
評論
0/150
提交評論