20190912-3 word frequency statistics are Xuedong

This job requires See https://edu.cnblogs.com/campus/nenu/2019fall/homework/6583

Program URL https://github.com/dxd1007/dxd

Function 1 small file input. To demonstrate the program run, and not the result of real persecution Five, ask him personally key
pad to enter commands at the console.

code show as below:

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

Screenshot below:

Several other features to consider for a long time, there will not do to rely on their own understanding to achieve some functions

code show as below:

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

 

Screenshot below:

PSP:

  It expected to spend time The actual time spent

     Time difference     

Function 1 30min 74min 44min
Function 2 90min 177min 87min
Function 3
Function 4

Analysis: After you get the job title, the original looks difficult, but the statistical frequency of occurrences of the word only, but the real hands-only to find there are a lot of points to note, so the actual time spent is much higher than the expected time, However, the final result was unsatisfactory, so I understand a lot, to work harder to learn can.

 

Guess you like

Origin www.cnblogs.com/nenu-dxd/p/11537761.html