学习啦>创业指南>职场>面试题>

腾讯技术类校园招聘笔试试题及答案(4)

敏敏分享

  二. 填空题(共40分)

  本程序从正文文件text.in读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典编辑顺序将单词及它的出现次数输出到正文文件word.out中.

  程序用一棵有序二叉树存储这些单词及其出现的次数,一边读入一边建立.然后中序遍历该二叉树,将遍历经过的二叉树上的节点的内容输出.

  程序中的外部函数

  int getword(FILE* pFile,char* pszWordBuffer,int nBufferLen);

  从与pFile所对应的文件中读取单词置入pszWordBuffer,并返回1;若单词遇文件尾,已无单词可读时,则返回0.

  #include

  #include

  #include

  #include

  #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);

  void binary_tree(BNODE** ppNode,char* pszWord)

  {

  if(ppNode != NULL && pszWord != NULL)

  {

  BNODE* pCurrentNode = NULL;

  BNODE* pMemoNode = NULL;

  int nStrCmpRes=0;

  ____(1)_____;pCurrentNode=*ppNode

  while(pCurrentNode)

  {

  /*寻找插入位置*/

  nStrCmpRes = strcmp(pszWord, ___(2)___ );pCurrentNode->nCount

  if(!nStrCmpRes)

  {

  ___(3)___; pCurrentNode->nCount++

  return;

  }

  else

  {

  ___(4)___; pMemoNode=pCurrentNode

  pCurrentNode = nStrCmpRes>0? pCurrentNode->pRight : pCurrentNode->pLeft;

  }

  }

  }

  pCurrent=new BNODE;

  if(pCurrentNode != NULL)

  {

  memset(pCurrentNode,0,sizeof(BNODE));

  strncpy(pCurrentNode->szWord,pszWord,MAX_WORD_LEN-1);

  pCurrentNode->nCount=1;

  }

  if(pMemoNode==NULL)

  {

  ___(5)___; *ppNode= pCurrentNode

  }

  else if(nStrCmpRes>0)

  {

  pMemoNode->pRight=pCurrentNode;

  }

  else

  {

  pMemoNode->pLeft=pCurrentNode;

  }

  }

  void midorder(FILE* pFile,BNODE* pNode)

  {

  if(___(6)___) return;!pNode||!pFile

  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(___(7)___);// pRootNode,szWord

  }

  fclose(pFile);

  pFile=fopen(OUTPUT_FILE,"w");

  midorder(pFile,pRootNode);

  fclose(pFile);

  }

228953