| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
27 | 28 | 29 | 30 | 31 | 1 | 2 | |||
3 | 4 | 5 | 6 | 7 | 8 | 9 | |||
10 | 11 | 12 | 13 | 14 | 15 | 16 | |||
17 | 18 | 19 | 20 | 21 | 22 | 23 | |||
24 | 25 | 26 | 27 | 28 | 29 | 30 | |||
31 | 1 | 2 | 3 | 4 | 5 | 6 |
Another value to defining variables before writing code is that many times you can accomplish an entire program without many functions; this fact is especially true when you are a beginner making simple programs. The variables give you the raw materials you need to begin working with the tools: loops, if statements, library functions, and perhaps user defined functions.
現(xiàn)在讓我們來看一個(gè)關(guān)于怎樣開始寫一個(gè)完整程序的例子。假設(shè)你要寫的程序是要模擬一個(gè)DVD商店的租售系統(tǒng),這個(gè)系統(tǒng)需要計(jì)算出出租DVD的總收入。你的程序有可能要求,需要輸入一個(gè)代碼,告訴你這個(gè)DVD租售的價(jià)格是2元一天還是是3元一天,然后還需要它出租了多少天,最后如果這個(gè)輸入的代碼是0,整個(gè)程序就結(jié)束了。你應(yīng)該要分別計(jì)算出租金為3元/天和2元/天的DVD的出租的總天數(shù)。拿這個(gè)程序來說,思考設(shè)計(jì)程序的最好的方式是,想象為了計(jì)算出租金的收入,你需要存儲(chǔ)哪些信息:
Let's take a look at an example of how to go about thinking about a program. If you were to write a program to simulate a video store rental system that calculates the gross revenue from rentals, you might be asked to write a program that accepts a code telling you whether a certain video was rented at $2.00 (input as 2) a day or $3.00 (input as 3) a day and then asks for how many days it was rented out for; finally, if the code for the cost of rental is 0 the program should terminate. You should also count the number of days videos were rented at $3.00 per day and $2.00 per day. The best way to think about the design for a program such as this one is to imagine what information you need to store in order to calculate the revenue:
一旦你認(rèn)識(shí)到你需要這些數(shù)據(jù),那么你就很容易想出如何處理這些數(shù)據(jù):比如,你知道租金2元/天的DVD的總收入=所有租金為2元/天DVD的出租天數(shù)之和*2;類似的也可以計(jì)算出租金3元/天的DVD的總收入。你也會(huì)理解這個(gè)“代表DVD的租金的代號(hào)”,這個(gè)變量的用處是,當(dāng)用戶輸入某個(gè)DVD出租的天數(shù)時(shí),決定哪個(gè)變量會(huì)被操作。在你的程序中你需要一個(gè)循環(huán)結(jié)構(gòu)。
Once you realize you need these variables, you can easily imagine how to translate them in terms of each other: for example, you know the total amount of revenue is the number of days videos at $2.00 were rented times $2.00; in similar fashion, you know the relationship for $3.00 a day videos. You should understand that the transaction 'code' determines which variables are manipulated when the user inputs the number of days a specific video was rented (for example, whether to add to the count of days for $2.00 videos or $3.00 videos). You'll probably need a loop in your program (although you can't necessarily infer this from the variables).
程序的代碼有可能會(huì)像下面那樣:
The code might look as follows:
我希望,你現(xiàn)在已經(jīng)有了一個(gè)基本的思路,在寫代碼之前,應(yīng)該如何安排你的程序的結(jié)構(gòu)。
Hopefully, you now have a basic idea of how to lay out your program structure in your mind before you begin to write code.
printf的格式控制的完整格式:
% - 0 m.n l或h 格式字符
下面對(duì)組成格式說明的各項(xiàng)加以說明:
①%:表示格式說明的起始符號(hào),不可缺少。
②-:有-表示左對(duì)齊輸出,如省略表示右對(duì)齊輸出。
③0:有0表示指定空位填0,如省略表示指定空位不填。
④m.n:m指域?qū)挘磳?duì)應(yīng)的輸出項(xiàng)在輸出設(shè)備上所占的字符數(shù)。N指精度。用于說明輸出的實(shí)型數(shù)的小數(shù)位數(shù)。為指定n時(shí),隱含的精度為n=6位。
⑤l或h:l對(duì)整型指long型,對(duì)實(shí)型指double型。h用于將整型的格式字符修正為short型。
---------------------------------------
格式字符
格式字符用以指定輸出項(xiàng)的數(shù)據(jù)類型和輸出格式。
①d格式:用來輸出十進(jìn)制整數(shù)。有以下幾種用法:
%d:按整型數(shù)據(jù)的實(shí)際長度輸出。
%md:m為指定的輸出字段的寬度。如果數(shù)據(jù)的位數(shù)小于m,則左端補(bǔ)以空格,若大于m,則按實(shí)際位數(shù)輸出。
%ld:輸出長整型數(shù)據(jù)。
②o格式:以無符號(hào)八進(jìn)制形式輸出整數(shù)。對(duì)長整型可以用"%lo"格式輸出。同樣也可以指定字段寬度用“%mo”格式輸出。
例:
main()
{ int a = -1;
printf("%d, %o", a, a);
}
運(yùn)行結(jié)果:-1,177777
程序解析:-1在內(nèi)存單元中(以補(bǔ)碼形式存放)為(1111111111111111)2,轉(zhuǎn)換為八進(jìn)制數(shù)為(177777)8。
③x格式:以無符號(hào)十六進(jìn)制形式輸出整數(shù)。對(duì)長整型可以用"%lx"格式輸出。同樣也可以指定字段寬度用"%mx"格式輸出。
④u格式:以無符號(hào)十進(jìn)制形式輸出整數(shù)。對(duì)長整型可以用"%lu"格式輸出。同樣也可以指定字段寬度用“%mu”格式輸出。
⑤c格式:輸出一個(gè)字符。
⑥s格式:用來輸出一個(gè)串。有幾中用法
%s:例如:printf("%s", "CHINA")輸出"CHINA"字符串(不包括雙引號(hào))。
%ms:輸出的字符串占m列,如字符串本身長度大于m,則突破獲m的限制,將字符串全部輸出。若串長小于m,則左補(bǔ)空格。
%-ms:如果串長小于m,則在m列范圍內(nèi),字符串向左靠,右補(bǔ)空格。
%m.ns:輸出占m列,但只取字符串中左端n個(gè)字符。這n個(gè)字符輸出在m列的右側(cè),左補(bǔ)空格。
%-m.ns:其中m、n含義同上,n個(gè)字符輸出在m列范圍的左側(cè),右補(bǔ)空格。如果n>m,則自動(dòng)取n值,即保證n個(gè)字符正常輸出。
⑦f格式:用來輸出實(shí)數(shù)(包括單、雙精度),以小數(shù)形式輸出。有以下幾種用法:
%f:不指定寬度,整數(shù)部分全部輸出并輸出6位小數(shù)。
%m.nf:輸出共占m列,其中有n位小數(shù),如數(shù)值寬度小于m左端補(bǔ)空格。
%-m.nf:輸出共占n列,其中有n位小數(shù),如數(shù)值寬度小于m右端補(bǔ)空格。
⑧e格式:以指數(shù)形式輸出實(shí)數(shù)。可用以下形式:
%e:數(shù)字部分(又稱尾數(shù))輸出6位小數(shù),指數(shù)部分占5位或4位。
%m.ne和%-m.ne:m、n和”-”字符含義與前相同。此處n指數(shù)據(jù)的數(shù)字部分的小數(shù)位數(shù),m表示整個(gè)輸出數(shù)據(jù)所占的寬度。
⑨g格式:自動(dòng)選f格式或e格式中較短的一種輸出,且不輸出無意義的零。
---------------------------------------
關(guān)于printf函數(shù)的進(jìn)一步說明:
如果想輸出字符"%",則應(yīng)該在“格式控制”字符串中用連續(xù)兩個(gè)%表示,如:
printf("%f%%", 1.0/3);
輸出0.333333%。
---------------------------------------
對(duì)于單精度數(shù),使用%f格式符輸出時(shí),僅前7位是有效數(shù)字,小數(shù)6位.
對(duì)于雙精度數(shù),使用%lf格式符輸出時(shí),前16位是有效數(shù)字,小數(shù)6位.
######################################拾遺########################################
由高手指點(diǎn)
對(duì)于m.n的格式還可以用如下方法表示(例)
char ch[20];
printf("%*.*s\n",m,n,ch);
前邊的*定義的是總的寬度,后邊的定義的是輸出的個(gè)數(shù)。分別對(duì)應(yīng)外面的參數(shù)m和n 。我想這種方法的好處是可以在語句之外對(duì)參數(shù)m和n賦值,從而控制輸出格式。