




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第4章 鍵盤輸入和屏幕輸出本章學(xué)習(xí)內(nèi)容 字符常量與轉(zhuǎn)義字符 字符輸出函數(shù)putchar() 字符輸入函數(shù)getchar() 數(shù)據(jù)的格式化輸出函數(shù)printf() 數(shù)據(jù)的格式化輸入函數(shù)scanf() 字符常量字符常量是用單引號(hào)括起來(lái)的一個(gè)字符a是字符常量,而a則是一個(gè)標(biāo)識(shí)符3表示一個(gè)字符常量,而3則表示一個(gè)整數(shù) 轉(zhuǎn)義字符(Escape Character)一些特殊字符(無(wú)法從鍵盤輸入或者另有他用)用轉(zhuǎn)義字符表示字符型變量的取值范圍取決于計(jì)算機(jī)系統(tǒng)所使用的字符集ASCII(美國(guó)標(biāo)準(zhǔn)信息交換碼)字符集規(guī)定了每個(gè)字符所對(duì)應(yīng)的編碼 一個(gè)字符以其對(duì)應(yīng)的ASCII碼的二進(jìn)制形式存儲(chǔ)在內(nèi)存中一個(gè)字節(jié),保存一個(gè)
2、字符(英文字母、數(shù)字、符號(hào))字符常數(shù)就是一個(gè)普通整數(shù),也可參與各種數(shù)學(xué)運(yùn)算每個(gè)字符具有一個(gè)0255之間的數(shù)值,可從ASCII表查出注意:5和整數(shù)5的區(qū)別5的ASCII碼值是53字符常量4.1單個(gè)字符的輸入/輸出通過(guò)調(diào)用標(biāo)準(zhǔn)庫(kù)函數(shù)來(lái)實(shí)現(xiàn) #include 字符輸出函數(shù)putchar(ch)輸出一個(gè)字符ch字符輸入函數(shù)getchar()無(wú)參數(shù)函數(shù)值為從輸入設(shè)備接收的字符【例4.1】大小寫(xiě)英文字母轉(zhuǎn)換Press a key and then press Enter: Bb ch = ch - (a A); What does this statement mean?4.2數(shù)據(jù)的格式化屏幕輸出格式pr
3、intf(格式控制字符串, 輸出項(xiàng)表列);printf(a=%d b=%f, a, b);可以輸出若干任意類型的數(shù)據(jù) 函數(shù)名可選輸出表列普通字符格式說(shuō)明%c character以字符形式輸出單個(gè)字符%s string 輸出一個(gè)字符串%d decimal 以帶符號(hào)十進(jìn)制整數(shù)輸出%f float 以小數(shù)形式輸出浮點(diǎn)數(shù)(6位小數(shù))%e exponent 以標(biāo)準(zhǔn)指數(shù)形式輸出(6位小數(shù))%g 選用%f,%e中輸出寬度較小的一種格式%o octal 以八進(jìn)制無(wú)符號(hào)整數(shù)輸出(無(wú)前導(dǎo)0)%x hex 以十六進(jìn)制無(wú)符號(hào)整數(shù)輸出(無(wú)前導(dǎo)0 x)%u unsigned 以十進(jìn)制無(wú)符號(hào)整數(shù)輸出printf()格式字符
4、【例4.2】大小寫(xiě)英文字母轉(zhuǎn)換Press a key and then press Enter: Bb, 98m 表示數(shù)據(jù)占用的最小寬度 數(shù)據(jù)寬度大于m,按實(shí)際寬度輸出 數(shù)據(jù)寬度小于m時(shí),補(bǔ)空格n 對(duì)實(shí)數(shù)表示輸出n位小數(shù) 對(duì)字符串表示最多輸出的字符個(gè)數(shù)l 長(zhǎng)整型整數(shù),加在d、o、x、u前L long double型數(shù),加在f、e、g前+/-/# 標(biāo)志printf()的格式修飾符一般形式: % 標(biāo)志輸出最小寬度.精度長(zhǎng)度說(shuō)明:(1) 中的內(nèi)容為可選項(xiàng)(2)標(biāo)志:為+或-、# 等,含義如下: +或- :輸出符號(hào)如:i=3;printf(“%+dn”,i); # :對(duì)c、s、d、u無(wú)影響,對(duì)o類型輸
5、出時(shí)加前綴0 對(duì)x類型輸出時(shí)加前綴0 x,對(duì)e、g、f類型當(dāng) 結(jié)果有小數(shù)時(shí)才給出小數(shù)點(diǎn)。 如:i=0 xf0;printf(“%#xn”,i); printf()的格式修飾符(3)輸出最小寬度(域?qū)抦,m必須是整數(shù)) 域?qū)抦是輸出項(xiàng)在輸出設(shè)備上所占的列數(shù)。若m是正整數(shù)當(dāng)實(shí)際位數(shù)多于定義寬度m,則按實(shí)際位數(shù)輸出;若實(shí)際位數(shù)少于定義寬度,則在域內(nèi)向右對(duì)齊,左邊多余位補(bǔ)以空格;若m有前導(dǎo)0,則左邊多余位補(bǔ)0。 若m是負(fù)整數(shù)則輸出數(shù)據(jù)在域內(nèi)向左對(duì)齊如:int a=23, b=4 ; printf(“%5d,%5d”,a,b);結(jié)果:_ _ _ 23,4_ _ _ _(4)精度 .n(n為正整數(shù))對(duì)于浮
6、點(diǎn)數(shù),表示輸出的小數(shù)的位數(shù);若實(shí)際位數(shù)大于所定義的精度數(shù),則四舍五入后,截去超過(guò)的部分。對(duì)于字符串,指定從字符串左側(cè)所截取的子串字符的個(gè)數(shù);float k=65.678; char s10=“Hello Students”printf(“%.2f”, k); printf(“%.5s”,s);輸出結(jié)果: 65.68 輸出結(jié)果: Hello( 5 )長(zhǎng)度 長(zhǎng)度格式符為h、l、L。 h 表示按短整型量輸出; l 加在d、o、x、u前,表示按長(zhǎng)整型數(shù)據(jù)輸出; l 加在f、e、g前,表示按double型數(shù)據(jù)輸出; L 加在f、e、g前,表示按long double型數(shù)據(jù)輸出printf()的格式修飾符
7、Format for float: %f General format:% . fExample:printf(Value is:%10.4f, 32.6784728); Value is: 32.6785 10 characters4 digitsprintf()的格式修飾符Example:printf(Value is:%10f, 32.6784728); Value is: 32.67847310 characters6 digits (default)Example:printf(Value is:%.3f, 32.6784728); Value is:32.678 3 digitsp
8、rintf()的格式修飾符Input r: 5.3printf WITHOUT width or precision specifications:circumference = 33.300854, area = 88.247263printf WITH width and precision specifications:circumference = 33.30, area = 88.25【例4.3】計(jì)算圓的周長(zhǎng)和面積 4.3數(shù)據(jù)的格式化鍵盤輸入格式scanf(格式控制字符串, 地址表列); scanf(%d,%f, &a, &b);格式字符指定輸入數(shù)據(jù)格式輸入數(shù)據(jù)地址表列非格式字符輸入
9、數(shù)據(jù)以,分隔沒(méi)有時(shí)可以空格、Tab或回車分隔c 以字符形式輸入單個(gè)字符s 輸入字符串,以非空字符開(kāi)始,遇第一個(gè) 空白字符結(jié)束d 以帶符號(hào)十進(jìn)制形式輸入整型數(shù)據(jù)f 以小數(shù)形式輸入浮點(diǎn)數(shù)e 以標(biāo)準(zhǔn)指數(shù)形式輸入o 以八進(jìn)制無(wú)符號(hào)形式輸入(無(wú)前導(dǎo)0)x 以十六進(jìn)制無(wú)符號(hào)形式輸入(無(wú)前導(dǎo)0 x)scanf()的格式字符m 表示數(shù)據(jù)占用的寬度l 加在d、o、x、u前:輸入長(zhǎng)整型 加在f、e 前:輸入雙精度型L 加在f、e 前:輸入long double型h 加在d、o、x 前:輸入短整型* 本輸入項(xiàng)在讀入后不賦給相應(yīng)的變量scanf ()的格式修飾符一般格式:%*輸入數(shù)據(jù)寬度長(zhǎng)度 說(shuō)明:(1)“*”號(hào):表
10、示該輸入項(xiàng)讀入后,不賦予相應(yīng)的變量,即跳過(guò)該輸入值。 如:scanf(%d%*d%d”,&a,&b);當(dāng)輸入1 2 3時(shí), 1a 3b,2被跳過(guò)scanf ()的格式修飾符(2)輸入數(shù)據(jù)寬度 :用十進(jìn)制整數(shù)指定輸入 的寬度。 如:scanf(“%5d”,&a); 輸入12345678后 僅把12345賦予a,其余截去。又如:scanf(“%4d%4d”,&a,&b); 輸入12345678后 1234賦予a,5678賦予b,按寬度自動(dòng)截取。 (3)長(zhǎng)度 長(zhǎng)度格式符l表示長(zhǎng)整型和雙精度浮點(diǎn)數(shù);h表示短整型數(shù)據(jù);L表示長(zhǎng)雙精度浮點(diǎn)數(shù)。long li;double d;long double ld;
11、scan(“%ld%lf%Lf”,&li,&d,&ld); 使用scanf( )函數(shù)需注意 :(1)scanf( )函數(shù)中沒(méi)有精度控制。 如:scanf(“%4.2f”,&a);是非法的 (2)scanf()函數(shù)中一定給出變量地址,不可 是變量名,否則出錯(cuò)。 如: scanf(“%d”,a); 錯(cuò) scanf(“%d”,&a); 對(duì)(3)輸入多個(gè)數(shù)據(jù)時(shí),格式控制串中沒(méi)有非 格式字符,則輸入時(shí)數(shù)據(jù)間的分隔符可 用空格、TAB鍵、回車鍵。 (4)輸入字符數(shù)據(jù)時(shí),若格式控制符中無(wú)非 格式符,則認(rèn)為所有輸入的字符均為有 效字符。scanf(“%c%c%c”,&a,&b,&c);printf(“a=%c
12、,b=%c,c=%c”,a,b,c); 輸入:A,B,C 輸出:a=A,b=,c=B (5)如果格式控制字符串中有非格式字符, 則輸入時(shí)也要輸入非格式字符。 scanf(“%d,%d,%d”,&a,&b,&c);輸入應(yīng)為:5,6,7若為: scanf(“a=%d,b=%d,c=%d”,&a,&b,&c);輸入應(yīng)為:a=5,b=6,c=7如:(6)輸入、輸出格式符不相符,則輸出數(shù)據(jù)錯(cuò)誤。int i;float f;double d;scanf(“%f”,&i); scanf(“%d”.&f);scanf(“%f”.&d);#include main() int a, b; printf(Plea
13、se input a and b:); scanf(%2d%*2d%2d, &a, &b); printf(a=%d, b=%d, a+b=%dn,a,b,a+b);Please input a and b:a=12, b=56, a+b = 68123456跳過(guò)一個(gè)輸入項(xiàng)輸入數(shù)據(jù)的格式控制#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題1:當(dāng)要求程序輸出結(jié)果為 a = 12, b = 34時(shí),用戶應(yīng)該如何輸入數(shù)據(jù)? 12 34輸入數(shù)據(jù)的格式控制例4.4#include main() i
14、nt a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題2:當(dāng)限定用戶輸入數(shù)據(jù)以逗號(hào)為分隔符,即輸入數(shù)據(jù)格式為: 12,34時(shí),應(yīng)修改程序中的哪條語(yǔ)句?怎樣修改? , 輸入數(shù)據(jù)的格式控制例4.4#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題3:語(yǔ)句scanf(%d %d, &a, &b);修改為scanf(a = %d, b = %d, &a, &b);時(shí),用戶應(yīng)該如何輸入數(shù)據(jù)?a = 12, b = 34輸入數(shù)據(jù)的格式
15、控制例4.4#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b); 問(wèn)題4:限定用戶輸入數(shù)據(jù)為以下格式 1234 同時(shí)要求程序輸出結(jié)果為a = 12, b = 34%2d%2d 輸入數(shù)據(jù)的格式控制例4.4#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d , b = %d n, a, b); 問(wèn)題5:限定用戶輸入數(shù)據(jù)為以下格式1234 同時(shí)要求程序輸出結(jié)果為a = 12 ,b = 34%d ,b = %d輸入數(shù)據(jù)的格式控制例4.4
16、#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題6:設(shè)計(jì)程序使得用戶可以以任意字符(回車、空格、制表符、逗號(hào)、其他)作為分隔符進(jìn)行數(shù)據(jù)的輸入輸入數(shù)據(jù)的格式控制例4.4%*c#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題7:輸入123456時(shí)程序的輸出結(jié)果是什么?輸入數(shù)據(jù)的格式控制例4.4%*2da = 12, b = 56#include main() int a, b;scan
17、f(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題8:如果用戶輸入了非法字符,例如輸入了12 3a,那么程序運(yùn)行結(jié)果如何?輸入數(shù)據(jù)的格式控制例4.412 3a a = 12, b = 3#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題9:如果用戶輸入的是123a,那么結(jié)果又會(huì)如何呢?輸入數(shù)據(jù)的格式控制例4.4123a a = 123, b = -858993460#include main() int a, b;scanf(%d %d, &
18、a, &b);printf(a = %d, b = %dn, a, b);問(wèn)題10:如果程序第5行語(yǔ)句修改為.,那么結(jié)果又會(huì)如何呢?輸入數(shù)據(jù)的格式控制例4.4 a, b#include main()int data1, data2, sum;char op; printf(Please enter the expression data1 + data2n);scanf(%d%c%d,&data1, &op, &data2);printf(%d%c%d = %dn, data1, op, data2, data1+data2);Please enter the expression data1
19、 + data2 第1次測(cè)試12 + 312 4199288 = 4199300 C格式符的問(wèn)題及解決例4.5#include main()int data1, data2, sum;char op; printf(Please enter the expression 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 第2次測(cè)試12 312 3 = 15 C
20、格式符的問(wèn)題及解決例4.5#include main()int data1, data2, sum;char op; printf(Please enter the expression 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 第3次測(cè)試12+312+3 = 15 C格式符的問(wèn)題及解決例4.5#include main() int a; char b
21、; 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);Please input an integer:希望得到的運(yùn)行結(jié)果12Please input an character :a
22、Please input a float number:3.5integer:12character :afloat number:3.500000C格式符的問(wèn)題及解決例4.6#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
23、 float number:); scanf(%f, &c); printf(float: %fn, c);Please input an integer:結(jié)果好像很奇怪呀!12Please input an character :aPlease input a float number:3.5integer:12float number:3.500000C格式符的問(wèn)題及解決例4.6#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:); getchar(); /*將存于緩沖區(qū)中的回車字符讀入,避免被后面的變量作為有效字符讀入*/ scanf(%c, &b); printf(character: %cn, b); printf(Please input a float number:); scanf(%f, &c); printf(float: %fn, c);Please input an integer:程序修改后得到的運(yùn)行結(jié)果12Please input an characte
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 火鍋店設(shè)計(jì)方案
- 溺水急救操作手冊(cè)
- 浙江嘉興市新韋進(jìn)出口有限公司招聘筆試題庫(kù)2025
- 2025年?duì)I養(yǎng)與健康科學(xué)基礎(chǔ)知識(shí)學(xué)習(xí)考試試卷及答案
- 2025年職業(yè)培訓(xùn)與認(rèn)證管理的考試試題及答案
- 2025年文物與博物館管理專業(yè)考試試卷及答案
- 2025年文案創(chuàng)作專業(yè)資格考試試卷及答案
- 2025年人工智能與道德倫理相關(guān)知識(shí)考試卷及答案
- 2025年市場(chǎng)營(yíng)銷與品牌管理考研試卷及答案
- 2025年企業(yè)法律事務(wù)與合規(guī)管理考試卷及答案
- 電力交易員試題及答案
- 宗地圖測(cè)繪合同協(xié)議
- 網(wǎng)約車租賃合同協(xié)議書(shū)
- 2025年04月工業(yè)和信息化部產(chǎn)業(yè)發(fā)展促進(jìn)中心社會(huì)公開(kāi)招聘29人筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 寫(xiě)字樓保安知識(shí)培訓(xùn)課件
- 2025-2030中國(guó)鼻腔護(hù)理液行業(yè)市場(chǎng)現(xiàn)狀分析及競(jìng)爭(zhēng)格局與投資發(fā)展研究報(bào)告
- 奶茶行業(yè)市場(chǎng)調(diào)研
- 血透患者的延續(xù)性護(hù)理
- 《重慶市中小學(xué)校園食品安全和膳食經(jīng)費(fèi)管理監(jiān)督辦法》知識(shí)專題培訓(xùn)
- 渣土車駕駛員安全教育
- 豎井爆破方案
評(píng)論
0/150
提交評(píng)論