國立屏東大學 資訊工程系 程式設計(一)
本次Turnin作業包含兩個程式題,請將它撰寫完成、編譯無誤後,使用turnin指令上傳作業。以下的示範 假設 同學在ws的家目錄裡已有一個專門用於繳交作業的 homework 資料夾,請在其中建立一個名為 hw2 的子資料夾,然後在homework/hw2裡進行本次作業的撰寫。
[7:17 user@ws in somewhere] cd▴~↵
[7:17 user@ws in ~] cd▴homework↵
[7:17 user@ws in homework] mkdir▴hw2↵
[7:17 user@ws in ~] cd▴hw2↵
[7:17 user@ws in hw2] joe▴do_math.c↵ <--撰寫do_math.c程式
[7:17 user@ws in hw2] joe▴year_convert.c↵ <--撰寫year_convert.c程式
[7:17 user@ws in hw2] joe▴time_convert.c↵ <--撰寫time_convert.c程式
[7:17 user@ws in hw2] joe▴price.c↵ <--撰寫price.c程式
[7:17 user@ws in hw2] joe▴reverse.c↵ <--撰寫reverse.c程式
[7:17 user@ws in hw2] joe▴feet.c↵ <--撰寫feet.c程式
[7:17 user@ws in hw2] joe▴salary.c↵ <--撰寫salary.c程式
[7:17 user@ws in hw2] joe▴age.c↵ <--撰寫age.c程式
[7:17 user@ws in hw2] joe▴angle.c↵ <--撰寫angle.c程式
[7:17 user@ws in hw2] joe▴perimeter.c↵ <--撰寫perimeter.c程式
本次作業撰寫完成後,請先使用以下指令對do_math.c進行編譯與執行:
[7:17 user@ws in hw2] cc▴do_math.c↵
[7:17 user@ws in hw2] ./a.out↵
待編譯與執行結果皆確認無誤後,請使用 turnin 指令上傳作業。請參考以下指令:
[7:17 user@ws in hw2] turnin▴c.hw2▴do_math.c↵
至於第二題的year_convert.c,也同樣使用類似指令完成編譯與執行:
[7:17 user@ws in hw2] cc▴year_convert.c↵
[7:17 user@ws in hw2] ./a.out↵
同樣地,待編譯與執行結果皆確認無誤後,再次使用 turnin 指令上傳作業。請參考以下指令:
[7:17 user@ws in hw2] turnin▴c.hw2▴year_convert.c↵
你也可以使用以下指令,一次將homework/hw2裡的所有c程式上傳:
[7:17 user@ws in hw2] turnin▴c.hw2▴*.c↵
如果一切順利,你應該可以使用以下指令確認已上傳的內容:
[7:17 user@ws in hw2] turnin▴-ls▴c.hw2↵
.:
total 40
-rw-rw----. 1 turninman turnin 279 Sep 7 13:12 age.c
-rw-rw----. 1 turninman turnin 324 Sep 7 13:12 angle.c
-rw-rw----. 1 turninman turnin 244 Sep 7 13:12 do_math.c
-rw-rw----. 1 turninman turnin 322 Sep 7 13:12 feet.c
-rw-rw----. 1 turninman turnin 322 Sep 7 13:12 perimeter.c
-rw-rw----. 1 turninman turnin 288 Sep 7 13:12 price.c
-rw-rw----. 1 turninman turnin 296 Sep 7 13:12 reverse.c
-rw-rw----. 1 turninman turnin 315 Sep 7 13:12 salary.c
-rw-rw----. 1 turninman turnin 579 Sep 7 13:12 time_convert.c
-rw-rw----. 1 turninman turnin 233 Sep 7 13:12 year_convert.c
[7:17 user@ws in hw2]
註:本文使用▴及↵代表空白字元與Enter換行字元,並且將使用者輸入的部份使用灰階方式顯示。另外,出現在程式執行結果中的符號皆為半形。
請設計一個C語言程式 do_math.c,讓使用者依序輸入兩個int整數變數x與y的數值後,計算 4x + 2y 的結果後加以輸出。本題的輸出結果可以參考如下:
[7:17 user@ws in hw2] ./a.out↵
Please▴input▴x:▴3↵
Please▴input▴y:▴5↵
(▴4x▴+▴2y▴)▴=▴22.↵
[7:17 user@ws in hw2] ./a.out↵
Please▴input▴x:▴10↵
Please▴input▴y:▴12↵
(▴4x▴+▴2y▴)▴=▴64.
[7:17 user@ws in hw2]
請設計一個C語言的程式 year_convert.c,用以轉換民國年到西元年。此程式的執行結果可參考以下的輸出內容:
小提示: 本題僅會輸入民國1年(含)後的年份,且不會輸入負值
[7:17 user@ws in hw2] ./a.out↵
Input▴a▴year▴in▴ROC:▴115↵
ROC▴115▴is▴AD▴2026.↵
[7:17 user@ws in hw2] ./a.out↵
Input▴a▴year▴in▴ROC:▴72↵
ROC▴72▴is▴AD▴1983.↵
[7:17 user@ws in hw2] ./a.out↵
Input▴a▴year▴in▴ROC:▴100↵
ROC▴100▴is▴AD▴2011.
[7:17 user@ws in hw2]
請設計一個 C 程式 time_convert.c,讀入一個「非負整數秒數」S,將其換算為「小時:分鐘:秒」的格式並輸出。
[3:23 user@ws hw] ./a.out↵
Enter▴seconds:▴7322↵
Converted▴time:▴2:2:2↵
[3:23 user@ws hw] ./a.out↵
Enter▴seconds:▴3600↵
Converted▴time:▴1:0:0↵
[3:23 user@ws hw] ./a.out↵
Enter▴seconds:▴105↵
Converted▴time:▴0:1:45↵
[3:23 user@ws hw]
請設計一個C語言的程式 price.c,讓使者輸入產品的價格(price)及購買的數量(quantity),假設使用者所輸入的值皆為大於0的正整數,請計算消費者購買產品的總價後輸出,其執行結果可參考以下的內容:
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Enter▴the▴price:▴50↵
Please▴input▴the▴quantity:▴6↵
The▴total▴price▴is▴NT\$300.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Enter▴the▴price:▴18↵
Please▴input▴the▴quantity:▴2↵
The▴total▴price▴is▴NT\$36.↵
[7:17▴user@ws▴in▴hw2]
請設計一個C語言程式 reverse.c,讓使用者輸入為一個 大於等於10,並且 小於等於99 的兩位數,將其 十位數與個位數 交換後加以輸出,其執行結果可參考如下:
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Please▴input▴a▴two-digit▴number▴(10-99):▴57↵
Number▴57▴is▴reversed▴to▴75.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Please▴input▴a▴two-digit▴number▴(10-99):▴18↵
Number▴57▴is▴reversed▴to▴81.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Please▴input▴a▴two-digit▴number▴(10-99):▴10↵
Number▴57▴is▴reversed▴to▴01.↵
[7:17▴user@ws▴in▴hw2]
【提示:可以使用除法 / 以及餘除 % 來取得兩個整數相除的商及餘數。】
請設計一個C語言程式 feet.c,讓使用者分別輸入在籠子內雞與兔子的數量,計算並輸出籠內的動物總共有幾隻腳(每隻雞有兩隻腳,兔子則有四隻腳),其執行結果可參考如下:
[7:17▴user@ws▴in▴hw2]▴./a.out↵
How▴many▴chickens▴are▴in▴the▴cage?▴5↵
How▴many▴rabbits▴are▴in▴the▴cage?▴3↵
There▴are▴total▴22▴feet▴in▴the▴cage.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
How▴many▴chickens▴are▴in▴the▴cage?▴7↵
How▴many▴rabbits▴are▴in▴the▴cage?▴6↵
There▴are▴total▴38▴feet▴in▴the▴cage.↵
[7:17▴user@ws▴in▴hw2]
小豪同學在速食店打工,請設計一個C語言程式 salary.c,讓他輸入時薪及工讀時數,計算他應領的薪資後加以輸出,其執行結果可參考如下:
[7:17▴user@ws▴in▴hw2]▴./a.out↵
How▴is▴the▴payment▴per▴hour?▴158↵
How▴many▴hours▴do▴you▴work▴in▴a▴week?▴30↵
Your▴salary▴is▴NT\$4740▴per▴week.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
How▴is▴the▴payment▴per▴hour?▴160↵
How▴many▴hours▴do▴you▴work▴in▴a▴week?▴52↵
Your▴salary▴is▴NT\$8320▴per▴week.↵
[7:17▴user@ws▴in▴hw2]
請設計一個C語言的程式 age.c,讓使用者輸入今年的年份及其出生年份(以上皆為 西元年)。計算使用者目前的年齡後並加以輸出。其執行結果可參考如下:
[7:17▴user@ws▴in▴hw2]▴./a.out↵
What▴year▴is▴it▴now?▴2024↵
What▴year▴were▴you▴born?▴1997↵
You▴are▴27▴years▴old.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
What▴year▴is▴it▴now?▴2018↵
What▴year▴were▴you▴born?▴1985↵
You▴are▴33▴years▴old.↵
[7:17▴user@ws▴in▴hw2]
請設計一個C語言程式 angle.c,讓使用者輸入三角形的其中兩個內角度數(假設輸入皆為 正整數,且兩角相加 必小於180),計算並輸出第三個角的度數。其執行結果可參考如下:
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Enter▴the▴first▴angle:▴60↵
Enter▴the▴second▴angle:▴45↵
The▴third▴angle▴is▴75▴degrees.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Enter▴the▴first▴angle:▴90↵
Enter▴the▴second▴angle:▴30↵
The▴third▴angle▴is▴60▴degrees.↵
[7:17▴user@ws▴in▴hw2]
請設計一個C語言程式 perimeter.c,讓使用者輸入矩形的長與寬(假設皆為 正整數)。計算並輸出該矩形的周長,其執行結果可參考如下:
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Please▴input▴the▴length:▴10↵
Please▴input▴the▴width:▴5↵
The▴perimeter▴is▴30.↵
[7:17▴user@ws▴in▴hw2]▴./a.out↵
Please▴input▴the▴length:▴7↵
Please▴input▴the▴width:▴3↵
The▴perimeter▴is▴20.↵
[7:17▴user@ws▴in▴hw2]