C++实验——读入文件判断词数句数

目标

     •熟悉string的处理

步骤

   读入待处理的文件

   处理文件:统计文件中共有多少个单词,多少句话。

思路

    •读入文件,文章中有好多[ ]一样的特殊字符,首先需要将特殊字符处理掉。

    •判断词数以空格为分隔符进行判断,遇到空格单词数加一。

   判断句数以句号、感叹号、问号(。!?)为界限符,遇到符号加一。

材料

将下面的材料复制粘贴,放入txt文件中到指定目录下D:\\STL\\material.txt。

Java is a general-purpose computer-programming language that is concurrent, class-based, object-oriented,[15] and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA),[16] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.[17] Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. As of 2016, Java is one of the most popular programming languages in use,[18][19][20][21] particularly for client-server web applications, with a reported 9 million developers.[22] Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform! The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them?

代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

void handleSpecialChar()   //处理特殊字符
{
    ifstream in("d:\\STL\\meterial.txt");
    ofstream out;
    string strText;
    getline(in,strText);

    cout << "特殊字符:" << endl;
    int n = 0;
    while( n = strText.find("[") != string::npos)
    {
        n = strText.find("[");
        cout << n << " ";
        string s(strText,n,4);
        cout << s << endl;
        strText.erase(n,4);
    }
    cout << "\n\n处理掉特殊字符的文章为:" << endl;
    cout << strText;

    out.open("d:\\STL\\material.txt");
    out << strText << endl;
    out.close();

}

void letterNum()      //判断词数
{
    ifstream in("d:\\STL\\material.txt");
    string strText;
    getline(in,strText);
    string strSeparator = " ";
    int size_pos = 0;
    int size_prev_pos = 0;

    int count = 0;
    while((size_pos = strText.find_first_of(strSeparator,size_pos))!= string :: npos)
    {
        count++;
        size_prev_pos = ++size_pos;
    }

    if(size_prev_pos != strText.size())
    {
        count++;
    }
    cout << count;
}
int wordNum()        //判断句数
{
    ifstream in("d:\\STL\\material.txt");
    string strText;
    getline(in,strText);

    int n = strText.find_first_of(".?!");
    int count  = 0;
    if(n) count++;
    cout << "\n文章中结尾字符的位置(.?!):";
    cout << n << '\t';
    while(n != -1)
    {
        n = strText.find_first_of(".?!",n + 1);
        if(n == -1) break;
        cout << n << '\t';
        if(n != -1)
        {
            count++;
        }

    }
    cout << endl;
    return count;
}

int main()
{
    handleSpecialChar();
    cout << "\n\n文章的单词数为:";
    letterNum();
    cout << endl;
    cout << "\n文章的句子数为:"<< wordNum() << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章