阅读以下说明和C语言函数,将应填入(n)处的语句写在对应栏内。【说明】本程序从正文文件text.in中读入一篇英文短文,统计该短文中不同单词及出现次数,并按词典编辑顺序将单词及出现次数输出到正文文件word.out中。程序用一棵有序二叉树存储这些单词及其出现的次数,边读入边建立,然后中序遍历该二叉树,将遍历经过的二叉树上的结点内容输出。【函数】include <stdio.h>include <malloc.h>include <ctype.h>include <string.h>define INF "text.in"define OUTF "word.our'typedef struct treenode {char *word;int count;struct treenode *left, *right;} BNODE;int getword(FILE *fpt, char *word){ char c;c=fgetc(tpt);if (c==EOF)return 0;while(!(tolower(c)>= 'a' tolower(c)<= 'z')){ c=fgetc(fpt);if (c==EOF)return 0;} /* 跳过单词间的所有非字母字符 */while(tolower(c)>= 'a' tolower(c)<= 'z'){ *word++=c;c=fgetc(fpt);}*word='\0';return 1;}void binary_tree(BNODE **t, char *word){ BNODE *ptr, *p; int compres;p=NULL;(1);while (ptr) /* 寻找插入位置 */{ compres=strcmp(word, ptr->word);/* 保存当前比较结果 */if (!compres){ (2); return;}else{ p=ptr;ptr=compres>0 ? ptr->right: ptr->left;}}ptr=(BNODE *)malloc(sizeof(BNODE));ptr->left=ptr->right=NULL;ptr->word=(char *)malloc(strlen(word)+1);strcpy(ptr->word, word);(3);if (p==NULL)*t=ptr;else if (compres>0)p->right=ptr;elsep->left=ptr;}void midorder(FILE *fpt, BNODE *t){ if (t==NULL)return;midorder(fpt,(4));fprintf(fpt, "%s %d\n", t->word, t->count);midorder(fpt, t->right);}void main(){ FILE *fpt; char word[40];BNODE *root=NULL;if ((fpt=fopen(INF, "r"))==NULL){ printf("Can't open file %s\n", INF);return;}while(getword(fpt, word)==1)binary_tree((5));fclose(fpt);fpt=fopen(OUTF, "w");if (fpt==NULL){ printf("Can't open fife %s\n", OUTF);return;}midorder(fpt, root);fclose(fpt);}

阅读以下说明和C语言函数,将应填入(n)处的语句写在对应栏内。

【说明】

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

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

【函数】

include <stdio.h>

include <malloc.h>

include <ctype.h>

include <string.h>

define INF "text.in"

define OUTF "word.our'

typedef struct treenode {

char *word;

int count;

struct treenode *left, *right;

} BNODE;

int getword(FILE *fpt, char *word)

{ char c;

c=fgetc(tpt);

if (c==EOF)

return 0;

while(!(tolower(c)>= 'a' && tolower(c)<= 'z'))

{ c=fgetc(fpt);

if (c==EOF)

return 0;

} /* 跳过单词间的所有非字母字符 */

while(tolower(c)>= 'a' && tolower(c)<= 'z')

{ *word++=c;

c=fgetc(fpt);

}

*word='\0';

return 1;

}

void binary_tree(BNODE **t, char *word)

{ BNODE *ptr, *p; int compres;

p=NULL;

(1);

while (ptr) /* 寻找插入位置 */

{ compres=strcmp(word, ptr->word);/* 保存当前比较结果 */

if (!compres)

{ (2); return;}

else

{ p=ptr;

ptr=compres>0 ? ptr->right: ptr->left;

}

}

ptr=(BNODE *)malloc(sizeof(BNODE));

ptr->left=ptr->right=NULL;

ptr->word=(char *)malloc(strlen(word)+1);

strcpy(ptr->word, word);

(3);

if (p==NULL)

*t=ptr;

else if (compres>0)

p->right=ptr;

else

p->left=ptr;

}

void midorder(FILE *fpt, BNODE *t)

{ if (t==NULL)

return;

midorder(fpt,(4));

fprintf(fpt, "%s %d\n", t->word, t->count);

midorder(fpt, t->right);

}

void main()

