统计单词数目的C++源代码(待修改)

作者在 2010-11-12 01:18:33 发布以下内容
#include "iostream"
#include "cstring"
using namespace std;
typedef struct WordTimes
{
    char *str;
    int times;
    WordTimes *next;
}WordTimes;
char *MakeWord(char *str)
{
    cout<<"MakeWord Done";
    if(*str>'A'&&*str<'Z')
    {
        *str=*str+'a'+'A';
    }
    if(str[strlen(str)-1]<'A'||str[strlen(str)-1]>'z')
        str[strlen(str)-1]='\0';
    return str;
}
void Insert(WordTimes *&head,char *str)
{
    cout<<"Insert Done";
    WordTimes *p;
    p=new WordTimes;
    p=head;;
    if(p->next!=NULL)
        p=p->next;
    p->str=str;
    p->times=1;
    delete p;
}
int Check(WordTimes *&head,char *str)
{
    cout<<"Check Done";
    WordTimes *p;
    p=new WordTimes;
    p=head;
    int flag=1;
    while(p->next!=NULL)
    {
        p=p->next;
        if(!strcmp(p->str,str))
            flag=0;
    }
    delete p;
    return flag;
}
void AddTimes(WordTimes *&head,char *str)
{
    cout<<"Add Done";
    WordTimes *p;
    p=new WordTimes;
    p=head;
    while(p->next!=NULL)
    {
        p=p->next;
        if(!strcmp(p->str,str))
        {
            p->times++;
        }
    }
    delete p;
}
void Print(FILE *fpw,WordTimes *head)
{
    cout<<"Print Done";
    WordTimes *p;
    p=new WordTimes;
    p=head->next;
    while(p)
    {
        fprintf(fpw, "%s:%d\n",p->str, p->times);
        p=p->next;
    }
}
void CountWord()
{
    char *word;
    word=new char;
    WordTimes *head;
    head=new WordTimes;
    //head->next=NULL;

    FILE *fpr=fopen("WutheringHeights1.txt","r");
    FILE *fpw=fopen("WHWords.txt","w");
    while(!feof(fpr));
    {
        cout<<"CountWord Done";
        fscanf(fpr, "%s", word);
        if(Check(head,word))
            Insert(head,word);        
        else
            AddTimes(head,word);
    }
    Print(fpw,head);
    delete word;
    delete head;
    fclose(fpr);
    fclose(fpw);
    
}
我的C++ | 阅读 479 次
文章评论,共0条
游客请输入验证码
浏览479次
文章归档
最新评论