阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明]
本程序实现对指定文件内的单词进行计数。其中使用二叉树结构来保存已经读入的不同单词,并对相同单词出现的次数进行计数。此二叉树的左孩子结点的字符串值小于父结点的字符串值,右孩子结点的字符串值大于父结点的字符串值。函数getword(char*filename,char*word)是从指定的文件中得到单词。char*strdup(char*S)是复制S所指向的字符串,并返回复制字符串的地址。
[C程序]
include <stdio.h>
include <ctype.h>
include <string.h>
define MAXWORD 100
struct node {
char*word;
int count;
struct node*left;
struct node*right;
}
struct node*addtree(struct node*P,char*w)
{ int cond;
if(p==NULL){ /*向树中插入结点*/
P=(struct node*)malloc(sizeof(struct node));
P->word=strdup(w);
P->count=1;
(1) ;
}
elseif((oond=strcmp(w,p->word))==0) (2) ;
else if(cond<0)p->left=(3);
else p->right=(4);
return p;
}
main()
{ Struct node*root;
char word[MAXWORD];
root=NULL;
filename="example.dat";
while(getword(filename,word)!=EOF))
root=(5);
}
第1题:
阅读下列程序说明和C程序,将应填入(n)处的字句写在对应栏内。
[函数2.1说明]
下面程序的功能是计算x和y的最小公倍数。
[函数2.1]
main()
{ int m,n,d,r;
seanf("%d %d",&m,&n);
if(m<n) {r=m;m=n;n=r;}
(1);
while (d%n! =0) (2);
printf("%d\n",d);
}
[函数2.2说明]
下述程序接收键盘输入,直到句点“.”时结束。输入的字符被原样输出,但连续的空格输入将转换成一个空格。
[函数2.2]
include <stdio.h>
main()
{ char c,preChar='\0';
c = getchar();
while(c! = '.'){
if((3)) putchar(c);
else if(preChar! =' ') putchar(c);
(4);
c=(5);
}
}
第2题:
●试题二
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
该程序运行后,输出下面的数字金字塔
【程序】
include<stdio.h>
main ()
{char max,next;
int i;
for(max=′1′;max<=′9′;max++)
{for(i=1;i<=20- (1) ;++i)
printf(" ");
for(next= (2) ;next<= (3) ;next++)
printf("%c",next);
for(next= (4) ;next>= (5) ;next--)
printf("%c",next);
printf("\n");
}
}
第3题:
()阅读下列说明和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); }
第4题:
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明]
已知r[1...n]是n个记录的递增有序表,用折半查找法查找关键字为k的记录。若查找失败,则输出“failure",函数返回值为0;否则输出“success”,函数返回值为该记录的序号值。
[C函数]
int binary search(struct recordtype r[],int n,keytype k)
{ intmid,low=1,hig=n;
while(low<=hig){
mid=(1);
if(k<r[mid].key) (2);
else if(k==r[mid].key){
printf("succesS\n");
(3);
}
else (4);
}
printf("failure\n");
(5);
}
第5题:
试题三(共 15 分)
阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。
第6题: