题目
阅读以下说明和C函数,将应填入(n)处的字句写在对应栏内。
【说明】
已知某二叉树的非叶子结点都有两个孩子结点,现将该二叉树存储在结构数组Ht中。结点结构及数组Ht的定义如下:
define MAXLEAFNUM 30
struct node{
char ch; /*当前结点表示的字符,对于非叶子结点,此域不用*/
char *pstr; /*当前结点的编码指针,非叶子结点不用*/
int parent; /*当前结点的父结点,为0时表示无父结点*/
int lchild,rchild;
/*当前结点的左、右孩子结点,为0时表示无对应的孩子结点*/
};
struct node Ht[2*MAXLEAFNUM]; /*数组元素Ht[0]不用*/
该二叉树的n个叶子结点存储在下标为1~n的Ht数组元素中。例如,某二叉树如果其存储结构如下图所示,其中,与叶子结点a对应的数组元素下标为1,a的父结点存储在Ht[5],表示为Ht[1].parent=5。Ht[7].parent=0表示7号结点是树根,Ht[7].child=3、Ht[7].rchild=6分别表示7号结点的左孩子是3号结点、右孩子是6号结点。
如果用0或1分别标识二叉树的左分支和右分支(如上图所示),从根结点开始到叶子结点为止,按所经过分支的次序将相应标识依次排列,可得到一个0、1序列,称之为对应叶子结点的编码。例如,上图中a,b,c,d的编码分别是100,101,0,11。
函数LeafCode(Ht[],n)的功能是:求解存储在Ht中的二叉树中所有叶子结点(n个)的编码,叶子结点存储在Ht[1]~Ht[n]中,求出的编码存储区由对应的数组元素pstr域指示。
函数LeafCode从叶子到根逆向求叶子结点的编码。例如,对上图中叶子结点a求编码的过程如下图所示。
typedef enum Status {ERROR,OK} Status;
【C函数】
Status LeafCode(struct node Ht[], int n)
{
int pc, pf; /*pc用于指出树中的结点,pf则指出pc所对应结点的父结点*/
int i,start;
char tstr[31] = {'\0'}; /*临时存储给定叶子结点的编码,从高下标开始存入*/
for(i = 1;(1); i++){ /*对所有叶子结点求编码,i表示叶结点在HT数组中的下标*/
start = 29;
pc = i; pf = Ht[i].parent;
while (pf !=(2)) { /*没有到达树根时,继续求编码*/
if ((3).lchild == pc ) /*pc所表示的结点是其父结点的左孩子*/
tstr[--start] = '0';
else
tstr[--start] = '1';
pc =(4); pf = Ht[pf].parent; /*pc和pf分别向根方向回退一层*/
}/* end of while */
Ht[i].pstr = (char *) malloc(31-start);
if (!Ht[i].pstr) return ERROR;
strcpy(Ht[i].pstr,(5));
}/* end of for */
return OK;
}/* and of LeafCode */
第1题
阅读以下说明和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);
}
第2题
阅读以下说明和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);
}
}
第3题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【说明】
给定函数fun的功能是:将从键盘上输入的每个单词的第一个字母转换为大写字母,输入时各单词必须用空格隔开,用“.”结束输入。
【函数】
int fun(char *c,int status)
{
if((1)=='')
return 1;
else
{
if((2)&&(3)&&(4))
(5)='A'-'a';
return 0;
}
}
main()
{
int flag=1;
char ch;
printf("请输入一字符串,用点号结束输入!\n");
do {
ch=getchar();
flag=fun(&ch,flag);
putchar(ch);
}while(ch!='.');
printf("\n");
}
第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题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【说明】
有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。
【函数】
main()
{
int number[20],n,m,i;
printf("the total numbers is:");
scanf("%d",&n);
printf("back m:");
scanf("%d",&m);
for(i=0;i<=n-1;i)
scanf("%d,",&number[i]);
(1);
for(i=0;i<=n-1;i)
printf("%d,",number[i]);
}
move(array,n,m)
int n,m,array[20];
{
int *p,array_end;
array_end=(2);
for(p=array[n-1];(3);p--)
*p=(4);
*array=array_end;
m--;
if(m>0) (5);
}
第6题
阅读以下说明,以及用C++在开发过程中所编写的程序代码,将应填入(n)处的字句写在对应栏内。
【说明】
在下面函数横线处填上适当的字句,使其输出结果为:
构造函数.
构造函数.
1,2
5,6
析构函数
析构函数.
【C++代码】
include "iostream.h"
class AA
{ public;
AA(int i,int j)
{A=i; B=j;
cout<<"构造函数.\n";
}
~AA(){(1);}
void print();
private:
int A, B;
};
void AA∷print()
{cout<<A<<","<<B<<endl;}
void main()
{
AA *a1, *a2;
(2)=new AA(1, 2);
a2=new AA(5, 6);
(3);
a2->print();
(4) a1;
(5) a2;
}
第7题
阅读以下说明和C语言函数,将应填入(n)处的语句写在对应栏内。
【说明】
设串s和串t采用顺序存储结构,编写函数实现串s和串t的比较操作,要求比较结果包括大于、小于和等于3种情况。
【函数】
int StrCompare(SStrType s, SStrType t)
{
int n=s.length, m=(1), i,j,tag;
i=0; j=0;
while((2))
{
if((3))
{
i++;
j++;
}
else if(s.str[i]>t.str[j])
{
tag=1;
return tag;
}
else
{
tag=-1;
return tag;
}
}
if(n==m)
tag=0;
else if((4))
tag=1;
else if(n<m)
tag=-1;
(5);
}
第8题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【程序2.1说明】
求所有满足如下条件的三位数:它除以11得的商等于它各位数字的平方和。例如 550,除以11商为50,50=52+52+02。
【程序2.1】
void main()
{
int i, j,n,s;
for(i=100;i<=999;i++)
{
n=i;
j=n/11;
s=0;
while((1))
{
(2)
n/=10;
}
if((3))
printf("%d\t",i);
}
}
【程序2.2说明】
本程序输入一字符串,将其中的大写字母改变成小写字母。
【程序2.2】
void main()
{
int i=0;
char s[120];
scanf("%s",s);
while((4))
{
if((5))
s[i]=s[i]- 'A'+'a';
i++;
}
printf("%s\n",s);
}
第9题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明1]
函数int function(int a)的功能是判断指定的正整数是否为素数,若是,返回1,否则返回0。
[C函数1]
int function(int a)
{ int yes,i;
i=2;yes=1;
while(i<=a/2 && (1) ){
if((2) ) yes=0;
i++;
}
return yes;
}
[说明2]
函数int deleteARR(int*arr,intn)的功能是指定的有序数组压缩成各元素互不相同的有序数组,即相同数只保留一个,多余的被删除。函数返回值是互不相同的元素个数。
[C函数2]
int deleteARR(int*arr,int n)
{ int k,j;
k=0;j=1;
while(j<n){
if((3) )
(4)=arr[j];
j++;
}
return (5);
}
第10题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明1]
本程序输入一字符串,并将其中的大写字母变成小写字母。
[C函数1]
include<stdio.h>
void main()
{ int i=0;
char s[120];
printf("Enter a string.\n");
scanf("%s",s);
while((1) ){
if((2) )
s[i]=s[i]-'A'+'a';
i++;
}
printf("%s\n",S);
}
[说明2]
本程序用二分法,在已按字母次序从小到大排序的字符数组list[len]中,查找字符c,若c在数组中,函数返回字符c在数组中的下标,否则返回-1。
[C函数2]
int search(char list[],char c,int len)
(intlow=0,high=len-1,k;
while((3) );
k=(10w+high)/2;
if((4) ) return k;
else if((5) )high=k-1;
else low=k+1;
return -1;
}
第11题
阅读以下说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明]
编写一个函数,输入为偶数时,调用函数求1/2+?/+…+1/n,当输入n为奇数时,调用函数1/1+1/3+…+1/n (利用指针函数)。
[函数]
include "stdio. h",
main()
{
float peven (),podd (),dcall ();
float sum;
int n;
while (1)
{
scanf("%d",&n);
if (n>1)
break;
}
if(n%2==0)
{
printf("Even="):
(1);
}
else
{
pfinff("Odd=");
(2);
}
printf("%f",sum);
}
float peven (int n)
{
float s;
int i
s=1;
for(i=2;i<=n;i+=2)
(3);
return (s);
}
float podd (n)
int n;
{
float s;
int i;
s=0;
for(i=1 i<=n;i+=2)
(4);
return (s);
}
float dcall(fp,n)
float (*fp) ();
int n;
{
float s;
(5);
returu (s);
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!