目錄表

Turnin作業5


繳交方式說明

本次Turnin作業包含8個程式題,請將它撰寫完成、編譯無誤後,使用turnin指令上傳作業。以下的示範 假設 同學在ws的家目錄裡已有一個專門用於繳交作業的 homework 資料夾,請在其中建立一個名為 hw5 的子資料夾,然後在homework/hw5裡進行本次作業的撰寫。

[7:17 user@ws in somewhere] cd▴~↵
[7:17 user@ws in ~] cd▴homework↵
[7:17 user@ws in homework] mkdir▴hw5↵
[7:17 user@ws in ~] cd▴hw5↵
[7:17▴user@ws▴in▴hw5]\$▴joe▴heron_formula.c↵ <--撰寫 heron_formula.c 程式
[7:17▴user@ws▴in▴hw5]\$▴joe▴verification.c↵ <--撰寫 verification.c 程式
[7:17▴user@ws▴in▴hw5]\$▴joe▴sum2_digits.c↵ <--撰寫 sum2_digits.c 程式
[7:17▴user@ws▴in▴hw5]\$▴joe▴temp_convert.c↵ <--撰寫 temp_convert.c 程式
[7:17▴user@ws▴in▴hw5]\$▴joe▴coin_change.c↵ <--撰寫 coin_change.c 程式
[7:17▴user@ws▴in▴hw5]\$▴joe▴weighted_score.c↵ <--撰寫 weighted_score.c 程式
[7:17▴user@ws▴in▴hw5]\$▴joe▴bitops.c↵ <--撰寫 bitops.c 程式
[7:17▴user@ws▴in▴hw5]\$▴joe▴address.c↵ <--撰寫 address.c 程式

本次作業撰寫完成後,請先使用以下指令對heron_formula.c進行編譯與執行:

[7:17▴user@ws▴in▴hw5]\$▴cc▴heron_formula.c▴-lm↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵

待編譯與執行結果皆確認無誤後,請使用 turnin 指令上傳作業。請參考以下指令:

[7:17▴user@ws▴in▴hw5]\$▴turnin▴c.hw5▴heron_formula.c↵

至於第二題的verification.c,也同樣使用類似指令完成編譯與執行:

[7:17▴user@ws▴in▴hw5]\$▴cc▴verification.c↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵

同樣地,待編譯與執行結果皆確認無誤後,再次使用 turnin 指令上傳作業。請參考以下指令:

[7:17▴user@ws▴in▴hw5]\$▴turnin▴c.hw5▴verification.c↵

你也可以使用以下指令,一次將homework/hw5裡的所有c程式上傳:

[7:17▴user@ws▴in▴hw5]$▴turnin▴c.hw5▴*.c↵

如果一切順利,你應該可以使用以下指令確認已上傳的內容:

[7:17▴user@ws▴in▴hw5]\$▴turnin▴-ls▴c.hw5↵
.:
total 0
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 address.c
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 bitops.c
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 coin_change.c
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 heron_formula.c
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 sum2_digits.c
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 temp_convert.c
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 verification.c
-rw-rw----. 1 turninman turnin 0 Sep 29 16:16 weighted_score.c
[7:17▴user@ws▴in▴hw5]\$▴

註:本文使用▴及↵代表空白字元與Enter換行字元,並且將使用者輸入的部份使用灰階方式顯示。另外,出現在程式執行結果中的符號皆為半形。


p1 海龍公式(heron_formula.c)

