统计该短文中不同单词和它的出现次数

本文介绍了一个使用有序二叉树实现的程序,该程序能够读取英文短文,统计其中的不同单词及其出现次数,并按字母顺序输出结果。通过中序遍历二叉树的方式实现了单词的有序输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本程序从正文文件text.in读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典编辑顺序将单词及它的出现次数输出到正文文件word.out中.
程序用一棵有序二叉树存储这些单词及其出现的次数,一边读入一边建立.然后中序遍历该二叉树,将遍历经过的二叉树上的节点的内容输出.
程序中的外部函数
int getword(FILE* pFile,char* pszWordBuffer,int nBufferLen);
从与pFile所对应的文件中读取单词置入pszWordBuffer,并返回1;若单词遇文件尾,已无单词可读时,则返回0.



// testtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//
// #include <iostream>
// #include <istream>
// #include <ostream>
// #include <ios>
//
// using   namespace   std;


#include <stdio.h>
#include <malloc.h>
#include <ctype.h>
#include <string.h>
 
#define SOURCE_FILE "text.in"
#define OUTPUT_FILE "word.out"
#define MAX_WORD_LEN 128
 
typedef struct treenode
{
      char szWord[MAX_WORD_LEN];
      int nCount;
      struct treenode* pLeft;
      struct treenode* pRight;
}BNODE;
 
int getword(FILE* pFile,char* pasWordBuffer,int nBufferLen)
{
    int result = 0;
    result = fscanf(pFile, "%s", pasWordBuffer);
    if(EOF == result || 0 == result)
        result = 0;
    else
        result = 1;

    return result;
}
 
void binary_tree(BNODE** ppNode,char* pszWord)
{
    BNODE* pCurrentNode = NULL; //
    BNODE* pMemoNode = NULL; //
    int nStrCmpRes = 0; //

      if(ppNode != NULL && pszWord != NULL)
      {
//              BNODE* pCurrentNode = NULL;
//              BNODE* pMemoNode = NULL;
//              int nStrCmpRes=0;
 
             pCurrentNode = *ppNode; //____(1)_____
 
             while(pCurrentNode)
             {
                    /*寻找插入位置*/
                    nStrCmpRes = strcmp(pszWord, pCurrentNode->szWord); //___(2)___
 
                    if(!nStrCmpRes)
                    {
                           pCurrentNode->nCount++; //___(3)___
 
                           return;
                    }
                    else
                    {
                           pMemoNode=pCurrentNode;// ___(4)___
                           pCurrentNode = nStrCmpRes>0? pCurrentNode->pRight : pCurrentNode->pLeft;
                    }
             }
      }
 
      pCurrentNode = new BNODE;
 
      if(pCurrentNode != NULL)
      {
             memset(pCurrentNode,0,sizeof(BNODE));
             strncpy(pCurrentNode->szWord,pszWord,MAX_WORD_LEN-1);
             pCurrentNode->nCount=1;
      }
 
      if(pMemoNode==NULL)
      {
             *ppNode= pCurrentNode; // ___(5)___
      }
      else if(nStrCmpRes>0)
      {
             pMemoNode->pRight=pCurrentNode;
      }
      else
      {
             pMemoNode->pLeft=pCurrentNode;
      }
}
 
void midorder(FILE* pFile,BNODE* pNode)
{
      if(!pNode||!pFile) return; //___(6)___
 
      midorder(pFile,pNode->pLeft);
      fprintf(pFile,"%s %d/n",pNode->szWord,pNode->nCount);
      midorder(pFile,pNode->pRight);
}
 
void main()
{
      FILE* pFile=NULL;
      BNODE* pRootNode=NULL;
      char szWord[MAX_WORD_LEN]={0};
 
      pFile=fopen(SOURCE_FILE,"r");
 
      if(pFile==NULL)
      {
             printf("Can't open file %s/n",SOURCE_FILE);
             return;
      }
 
      while(getword(pFile,szWord,MAX_WORD_LEN) == 1)
      {
             binary_tree(&pRootNode,szWord);  // ___(7)___
      }
 
      fclose(pFile);
 
      pFile=fopen(OUTPUT_FILE,"w");
      midorder(pFile,pRootNode);
      fclose(pFile);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值