2018q3 HomeWork2 (lab0)
contributed by <LiuJuiHung>
英文閱讀
C Programing Lab
學習使用 Git 與 GitHub
- GitHub 使用方法
- 先建立出自己的 SSH key , 在 terminal 輸入以下指令
- 將電腦上的 SSH key (輸入以下指令)複製到 GitHub 的 SSH key 裡
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 在 terminal 中輸入以下指令驗證
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 本機安裝 git
- 初次設定 Git
git config --global user.name "your_name"
設定使用者名稱
git config --global user.email yourmail@example.com
設定 email
- 將老師提供的 lab0-c 作業 fork 到自己的 github
- 在 terminal 輸入以下指令,將作業 git clone 下來
- 程式碼更改後
git add .
加入更改過的全部的檔案
git commit
撰寫 commit message
git push
將檔案 push 到 github 上
Makefile
了解 Makefile 運作
- CC 表示使用 gcc
- CFLAGS:宣告使用的參數
- -O:表示最佳化的程度
- -O 預設就是 -O1,你可以指定成 -O2 或 -O3 ,數字越大表示最佳化程度越好,但是會增加編譯時間
- -g:編入偵錯資訊
- 當使用 GDB 軟體進行偵錯,必須加入 -g 使 GDB 能夠讀取。一般情況下可以不用 -g,因為他也會增加 binary 大小
- -Wall:顯示警告訊息
- 使用這個參數會在編譯時顯示更多的警告訊息
- 這個參數相當有用,特別是找不到 libs/headers 之類的問題
- -Werror:將警告訊息轉為錯誤訊息
- GCC 常用參數詳解
- all:預設執行的目標,這裡包含
GIT_HOOKS
和 qtest
- queue.o 的項目裡有
queue.c
、queue.h
、 haeness.h
- 輸入
make qtest
會依照 CFLAGS
所定義的參數進行編譯,編譯出一個 qtest
執行檔
- 執行
make test
會先查看是否有 qtest
scripts/driver.py
這兩個檔案,若存在則執行 driver.py
測試並計算分數
clean
是清除編譯出的 *.o
qtest
和其他生成的檔案
Queue 開發過程
題目敘述:
- This program implements a queue supporting both FIFO and LIFO operations
- It uses a singly-linked list to represent the set of queue elements
Queue Structure
- 增加 queue 內的總數 size
- 增加 pointer to tail node
Create empty queue
- 為一個新的 queue 配置一個記憶體空間,將此空間初始化並回傳
Free all storage used by queue
- 主要目的:free 掉整個 queue 的空間
- 在 insert 每個 element 時,都會配置一個記憶體空間,因此利用 while 迴圈將每個 element 的記憶體空間循環的 free 掉
Insert element at head of queue
- 主要目的:在前端 insert 一個新的 element
- 判斷 queue 是否為 NULL
- 利用 strdup() 將 s 複製到 newh->value
- 判斷為第一筆 element 時,tail pointer 要指向此 element
- q_size 要增加
Attempt to insert element at tail of queue
- 主要目的:在後端 insert 一個新的 element
- 判斷 queue 是否為 NULL
- 利用 strdup() 將 s 複製到 newt->value
- 判斷為第一筆 element 時,head pointer 要指向此 element
- q_size 要增加
Attempt to remove element from head of queue
- 主要目的:刪除前端的 element
- 判斷 sp 是否為 NULL
- 利用 strncpy() 將要被移除的 element 複製到 sp
- 並將此 element free
Return number of elements in queue
- 主要目的:回傳 queue 的 size
- 若 queue = NULL ,則回傳 '0'
- 將 element 的個數回傳
Reverse elements in queue
評分結果
自動評分系統運作原理
當執行 $ make test
時,會執行 Makefile
裡的以下這段程式