mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-02-11 06:26:59 +08:00
4-16
This commit is contained in:
37
code/practise/15 编写大型程序/01 文本格式化/justify.c
Normal file
37
code/practise/15 编写大型程序/01 文本格式化/justify.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#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;
|
||||
}
|
||||
63
code/practise/15 编写大型程序/01 文本格式化/line.c
Normal file
63
code/practise/15 编写大型程序/01 文本格式化/line.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#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);
|
||||
}
|
||||
57
code/practise/15 编写大型程序/01 文本格式化/line.h
Normal file
57
code/practise/15 编写大型程序/01 文本格式化/line.h
Normal file
@@ -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
|
||||
33
code/practise/15 编写大型程序/01 文本格式化/word.c
Normal file
33
code/practise/15 编写大型程序/01 文本格式化/word.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#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';
|
||||
}
|
||||
14
code/practise/15 编写大型程序/01 文本格式化/word.h
Normal file
14
code/practise/15 编写大型程序/01 文本格式化/word.h
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user