Google Code Prettify

2010年12月18日 星期六

C - 文章排版程式(Main Program & Makefile)


justify.c
#include <stdio.h>
#include <string.h>
#include "line.h"
#include "word.h"
int main(void)
{
char word[MAX_WORD_LEN * 2]; //宣告字元陣列、單字長度變數&行數變數。
int word_len, num_line;
clear_line(); //清空陣列。
for (num_line = 1;;num_line++) { //開始讀入文章並修改編排,而後輸出。
read_word(word, MAX_WORD_LEN + 1); //呼叫讀取單字的函數。
word_len = strlen(word); //取得所讀取單字的長度。
if (word_len == 0) { //若無字可讀了,便將最後一行輸出,結束程式。
flush_line();
return 0;
}
if (word_len > MAX_WORD_LEN) //若單字長度超過最大長度,則超過的部分以一個*取代。
word[MAX_WORD_LEN] = '*';
if (word_len + 1 > space_remaining()) { //當該行已容不下下一個單字時,開始執行編排並輸出。
if (num_line % 2 == 1) { //若為奇數行,則將該行多餘的空格擺至最右端。
write_line_odd();
clear_line();
}
else { //若為偶數行,則將該行多餘的空格擺至最左端。
write_line_even();
clear_line();
}
}
add_word(word); //將所讀取單字加入該行。
}
}
view raw justify.c hosted with ❤ by GitHub

Makefile
justify: justify.o word.o line.o
gcc justify.o word.o line.o -o justify
justify.o: justify.c word.h line.h
gcc -DMAX_WORD_LEN=10 -c justify.c
word.o: word.c word.h
gcc -c word.c
line.o: line.c line.h
gcc -DMAX_LINE_LEN=60 -c line.c
view raw Makefile hosted with ❤ by GitHub

沒有留言:

張貼留言