假設三角形的三個邊長分別為 $a$、$b$ 與 $c$,海龍公式(Heron's formula)可用以計算三角形面積如下:

\[ S = \frac{a + b + c}{2} \]

\[ 面積 = \sqrt{S(S-a)(S-b)(S-c)} \]

,其中$S$為半周長。

請設計一個 C 語言程式 heron_formula.c,讓使用者輸入一個三角形的三個邊長(皆爲大於0的正整數),將面積四捨五入到整數位後加以輸出。

本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Input▴the▴lengths▴of▴sides▴a,▴b,▴and▴c:▴3▴4▴5↵
The▴rounded▴area▴of▴the▴triangle▴is▴6.↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Input▴the▴lengths▴of▴sides▴a,▴b,▴and▴c:▴7▴24▴25↵
The▴rounded▴area▴of▴the▴triangle▴is▴84.↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Input▴the▴lengths▴of▴sides▴a,▴b,▴and▴c:▴2▴6▴8↵
The▴rounded▴area▴of▴the▴triangle▴is▴0.↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Input▴the▴lengths▴of▴sides▴a,▴b,▴and▴c:▴2▴3▴4▴↵
The▴rounded▴area▴of▴the▴triangle▴is▴3.↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Input▴the▴lengths▴of▴sides▴a,▴b,▴and▴c:▴4▴5▴6↵
The▴rounded▴area▴of▴the▴triangle▴is▴10.↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Input▴the▴lengths▴of▴sides▴a,▴b,▴and▴c:▴10▴11▴12↵
The▴rounded▴area▴of▴the▴triangle▴is▴52.↵
[7:17 user@ws in hw5]$▴

本題若有浮點數運算需求,一律使用double 型態

關於海龍公式需要計算平方根的部分,同學們可以使用定義在 “math.h” 中的 “sqrt($x$)“ 或是 “pow($x$,0.5)“ 的方式取得變數$x$開根號的值。

另外,若是使用了 “math.h” 的函式,請在編譯的時候記得加上 ”-lm” 參數,如:“gcc HeronFormula.c -lm”。


p2 驗證碼計算(verification.c)

請設計一個C語言程式verification.c,讓使用者輸入一個由5位數字組成的代碼(可以由0開頭),並依據以下項要求完成其驗證碼之計算並加以輸出。注意,在以下要求中,5位數之代碼由左到右依序被稱為第一位數、第二位數、…、第五位數。

  1. 將第一位數與第三位數相加
  2. 將第二位數及第五位數相乘後取其結果之個位數,再加3
  3. 將第1項的運算結果除以第2項的運算結果(若運算結果為浮點數,則將小數部份無條件捨棄)
  4. 將第四位數加上第3項的運算結果後,將其結果加以輸出

本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴an▴ID:▴12345↵
The▴verification▴code▴of▴12345▴is▴5.↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴an▴ID:▴98765↵
The▴verification▴code▴of▴98765▴is▴11.↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴an▴ID:▴00359↵
The▴verification▴code▴of▴00359▴is▴6.↵
[7:17▴user@ws▴in▴hw5]\$▴


p3 兩位數加總(sum2_digits.c)

請設計一個 C 語言程式 sum2_digits.c,要求使用者輸入一個兩位數的正整數,將其十位數與個位數相加後輸出。

本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴a▴two-digit▴number:▴58↵
The▴sum▴of▴the▴two▴digits▴is:▴13↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴a▴two-digit▴number:▴10↵
The▴sum▴of▴the▴two▴digits▴is:▴1↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴a▴two-digit▴number:▴98↵
The▴sum▴of▴the▴two▴digits▴is:▴17↵
[7:17▴user@ws▴in▴hw5]\$▴


p4 溫度轉換(temp_convert.c)

請設計一個 C 語言程式 temp_convert.c,讓使用者輸入一個攝氏溫度(Celsius),先計算並輸出對應的華氏溫度(Fahrenheit),接著再將該華氏溫度轉換為克氏溫度(Kelvin)並輸出。溫度轉換公式為:

$$ Fahrenheit = Celsius \times \frac{9}{5} + 32 $$

$$ Kelvin = (Fahrenheit - 32) \times \frac{5}{9} + 273.15 $$ 本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Celsius:▴30↵
Fahrenheit:▴86.000000↵
Kelvin:▴303.150000↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Celsius:▴47↵
Fahrenheit:▴116.600000↵
Kelvin:▴320.150000↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Celsius:▴-5↵
Fahrenheit:▴23.000000↵
Kelvin:▴268.150000↵
[7:17▴user@ws▴in▴hw5]\$▴

本題若有浮點數運算需求,一律使用double 型態


p5 零錢換算(coin_change.c)

請設計一個 C 語言程式 coin_change.c,讓使用者輸入一個金額(大於 0 的整數),請將這個金額換算成最少數量的 50 元、10 元、5 元與 1 元硬幣組合,並將各硬幣的數量輸出。

本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴the▴total▴amount:▴341↵
50▴coin(s):▴6↵
10▴coin(s):▴4↵
5▴coin(s):▴0↵
1▴coin(s):▴1↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴the▴total▴amount:▴197↵
50▴coin(s):▴3↵
10▴coin(s):▴4↵
5▴coin(s):▴1↵
1▴coin(s):▴2↵
[7:17▴user@ws▴in▴hw5]\$▴./a.out↵
Please▴input▴the▴total▴amount:▴36↵
50▴coin(s):▴0↵
10▴coin(s):▴3↵
5▴coin(s):▴1↵
1▴coin(s):▴1↵
[7:17▴user@ws▴in▴hw5]\$▴


p6 加權分數(weighted_score.c)

請設計一個 C 語言程式 weighted_score.c,讓使用者輸入三項成績(皆為整數):平時成績、期中考成績與期末考成績。這三項成績佔總學期成績的比例分別為:平時成績 30%、期中考 30%、期末考 40%。請計算出該名學生的學期加權總成績。

本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Please▴input▴your▴scores▴(Assignments,▴Midterm,▴Final):▴77▴89▴91↵
Your▴score▴is:▴86.200000.↵
[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Please▴input▴your▴scores▴(Assignments,▴Midterm,▴Final):▴56▴73▴64↵
Your▴score▴is:▴64.300000.↵
[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Please▴input▴your▴scores▴(Assignments,▴Midterm,▴Final):▴90▴80▴85↵
Your▴score▴is:▴85.000000.↵
[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Please▴input▴your▴scores▴(Assignments,▴Midterm,▴Final):▴60▴70▴80↵
Your▴score▴is:▴71.000000.↵
[7:17▴user@ws▴in▴hw5]$▴

本題若有浮點數運算需求,一律使用double 型態


p7 位元運算(bitops.c)

請設計一個C語言的程式bitops.c,讓使用者輸入兩個整數,並將下表所列出的位元運算子進行計算並輸出。

位元NOT ~
位元右移 >>
位元左移 <<
位元AND &
位元OR |
位元XOR ^

本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Enter▴2▴numbers:▴1▴1↵
~(1)▴=▴-2↵
~(1)▴=▴-2↵
(1)▴>>▴(1)▴=▴0↵
(1)▴<<▴(1)▴=▴2↵
(1)▴&▴(1)▴=▴1↵
(1)▴|▴(1)▴=▴1↵
(1)▴^▴(1)▴=▴0↵
[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Enter▴2▴numbers:▴-1▴-1↵
~(-1)▴=▴0↵
~(-1)▴=▴0↵
(-1)▴>>▴(-1)▴=▴-1↵
(-1)▴<<▴(-1)▴=▴-2147483648↵
(-1)▴&▴(-1)▴=▴-1↵
(-1)▴|▴(-1)▴=▴-1↵
(-1)▴^▴(-1)▴=▴0↵
[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Enter▴2▴numbers:▴1▴0↵
~(1)▴=▴-2↵
~(0)▴=▴-1↵
(1)▴>>▴(0)▴=▴1↵
(1)▴<<▴(0)▴=▴1↵
(1)▴&▴(0)▴=▴0↵
(1)▴|▴(0)▴=▴1↵
(1)▴^▴(0)▴=▴1↵
[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Enter▴2▴numbers:▴0▴1↵
~(0)▴=▴-1↵
~(1)▴=▴-2↵
(0)▴>>▴(1)▴=▴0↵
(0)▴<<▴(1)▴=▴0↵
(0)▴&▴(1)▴=▴0↵
(0)▴|▴(1)▴=▴1↵
(0)▴^▴(1)▴=▴1↵
[7:17▴user@ws▴in▴hw5]$▴


p8 取址運算(address.c)

請設計一個C語言的程式address.c,宣告一個int型態的變數x,讓使用者輸入一個數字,並將其數值以及所分配到的記憶體位址(包含開始的位元組位址到結束的位元組位址)輸出。

本題的執行結果可參考以下的內容:

[7:17▴user@ws▴in▴hw5]$▴./a.out↵
Enter▴x:▴5↵
x=5▴[0x7ffe13682a8c-0x7ffe13682a8f]↵
[7:17▴user@ws▴in▴hw5]$

上述執行結果僅供參考,變數實際分配到的記憶體位址將依作業系統分配而定,每次執行結果並不相同。
因此,本題將由助教進行人工批改。

你可以使用&x來取得x所分配到的記憶體空間的起始位址,再利用sizeof(int)或sizeof(x)來取得一個int整數變數所占用的記憶體空間為多少後,計算出所分配到的記憶體空間的結束位址為何