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;
}

 

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