词频统计

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
#define Name_Max 180   //文件名最大长度
#define Word_Max 60    //每个单词最大长度


typedef struct word        //连表 单词结构
{
    char w[Word_Max];    //单词
    int count;                //个数
    struct word *next;
}link;


link *head=NULL;    //连表头
FILE *fp;            //文件指针
int count=0;


int isnotWord(char a)        //判断是否为字母
{
    if(a <= 'z' && a >= 'a')
    {
        return 0;
    }
    else if(a <= 'Z' && a >= 'A')
    {
        return 0;
    }
    else
        return 1;
}


void addWord(char *w1)        //添加单词
{
    link *p1,*p2;
    if(w1[0] <= 'Z' && w1[0] >= 'A')    //转换成小写字母
    {
        w1[0]+=32;
    }
    for(p1=head;p1!=NULL;p1=p1->next)    //判断单词在连表中是否存在
    {
        if(!strcmp(p1->w,w1))
        {
            p1->count++;            //存在就个数加1
            return;
        }
    }
                                    
    p1=(struct word *)malloc(sizeof(word));//不存在添加新单词
    strcpy(p1->w,w1);
    p1->count=1;
    p1->next=NULL;
    count++;


    if(head==NULL)
    {
        head=p1;
    }
    else
    {
        for(p2=head;p2->next!=NULL;p2=p2->next);
        p2->next=p1;
    }
}


void wordCount()    //统计单词
{
    int i=0,j=0;
    char word[Word_Max],c;
    while(!feof(fp))
    {
        fscanf(fp,"%c",&c);
        if(isnotWord(c))
        {
            word[j]='\0';
            if(j>0)
            {
                addWord(word);
            }
            j=0;
        }
        else
        {
            word[j]=c;
            j++;
        }
        i++;
    }
}


void readWord()        //读取文件中的单词
{
    char name[Name_Max];
    printf("请输入要读取的单词文件名[如:test.cpp]:");
    scanf("%s",name);
    getchar();
    fp=fopen(name,"r");
    wordCount();
    fclose(fp);
}


void showWord(int a,int b)        //显示单词统计情况
{
    link *p;
    printf("个数\t单词\n");
    for(p=head;p!=NULL;p=p->next)
    {
        if((p->count > a)&&(p->count < b))
            printf("%d\t%s\n",p->count,p->w);
    }
}


int main()
{
    readWord();
    int a,b;
    printf("请输入a,b:");
    scanf("%d%d",&a,&b);
    getchar();
    showWord(a,b);
    getchar();
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值