【ThinkingInC++】4、统计txt文本中单词的个数

其中要使用的txt文本!

 header defines classes for file IO, including ifstream, whose constructor takes a file name an argument. The expression f >> word extracts the next non-whitespace token from the file and returns the stream. When a stream appears in a boolean context, such as the while statement above, it evaluates to true until end-of-file reached or an error occurs.

It turns out that on many operating systems you can take advantage of i/o redirection, which allows you to map standard input or output to a file on the command line. If you rewrite Words.cpp to read from cin, then you can read any file you want, as the following illustrates.


源程序


/**
* 功能:统计txt文本中单词的个数
* 时间:2014年8月9日09:09:33
* 作者:cutter_point
*/

#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#include<bitset>

using namespace std;

int main()
{
    ifstream fin("words.txt");
    string word;
    int wordNo=0;

    while(fin>>word)
    {
        ++wordNo;
    }

    std::bitset<8> b2(wordNo);

    cout<<"一共有:2进制"<<b2<<"个单词"<<endl;
    cout<<"一共有:8进制"<<oct<<wordNo<<"个单词"<<endl;
    cout<<"一共有:10进制"<<wordNo<<"个单词"<<endl;
    cout<<"一共有:16进制"<<hex<<wordNo<<"个单词"<<endl;


    system("pause");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己写的,版权所有哈~~~~有错误请指点 题目描述: 从硬盘上读取一文本文件(一篇英语文章),将这篇文章的内容使用适当的数据结构保存起来,能够方便的统计出各个单词出现的频率、和查询特定的单词。 在主函数实现下列控制命令 openfile <输入文件名> display 显示文件单词统计结果 find <查询的单词> quit 对于执行失败的命令,给出相应的提示信息。 操作过程: 1) 打开文件 command: openfile <输入文件名> 2) 显示文件统计结果 command: display 输出结果:显示各个单词出现的频率并且降序排列:<单词> <次数> <频率> 3) 查询单词 cmmand: find <查询的单词> 输出结果: <段序号>-<句序号>-<单词序号> <段序号>-<句序号>-<单词序号> <段序号>-<句序号>-<单词序号> 共出现了<单词个数>次 4) 退出 command: quit 5) 显示帮助 command: help 输出结果: openfile <输入文件名> display 显示单词统计结果 find <查询的单词> quit 退出 考察点: 1) 对象的继承和使用 2) 对象的构造和析构 3) 控制台的输入和输出 4) 程序的调试和运行 提示: 1) 将文章分为词、句、段等三级结构,定义三个对象保存词、句、段。单词的分割符是空格、分号、顿号(ascii码0x20,0x2D、0x3B),句子的分割符是句号、问号和感叹号(ascii码0x21、0x2E、0x2F),段落的分割符是回车(ascii码0x0D 0x0A),其它符号省略。 2) 对象保存可以使用Vector或动态数组

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值