{ FILE *fpt; char word[40];

BNODE *root=NULL;

if ((fpt=fopen(INF, "r"))==NULL)

{ printf("Can't open file %s\n", INF);

return;

}

while(getword(fpt, word)==1)

binary_tree((5));

fclose(fpt);

fpt=fopen(OUTF, "w");

if (fpt==NULL)

{ printf("Can't open fife %s\n", OUTF);

return;

}

midorder(fpt, root);

fclose(fpt);

}


相关考题:

阅读以下说明和流程图,将应填入(n)处的字句写在对应栏内。【说明】已知头指针分别为La和lb的有序单链表,其数据元素都是按值非递减排列。现要归并La和Lb得到单链表Lc,使得Lc中的元素按值非递减排列。程序流程图如下所示:

阅读以下说明和C语言函数,将应填入(n)处的字句写在对应栏内。[说明]完成以下中序线索化二叉树的算法。[函数]Typedef int datatype;Typedef struct node {Int ltag, rtag;Datatype data;*lchild,* rchild;}bithptr;bithptr pre;void inthread ( p );{if{inthread ( p->lchild );if ( p->lchild==unll ) (1);if ( P->RCHILD=NULL) p->rtag=1;if (2){if (3) pre->rchild=p;if ( p->1tag==1 )(4);}INTHREAD ( P->RCHILD );(5);}}

阅读以下说明和流程图,将应填入(n)处的字句写在对应栏内。[说明]设学生某次考试的成绩按学号顺序逐行存放于某文件中,文件以单行句点“.”为结束符。下面的流程图读取该文件,统计出全部成绩中的最高分max和最低分min。

阅读以下说明和c++码,将应填入(n)处的字名写在的对应栏内。[说明] 以下函数完成求表达式的值,请填空使之完成此功能。float sum ( float x ){ float s=0.0;int sign = 1;(1);for(inti=1;(2); i+ +){t=t*x;s=s+(3);sign = - sign;(4);}

阅读下列程序说明和C++程序,把应填入其中(n)处的字句,写在对应栏内。【说明】阅读下面几段C++程序回答相应问题。比较下面两段程序的优缺点。①for (i=0; i<N; i++ ){if (condition)//DoSomething…else//DoOtherthing…}②if (condition) {for (i =0; i<N; i++ )//DoSomething}else {for (i=0; i <N; i++ )//DoOtherthing…}

阅读下列说明和流程图,将应填入(n)处的语句写在对应栏内。【说明】设学生(学生数少于50人)某次考试的成绩按学号顺序逐行存放于某文件中,文件以单行句点“.”为结束符。下面的流程图用于读取该文件,并把全部成绩从高到低排序到数组B[50]中。【流程图】

试题三(共 15 分)阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。

()阅读下列说明和C语言程序,将应填入 (n)处的语句写在答题纸的对应栏内。[说明]有一个一维数组cj,内放20个学生成绩,求平均成绩。函数ave用来求20个学生的平均成绩。[C语言函数]float ave(float a[20]){ int i;float aver,sum= (1) ;for(i=1;i20;i++) sum= (2) ;aver= (3) ;return( (4) );}main(){ float cj[20],aver;int i;printf(“input 20 cj:\n”);for(i=0;i20;i++) scanf(“%f”,cj[i]);printf(“\n”);aver= (5) ;printf(“average cj is %6.2f”,aver);}

()阅读下列说明和C语言程序,将应填入 (n)处的语句写在答题纸的对应栏内。[说明]下面程序是一个带参数的主函数,其功能是显示在命令行中输入的文本文件内容。[C语言函数]#include"stdio.h"main(argc,argv) int argc; char *argv[]; { (1) ; if((fp=fopen(argv[1],”r’’))== (2) ) { printf(”file not open!\n”);exit(0);} while( (3) ) putchar( (4) ); (5); }

阅读下列说明和?C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】阅读下列说明和?Java代码,将应填入?(n)?处的字句写在答题纸的对应栏内。【说明】某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类可能不同,但其制作过程相同。前台服务员?(Waiter)?调度厨师制作套餐。现采用生成器?(Builder)?模式实现制作过程,得到如图?6-1?所示的类图。