其他分享
首页 > 其他分享> > 都雪冬 20190912-3 词频统计

都雪冬 20190912-3 词频统计

作者:互联网

此作业要求参见 https://edu.cnblogs.com/campus/nenu/2019fall/homework/6583

功能1 小文件输入。 为表明程序能跑,结果真实而不是迫害老五,请他亲自键
盘在控制台下输入命令。

代码如下:

void CountWord(char *current)
{
    wordNode *pNode = NULL;
    pNode = SearchWord(current);
    if(NULL == pNode)
    {
        return;
    }
    else
    {
        pNode->iWordCount++;
    }
}

截图如下:

其它几个功能考虑了很久,目前还不会做,只能靠自己的理解实现部分功能

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
const int WORD_LENGTH = 256; 
const int FREQUENCY_COUNT=6;    
struct wordNode
{
    char word[WORD_LENGTH];             
    int iWordCount;               
    wordNode *pNext;           
};
 
wordNode *pHeader = NULL;  
 
void CountWord(char *current);
wordNode * SearchWord(char *current);
void PrintResult();
void Release();
 
int main()
{
    char temp[WORD_LENGTH];
    FILE *fp; 
    if((fp=fopen("123.txt", "r"))==NULL )   
    {
        printf("Open file failed!!\n");
        exit(1);
    }
        while( EOF != (fscanf(fp,"%s",temp)) )
    {
        CountWord(temp);
    }
       fclose(fp); 
       PrintResult();
          Release(); 
}
 
void CountWord(char *current) 
{
    wordNode *pNode = NULL;
    pNode = SearchWord(current);
    if(NULL == pNode)
    {
        return;
    }
    else
    {
        pNode->iWordCount++;
    }
}
 
wordNode * SearchWord(char *current)
{
    if( NULL == pHeader)
    {
        pHeader = new wordNode;
        strcpy(pHeader->word, current);
        pHeader->iWordCount = 0;
        pHeader->pNext = NULL;
        return pHeader;
    }
    wordNode *pCurr = pHeader;
    wordNode *pPre = NULL;
    while( (NULL != pCurr) && (0 != strcmp(pCurr->word, current)) )
    {
        pPre = pCurr;
        pCurr = pCurr->pNext;
    }
 
    if(NULL == pCurr) 
    {
        pCurr = new wordNode;
        strcpy(pCurr->word, current);
        pCurr->iWordCount = 0;
        pCurr->pNext = NULL;
        pPre->pNext = pCurr;
    }
    return pCurr;
}
 
void PrintResult()
{
    FILE* fp1=fopen("out_all_word_frequency.txt", "w");
    FILE* fp2=fopen("out_high_frequency.txt","w");
    if(NULL == pHeader)
    {
        printf("No Word!!\n");
    }
    else
    {
        wordNode *pCurr = pHeader;
 
        while(NULL != pCurr)
        {    
            fprintf(fp1,"%s\t%d\n", pCurr->word, pCurr->iWordCount);
            printf("%-20s%-20d\n", pCurr->word, pCurr->iWordCount);
 
 
            if(pCurr->iWordCount >= FREQUENCY_COUNT)
                fprintf(fp2,"%s\t%d\n", pCurr->word, pCurr->iWordCount);
 
            pCurr = pCurr->pNext;
        }
         fclose(fp1);
         fclose(fp2);
    }
}
 
void Release()
{
    if(NULL == pHeader)
    {
        return;
    }
    wordNode *pCurr = pHeader;
    while(NULL != pCurr)
    {
        pHeader = pCurr->pNext;
        delete pCurr;
        pCurr = pHeader;
    }
}

 

截图如下:

PSP:

  预计花费时间 实际花费时间

     时间差     

功能1 30min 74min 44min
功能2 90min 177min 87min
功能3
功能4

原因分析:拿到作业题目之后,原本看上去不难,只是统计单词出现的频率而已,但是真正动手做了才发现有很多需要注意的点,所以实际花的时间远远高于预计的时间,但是最后的结果还是不尽人意,让我明白了要学的还有很多,要更加努力才可以。

 

标签:pHeader,current,iWordCount,wordNode,雪冬,20190912,pCurr,词频,NULL
来源: https://www.cnblogs.com/nenu-dxd/p/11537761.html