C語(yǔ)言課件(簡(jiǎn)單C程序)_第1頁(yè)
C語(yǔ)言課件(簡(jiǎn)單C程序)_第2頁(yè)
C語(yǔ)言課件(簡(jiǎn)單C程序)_第3頁(yè)
C語(yǔ)言課件(簡(jiǎn)單C程序)_第4頁(yè)
C語(yǔ)言課件(簡(jiǎn)單C程序)_第5頁(yè)
已閱讀5頁(yè),還剩32頁(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、C程序設(shè)計(jì)2C語(yǔ)言中的語(yǔ)句 變量聲明語(yǔ)句 int x, y; 表達(dá)式語(yǔ)句 i+; i10; 空語(yǔ)句;注釋語(yǔ)句 - /* */控制語(yǔ)句 - 條件、循環(huán)復(fù)合語(yǔ)句 t = x; x = y; y = t;3復(fù)合語(yǔ)句 在什么情況下使用復(fù)合語(yǔ)句? 當(dāng)分支需要進(jìn)行多項(xiàng)操作時(shí) t=x; x=y; y=t;被當(dāng)作一條被當(dāng)作一條語(yǔ)句來(lái)處理語(yǔ)句來(lái)處理4空語(yǔ)句 空語(yǔ)句有什么作用? 什么也不做,只表示語(yǔ)句的存在 自頂向下程序設(shè)計(jì)時(shí)用在那些未完成的模塊中 延時(shí)用的空循環(huán)DataInitialze() ; 5順序結(jié)構(gòu)程序的基本操作 如何在程序中給變量賦值? 賦值表達(dá)式; 例Total = m * n; 如何進(jìn)行數(shù)據(jù)的輸入

2、輸出? C語(yǔ)言中通過(guò)調(diào)用標(biāo)準(zhǔn)庫(kù)函數(shù)來(lái)實(shí)現(xiàn) #include stdio.h 輸入:從標(biāo)準(zhǔn)輸入設(shè)備上輸入數(shù)據(jù)到計(jì)算機(jī) 輸出:將計(jì)算機(jī)中的數(shù)據(jù)送到標(biāo)準(zhǔn)輸出設(shè)備6字符輸入輸出函數(shù) 字符輸出函數(shù) 格式 putchar(ch) 說(shuō)明輸出一個(gè)字符ch 字符輸入函數(shù) 格式 getchar() 說(shuō)明無(wú)參數(shù), 值為從輸入設(shè)備接收的字符, 例 a = getchar()注必須使用#include “stdio.h”7#include main() ch; (Press a key and then press Enter:);ch = (); (You pressed );(ch); (n);演示例演示例3.2:

3、3.2:運(yùn)行程序運(yùn)行程序Press a key and then press Enter: AYou pressed A8格式輸出函數(shù)_ 格式:_ 作用:輸出若干個(gè)任意類型的數(shù)據(jù)_ 格式控制:控制格式的字符串_ 輸出項(xiàng)表列:即需要輸出的數(shù)據(jù),可 為常量、變量、表達(dá)式9printf(“a=%d b=%f ”, a,b); 函數(shù)名格式說(shuō)明輸出表列普通字符v格式說(shuō)明:由%和格式字符構(gòu)成 v普通字符:需原樣輸出的字符,可 為轉(zhuǎn)義字符10d 以帶符號(hào)十進(jìn)制整數(shù)輸出o 以八進(jìn)制無(wú)符號(hào)整數(shù)輸出(無(wú)前導(dǎo)0)x 以十六進(jìn)制無(wú)符號(hào)整數(shù)輸出(無(wú)前導(dǎo)0 x)u 以十進(jìn)制無(wú)符號(hào)整數(shù)輸出c 以字符形式輸出單個(gè)字符s 輸出

4、一個(gè)字符串f 以小數(shù)形式輸出浮點(diǎn)數(shù)(6位小數(shù))l 長(zhǎng)整型整數(shù),加在d、o、x、u前L long double型數(shù),加在f、e、g前e 以標(biāo)準(zhǔn)指數(shù)形式輸出(6位小數(shù))g 選用%f,%e中輸出寬度較小的一種格式printf格式字符11Printf寬度控制說(shuō)明符m 表示數(shù)據(jù)占用的最小寬度(如%3d) 數(shù)據(jù)寬度=m,按實(shí)際寬度輸出 數(shù)據(jù)寬度=m,按實(shí)際寬度輸出 數(shù)據(jù)寬度m,補(bǔ)零m n 以寬m輸出小數(shù)實(shí)型,小數(shù)位為n位 如%2.1f1213格式輸入函數(shù) 格式輸入函數(shù) scanf(格式控制字符串, 地址表列);scanf(%d,%f”, &a,&b);非格式字符非格式字符格式說(shuō)明格式說(shuō)明地

5、址表列地址表列14格式輸入函數(shù) 常見(jiàn)錯(cuò)誤scanf(%d,%fn”,&a,&b);scanf(%d,%f”,a,b);scanf(%7.2f,&a);15scanf格式字符1617#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 18#include main() int a, b; p

6、rintf(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 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=34,

7、 a+b = 461234 20#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 21#include main() int a, b; printf(Please input a and b:); scanf(%d,%d, &a, &b); printf(a=%d, b=%d, a+b=%dn

8、,a,b,a+b);Please input a and b:a=12, b=34, a+b = 4612,34 22#include main() int a, b; printf(Please 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 23#include main() int a, b;scanf(%d %d, &a, &b);printf(a

9、= %d, b = %dn, a, b);問(wèn)題1:當(dāng)要求程序輸出結(jié)果為 a = 12, b = 34 時(shí),用戶應(yīng)該如何輸入數(shù)據(jù)? 輸入數(shù)據(jù)的格式控制124#include main() int 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ǔ)句?怎樣修改? scanf(%d,%d, &a, &b); 輸入數(shù)據(jù)的格式控制225#include main() int a, b;scanf(%d %d, &am

