外语电影
初级程序员下午模拟试题及答案解析(20)
(1~5/共20题)填空题
[说明]
本程序实现对指定文件内的单词进行计数。其中使用二叉树结构来保存已经读入的不同单词,并对相同单词出现的次数进行计数。此二叉树的左孩子结点的字符串值小于父结点的字符串值,右孩子结点的字符串值大于父结点的字符串值。函数getword(char *filename,char*word)是从指定的文件中得到单词。char* strdup(char* s)是复制s所指向的字符串,并返回复制字符串的地址。
[函数]
#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) ;el if((cond=strcmp (w,p->word))==0) (2) ;
el if (cond<0) p->left= (3) ;
el 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题
第2题
第3题
第4题
第5题
下一题
(6~10/共20题)填空题
[说明]
设计一个评选优秀教师和学生的程序,其类结构如下图所示。当输入一系列教师或学生的记录后,将优秀学生及教师的姓名列出来。
图片
[C++程序]
#include<iostream. h>
#include<stdio. h>
enum boolean Fal,True;
class baProtected:
char name [8];
public :
void getname() cout<<"姓名:";cin>>name;
英语专业考研科目void printname() cout<<"姓名:"<<name<<endU
virtual boolean isgood () =0;class student: (1)
int num;
public :
voici getnum ()cout<<"考绩:"; cin>>num;boolean isgood() return (2) ;
;
class teacher: (3)
int num;
public :
void getnum ()cout<<"每年发表论文数:"; cin>>num;boolean isgood() return (4) ; void main ()ba *p[50];
student*pstud;
teacher*ptech;
char ch;
int count=0;
docunt<<"输入教师(t)或学生(S):";
cin>>ch;
if(ch==´s´)pstud=new student;
pstud->getname ();
pstud->getnum();
p [count++] =pstud;el if(ch==´t´)ptech=newteacher;
哈佛大学公开课下载
ptech->getname();
ptech->getnum();
p[count++]=ptech;el
cout<<"输入错误"<<endl;
cout<<"继续输入吗(Y/n)";
cin>>ch;
while(ch==´Y´);
for (int i=0;i<count;i++)if( (5) ) //若为优秀,则输出之
p [i]->printname();
第6题
第7题
第8题
第9题
第10题
上一题 下一题
(11~15/共20题)填空题
[说明]
下面程序定义了一个类Shape及其成员函数,然后从类Shape派生出类Point、类Circle、类Cylinder,以及它们的成员函数。
[Java程序]
(1) Shape
gooffpublic abstract void printShapeName(); //纯虚函数。
public double area()return 0.0;
public double volumn()return 0.0;class Point extends Shape
private double x.y;
public Point (double a, double b)x=a; y=b;
public void tPoint (double a,double b)x=a;y=b;
final public double getX() return x;
final public double getY()return y;
public void printShapeName()
(2) ;
德国留学费用一览表class Circle extends Point
private double radius;
public Circle(double r, double a, double b)
(3) ;
radius=r>0? r:0;public void tRadius (double r)
钟欣桐简历radius=r>0 ? r:0;public double getRadius () return radius;
public double area()return 3.14259 * radius * radius;
public void printshapeName ()
System.out.print ("circle: ") ;
;
;
class Cylinder (4)
private double height;
public Cylinder(double h, double r, double x, double y)
super (r, x, y) ;
height = h>0 ? h:0;public void tHeight (double h)
height = h>0 ? h:0;public double area () //计算表面积
return (5) ;public double volumn ()
monga
double Radius () ;
quo
return 3.14159 * r * r * height;public void printShapeName ()
System. out .print ("cylinder: ") ;第11题
第12题
第13题
第14题
第15题
上一题 下一题
(16~20/共20题)填空题
[说明]
一个数如果恰好等于它的因子之和,这个数就是“完数”,如6=1+2+3,下面流程图完成寻找1000以内的完数,并将其因子存放在k[n]中,最后打印结果。
注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述。
[问题]
将流程图的(1)~(5)处补充完整。
图片
第16题
第17题
权威的意思
第18题
第19题
第20题
上一题 交卷
交卷
答题卡
答案及解析
(1~5/共20题)填空题
[说明]
本程序实现对指定文件内的单词进行计数。其中使用二叉树结构来保存已经读入的不同单词,并对相同单词出现的次数进行计数。此二叉树的左孩子结点的字符串值小于父结点的字符串值,右孩子结点的字符串值大于父结点的字符串值。函数getword(char *filename,char*word)是从指定的文件中得到单词。char* strdup(char* s)是复制s所指向的字符串,并返回复制字符串的地址。
[函数]
#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) ;el if((cond=strcmp (w,p->word))==0) (2) ;
el if (cond<0) p->left= (3) ;
el 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题
参考答案: p->left=p->right=NULL
详细解答:
第2题
参考答案: p->count++
详细解答:
第3题
参考答案: adtree(p->left,w)
详细解答:
第4题
参考答案: adtree(p->right,w)
详细解答:
第5题
参考答案: adtree(root,word)
详细解答: [解析] 在用二叉树结构来保存指定文件内的单词时,采用递归调用。首先在树中创建一个结点,因此空(1)填p->left=p->right=NULL。如果要插入的字符串已经存在,则计数值加一,即空(2)填p->count++;如果要插入的字符串小于此结点上字符串的值,则再次调用此函数,即空(3)填adtree(p->left,w);如果要插入的字符串大于此结点上字符串的值,则再次调用函数为空(4),即adtree(p->right,w)。在主函数中调用空(5),即adtree(root,word)。