diff --git a/code/practise/15 编写大型程序/01 文本格式化/justify.c b/code/practise/15 编写大型程序/01 文本格式化/justify.c new file mode 100644 index 0000000..44c1a27 --- /dev/null +++ b/code/practise/15 编写大型程序/01 文本格式化/justify.c @@ -0,0 +1,37 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include +#include"word.h" +#include"line.h" + +#define MAX_WORD_LEN 20 //ÿʵ󳤶 + +int main(void) { + + char word[MAX_WORD_LEN + 2]; + int word_len; + + clear_line(); + for (;;) { + // read_word 1 ַʳȳ 20Ҫض + read_word(word, MAX_WORD_LEN + 1); + word_len = strlen(word); + if (word == 0) { + flush_line(); + return 0; + } + // ضϳ 20 ַĵ + if (word_len > MAX_WORD_LEN) + word[MAX_WORD_LEN] = '*'; + // + 1 ΪҪһʺӿո + // Ҫǰвյǰ + if (word_len + 1 > space_remaining()) { + write_line(); + clear_line(); + } + add_word(word); + } + + return 0; +} diff --git a/code/practise/15 编写大型程序/01 文本格式化/line.c b/code/practise/15 编写大型程序/01 文本格式化/line.c new file mode 100644 index 0000000..fe99bf9 --- /dev/null +++ b/code/practise/15 编写大型程序/01 文本格式化/line.c @@ -0,0 +1,63 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include +#include"line.h" + +#define MAX_LINE_LEN 60 // ÿеַ + +char line[MAX_LINE_LEN + 1]; +int line_len = 0; // ǰг +int num_words = 0; // ǰеĵ + +void clear_line() { + + line[0] = '\0'; + line_len = 0; + num_words = 0; +} + +void add_word(const char* word) { + + // ׸,Ҫһʺӿո + if (num_words > 0) { + line[line_len] = ' '; + line[line_len + 1] = '\0'; + line_len++; + } + + strcat(line, word); + line_len += strlen(word); + num_words++; +} + +int space_remaining() { + + return MAX_LINE_LEN - line_len; +} + + +void write_line() { + + int extra_space, spaces_to_insert, i, j; + + extra_space = MAX_LINE_LEN - line_len; // ǰδַ + for (i = 0; i < line_len; i++) { + if (line[i] != ' ') + putchar(line[i]); + else { + spaces_to_insert = extra_space / num_words; // ѭʽӿո + for (j = 0; j < spaces_to_insert; j++) + putchar(' '); + extra_space -= spaces_to_insert; + num_words--; + } + } + putchar('\n'); +} + +void flush_line(void) { + + if (line_len > 0) + puts(line); +} diff --git a/code/practise/15 编写大型程序/01 文本格式化/line.h b/code/practise/15 编写大型程序/01 文本格式化/line.h new file mode 100644 index 0000000..85d197c --- /dev/null +++ b/code/practise/15 编写大型程序/01 文本格式化/line.h @@ -0,0 +1,57 @@ +#ifndef LINE_H +#define LINE_H + +/******************************************************** +* +* clear_line: Clears the current line. +* +*********************************************************/ +void clear_line(); + + +/******************************************************** +* +* add_word: Adds word to the end of current line. +* If this is not the first word on the line, +* puts one space before word. +* +*********************************************************/ +void add_word(const char* word); + + +/******************************************************** +* +* space_remaining: Returns the number of characters left +* in the current line. +* +*********************************************************/ +int space_remaining(); + + +/******************************************************** +* +* write_line: Writes the current line with justification. +* +*********************************************************/ +void write_line(); + + +/******************************************************** +* +* flush_line: Write the current line without +* justification.If the line is empty, +* does nothing. +* +*********************************************************/ +void flush_line(void); + + +/******************************************************** +* +* +* +* +* +*********************************************************/ + +#endif \ No newline at end of file diff --git a/code/practise/15 编写大型程序/01 文本格式化/word.c b/code/practise/15 编写大型程序/01 文本格式化/word.c new file mode 100644 index 0000000..0aba924 --- /dev/null +++ b/code/practise/15 编写大型程序/01 文本格式化/word.c @@ -0,0 +1,33 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include"word.h" + +// зƱ +int read_char(void) { + + int ch = getchar(); + + if (ch == '\n' || ch == '\t') + return ' '; + + return ch; +} + +void read_word(char* word, int len) { + + int ch, i; + + while ((ch = read_char()) == ' ') + ; + + i = 0; + while (ch != ' ' && ch != EOF) { + if (i < len) + word[i++] = ch; + + ch = read_char(); + } + + word[i] = '\0'; +} diff --git a/code/practise/15 编写大型程序/01 文本格式化/word.h b/code/practise/15 编写大型程序/01 文本格式化/word.h new file mode 100644 index 0000000..64a2000 --- /dev/null +++ b/code/practise/15 编写大型程序/01 文本格式化/word.h @@ -0,0 +1,14 @@ +#ifndef WORD_H +#define WORD_H + +/*********************************************************************** +* +* read_word: Read the next word from the input and stores it in word. +* Make word empty if no word could be read because of EOF. +* Truncates the word if its length exceeds len. +* +************************************************************************/ + +void read_word(char* word, int len); + +#endif \ No newline at end of file