版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、1C語言中的語句變量聲明語句int x, y;表達式語句i+;i10;空語句;注釋語句 - /* */控制語句 - 條件、循環(huán)復合語句 t = x; x = y; y = t;第1頁,共36頁。2復合語句在什么情況下使用復合語句?當分支需要進行多項操作時 t=x; x=y; y=t;被當作一條語句來處理第2頁,共36頁。3空語句空語句有什么作用?什么也不做,只表示語句的存在自頂向下程序設計時用在那些未完成的模塊中延時用的空循環(huán)DataInitialze() ; 第3頁,共36頁。4順序結構程序的基本操作 如何在程序中給變量賦值?賦值表達式;例Total = m * n;如何進行數據的輸入輸出?
2、C語言中通過調用標準庫函數來實現#include stdio.h輸入:從標準輸入設備上輸入數據到計算機輸出:將計算機中的數據送到標準輸出設備第4頁,共36頁。5字符輸入輸出函數字符輸出函數格式 putchar(ch)說明輸出一個字符ch字符輸入函數格式 getchar()說明無參數, 值為從輸入設備接收的字符,例 a = getchar()注必須使用#include “stdio.h”第5頁,共36頁。6#include main()char ch; printf(Press a key and then press Enter:);ch = getchar(); printf(You pre
3、ssed );putchar(ch); putchar(n);演示例3.2:運行程序Press a key and then press Enter:AYou pressedA第6頁,共36頁。7格式輸出函數 格式:printf(格式控制字符串,輸出項表列); 作用:輸出若干個任意類型的數據 格式控制:控制格式的字符串 輸出項表列:即需要輸出的數據,可 為常量、變量、表達式第7頁,共36頁。8printf(“a=%d b=%f ”, a,b); 函數名格式說明輸出表列普通字符格式說明:由%和格式字符構成 普通字符:需原樣輸出的字符,可 為轉義字符第8頁,共36頁。9d 以帶符號十進制整數輸出o
4、 以八進制無符號整數輸出(無前導0)x 以十六進制無符號整數輸出(無前導0 x)u 以十進制無符號整數輸出c 以字符形式輸出單個字符s 輸出一個字符串f 以小數形式輸出浮點數(6位小數)l 長整型整數,加在d、o、x、u前L long double型數,加在f、e、g前e 以標準指數形式輸出(6位小數)g 選用%f,%e中輸出寬度較小的一種格式printf格式字符第9頁,共36頁。10Printf寬度控制說明符m 表示數據占用的最小寬度(如%3d) 數據寬度=m,按實際寬度輸出 數據寬度=m,按實際寬度輸出 數據寬度m,補零m n 以寬m輸出小數實型,小數位為n位 如%2.1f第10頁,共36
5、頁。11#include main()float f1 = 100.15799, f2 = 12.55, f3 = 1.7;int n1 = 123, n2 = 45, n3 = 6; printf(printf WITHOUT width or precision specifications:n);printf(%fn%fn%fn, f1, f2, f3);printf(%dn%dn%dn, n1, n2, n3);printf(printf WITH width and precision specifications:n);printf(%5.2fn%6.1fn%3.0fn, f1,
6、f2, f3);printf(%5dn%6dn%3dnn, n1, n2, n3);第11頁,共36頁。12格式輸入函數格式輸入函數scanf(格式控制字符串, 地址表列);scanf(%d,%f”, &a,&b);非格式字符格式說明地址表列第12頁,共36頁。13格式輸入函數常見錯誤scanf(%d,%fn”,&a,&b);scanf(%d,%f”,a,b);scanf(%7.2f,&a);第13頁,共36頁。14d 以帶符號十進制形式輸入整型數據o 以八進制無符號形式輸入(無前導0)x 以十六進制無符號形式輸入(無前導0 x)c 以字符形式輸入單個字符s 輸入字符串,以非空字符開始,遇第一
7、個 空白字符結束f 以小數形式輸入浮點數e 以標準指數形式輸入scanf格式字符第14頁,共36頁。15l 加在d、o、x、u前:輸入長整型 加在f、e 前:輸入雙精度型L 加在f、e 前:輸入long double型h 加在d、o、x 前:輸入短整型m 表示數據占用的寬度,沒有%mnf格式* 本輸入項在讀入后不賦給相應的變量scanf附加格式說明符第15頁,共36頁。16#include main() int a, b; printf(Please input a and b:); scanf(%d%d, &a, &b); printf(a=%d, b=%d, a+b=%dn,a,b,a+b
8、);Please input a and b:a=12, b=34, a+b = 4612 34遇空格、TAB 鍵時結束第16頁,共36頁。17#include main() int a, b; printf(Please input a and b:); scanf(%d%d, &a, &b); printf(a=%d, b=%d, a+b=%dn,a,b,a+b);Please input a and b:a=12, b=34, a+b = 4612遇回車鍵時結束34第17頁,共36頁。18#include main() int a, b; printf(Please input a an
9、d b:); scanf(%2d%2d, &a, &b); printf(a=%d, b=%d, a+b=%dn,a,b,a+b);Please input a and b:a=12, b=34, a+b = 461234遇寬度時結束第18頁,共36頁。19#include main() int a, b; printf(Please input a and b:); scanf(%2d%2d, &a, &b); printf(a=%d, b=%d, a+b=%dn,a,b,a+b);Please input a and b:a=12, b=3, a+b = 15123a遇非法輸入時結束第19
10、頁,共36頁。20#include main() int a, b; printf(Please input a and b:); scanf(%d,%d, &a, &b); printf(a=%d, b=%d, a+b=%dn,a,b,a+b);Please input a and b:a=12, b=34, a+b = 4612,34這里逗號需要原樣輸入第20頁,共36頁。21#include main() int a, b; printf(Please input a and b:); scanf(%2d%*2d%2d, &a, &b); printf(a=%d, b=%d, a+b=%
11、dn,a,b,a+b);Please input a and b:a=12, b=56, a+b = 68123456跳過一個輸入項第21頁,共36頁。22#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問題1:當要求程序輸出結果為 a = 12, b = 34 時,用戶應該如何輸入數據? 12 34輸入數據的格式控制1第22頁,共36頁。23#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b)
12、;問題2:當限定用戶輸入數據以逗號為分隔符,即輸入數據格式為: 12,34時,應修改程序中的哪條語句?怎樣修改? scanf(%d,%d, &a, &b); 輸入數據的格式控制2第23頁,共36頁。24#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問題3:語句scanf(“%d %d”, &a, &b);修改為scanf(“a = %d, b = %d”, &a, &b);時,用戶應該如何輸入數據?a = 12, b = 34輸入數據的格式控制3第24頁,共36頁。25#include m
13、ain() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b); 問題4:限定用戶輸入數據為以下格式為 1234 同時要求程序輸出結果為a = 12, b = 34scanf(%2d%2d, &a, &b); 輸入數據的格式控制4第25頁,共36頁。26#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b); 問題5:限定用戶輸入數據為以下格式為1234 同時要求程序輸出結果為a = 12, b = 34scanf(%d%d, &a
14、, &b);printf(a = %d, b = %dn, a, b);輸入數據的格式控制5第26頁,共36頁。27#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問題6:設計程序使得用戶可以以任意字符(回車、空格、制表符、逗號、其它)作為分隔符進行數據的輸入scanf(%d%*c%d, &a, &b); 輸入數據的格式控制6第27頁,共36頁。28#include main()int data1, data2, sum;char op; printf(Please enter the ex
15、pression data1 + data2n);scanf(%d%c%d,&data1, &op, &data2);printf(%d%c%d = %dn, data1, op, data2, data1+data2);Please enter the expression data1 + data2 第1次測試12 + 312 64 = 76 C格式符存在的問題及其解決1第28頁,共36頁。29#include main()int data1, data2, sum;char op; printf(Please enter the expression data1 + data2n);sc
16、anf(%d%c%d,&data1, &op, &data2);printf(%d%c%d = %dn, data1, op, data2, data1+data2);Please enter the expression data1 + data2 第2次測試12 312 3 = 15 C格式符存在的問題及其解決2第29頁,共36頁。30#include main()int data1, data2, sum;char op; printf(Please enter the expression data1 + data2n);scanf(%d%c%d,&data1, &op, &data2
17、);printf(%d%c%d = %dn, data1, op, data2, data1+data2);Please enter the expression data1 + data2 第3次測試12+312+3 = 15 C格式符存在的問題及其解決3第30頁,共36頁。31#include main()int data1, data2, sum;char op; printf(Please enter the expression data1 + data2n);scanf(%d%1s%d,&data1, &op, &data2);printf(%d%c%d = %dn, data1,
18、 op, data2, data1+data2);12+312 + 312+3再回頭來看例3.11,以任意分隔符輸入加法算式,可能嗎?第31頁,共36頁。32#include main() int a; char b; float c; printf(Please input an integer:); scanf(%d, &a); printf(integer: %dn, a); printf(Please input a character:); scanf(%c, &b); printf(character: %cn, b); printf(Please input a float nu
19、mber:); scanf(%f, &c); printf(float: %fn, c);Please input an integer:希望得到的運行結果12Please input an character :aPlease input a float number:3.5integer:12character :afloat number:3.500000C格式符存在的問題及其解決第32頁,共36頁。33#include main() int a; char b; float c; printf(Please input an integer:); scanf(%d, &a); prin
20、tf(integer: %dn, a); printf(Please input a character:); scanf(%c, &b); printf(character: %cn, b); printf(Please input a float number:); scanf(%f, &c); printf(float: %fn, c);Please input an integer:結果好像很奇怪嘛!12Please input an character :aPlease input a float number:3.5integer:12float number:3.500000C格
21、式符存在的問題及其解決第33頁,共36頁。34#include main() int a; char b; float c; printf(Please input an integer:); scanf(%d, &a); printf(integer: %dn, a); printf(Please input a character:); scanf(%c, &b); printf(character: %cn, b); printf(Please input a float number:); scanf(%f, &c); printf(float: %fn, c);C格式符存在的問題及其解決第34頁,共36頁。35#include main() int a; char b; float c; printf(Pl
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 沈陽高中語文試題及答案
- 融媒體招聘考試試題及答案
- 輔警入警培訓課件模板
- 輔助生殖技術176號文件
- 《GAT 1400.2-2017公安視頻圖像信息應用系統(tǒng) 第2部分:應用平臺技術要求》專題研究報告
- 2026 年初中英語《形容詞》專項練習與答案 (100 題)
- 《GAT 167-2019法醫(yī)學 中毒尸體檢驗規(guī)范》專題研究報告
- 2026年深圳中考英語拔尖培優(yōu)特訓試卷(附答案可下載)
- 2026年大學大二(交通運輸)交通規(guī)劃理論階段測試試題及答案
- 2026年深圳中考數學沖刺實驗班專項試卷(附答案可下載)
- 中央中國熱帶農業(yè)科學院院屬單位2025年第一批招聘筆試歷年參考題庫附帶答案詳解
- 研發(fā)費用加計扣除審計服務協議
- 2025年二年級上冊語文期末專項復習-按課文內容填空默寫表(含答案)
- 建筑施工公司成本管理制度(3篇)
- 2025年婦產科副高試題庫及答案
- 2025年度黨委黨建工作總結
- 新質生產力在體育產業(yè)高質量發(fā)展中的路徑探索
- 2025年公民素質養(yǎng)成知識考察試題及答案解析
- 老年人營養(yǎng)和飲食
- 《關鍵軟硬件自主可控產品名錄》
- 2025年濟南市九年級中考語文試題卷附答案解析
評論
0/150
提交評論