| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
27 | 28 | 29 | 30 | 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 | 7 |
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.
現在讓我們來看一個關于怎樣開始寫一個完整程序的例子。假設你要寫的程序是要模擬一個DVD商店的租售系統,這個系統需要計算出出租DVD的總收入。你的程序有可能要求,需要輸入一個代碼,告訴你這個DVD租售的價格是2元一天還是是3元一天,然后還需要它出租了多少天,最后如果這個輸入的代碼是0,整個程序就結束了。你應該要分別計算出租金為3元/天和2元/天的DVD的出租的總天數。拿這個程序來說,思考設計程序的最好的方式是,想象為了計算出租金的收入,你需要存儲哪些信息:
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:
一旦你認識到你需要這些數據,那么你就很容易想出如何處理這些數據:比如,你知道租金2元/天的DVD的總收入=所有租金為2元/天DVD的出租天數之和*2;類似的也可以計算出租金3元/天的DVD的總收入。你也會理解這個“代表DVD的租金的代號”,這個變量的用處是,當用戶輸入某個DVD出租的天數時,決定哪個變量會被操作。在你的程序中你需要一個循環結構。
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).
程序的代碼有可能會像下面那樣:
The code might look as follows:
我希望,你現在已經有了一個基本的思路,在寫代碼之前,應該如何安排你的程序的結構。
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 格式字符
下面對組成格式說明的各項加以說明:
①%:表示格式說明的起始符號,不可缺少。
②-:有-表示左對齊輸出,如省略表示右對齊輸出。
③0:有0表示指定空位填0,如省略表示指定空位不填。
④m.n:m指域寬,即對應的輸出項在輸出設備上所占的字符數。N指精度。用于說明輸出的實型數的小數位數。為指定n時,隱含的精度為n=6位。
⑤l或h:l對整型指long型,對實型指double型。h用于將整型的格式字符修正為short型。
---------------------------------------
格式字符
格式字符用以指定輸出項的數據類型和輸出格式。
①d格式:用來輸出十進制整數。有以下幾種用法:
%d:按整型數據的實際長度輸出。
%md:m為指定的輸出字段的寬度。如果數據的位數小于m,則左端補以空格,若大于m,則按實際位數輸出。
%ld:輸出長整型數據。
②o格式:以無符號八進制形式輸出整數。對長整型可以用"%lo"格式輸出。同樣也可以指定字段寬度用“%mo”格式輸出。
例:
main()
{ int a = -1;
printf("%d, %o", a, a);
}
運行結果:-1,177777
程序解析:-1在內存單元中(以補碼形式存放)為(1111111111111111)2,轉換為八進制數為(177777)8。
③x格式:以無符號十六進制形式輸出整數。對長整型可以用"%lx"格式輸出。同樣也可以指定字段寬度用"%mx"格式輸出。
④u格式:以無符號十進制形式輸出整數。對長整型可以用"%lu"格式輸出。同樣也可以指定字段寬度用“%mu”格式輸出。
⑤c格式:輸出一個字符。
⑥s格式:用來輸出一個串。有幾中用法
%s:例如:printf("%s", "CHINA")輸出"CHINA"字符串(不包括雙引號)。
%ms:輸出的字符串占m列,如字符串本身長度大于m,則突破獲m的限制,將字符串全部輸出。若串長小于m,則左補空格。
%-ms:如果串長小于m,則在m列范圍內,字符串向左靠,右補空格。
%m.ns:輸出占m列,但只取字符串中左端n個字符。這n個字符輸出在m列的右側,左補空格。
%-m.ns:其中m、n含義同上,n個字符輸出在m列范圍的左側,右補空格。如果n>m,則自動取n值,即保證n個字符正常輸出。
⑦f格式:用來輸出實數(包括單、雙精度),以小數形式輸出。有以下幾種用法:
%f:不指定寬度,整數部分全部輸出并輸出6位小數。
%m.nf:輸出共占m列,其中有n位小數,如數值寬度小于m左端補空格。
%-m.nf:輸出共占n列,其中有n位小數,如數值寬度小于m右端補空格。
⑧e格式:以指數形式輸出實數。可用以下形式:
%e:數字部分(又稱尾數)輸出6位小數,指數部分占5位或4位。
%m.ne和%-m.ne:m、n和”-”字符含義與前相同。此處n指數據的數字部分的小數位數,m表示整個輸出數據所占的寬度。
⑨g格式:自動選f格式或e格式中較短的一種輸出,且不輸出無意義的零。
---------------------------------------
關于printf函數的進一步說明:
如果想輸出字符"%",則應該在“格式控制”字符串中用連續兩個%表示,如:
printf("%f%%", 1.0/3);
輸出0.333333%。
---------------------------------------
對于單精度數,使用%f格式符輸出時,僅前7位是有效數字,小數6位.
對于雙精度數,使用%lf格式符輸出時,前16位是有效數字,小數6位.
######################################拾遺########################################
由高手指點
對于m.n的格式還可以用如下方法表示(例)
char ch[20];
printf("%*.*s\n",m,n,ch);
前邊的*定義的是總的寬度,后邊的定義的是輸出的個數。分別對應外面的參數m和n 。我想這種方法的好處是可以在語句之外對參數m和n賦值,從而控制輸出格式。