• 统计单词出现的频率


    统计单词出现的频率

    参考 《C程序设计语言》第6章 结构

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAXWORD 100
    #define BUFSIZE 100
    char buf[BUFSIZE];
    int bufp;
    struct tnode{ //树的节点
        char *word; //指向单词的指针
        int count; //单词出现的次数
        struct tnode *left; //左子节点
        struct tnode *right; //右子节点
    };
    int getch(void);//取一个字符,可能是压回的字符
    void ungetch(int);//把字符压回到输入栈中
    int getword(char *word,int lim);//从输入中读取下一个单词或字符
    struct tnode *talloc(void);
    //addtree函数: 在p的位置或p的下方增加一个w节点
    struct tnode *addtree(struct tnode *,char *);
    //treeprint函数: 按序打印树p
    void treeprint(struct tnode *);
    
    //统计关键字出现的次数; 采用指针方式的版本
    int main(){
        struct tnode *root;
        char word[MAXWORD];
    
        root=NULL;
        while(getword(word,MAXWORD)!=EOF)
            if(isalpha(word[0]))
            {
                root=addtree(root,word);
                printf("%s
    ",root->word);
            }
                
        treeprint(root);
        return 0;
    
    }
    //addtree函数: 在p的位置或p的下方增加一个w节点
    struct tnode *addtree(struct tnode *p,char *w){
        int cond;
        if(p==NULL){//该单词是一个新单词
            p=talloc();//创建一个新节点
            p->word=strdup(w);
            p->count=1;
            p->left=p->right=NULL;
            //printf("%s
    ",p->word);
            //printf("%d
    ",p->count);
        }
        else if((cond=strcmp(w,p->word))==0){
            p->count++;//新单词与节点中的单词匹配
            //printf("%s
    ",p->word);
            //printf("%d
    ",p->count);
        }
        else if(cond<0){//如果小于该节点的单词,则进入左子树
            p->left=addtree(p->left,w);
            //printf("%s
    ",p->word);
            //printf("%d
    ",p->count);
        }
        else{
            p->right=addtree(p->right,w);//如果大于该节点的单词,则进入右子树
            //printf("%s
    ",p->word);
            //printf("%d
    ",p->count);
        }
        return p;
    }
    //talloc函数: 创建一个tnode
    struct tnode *talloc(void){
        return (struct tnode *) malloc(sizeof(struct tnode));
    }
    char *strdup(char *s){
        char *p;
        p=(char *)malloc(strlen(s)+1);//执行加1操作是为了在结尾加上字符''
        if(p!=NULL)
            strcpy(p,s);
        return p;
    }
    int getword(char *word,int lim)//从输入中读取下一个单词或字符
    {
        int c;
        char *w=word;
        while(isspace(c=getch()))
            ;
        if(c!=EOF)
            *w++=c;
        if(!isalpha(c)){
            *w++='';
            return c;
        }
        for(;--lim>0;w++)
            if(!isalpha(*w=getch())){
                ungetch(*w);
                break;
            }
            *w='';
            return word[0];
    }
    
    //treeprint函数: 按序打印树p
    void treeprint(struct tnode *p){
        if(p!=NULL){
            treeprint(p->left);
            printf("%4d %s
    ",p->count,p->word);
            treeprint(p->right);
        }
    }
    
    int getch(void){//取一个字符,可能是压回的字符
        return bufp>0?buf[--bufp]:getchar();
    }
    void ungetch(int c){//把字符压回到输入栈中
        if(bufp>=BUFSIZE)
            printf("栈已满
    ");
        else
            buf[bufp++]=c;
    }

    实验结果

  • 相关阅读:
    Oracle之数据库的增删改查和格式的修改
    Oracle之表的相关操作
    Oracle之现有表上建新表、操作符、字符函数
    Oracle之用户和表空间
    相关Linux命令
    克隆环境
    机器学习笔记(四)神经网络的基本概念
    机器学习作业(二)逻辑回归——Python(numpy)实现
    机器学习作业(二)逻辑回归——Matlab实现
    机器学习笔记(三)逻辑回归
  • 原文地址:https://www.cnblogs.com/learning-c/p/5220703.html
Copyright © 2020-2023  润新知