一道筆試題:統計檔案中的單詞出現次數

來源:互聯網
上載者:User

 統計test.txt檔案中的單詞出現次數:

#include <stdio.h>
#include <malloc.h>
#include <ctype.h>
#include <string.h>
#include "windows.h"

#define SOURCE_FILE "test.txt"
#define OUTPUT_FILE "word.txt"
#define MAX_WORD_LEN 128

typedef struct treenode
{
    char szWord[MAX_WORD_LEN];
    int nCount;
    struct treenode* pLeft;
    struct treenode* pRight;
}BNODE;

int getword(FILE* pFile,char* pasWordBuffer,int nBufferLen)
{
    int result = 0;
    result = fscanf(pFile, "%s", pasWordBuffer);
    if(EOF == result || 0 == result)
        result = 0;
    else
        result = 1;

    return result;
}

void binary_tree(BNODE** ppNode,char* pszWord)
{
    BNODE* pCurrentNode = NULL; //
    BNODE* pMemoNode = NULL; //
    int nStrCmpRes = 0; //

    if(ppNode != NULL && pszWord != NULL)
    {
        //              BNODE* pCurrentNode = NULL;
        //              BNODE* pMemoNode = NULL;
        //              int nStrCmpRes=0;

        pCurrentNode = *ppNode; //____(1)_____

        while(pCurrentNode)
        {
            //尋找插入位置
            nStrCmpRes = strcmp(pszWord, pCurrentNode->szWord); //___(2)___ //如果 pszWord 大於 pCurrentNode->szWord,返回正數

            if(!nStrCmpRes)
            {
                pCurrentNode->nCount++; //___(3)___

                return;//如果當前節點的值等於pszWord就返回
            }
            else
            {
                pMemoNode=pCurrentNode;// ___(4)___
                pCurrentNode = nStrCmpRes>0? pCurrentNode->pRight : pCurrentNode->pLeft;
            }
        }
    }

    pCurrentNode = new BNODE;//已有的二叉樹裡面無節點的值為pszWord,建立一個新節點

    if(pCurrentNode != NULL)
    {
        memset(pCurrentNode,0,sizeof(BNODE));
        strncpy(pCurrentNode->szWord,pszWord,MAX_WORD_LEN-1);
        pCurrentNode->nCount=1;
    }

    if(pMemoNode==NULL)
    {
        *ppNode= pCurrentNode; // ___(5)___//建立二叉樹的第一個節點時
    }
    else
         if(nStrCmpRes>0)
        {
            pMemoNode->pRight=pCurrentNode;//pszWord的值比現有二叉樹右節點還大,所以放在右邊。
        }
         else
        {
            pMemoNode->pLeft=pCurrentNode;
        }
}

void midorder(FILE* pFile,BNODE* pNode)
{
    if(!pNode||!pFile) return; //___(6)___

    midorder(pFile,pNode->pLeft);
    fprintf(pFile,"%s %d\n",pNode->szWord,pNode->nCount);
    midorder(pFile,pNode->pRight);
}

void main()
{
    FILE* pFile=NULL;
    BNODE* pRootNode=NULL;
    char szWord[MAX_WORD_LEN]={0};

    pFile=fopen(SOURCE_FILE,"r");

    if(pFile==NULL)
    {
        printf("Can't open file %s\n%d",SOURCE_FILE,GetLastError());
        return;
    }

    while(getword(pFile,szWord,MAX_WORD_LEN) == 1)
    {
        binary_tree(&pRootNode,szWord); // ___(7)___
    }

    fclose(pFile);

    pFile=fopen(OUTPUT_FILE,"w");
    midorder(pFile,pRootNode);
    fclose(pFile);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.