2023: Statistic map用法

2023: Statistic


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
2s10240K584146Standard

This is my personal contest. Its purpose is to celebrate L.X.X's Birthday. Though L.X. and I have long distance to see each other, you know, she is in Chengdu, I can feel her and always think about her. I hope she will be happy everyday. May 13th is her birthday, happy birthday :)
It is not an easy job to think out some questions, write the test data and source codes. My English is Poor, so there may be some mistakes in the problem description. If you meet the mistakes, please access to http://bbs.jlu.edu.cn Algorithm Board, this is the formal place for you to report the bugs.
I cannot finish this job all by myself without other people's help, they are: Siyee, evil, sea, S—, Leon, zerray. Thanks for their help.
At last, enjoy this contest:)

Input

For this problem, you have to write a program to count the number of the words appeared in the input file. One word consists of 26 lower case letters. So you should convert the upper case letters into lower cases. Words are separated by other symbols. E.G. Sentence "L.X.X" has two different words: l, x. Your program should proceed to the end of the file.

Output

Output all the words appear in the article in the dictionary order, one word per line. For each line, you should output the word first, then a space, and the number of times of this word appeared in the article comes after the space.

Sample Input

Loves X.X

Sample Output

loves 1
x 2

 

Problem Source: billjeff

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<cctype>
using namespace std;
int main()
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    string str;char ch;
    while(scanf("%c",&ch)==1)  str+=tolower(ch);
    map<string ,int  > q;
    for(int i=0;i<str.size();)
    {
        if(isalpha(str[i]))
        {
            string temp;
            while(isalpha(str[i]))
            {
                temp+=str[i];
                i++;
            }
            q[temp]++;
        }
        else i++;
    }
    map<string,int>::iterator it;
    for(it=q.begin();it!=q.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}             
               
   

# C/C++ 项目考核:简易文件内容统计工具 ## 一、项目背景与目标 日常学习中,我们经常需要统计文本文件的基本信息(如行数、单词数)。本项目要求开发一个 “简易文件内容统计工具”,以**C/C++ 为核心**实现文件分析,用 Python 生成简单可视化图表(比如词云),通过 CMake 完成编译构建。 项目旨在考验基础能力: 1. C/C++ 核心语法:变量、函数、数组 / 容器、文件 IO、字符串处理; 2. STL 基础应用:`std::vector`/`std::map`的简单使用; 3. 跨语言配合:C/C++ 输出统计结果到文本文件,Python 读取并绘图; 4. 工程基础:CMake 基本配置(单文件 / 多文件编译)、命令行参数使用。 ## 二、技术栈要求 | 技术模块 | 核心工具 / 库 | 关键知识点 | | -------- | ----------------------- | --------------------------------------- | | 核心逻辑 | C/C++11 及以上 | 文件读取、字符串分割、基础统计 | | 数据存储 | 文本文件(.txt) | 结构化数据写入(如每行一个统计项) | | 可视化 | Python 3.x + matplotlib | 读取文本文件、绘制简单柱状图 | | 构建工具 | CMake 3.10+ | 定义可执行目标、指定 C++ 标准、编译命令 | ## 三、项目功能需求 ### 3.1 核心功能(必做) 1. **C/C++ 统计模块**: - 读取用户通过命令行指定的文本文件(如`input.txt`),支持英文文本即可; - 实现 3 类基础统计: - 总行数(空行不计入,空行定义:仅含空格 / 换行符的行); - 总单词数(以空格 / 标点`!,.?;:`分割,如 “hello,”“hello!” 均算 “hello”); - 高频单词 Top5(仅统计出现次数≥2 的单词,忽略大小写,如 “Hello” 和 “hello” 视为同一单词); - 将统计结果写入result.txt,格式示例: ```plaintext 总行数:15 总单词数:280 高频单词Top5: hello 8次 world 5次 cpp 4次 python 3次 code 2次 ``` 2. **Python 可视化模块**: - 读取`result.txt`中的 “高频单词 Top5” 数据; - 用`matplotlib`绘制柱状图(横轴为单词,纵轴为次数),保存为`top5.png`; - 图表要求:包含标题(如 “高频单词统计”)、横轴标签(“单词”)、纵轴标签(“出现次数”)。 3. **CMake 构建模块**: - 编写CMakeLists.txt,实现: - 指定 C++ 标准为 C++11; - 将 C++ 代码编译为可执行文件(名称建议为`file_stat`); - 编译后可通过命令行运行:`./file_stat input.txt`(Linux/macOS)或`file_stat.exe input.txt`(Windows); - 提供`README.md`,说明编译步骤(如 “mkdir build && cd build && cmake .. && make”)和运行方法。 ### 3.2 扩展功能(选做) 1. C++ 模块: - 支持命令行参数指定输出文件路径(如`./file_stat input.txt --output my_result.txt`); - 新增统计:文本中所有中文字符的总出现次数。 2. Python 模块: - 柱状图中为每个柱子设置不同颜色(如用`['red', 'green', 'blue', 'yellow', 'purple']`)。 3. 构建优化: - 用 CMake 定义 “run” 目标,执行`make run`(Linux/macOS)或`cmake --build . --target run`(Windows)时,自动运行 C++ 程序(默认读取`test.txt`)并调用 Python 脚本生成图表。 ## 四、实现提示 1. **C++ 文件读取**: - 用`std::ifstream`打开文件,`getline()`逐行读取,通过判断行是否为空统计有效行数; - 单词分割:遍历每行字符,遇到空格或标点时,将之前的字符拼接为单词,用`tolower()`统一转为小写后存入`std::map<std::string, int>`计数。 2. **高频词排序**: - 用`std::map`统计完单词次数后,转为`std::vector`(存储`pair<单词, 次数>`),再用`std::sort`排序,取前 5 个次数≥2 的单词。 3. **Python 绘图**: - 读取`result.txt`:用`open()`打开文件,遍历行找到 “高频单词 Top5:” 后的内容,按行分割提取单词和次数; - 绘图示例代码: ```python import matplotlib.pyplot as plt # 假设提取的数据为: words = ["hello", "world", "cpp", "python", "code"] counts = [8, 5, 4, 3, 2] plt.bar(words, counts) plt.title("高频单词统计") plt.xlabel("单词") plt.ylabel("出现次数") plt.savefig("top5.png") ``` 4. **CMake 基础配置示例**: ```cmake cmake_minimum_required(VERSION 3.10) project(FileStatTool) # 项目名称 set(CMAKE_CXX_STANDARD 11) # 指定C++11标准 add_executable(file_stat main.cpp) # 编译main.cpp为file_stat可执行文件 ``` ## 五、提交要求 1. 源代码: - C++ 代码(如`main.cpp`,若分模块可拆分为`statistic.cpp`+`statistic.h`等); - Python 脚本(如`plot.py`); - `CMakeLists.txt`; - 测试文本`test.txt`(自行准备,建议 100-500 单词)。 2. 运行结果: - 生成的`result.txt`; - 生成的`top5.png`。 3. 文档: - `README.md`:包含编译步骤、运行命令、功能说明、实现过程中遇到的问题及解决方法(至少 1 个问题)。 ## 六、难度说明 - 核心功能仅涉及 C++ 基础语法(循环、条件判断)、STL 入门(map 存储键值对、vector 排序)、文件 IO(读写文本),逻辑简单易实现; - Python 部分无需复杂数据处理,仅需基础文件读取和 matplotlib 调用(可直接参考示例代码修改); - CMake 配置简化,无需依赖外部库,3-5 行核心代码即可完成编译配置,降低工程化门槛。
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值