如何计算文件

时间:2017-01-05 16:41:16

标签: c++ visual-studio

我一直在尝试编写一个用c ++编写文件内容的程序,我使用了Ascii表和if语句来定义字符,句子,但我不确定如何定义单词。代码还有另一个问题,它将文件的最后两个字符计算两次,我一直试图通过设置" count = -1"来解决这个问题。但事实并非如此。任何帮助都会非常感激。

#include <iostream>
#include <fstream>  

using namespace std;


int main()
{


ifstream infile;
infile.open("cpp.txt");
int count = 0; // counting characters
int count_t = 0; // text objects
int count_s = 0; // sentence count
int count_i_s = 0; // e-sentence
int count_in_s = 0; // question count
char C;
//char word[30];




while (!infile.eof())
{
    infile.read(&C, 1);

    {
        // counting characters
        if (C >= ' '&& C <= '~'|| C == '\t' || C == '\n')
            count++;

    }
    //{
    //  infile << word;
    //  count_w++;
    //}

    {
        //counting sentances
        if (C == '.'|| C == '!'|| C == '?')
            count_s++;
    }
    {
        //counting exclamatory sentance
        if (C == '!')
            count_i_s++;
    }
    {
        //counting question sentances
        if (C == '?')
            count_in_s++;
    }

    {
        //counting text objector
        if (C == ' '|| C == '\t'|| C == '\n')
            count_t++;
    }





}



cout << "the number of charaters :" << count << endl;
cout << "the number of sentances :" << count_s << endl;
cout << "the number of exclamtory sentances :" << count_i_s << endl;
cout << "the number of interrogative sentances :" << count_in_s << endl;
cout << "the number of text objects :" << count_t << endl;




return 0;
}

0 个答案:

没有答案