1. 程式人生 > 其它 >單詞計數

單詞計數

技術標籤:c++

單詞計數-軟體工程作業


連結: link.

一、專案要求

程式處理使用者需求的模式為:

wc.exe [parameter] [file_name] 

基本功能列表

wc.exe -c file.c    //返回檔案 file.c 的字元數
wc.exe -w file.c    //返回檔案 file.c 的詞的數目 
wc.exe -l file.c    //返回檔案 file.c 的行數

擴充套件功能1

 -s   遞迴處理目錄下符合條件的檔案。
-a   返回更復雜的資料(程式碼行 / 空行 / 註釋行)。

二、程式碼實現

1. 統計單詞數

此處對輸入檔案預設要求有:
(1)預設輸入文字單詞與單詞間,逗號與單詞間可以有一個或多個空格
(2)換行時預設不會斷裂某個單詞,使一個單詞被計數為兩個
(3)連字元兩側視為兩個單詞,如hip-hop
(4)接收二進位制檔案

判斷單詞的方法:
遇到第一個字母,單詞開始,遇到非字母,單詞結束,
遇到新字母時,若上一個單詞已結束,則單詞數+1,且新單詞標記未已開始

int wordcounter(char* filename, long int
* num) { char theline[1100];//txt檔案每行最多1024個字元 long int linenum = 0, charnum = 0, wordnum = 0; FILE* p = NULL; if ((p = fopen(filename, "rb")) == NULL) { perror(filename); return 0; } //各行資料分別處理,linenum+1 int l_lenth = 0; // 該行長度 char c; //當前字元 int
beword;//上一個單詞是否結束,未結束為1,遇到下一字母word+1 while (fgets(theline, 1024, p) != NULL) { linenum++; l_lenth = strlen(theline); //最初,本行上一個單詞已結束,則beword=0 beword = 0; for (int i = 0; i < l_lenth; i++) { c = theline[i]; charnum++; // c是否是一個字母且上一單詞已結束 if (Letter(c) && !beword) { wordnum++; beword = 1; //對於後面的字元,上一單詞未結束 // cout << "wordbegin:" << c << endl; } if (!Letter(c) && beword) //c不是字母且上一單詞未結束,應標記為單詞結束 beword = 0; } theline[1099] = { 0 }; // cout << linenum << " line " << wordnum << "words lenth:" <<l_lenth<< endl; } num[0] = linenum; num[1] = charnum; num[2] = wordnum; cout << "the number of words is:" << num[2] << endl; return linenum; }

2. 統計字元數


int charcounter(char* filename, long int* num)//計算char
{

    char theline[1100];//txt檔案每行最多1024個字元
    long int charnum = 0;
    FILE* p = NULL;
    if ((p = fopen(filename, "rb")) == NULL)
    {
        perror(filename);
        return 0;
    }

    int  l_lenth = 0; // 該行長度
    

    while (fgets(theline, 1024, p) != NULL)
    {

        l_lenth = strlen(theline);

        for (int i = 0; i < l_lenth; i++)
        {
            charnum++;

        }
        theline[1099] = { 0 };
    }

    num[1] = charnum;
    cout << "the number of charactors:" << num[1] << endl;
    return  charnum;
}

3. 統計行數

int linecounter(char* filename, long int* num) //計算line
{
    char theline[1100];//txt檔案每行最多1024個字元
    long int  linenum = 0;
    FILE* p = NULL;
    if ((p = fopen(filename, "rb")) == NULL)
    {
        perror(filename);
        return 0;
    }
    //各行資料分別處理,linenum+1

    while (fgets(theline, 1024, p) != NULL)
    {
        linenum++;
    }
    num[0] = linenum;
    cout << "the number of  lines:" << num[0] << endl;
    return  linenum;
}

4. 統計程式碼行,空行,註釋行

空行統計:
用effector()函式檢測字元,本行不存在除空格與控制符以外的字元則為空行

註釋行統計:
本行有連續存在的兩個“\”


int morecounter(char* filename, long int* num)
{

    char theline[1100];//txt檔案每行最多1024個字元
    long int  linenum = 0, charnum = 0, wordnum = 0;
    long int codeline = 0, blackline = 0, noteline = 0;

    FILE* p = NULL;
    if ((p = fopen(filename, "rb")) == NULL)
    {
        perror(filename);
        return 0;
    }
    //各行資料分別處理,linenum+1
    int  l_lenth = 0; // 該行長度
    char c; //當前字元

    int  balckflag = 0, noteflag = 0;
    while (fgets(theline, 1024, p) != NULL)
    {
         l_lenth = strlen(theline);
         if (l_lenth)codeline++;  //判定程式碼行
         int flagblack = 0;
        for (int i = 0; i < l_lenth; i++)
        {
            c = theline[i];
      
            //判定註釋行,連續兩個//為註釋行
            if (c == '/')noteflag++;
            if (noteflag == 2)
            {
                noteflag = 0;
                noteline++;
                break;  //結束本行迴圈
            } 
            //判定空行
            //判定是否是格式控制字元,是則返回0,不是返回1
            if (effector(c)) //有任一字元不是格式控制字元或空格
            {
                flagblack = 1; // 本行不是空行
                break;
            }
        }
        theline[1099] = { 0 };
        if (!flagblack)blackline++;
    }
     
    cout << "the number of codelines is:" << codeline << endl;
    cout << "the number of notelines is:" << noteline << endl;
    cout << "the number of blacklines is:" << blackline << endl;

}

三、執行結果

1. 統計行數

在這裡插入圖片描述

2. 統計字元數

在這裡插入圖片描述

3. 統計單詞數

在這裡插入圖片描述

4. 統計程式碼行,空行,註釋行,

在這裡插入圖片描述

四、開發耗時

預估時間與實際耗時記錄表:

Personal software Process Stages預估耗時(min)實際耗時 (min)
Planning
Estimate3040
Development
Analysis3030
Design Spec1010
Design Review2020
Coding standard1010
Design6080
Coding6090
Code Reviewing2020
Test3050
Reporting
Test Report3040
Size Measurement1010
Postmortem & Process Improvement Plan2030
sum330430