问答题

【程序5说明】
设M叉树采用列表法表示,即每棵子树对应一个列表,列表的结构为:子树根结点的值部分(设为一个字符)和用“()”括起来的各子树的列表(如有子树的话),各子列表间用“,”分隔。例如下面的三叉树可用列表a(b(c,d),e,f(g,h,i))表示。
本程序输入列表,生成一棵M叉树,并由M叉树输出列表。假定输入无错误。
【程序5】
#include<Stdio.h>
#include<Stdlib.h>
#define M 3
typedef struct nodechar val;
struct node,subTree[M];
NODE;
char buf[255],*Str=buf;
NODE * d=NULL
NODE*makeTree()/*由列表生成M叉树*/
int k;NODE*s;
s= (1)
s->val= *Str++;
for(k=0;k<M;k++)s->subTree[k]=NULL;
if(* str=’(’)
k=0;
dostr++;
s->sub Tree[k]= (2)
if(*Str==’)’)Str++;break;
k=k+1;
while( (3) );

return s;

void walkTree(NODE*t)/*由M又树输出列表*/
int i;
if(t!=NULL)
(4)
if(t->subTree[0]==NULL)return;
putchar(’(’);
for(i=0;i<M;i++)
(5)
if(i!=M-1&&t->subTree[i+1]!=NULL)
putchar(’,’);

putchar(’)’);

void main()
printf("Enter exp:");
scanf("%s",str);
d=makeTree();
walkTree(d);putchar(’\n");

【参考答案】

(1)(NODE *)malloc(sizeof(NODE)) (2)makeTree()
(3)*str==’......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
【说明】 源程序中定义了Circle类与Money类,Circle类可对半径为r的圆进行周长与面积的计算,而Money类用于计算一圆形游泳池的造价。游泳池四周有原形过道,过道外围上栅栏,过道宽度为3米,根据键入的游泳池半径,每米栅栏价格及每平方米过道价格,即可计算出游泳池的造价。请按要求完成下列操作,将程序补充完整。 ①定义符号常量PI(值为3.14159f)与WIDTH(值为3.00f),分别用于表示圆周率与过道的固定宽度。 ②定义Circle类默认构造函数,把私有成员radius初始化为参数r的值。 ③完成Money类默认构造函数的定义,把私有成员FencePrice(每米栅栏的价格)、ConcretePrice(每平方米过道的价格)初始化为参数f,c的值。 ④完成Money类成员函数float Money::TotalMoney(float fencelen,float conarea)的定义,根据参数fencelen(栅栏的长度)和conarea(过道的面积),返回栅栏与过道的总造价。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件test4.cpp清单如下: #include<iostream.h> (1) class Circle private: float radius; public: (2) float Circumference()return 2 * P| * radius; float Area()return P|*radius * radius; ; class Money private: float FencePrice; float ConcretePrice; public: Money(float f,float c); float Tota|Money(float fencelen, float conarea); ; Money::Money(float f,float c) (3) float Money::Tota|Money(float fencelen, float conarea) (4) void main( ) float radius, fence, concrete; cout. setf(ios:: fixed); cout. setf(ios:: showpoint); cout. precision (2); cout << Enter the radius of the pool: ; cin > > radius; cout << Enter the FencePrice: ; cin > > fence; cout << Enter the ConcretePrice: ; cin > > concrete; Circle Pool(radius); Circle PoolRim(radius + WIDTH); Money mon( fence, concrete); float totalmoney = mon. TotalMoney( PoolRim. Circumference( ), ( PoolRim. Area ( ) - Pool. Area ( ) ) ); cout << The total money is RMB << totalmoney << end|;
问答题
下列给定程序中,函数fun()的功能是:对N名学生的学习成绩,按从高到低的顺序找出前m(m≤10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。 试题程序: #include<stdio.h> #include<malloc.h> #include<string.h> #include<conio.h> #define N 10 typedef struct ss char num[10]; int order; STU; STU*fun(STU a[],int m) STU b[N],*tt; int i,j,k; (1) ; for(i=0;i<N;i++) b[i]=a[i]; for(k=0;k<m;k++) for(i=j=0;i<N;i++) if( (2) ) j=i; tt[k]=b[j]; b[j].order=0; return (3) ; outresult(STU a[],FILE*pf) int i; for(i=0;i<N;i++) fprintf(pf, No=%s Mark=%d n ,a[i].num,a[i].order); fprintf(pf, n n ); main() STU[N]= A01 ,80, A02 ,79, A03 ,66, A04 ,82, A05 ,87, A06 ,93, A07 ,78, A08 ,60, A09 ,85, A10 ,73; STU * p_order; int i,m; clrscr(); printf( * * * The Origial data * * * n ); outresult(a,stdout); printf( nGive the numeber of the students who have better score: ); scanf( %d ,&m); while(m>10) printf( nGive the number of the studets who have better score: ); scanf( %d ,&m); p_order=fun(a,m); printf( * * *THE RESULT* * * n ); printf( * * * The top students * * * n ); for(i=0;i<m;i++) printf( %s %d n ,p_order[i].num,p_order[i].order); free(p_order);