10、p;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ù)?輸入數(shù)據(jù)的格式控制326#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 = 34scanf(%2d%2d, &

11、;a, &b);輸入數(shù)據(jù)的格式控制427#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b = %dn, a, b); 問(wèn)題5:限定用戶輸入數(shù)據(jù)為以下格式為1234 同時(shí)要求程序輸出結(jié)果為a = 12, b = 34scanf(%d%d, &a, &b);printf(a = %d, b = %dn, a, b);輸入數(shù)據(jù)的格式控制528#include main() int a, b;scanf(%d %d, &a, &b);printf(a = %d, b =

12、%dn, a, b);問(wèn)題6:設(shè)計(jì)程序使得用戶可以以任意字符(回車、空格、制表符、逗號(hào)、其它)作為分隔符進(jìn)行數(shù)據(jù)的輸入scanf(%d%*c%d, &a, &b); 輸入數(shù)據(jù)的格式控制629#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);Pl

13、ease enter the expression data1 + data2 12 + 312 64 = 76C格式符存在的問(wèn)題及其解決130#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

14、 + data2 12 312 3 = 15C格式符存在的問(wèn)題及其解決231#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 12+312+3 = 15 C格式符存在的問(wèn)題及其

15、解決332再回頭來(lái)看例3.11,以任意分隔符輸入加法算式,可能嗎?33#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(fl

16、oat: %fn, c);Please input an integer:12 Please input an character :a Please input a float number: 3.5 integer:12character :afloat number:3.500000C格式符存在的問(wèn)題及其解決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

17、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:12 Please input an character :a Please input a float number: 3.5 integer:12float number:3.500000C格式符存在的問(wèn)題及其解決35#include main() int a; char

18、 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格式符存在的問(wèn)題及其解決36#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(

溫馨提示

  • 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)論