第二次作业

这个作业属于哪个课程:https://edu.cnblogs.com/campus/zjlg/26rjjc

项目 内容
这个作业属于哪个课程 https://edu.cnblogs.com/campus/zjlg/26rjjc
这个作业的目标 实现英文 TXT 统计
姓名—学号 牛家豪-2024331200101
码云仓库地址 https://gitee.com/nnjh66/nnderuanjianjishujichu

命令行文本计数统计工具 wc.exe

C 语言实现 | 软件技术基础课程作业项目

一、项目简介

本项目是基于 C 语言开发的命令行文本统计工具,对标 Unix 经典 wc 命令,支持统计文本的字符数、单词数、行数。纯 C 实现,无第三方依赖,GCC 编译即可运行。

核心特性

  • -c 统计字符数量
  • -w 统计单词数量
  • -l 统计行数
  • 命令行传参,简洁易用

二、项目目录结构

.
├── src
│   └── wc.c          // 主程序源代码
├── tests
│   └── test.c        // 单元测试代码
├── samples
│   ├── test_base.txt // 基础测试样例
│   └── test_code.c    // C代码样例文件
├── .gitignore
├── LICENSE
└── README.md

三、源代码

src/wc.c

#include <stdio.h>

int main(int argc, char *argv[])
{
    // 判断参数数量是否正确
    if (argc != 3)
    {
        printf("Usage: %s -[c/w/l] filename\n", argv[0]);
        return 1;
    }

    FILE *fp = fopen(argv[2], "r");
    if (fp == NULL)
    {
        printf("File open error!\n");
        return 1;
    }

    int ch;
    long char_cnt = 0;   // 字符数
    long word_cnt = 0;   // 单词数
    long line_cnt = 0;   // 行数
    int in_word = 0;     // 是否处于单词内

    while ((ch = fgetc(fp)) != EOF)
    {
        char_cnt++;

        if (ch == '\n')
        {
            line_cnt++;
        }

        // 分隔符:空格、换行、制表符
        if (ch == ' ' || ch == '\n' || ch == '\t')
        {
            in_word = 0;
        }
        else if (in_word == 0)
        {
            in_word = 1;
            word_cnt++;
        }
    }
    fclose(fp);

    // 判断选项
    if (argv[1][1] == 'c')
    {
        printf("%ld\n", char_cnt);
    }
    else if (argv[1][1] == 'w')
    {
        printf("%ld\n", word_cnt);
    }
    else if (argv[1][1] == 'l')
    {
        printf("%ld\n", line_cnt);
    }
    else
    {
        printf("Unknown option!\n");
        return 1;
    }
    return 0;
}

tests/test.c

#include <stdio.h>

int main()
{
    printf("==== Unit Test Start ====\n");
    printf("Test file: samples/test_base.txt\n");
    FILE *fp = fopen("samples/test_base.txt", "r");
    if (fp == NULL)
    {
        printf("ERROR: Cannot open test file!\n");
        return 1;
    }
    fclose(fp);
    printf("File open SUCCESS\n");
    printf("==== Unit Test End ====\n");
    return 0;
}

samples/test_base.txt

Hello World C

samples/test_code.c

int main()
{
    return 0;
}

.gitignore

*.exe
*.o
*.out
*.bin
.vscode/
.DS_Store

LICENSE (MIT)

MIT License

Copyright (c) 2026

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

四、编译与运行

4.1 编译命令(项目根目录 PowerShell)

gcc src/wc.c -o wc.exe -mconsole
gcc tests/test.c -o test.exe -mconsole

4.2 运行命令

# 统计字符
.\wc.exe -c samples/test_base.txt
# 统计单词
.\wc.exe -w samples/test_base.txt
# 统计行数
.\wc.exe -l samples/test_base.txt
# 运行单元测试
.\test.exe

4.3 预期输出

输入文件内容:Hello World C

  • -c → 13
  • -w → 3
  • -l → 1

五、使用说明

命令格式:

.\wc.exe -参数 文本路径

参数说明:

  • -c:统计文件全部字符(包含空格)
  • -w:统计单词,以空格 / 换行 /tab 分隔
  • -l:统计文件行数
posted @ 2026-11-02 17:21  nn#  阅读(3)  评论(0)    收藏  举报