




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、計算概論A課程 程序設計部分C 語言的基本成分 控制成分李戈北京大學 信息科學技術學院 軟件研究所計算機程序的基本結構什么樣的結構才能支持程序運行的邏輯?1966年,G. Jacopini 和 C. Bohm在“Communications of the ACM”上發(fā)表論論文“Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules” 。從理論上證明了:任何具有單入口單出口的程序都可以用三種基本結構表達:順序結構分支結構循環(huán)結構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 語句前先對表達式求解if ( )內可以是任意的數(shù)值類型(包括整型、實型、字符型、指針型數(shù)據(jù))if (a)coutaendl;if (3)cout“OK”endl;若表達式的值為0,按“假”處理若表達式的值為 非0,按“真”處理多分支語句Switch語句的一般形式如下:switch(表達式)case常量表達式1:語句1; case常量表達式2:語句2;case常量表
5、達式n:語句n;default: 語句n1;當表達式的值與某一個case后面的常量表達式的值相等時,就執(zhí)行此case后面的語句,若所有的case中的常量表達式的值都沒有與表達式的值匹配的,就執(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;思考與練習問題:學校要求實
10、行成績等級制度?,F(xiàn)在已經(jī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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 單位改造車棚合同范例
- 合作合同范本 英文
- 主播合同范本個人
- 化工藥劑供貨合同范本
- 公司內勤合同范本
- 合租廠房合同范本
- 醫(yī)院大型設備合同范例
- 單獨設計合同范例
- 送貨付款合同范本模板
- 吳中區(qū)解約合同范例
- 施工現(xiàn)場應對極端天氣的措施
- 江蘇2025年01月江蘇省揚州生態(tài)科技新城管委會2025年招考6名勞務派遣人員筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 2025年內蒙古呼倫貝爾農墾拉布大林上庫力三河蘇沁農牧場招聘115人歷年高頻重點模擬試卷提升(共500題附帶答案詳解)
- 中學創(chuàng)客教育教學活動計劃
- 《移動通信市場推廣策略》課件
- 2025年四川成都職業(yè)技術學院招聘筆試參考題庫含答案解析
- 2025年國家藥品監(jiān)督管理局藥品審評中心招聘11人歷年高頻重點提升(共500題)附帶答案詳解
- 2024年廣東省《輔警招聘考試必刷500題》考試題庫含必背答案
- 餐飲企業(yè)牛奶產品推廣方案
- 《絲巾無限可能》課件
- 核安全文化培訓
評論
0/150
提交評論