目錄表
國立屏東大學 資訊工程系 程式設計(一)
Turnin作業4
- chapter5
- Turnin Code: c.hw4
- Due Date: 2025/10/28 23:59 Hard Deadline
繳交方式說明
本次Turnin作業包含7個程式題,建議可以為它們建立一個資料夾,並將此次作業所有要上傳的檔案放置於該資料夾後,再使用 turnin
指令上傳作業。例如下面的操作在同學的家目錄裡建立了一個名為hw4的資料夾,並且切換到該資料夾後,針對第1題所要求的Cylinder.c進行程式編撰:
[user@ws ~]$ mkdir hw4 [user@ws ~]$ cd hw4 [user@ws hw4]$ joe Cylinder.c
等到我們完成BMI.c的撰寫後,請自行加以編譯與執行程式,確認正確後使用 turnin▴c.hw4▴Cylinder.c↵
指令將加以上傳:
[user@ws c.hw4]$ turnin c.hw4 Cylinder.c Turning in: Cylinder.c -- ok All done. [user@ws c.hw4]$
當然,你也可以等到本次作業要求的所有題目都在hw4資料夾裡完成後,再使用 turnin▴c.hw4▴.↵
指令一次將所有在目前資料夾中的所有檔案都加以上傳。假設你已經在hw4資料夾裡撰寫完所有題目,並且每個題目的程式檔案皆已編譯並確認執行結果正確後,我們可以使用以下指令將多餘的(不需要繳交的)檔案加以刪除後,一次將所有檔案繳交:
[user@ws c.hw4]$ ls Cylinder.c HowManyDigits.c Num_table.c Score.c FloatRight.c IntRight.c ScientificNotation.c a.out [user@ws c.hw4]$ rm a.out [user@ws c.hw4]$ ls Cylinder.c HowManyDigits.c Num_table.c Score.c FloatRight.c IntRight.c ScientificNotation.c [user@ws c.hw4]$ turnin c.hw4 . Turning in: ./Cylinder.c -- ok ./Score.c -- ok ./IntRight.c -- ok ./Num_table.c -- ok ./ScientificNotation.c -- ok ./HowManyDigits.c -- ok ./FloatRight.c -- ok All done. [user@ws c.hw4]$
如果繳交後想要查看已繳交的檔案及相關資訊,可以輸入turnin▴-ls▴c.hw4↵
指令,例如:
[user@ws c.hw4]$ turnin -ls c.hw4 .: total 28 -rw-rw----. 1 turninman turnin 377 Oct 22 03:31 Cylinder.c -rw-rw----. 1 turninman turnin 159 Oct 22 03:31 FloatRight.c -rw-rw----. 1 turninman turnin 372 Oct 22 03:31 HowManyDigits.c -rw-rw----. 1 turninman turnin 147 Oct 22 03:31 IntRight.c -rw-rw----. 1 turninman turnin 345 Oct 22 03:31 Num_table.c -rw-rw----. 1 turninman turnin 169 Oct 22 03:31 ScientificNotation.c -rw-rw----. 1 turninman turnin 387 Oct 22 03:31 Score.c [user@ws c.hw4]$
註:本文使用▴及↵代表空白字元與Enter換行字元,並且將使用者輸入的部份使用灰階方式
顯示。另外,出現在程式執行結果中的符號皆為半形。
P1 圓柱體的體積與表面積計算(Cylinder.c)
設計一個 C 語言程式 Cylinder.c ,讓使用者輸入圓柱體的半徑(radius)與高度(height),計算出其體積(volume)與表面積(surface area)。
【注意:本題若有浮點數的需求,請一律使用double型態,並將結果四捨五入到小數點第二位後輸出。】
$$ 體積公式: volume = \pi \times radius^2 \times height $$
$$ 表面積公式: Surface area = 2 \times \pi \times radius \times (radius + height) $$
註:$\pi$ 為圓周率,其值為3.14。
本題的執行結果可參考如下:
[3:23 user@ws hw] ./a.out↵
Please▴enter▴radius▴and▴height:▴3.5▴10↵
Volume▴=▴384.65↵
Surface▴Area▴=▴296.73↵
[3:23 user@ws hw] ./a.out↵
Please▴enter▴radius▴and▴height:▴9.12▴10.5↵
Volume▴=▴2742.26↵
Surface▴Area▴=▴1123.71↵
[3:23 user@ws hw]
P2 格式化置右對齊(IntRight.c)
設計一個C語言程式IntRight.c,讓使用者輸入一個整數後,請使用一組方括號包裹最少8位數、靠右對齊、並保證至少會顯示5位數的數值(不足5位數時補0)的方式加以輸出。此程式的執行結果可參考如下:
[3:23 user@ws hw] ./a.out↵
Please▴input▴an▴integer:▴12↵
Output:▴[▴▴▴00012]↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴an▴integer:▴1234↵
Output:▴[▴▴▴01234]↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴an▴integer:▴1234567↵
Output:▴[▴1234567]↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴an▴integer:▴123456789↵
Output:▴[123456789]↵
[3:23 user@ws hw]
P3 格式化置左對齊(FloatRight.c)
設計一個C語言程式FloatRight.c,讓使用者輸入一個浮點數後,請使用一組方括號包裹最少9位數、靠右對齊、並保證至少會顯示5位數的數值(不足5位數時補0)的方式加以輸出。
【注意:本題若有浮點數的需求,請一律使用double型態。】
此程式的執行結果可參考如下:
[3:23 user@ws hw] ./a.out↵
Please▴input▴a▴floating▴number:▴123.45↵
Output:▴[123.450▴▴]↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴a▴floating▴number:▴12345678.12↵
Output:▴[12345678.120]↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴a▴floating▴number:▴123.456789↵
Output:▴[123.457▴▴]↵
[3:23 user@ws hw]
P4 數字格式表格(Num_table.c)
請設計一個 C 程式 Num_table.c ,要求使用者輸入3個不超過8位數的十進制整數。請將使用者所輸入的3個整數以表格的方式加以輸出,其中第1欄為十進位(Decimal),第2欄為八進位(Octal),第3欄為十六進位(Hexadecimal)。另外要特別注意的是,除第1個十進制的欄位為靠左對齊外,其餘兩個欄位皆為靠右對齊。
【請注意,本題執行結果在輸出表格時因為格式問題故使用白色作為背景,表格皆為輸出。】
本題的執行結果如下:
[3:23 user@ws hw] ./a.out↵
Enter▴three▴integers:▴10▴20▴30↵
|▴Decimal|▴Octal▴▴▴|▴Hex▴▴▴|↵ +--------+---------+-------+ |10▴▴▴▴▴▴|▴▴▴▴▴▴▴12|▴▴▴▴▴▴a|↵ |20▴▴▴▴▴▴|▴▴▴▴▴▴▴24|▴▴▴▴▴14|↵ |30▴▴▴▴▴▴|▴▴▴▴▴▴▴36|▴▴▴▴▴1e|↵[3:23 user@ws hw]
./a.out↵
Enter▴three▴integers:▴
15▴50▴70↵
|▴Decimal|▴Octal▴▴▴|▴Hex▴▴▴|↵ +--------+---------+-------+ |15▴▴▴▴▴▴|▴▴▴▴▴▴▴17|▴▴▴▴▴▴f|↵ |50▴▴▴▴▴▴|▴▴▴▴▴▴▴62|▴▴▴▴▴32|↵ |70▴▴▴▴▴▴|▴▴▴▴▴▴106|▴▴▴▴▴46|↵[3:23 user@ws hw]
./a.out↵
Enter▴three▴integers:▴
35▴60▴80↵
|▴Decimal|▴Octal▴▴▴|▴Hex▴▴▴|↵ +--------+---------+-------+ |35▴▴▴▴▴▴|▴▴▴▴▴▴▴43|▴▴▴▴▴23|↵ |60▴▴▴▴▴▴|▴▴▴▴▴▴▴74|▴▴▴▴▴3c|↵ |80▴▴▴▴▴▴|▴▴▴▴▴▴120|▴▴▴▴▴50|↵[3:23 user@ws hw]
P5 輸出科學記號 (ScientificNotation.c)
設計一個C語言程式 ScientificNotation.c ,讓使用者輸入一個 double 型態的浮點數,使用 printf() 函式相關的輸出格式控制,將這個浮點數改以科學記號表示法之格式輸出。此程式的執行結果可參考如下:
[3:23 user@ws hw] ./a.out↵
Please▴input▴a▴floating▴number:▴123.456↵
Scientific notation:▴[1.234560e+02]↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴a▴floating▴number:▴3.1415926↵
Scientific notation:▴[3.141593e+00]↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴a▴floating▴number:▴2↵
Scientific notation:▴[2.000000e+00]↵
[3:23 user@ws hw]
P6 輸出有幾位數(HowManyDigits.c)
設計一個C語言程式 HowManyDigits.c ,讓使用者輸入一個不超過18446744073709551615的正整數,執行結果需計算該整數為幾位數,其執行結果可參考以下的畫面:
[3:23 user@ws hw] ./a.out↵
Please▴input▴an▴integer:▴4543232456443323↵
The▴number▴4543232456443323▴you▴have▴inputted▴has▴16▴digits.↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴an▴integer:▴345662↵
The▴number▴345662▴you▴have▴inputted▴has▴6▴digits.↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴an▴integer:▴18446744073709551615↵
The▴number▴18446744073709551615▴you▴have▴inputted▴has▴20▴digits.↵
[3:23 user@ws hw]
**P7 餐廳成績表(Score.c)
請設計一個 C 語言程式 Score.c ,讓使用者輸入一間餐廳的成績與連絡電話,其中:
- 成績:介於0到100的整數
- 電話:包含不超過3碼的區碼以及使用-號分隔的前碼與後碼(前碼為3-4碼,後碼則固定為4碼),其輸入格式為(###)####-####。
【提示:1. 在ws上面,一個\t(tab)鍵等於8個空白鍵。】 【提示:2. 本題所需的設計方法不超過課本第5章5-2節的範圍,不需要使用字串處理或if elae等條件敘述。】
本題的執行結果可參考如下:
[3:23 user@ws hw] ./a.out↵
Please▴input▴the▴score▴and▴phone▴number:▴100▴(08)▴766-3800↵
score▴▴▴phone-number↵
100▴▴▴▴▴(08)▴766▴-▴3800↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴the▴score▴and▴phone▴number:▴100▴(02)2311-3731↵
score▴▴▴phone-number↵
100▴▴▴▴▴(02)2311▴-▴3731↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴the▴score▴and▴phone▴number:▴90▴(001)▴1234-5678↵
score▴▴▴phone-number↵
▴90▴▴▴▴▴(001)1234▴-▴5678↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴the▴score▴and▴phone▴number:▴60▴(082)324-614↵
score▴▴▴phone-number↵
▴60▴▴▴▴▴(082)▴324▴-▴▴614↵
[3:23 user@ws hw] ./a.out↵
Please▴input▴the▴score▴and▴phone▴number:▴99▴(02)2358-5162↵
score▴▴▴phone-number↵
▴99▴▴▴▴▴(02)2358▴-▴5162↵
[3:23 user@ws hw]