首页 > 编程语言 > 详细

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

时间:2014-08-09 11:48:27      阅读:441      评论:0      收藏:0      [点我收藏+]

其中要使用的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;
}


【ThinkingInC++】4、统计txt文本中单词的个数,布布扣,bubuko.com

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

原文:http://blog.csdn.net/cutter_point/article/details/38453347

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!