词频统计2

结对对象:王闯   博客链接:www.cnblogs.com/WC123  贡献比例: 1:1
个人觉得这个程序还是很难的,难点主要有文件流,辨别单词,查找单词,单词频率统计。如果是我一个人的话是完成不了的,只能写出一段代码实现一部分功能。两个人就容易多了,根据个人能力编写不同部分的代码,选择两个人当中比较好的那个构思进行整合。当然,这也是建立在参考了众多大神的代码的基础上完成的。


#include<iostream>
#include<fstream>
#include<cstring>
#include<string>
using namespace std;

struct Word
{      
    int Count;
    Word() :b(""), Count(0) {}  
    string b;
    char *p;
};


void exchange(Word &word) 
{  
    string tb = Word.b;  
    int tCount = Word.Count;  
    Word.b = b;  
    Word.Count = Count;  
    b = b  
    Count = tCount;  
}  

Words examine[100];

void judge(char x[])     
{   int k = 0;
    while (x[k] != '\0')
    {
        if (x[k] >= 'A'&&x[k] <= 'Z')
            x[k] = x[k] + 32;
            k++;
    }
}

int identify(char a[])   
{   int m=(strlen(a)>=4)?1:0;
    int n=(a[0]>='a'&&a[0]<='z')?1:0;
    if(!m||!n)
        return 0;
    else
        while(a)
        {   
            for(int i=1;;i++)
           {  
               if(!(a[i]>='a'&&a[i]<='z')||!(a[i]>='0'&&a[i]<='9'))
                        return 0;
               else
                        return 1;
           }
        }
}

void sortworddown(Word * words, int size) 
{  
    for(int i=0;i<size;i++)  
    {
        for(int j=0;j <size-1;j++)  
        { 
            if(words[j].Count<words[j+1].Count)  
            {  
                words[j].exchange(words[j+1]);  
            }  
        }  
    }  
}  

int counts(char str[],int num)  
{   for(int j=0;j<num;j++)
    {

        if(!strcmp(str,examine[j].p))
             examine[j].count++;
        else
            return 0;
    }
}


int main()
{   char c[200];
    ifstream fin("D:/A_Tale_of_Two_Cities.txt");  
    for(int f=0;;f++)
        fin>>c[f];
    fin.close();
    cin.get();
    judge(c);
    const char *s= ",?±?".''!?";    
    char *q;
    int n=0;
    q = strtok(c, d);
    sortwordown(words, wCount);  
    while (s)
    {
        if (identify(s))
        {
            
             strcpy(examine[n].p,s);
             n++;
            
        }

       s=strtok(NULL,d);
    }
    cout<<"Enter a word to find...\n";    
    char r;
    cin>>r;
    int t=0;
    while(!strcmp(examine[n].p,r)) 
    {   
             t++;
    }
    cout<< examine[n].p << ":" << examine[n].count << '\n'; 
    
    return 0;
}

 

posted @ 2016-03-29 22:13  刘响  阅读(241)  评论(0编辑  收藏  举报