软件技术基础第二次作业
- 项目简介
- 基础功能:能正确统计导入的纯英文txt文本中的字符数,单词数,句子数
- 进阶功能:统计代码行、空行、注释行等,并提供相应命令接口
- 项目特点
本项目实现了一个仿Linux wc的命令行文本统计程序,使用C语言开发,编译后生成 wc.exe。
程序通过命令行参数接收用户指定的统计模式,读取指定的英文.txt文件,按参数输出对应的统计结果
| 编译器 | MSVC(Visual Studio) |
| 开发语言 | C语言 |
| 版本控制工具 | Git(分布式版本控制系统) |
| 代码托管平台 | 码云gitee | - 命令用法
使用格式:wc.exe [选项] <文件名>
示例
| 查看字符数 | wc.exe -c file.txt |
| 查看单词数 | wc.exe -w file.txt |
| 查看句子数 | wc.exe -s file.txt |
| 分析代码文件 | wc.exe -a code.c |
| 同时查看字符和单词 | wc.exe -cw file.txt|
| 默认三项全看 | wc.exe file.txt |
附:
- -c 字符统计
- -w 单词统计
- -s 句子统计
- -a 代码分析
说明:选项可以单独使用,也可以组合使用(如-cw表示同时统计字符和单词)。若未指定任何选项,程序默认输出字符数、单词数和句子数
- 代码展示
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct {
int chars;
int words;
int sentences;
int total_lines;
int blank_lines;
int comment_lines;
int code_lines;
} Stats;
void init_stats(Stats *s) {
s->chars = 0;
s->words = 0;
s->sentences = 0;
s->total_lines = 0;
s->blank_lines = 0;
s->comment_lines = 0;
s->code_lines = 0;
}
void process_file(const char *filename, Stats *s, int ext) {
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Error: Cannot open file %s\n", filename);
return;
}
int c, prev_c = 0;
int in_word = 0;
int line_len = 0;
int is_blank = 1;
int is_comment = 0;
int prev_line_was_comment = 0;
while ((c = fgetc(fp)) != EOF) {
s->chars++;
if (isalpha(c)) {
if (!in_word) {
s->words++;
in_word = 1;
}
is_blank = 0;
} else {
in_word = 0;
}
if (c == '.' || c == '!' || c == '?') {
s->sentences++;
}
if (c == '\n') {
s->total_lines++;
if (ext) {
if (line_len == 0 || is_blank) s->blank_lines++;
else if (prev_line_was_comment || (line_len >= 2 && prev_c == '/' && c == '/')) {
s->comment_lines++;
prev_line_was_comment = (line_len >= 2 && prev_c == '/' && c == '/');
} else {
s->code_lines++;
prev_line_was_comment = 0;
}
}
line_len = 0;
is_blank = 1;
} else {
line_len++;
if (c != ' ' && c != '\t') is_blank = 0;
}
prev_c = c;
}
if (line_len > 0) {
s->total_lines++;
if (ext) {
if (is_blank) s->blank_lines++;
else if (prev_line_was_comment) s->comment_lines++;
else s->code_lines++;
}
}
fclose(fp);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: wc.exe [options] <filename>\n");
printf("Options: -c (chars), -w (words), -s (sentences), -a (code stats)\n");
return 1;
}
const char *opt = argv[1];
const char *file = argv[2];
Stats s;
init_stats(&s);
int ext_mode = (strchr(opt, 'a') != NULL);
process_file(file, &s, ext_mode);
int printed = 0;
if (strchr(opt, 'c')) { printf("字符数: %d\n", s.chars); printed = 1; }
if (strchr(opt, 'w')) { printf("单词数: %d\n", s.words); printed = 1; }
if (strchr(opt, 's')) { printf("句子数: %d\n", s.sentences); printed = 1; }
if (ext_mode) {
printf("--- 代码行统计 ---\n");
printf("总行数: %d\n空行: %d\n注释行: %d\n代码行: %d\n",
s.total_lines, s.blank_lines, s.comment_lines, s.code_lines);
printed = 1;
}
if (!printed) {
printf("字符数: %d, 单词数: %d, 句子数: %d\n", s.chars, s.words, s.sentences);
}
return 0;
}
测试文本
“Hi!How are you?
I am fine.”
例1(字统计)
点击查看代码
``` PS D:\wc_project> bin\wc.exe -c samples\sample.txt 字符数:25 ```点击查看代码
``` PS D:\wc_project> bin\wc.exe -w samples\sample.txt 单词数:7 ```点击查看代码
``` PS D:\wc_project> bin\wc.exe -s samples\sample.txt 句子数:3 ```点击查看代码
``` PS D:\wc_project> bin\wc.exe -a samples\sample.txt 总行数:2 代码数:2 空行:0 注释行:0 ```2.【核心实现】"请用 C++ 实现一个命令行文本统计工具,支持 -c 统计字符数、-w 统计单词数、-s 统计句子数,扩展支持 -a 统计代码行/空行/注释行。要求单趟扫描,注释识别要能排除字符串字面量。"
3.【边界确认】"单词 don't、state-of-art 应该算几个?... 算几句?文件末尾没有换行符时行数怎么算?"
4.【单元测试】"帮我设计覆盖边界情况的测试用例,包括空文件、只有换行符、无结尾换行、连续空格、UTF-8 中文混合等。"
5.【文档撰写】"请帮我写 README.md,需包含项目简介、文件列表、例程运行结果、版本迭代说明。"
6.【结果核对】"帮我核对这段文本的统计结果:Hi!How are you?\nI am fine."
浙公网安备 33010602011771号