![C語言英文ch03_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/88dd605d-3aa0-4f4e-b4b9-aef3d527dad5/88dd605d-3aa0-4f4e-b4b9-aef3d527dad51.gif)
![C語言英文ch03_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/88dd605d-3aa0-4f4e-b4b9-aef3d527dad5/88dd605d-3aa0-4f4e-b4b9-aef3d527dad52.gif)
![C語言英文ch03_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/88dd605d-3aa0-4f4e-b4b9-aef3d527dad5/88dd605d-3aa0-4f4e-b4b9-aef3d527dad53.gif)
![C語言英文ch03_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/88dd605d-3aa0-4f4e-b4b9-aef3d527dad5/88dd605d-3aa0-4f4e-b4b9-aef3d527dad54.gif)
![C語言英文ch03_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/10/88dd605d-3aa0-4f4e-b4b9-aef3d527dad5/88dd605d-3aa0-4f4e-b4b9-aef3d527dad55.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Chapter 3Processing and Interactive InputAssignment(賦值)(賦值)The general syntax(語法)(語法) for an assignment statement(賦值語句)(賦值語句) isvariable(變量)(變量) = operand (操作數(shù))(操作數(shù)); The operand to the right of the assignment operator (=) can be a constant(常量)(常量), a variable, or an expression(表達(dá)式)(表達(dá)式)The equal si
2、gn in C does not have the same meaning as an equal sign in algebra(代數(shù)學(xué))(代數(shù)學(xué)) length=25; is read “l(fā)ength is assigned the value 25”Assignment(賦值)(賦值)Subsequent(后來的)(后來的) assignment statements can be used to change the value assigned to a variablelength = 3.7;length = 6.28;Assignment (continued)The ope
3、rand(操作數(shù))(操作數(shù)) to the right of the equal sign(等號)(等號) in an assignment statement(賦值語句)(賦值語句) can be a variable(變量)(變量) or any valid(有效的)(有效的) C expressionsum = 3 + 7;product = .05 * 14.6;TThe value of the expression to the right of = is computed(計(jì)算)(計(jì)算) first and then the calculated value(計(jì)算結(jié)果)(計(jì)算結(jié)果
4、) is stored (存儲)(存儲)in the variable to the left of =Assignment (continued)Variables used in the expression to the right of the = must be initialized(初始化)(初始化) if the result is to make sense (有(有意義)意義)amount + 1892 = 1000 + 10 * 5 is invalid(無效的)(無效的)!因?yàn)楸磉_(dá)式因?yàn)楸磉_(dá)式的值存儲在等號左邊的變量中,所以必須把變量直接列在等號的左邊。的值存儲在等號左
5、邊的變量中,所以必須把變量直接列在等號的左邊。Assignment (continued)If width was not initialized(初始化), the computer uses the value that happens to occupy that memory space previously (compiler(編譯器) would probably issue a warning)Assignment (continued)= has the lowest precedence(優(yōu)先級)(優(yōu)先級) of all the binary(二元)(二元) and unar
6、y(一元)(一元) arithmetic operators(算術(shù)(算術(shù)操作符)操作符)Assignment (continued)Multiple assignments(多次賦值)(多次賦值) are possible in the same statementa = b = c = 25;TAll = operators have the same precedenceTOperator has right-to-left Associativity(結(jié)合性)(結(jié)合性)c = 25;b = c;a = b;Implicit Type Conversions(隱式類型轉(zhuǎn)換)(隱式類型轉(zhuǎn)換)
7、Data type conversions(數(shù)據(jù)類型轉(zhuǎn)換)(數(shù)據(jù)類型轉(zhuǎn)換) take place across assignment operatorsdouble result;result = 4; /integer 4 is converted to 4.0Implicit Type Conversions(隱式類型轉(zhuǎn)換)(隱式類型轉(zhuǎn)換)The automatic(自動的)(自動的) conversion across an assignment operator is called an implicit type conversion(隱式類型轉(zhuǎn)(隱式類型轉(zhuǎn)換)換)int answe
8、r;answer = 2.764; /2.764 is converted to 2 Here the implicit conversion is from a higher precision(精度)(精度) to a lower precision data type; the compiler(編譯器)(編譯器) will issue a warning(警告)(警告)Explicit Type Conversions (Casts)(顯式類型轉(zhuǎn)換(顯式類型轉(zhuǎn)換-強(qiáng)制類型轉(zhuǎn)換)強(qiáng)制類型轉(zhuǎn)換)The operator used to force(強(qiáng)制)(強(qiáng)制) the conversio
9、n(轉(zhuǎn)換)(轉(zhuǎn)換) of a value to another type is the cast operator(強(qiáng)制類型轉(zhuǎn)(強(qiáng)制類型轉(zhuǎn)換運(yùn)算符)換運(yùn)算符)(dataType) expressionTwhere dataType is the desired(期望的)(期望的) data type of the expression following the castExplicit Type Conversions (Casts)(顯式類型轉(zhuǎn)換(顯式類型轉(zhuǎn)換-強(qiáng)制類型轉(zhuǎn)換)強(qiáng)制類型轉(zhuǎn)換)Example: If sum is declared as double sum; , (int)
10、sum is the integer value determined by truncating(截去)(截去)sums fractional part(小數(shù)部分)(小數(shù)部分)Assignment Variations(復(fù)合賦值)(復(fù)合賦值)sum = sum + 10 is not an equation(等式) it is an expression that is evaluated in two major steps(它是一個(gè)用兩個(gè)步驟計(jì)算的表達(dá)式)Assignment Variations (continued)Assignment Variations (continued)A
11、ssignment Variations (continued)Assignment expressions like sum = sum + 25 can be written using the following operators: += - -= *= /= %=sum = sum + 10 can be written as sum += 10price *= rate is equivalent(等價(jià))(等價(jià)) to price = price * rateprice *= rate + 1 is equivalent to price = price * (rate + 1)A
12、ccumulating (累加)(累加)Accumulating (continued)Counting(計(jì)數(shù))(計(jì)數(shù))A counting statement(計(jì)數(shù)語句)(計(jì)數(shù)語句) is very similar to the accumulating statementvariable = variable + fixedNumber; (變量(變量=變量變量+固定數(shù)值;)固定數(shù)值;)Examples: i = i + 1; and m = m + 2; Increment operator(自增運(yùn)算符)(自增運(yùn)算符) (+): variable = variable + 1 can b
13、e replaced by variable+ or +variableCounting (continued)Counting (continued)Counting (continued)When the + operator appears before a variable, it is called a prefix increment operator(前綴自增運(yùn)算符)(前綴自增運(yùn)算符); when it appears after a variable, it is called postfix increment operator(后綴自增運(yùn)算符)(后綴自增運(yùn)算符) k = +
14、n; is equivalent toTn = n + 1; / increment n firstTk = n; / assign ns value to k k = n+; is equivalent toTk = n; / assign ns value to kTn = n + 1; / and then increment nCounting (continued)Prefix decrement operator (前綴自減運(yùn)算符)(前綴自減運(yùn)算符) : the expression k = -n first decrements the value of n by 1 befor
15、e assigning the value of n to kPostfix decrement operator (后綴自減運(yùn)算符)(后綴自減運(yùn)算符) : the expression k = n- first assigns the current value of n to n and then reduces the value of n by 1Counting (continued)Mathematical Library Functions(數(shù)學(xué)庫函數(shù))(數(shù)學(xué)庫函數(shù))Mathematical Library Functions (continued)The argument to
16、 sqrt must be floating-point value; passing an integer value results in a compiler error Return value is double-precisionMust include #include Mathematical Library Functions (continued)Mathematical Library Functions (continued)Mathematical Library Functions (continued)3.0 * sqrt(5 * 33 - 13.91) / 5I
17、nteractive Input(交互式輸入)(交互式輸入)Interactive Input (continued)This program must be rewritten to multiply different numbersscanf() is used to enter data into a program while it is executing(用于正在(用于正在運(yùn)行程序時(shí)輸入數(shù)據(jù))運(yùn)行程序時(shí)輸入數(shù)據(jù)); the value is stored in a variable It requires a control string as the first argumen
18、t inside the function name parentheses(要求一個(gè)控制字符串作為這個(gè)函數(shù)名括號內(nèi)的第一個(gè)要求一個(gè)控制字符串作為這個(gè)函數(shù)名括號內(nèi)的第一個(gè)參數(shù))參數(shù))Interactive Input (continued)The control string passed to scanf() typically consists of conversion control sequences only(傳遞給傳遞給scanf()函數(shù)的控制字符函數(shù)的控制字符串典型地只由轉(zhuǎn)換控制序列符組成)串典型地只由轉(zhuǎn)換控制序列符組成)scanf() requires that a list
19、 of variable addresses follow the control string( scanf()函數(shù)要求變量地址列表跟隨控制字符串函數(shù)要求變量地址列表跟隨控制字符串) scanf(%d, &num1);Interactive Input (continued)Interactive Input (continued)This statement produces a prompt(提示)Address operator (&)Interactive Input (continued)scanf() can be used to enter many value
20、sscanf(%f %f,&num1,&num2); /%f%f is the sameA space(空格)(空格) can affect what the value being entered is when scanf() is expecting a character data type(字符數(shù)據(jù)類型)(字符數(shù)據(jù)類型)Interactive Input (continued)scanf(%c%c%c,&ch1,&ch2,&ch3); stores the next three characters typed in the variables
21、 ch1, ch2, and ch3; if you type x y z, then x is stored in ch1, a blank is stored in ch2, and y is stored in ch3scanf(“%c %c %c”,&ch1,&ch2,&ch3); causes scanf() to look for three characters, each character separated by exactly one space(尋找正好被一(尋找正好被一個(gè)空格分開的三個(gè)字符)個(gè)空格分開的三個(gè)字符)scanf() does not
22、 test the data type of the values being enteredIn scanf(%d %f, &num1, &num2), if user enters 22.87, 22 is stored in num1 and .87 in num2Formatted Output(格式化輸出)(格式化輸出)618124-148Output is not alignedFormatted Output (continued) 6 18124-148Field width specifier(域?qū)捴付ǎ〧ormatted Output (continued)
23、Format Modifiers(格式修飾符)(格式修飾符)Left justification(字段左對齊)(字段左對齊): printf(%-10d,59); produces the display 59?Explicit sign display(顯式符號的顯示)(顯式符號的顯示): printf(%+10d,59); produces the display ?+59Format Modifiers(格式修飾符)(格式修飾符)Format modifiers may be combined(格式修飾符可以組合(格式修飾符可以組合 ) %-+10d would cause an integer number to both display it
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年七年級歷史下冊 第16課 明朝的科技、建筑與文學(xué)說課稿 新人教版
- 2025瓷磚買賣合同
- Unit 3 Family Matters Understanding ideas Like Father,Like Son 說課稿 -2024-2025學(xué)年高中英語外研版(2019)必修第一冊
- 2024-2025學(xué)年高中語文 第三課 第4節(jié) 咬文嚼字-消滅錯(cuò)別字說課稿2 新人教版選修《語言文字應(yīng)用》
- 21 古詩三首 第一課時(shí) 說課稿-2024-2025學(xué)年統(tǒng)編版語文四年級上冊
- 2025購銷合同范本
- 森林安全監(jiān)管方案
- 企業(yè)派駐合同范例
- 網(wǎng)狀吊索拱橋施工方案
- 黔東南綠化草坪施工方案
- 慢性腎衰竭的護(hù)理課件
- 2024-2025學(xué)年河南省鄭州市高二上期期末考試數(shù)學(xué)試卷(含答案)
- 甲流乙流培訓(xùn)課件
- 兒科學(xué)川崎病說課
- 2025《省建設(shè)工程檔案移交合同書(責(zé)任書)》
- 2025年云南農(nóng)墾集團(tuán)總部春季社會招聘(9人)管理單位筆試遴選500模擬題附帶答案詳解
- 《石油鉆井基本知識》課件
- 電力兩票培訓(xùn)
- TCCEAS001-2022建設(shè)項(xiàng)目工程總承包計(jì)價(jià)規(guī)范
- 四百字作文格子稿紙(可打印編輯)
- 新概念二冊課文電子版
評論
0/150
提交評論