阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内
[说明]
以下程序的功能是计算三角形、矩形和正方形的面积并输出。
程序由4个类组成:类Triangle,Rectangle和Square分别表示三角形、矩形和正方形;抽象类Figure提供了一个纯虚拟函数getArea(),作为计算上述三种图形面积的通用接口。
[C++程序]
include<iostream.h>
include<math.h>
class Figure{
public:
virtual double getArea()=0; //纯虚拟函数
};
class Rectangle: (1) {
protected:
double height;
double width;
public:
Rectangle(){};
Rectangle(double height,double width){
This->height=height;
This->width=width;
}
double getarea(){
return (2);
}
};
class Square: (3)
public:
square(double width){
(4);
}
};
class triangle: (5) {
double la;
double lb;
double lc;
public:
triangle(double la,double lb,double lc){
this->la=la;thiS->ib;this->lc;
}
double getArea(){
double s=(la+lb+lc)/2.0;
return sqrt(s*(s-la)**(s-lb)*(s-lc));
}
};
viod main(){
figure*figures[3]={
new triangle(2,3,3),new Rectangle(5,8),new Square(5)};
for(int i=0; i<3;i++){
cout<<"figures["<<i<<"]area="<<(figures)->getarea()<<endl;
}
}
};
第1题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明]
函数int psort(int a[],int n)实现将含n个整数的数组a[]的不同元素按从小到大顺序存于数组a[]中。实现方法是从未确定的元素列中找到最小元素并将a[]的第i最小元素交换至a[i]位置。如该最小元素比已确定的最后一个最小元素大,则将它接在已确定的元素序列的后面;否则,忽视该元素。
[C函数]
int psort(int a[],int n)
{int i,J,k,P;
for(i=0,k=0;i<(1);i++){
for(j=i+1, (2) ;j<n; j++)
if(a[p]>a[j])
p=j;
if(p!=i){
t=a[p];
a[p]=a[i];
a[i]=t;
}
if( (3) ) k++;
else if( (4) <a[i])
(5)=a[i];
}
return k;
}
int a[]={5,7,5,6,4,3,4,6,7};
main()
{int k,n;
for(k=0;k<(Sizeof a)/Sizeof(int);k++)
printf("%5d",a[k]);
printf ("\n\n");
n=psort(a,(sizeof(a))/sizeof(int));
for(k=0;k<n;k++)
printf("%5d",a[k]);
printf("\n\n");
}
第2题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
[说明]
某银行共发出M张储蓄卡,每张储蓄卡拥有唯一的卡号,每天每张储蓄卡至多支持储蓄卡持有者的N笔“存款”或“取款”业务。程序中用数组card[M][N+3]中的每一行存放一张储蓄卡的有关信息,其中:
card[i][0]存放第i张卡的卡号;
card[i][1]存放第i张卡的余额;
card[i][2]存放第i张卡的当日业务实际发生笔数;
card[i][3]~card[i][N+2]存放第i张卡的当日存取款金额,正值代表存款,负值代表取款。
当持卡者输入正确的卡号、存款或取款金额后,程序进行相应的处理;若输入不正确的数据,程序会提示持卡者重新输入;若输入的卡号为负数时,银行终止该卡的当日业务。
[C程序]
include<stdio.H>
define M 6
define N 5
long card[M][N+3]={{9801,2000,0,},{9812,2000,2,},{9753,3000,1,},
{8750,500,0,},{9604,2800,3,),(8901,5000,5,}};
int locate(long card[][N+3],int m,long no)
{ int i;
for(i=0;i<m;i++)
if((1)==no) return i;
(2);
}
main()
{long cardNo,money;
int k;
while(1){
printf("请输入卡号:\n");
scanf("%1d",&cardNo);
if(cardNo<0) break;
k=locate(card,M,cardNo);
if(k==-1){
printf("不存在%id号的储蓄卡\n",cardNo);
continue;
}
printf("请输入金额(正值代表存款,负值代表取款):\n");
scanf("%id",&money);
if(card[k][1]+money<0){
printf("存款余额不足,不能完成本次的取款业务\n");
continue;
}
if(card[k][2]==N){
printf("已完成本卡的当日业务\n");
continue;
}
/*处理一笔业务的数据*/
card[k] (3)=money;
(4);
(5);
}
}
第3题
阅读以下函数说明和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);
}
第4题
阅读以下说明、C函数和问题,将解答写入答题纸的对应栏内。
【说明1】
函数test_fl(int m,im n)对整数m、n进行某种运算后返回一个整数值。
【c函数1】
int test_f1 (int m,int n)
{ int k;
k=m>n?m:n;
for(; (k%m! =O) I I (k%n! =0);k++);
return k;
)
【问题1】(5分)(1)请写出发生函数调用test_fl(9,6)时,函数的返回值;(2)请说明函数test_f1的功能。
【说明2】
设在某c系统中为每个字符分配1个字节,为每个指针分配4个字节,sizeof(x)计算为x分配的字节数。
函数test_12Q用于测试并输出该c系统为某些数据分配的字节数。
【c函数2】
void test_f2()
{ char str[] =“NewWorld”; char*p=str; char i=‘\0’; VOid*ptr=malloc(50);
printf(”%d\t”,sizeof(str));printf(“%d\n”,sizeof(p)),
printf(”%d\t”,sizeof(i)); printf(”%d\n”,sizeof(ptr));
}
【问题2】(4分)
请写出函数test_f2()的运行结果。
【说明3】
函数test_f3(char S[])的功能是:将给定字符串S中的所有空格字符删除后形成的串保存在字符数组tstr中(串S的内容不变),并返回结果串的首地址。
【C函数3】
char*test_f3(const char s[])( char tstr[50]=(‘\0’); unslgned int i,k=0;
for(i=0;i(strlen(s);i++)
if(s[i]!=‘’)tstr[k++]=s[i];
return tStr ;
)
【问题3】(6分)
函数test_f3()对返回值的处理有缺陷,请指出该缺陷并说明修改方法。
第5题
阅读下列说明、图和C抖代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
已知对某载客车辆(Car)进行类建模,如图5-1所示,其中类Engine表示发动机引擎,类Wheel表示车轮,类Body表示车身,类Driver表示司机,类Passenger表示乘客。
【C++代码】
const int(1)=7; //定义最多载客数
const int MAX WHEELS=5; //定义最多轮胎数
class Body{ //此处代码省略 }; //车身类
class Passenger{ //此处代码省略 }; //乘客类
class Wheel{ //此处代码省略 }; //车轮类
class Driver{ //司机类
public:
string name;//表示第几路公交车司机
Driver(string driverName):name((2)){}///构造函数
};
class Engine{//引擎类
public:
string engineNo;//引擎编号
Engine(string engineNo){(3)->engineNo=engineNo;}//构造函数
};
class Car{//汽车类
protected:
Engine * engine; Driver * driver; Body body;
Wheel * wheels [MAX_WHEELS];Passenger * passengers [MAX_PASSENGERS];
public:
Car(Driver *driver){ //构造函数
this->driver=driver;
engine=new Engine("TX6536型号引擎");
for (int index=0; index<MAX WHEELS;index++){
wheels[index]=new Wheel();
}
for(int index=0;index<MAX_PASSENGERS;index++){
passengers[index]=NULL;
}
}
virtual ~Car(){//析构函数
for( int index=0;index<MAX_WHEELS;index++)
delete wheels[index];
delete(4);
}
int getPassengerNumber(){//获取车上乘客数量
//此处代码省略
}
void getOnPassenger(Passenger * aPassenger){//乘客上车
//此处代码省略
}
void run(){ //开车
if(driver==NULL){cout<<"司机尚未上车!"; return; }
//此处代码省略
}
};
void main(){
Driver driver("第五路公交车司机");
Car car((5));
Passenger passengers[MAX_PASSENGERS];
for(int index=0;index<MAX PASSENGERS;index ++) //乘客上车处理
car.getOnPassenger(&passengers [index]);
car.run();
}
第6题
阅读以下说明和c++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
c++标准模板库中提供了map模板类,该模板类可以表示多个“键一值”对的集合,其中键的作用与普通数组中的索引相当,而值用作待存储和检索的数据。此外,c++模板库还提供了pair模板类,该类可以表示一个“键-值”对。pair对象包含两个属性:first和second,其中first表示“键-值”中的“键”,而Second表示“键-值”中的“值”。map类提供了insert方法和find方法,用于插入和查找信息。应用时,将一个pair。对象插入(insert)到map对象后,根据“键”在map对象中进行查找(find),即可获得一个指向pair对象的迭代器。下面的c++代码中使用了map和pair模板类,将编号为1001、1002、1003的员工信息插入到map对象中,然后输入一个指定的员工编号,通过员工编号来获取员工的基本信息。员工编号为整型编码,员工的基本信息定义为类employee。map对象与员工对象之间的关系及存储结构如图5—1所示。
【c++代码】
include
include
include
using namespace std;
class employee {(1) :
employee(string name,string phoneNumber,string address){
this->name=name;
this->phoneNumber=phoneNumber ;
this->address=address;
}
string name;
string phoneNumber;
string address;
);
int main()
{
mapemployeeMap;
typedef pair>employeeNo; //从标准输入获得员工编号
map::const_iterator it;
it= (5) .find(employeeNo); //根据员工编号查找员工信息
if(it==employeeMap.end()){
cout<first<second一>nafae(phoneNumber<second->address<
第7题
阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
本程序用于评选优秀教师和学生。当输入一系列教师或学生的记录后,将优秀学生及教师的姓名列出来。其类结构如下图所示:
【程序】
include <iostream.h>
include <stdio. h>
class base
{
protected:
char name[8];
public:
void getname(){cout<<"name:"; cin>>name;}
void printname(){cout<<"name:"<<name<<endl;}
(1)
};
class student: (2)
{
int num;
public:
void getnum()
{cout<<"score:"; cin>>num;}
bool isgood()
{return (3) }
};
class teacher: (2)
{
int num;
public:
void getnum()
{cout<<"paper:"; cin>>num;}
bool isgood()
{return (num>3)?true:false;}
void main()
{
base *p[50];
student *pstud;
teacher *ptech;
char ch;
int count=0;
do{
cout<<"input teacher(t) or student(s):";
cin>>ch;
if(ch=='s')
{
pstud=new student;
pstud->getname();
pstud->getnum();
p[count++]=pstud;
}
else if(ch=='t')
{
(4)
ptech->getname();
ptech->getnum();
p[count++]=ptech;
}
else
cout<<"input is wrong"<<endl;
cout<<"continue to iput(y/n)?";
cin>>ch;
}while(ch=='y');
for(int i=0;i<count;i++)
if((5))
p[i]->printname();
}
第8题
阅读以下说明和C语言程序,将应填入(n)处的字句写在对应栏内。
【说明】
以字符流形式读入一个文件,从文件中检索出6种C语言的关键字,并统计、输出每种关键字在文件中出现的次数。本程序中规定:单词是一个以空格或'\t'、'\n'结束的字符串。其中6种关键字在程序中已经给出。
【程序】
include <stdio.h>
include <stdlib.h>
FILE *cp;
char fname[20], buf[100];
int NUM;
struct key
{ char word[10];
int count;
}keyword[]={ "if", 0, "char", 0, "int", 0,
"else", 0, "while", 0, "return", 0};
char *getword (FILE *fp)
{ int i=0;
char c;
while((c=getc(fp))!= EOF &&(1));
if(c==EOF)
return (NULL);
else
buf[i++]=c;
while((c=fgetc(fp))!=EOF && c!="&& c!='\t' && c!='\n' )
buf[i++]=c;
buf[i]='\0';
return(buf);
}
void lookup(char *p)
{ int i;
char *q, *s;
for(i=0; i<NUM; i++)
{ q=(2);
s=p;
while(*s && (*s==*q))
{ (3))
if((4))
{ keyword[i].count++;
break;
}
}
return;
}
void main()
{ int i;
char *word;
printf("lnput file name:");
scanf("%s", fname);
if((cp=fopen(fname, "r"))==NULL)
{ printf("File open error: %s\n", fname);
exit(0);
}
NUM=sizeof(keyword)/sizeof(struct key);
while((5))
lookup(word);
fclose(cp);
for(i=0;i<NUM;i++)
printf("keyword:%-20s count=%d\n",keyword[i].word,keyword[i].count);
}
第9题
阅读以下说明和Java程序,将应填入(n)处的字句写在对应栏内。
[说明]
下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。
[Java程序]
public class MainJava {
public static void main(String[] args){
Lot_size small=new Lot_size();
Lot_size medium=new Lot_size();
small.set(5,5,5,25);
medium.set(10,10,10,50);
System.out.println("For a small lot of area"
+small.get_area()+"\n");
System.out.println("the actual crops are $"
+small.get_data2()+"\n");
System.out.println("and ideal crops are $"
+small.get data()+"\n");
System.out.println("For a medium lot of area"
+medium.get_area()+“\n”);
System.out.println("the actual crops are $"
+medium.get_data2()+"\n");
System.out.println ("and ideal crops are $"
+medium.get_data()+"\n");
}
}
class Crop_assessment{
private int actual_crop;
private int ideal_crop;
public void set(int in_actual,int in ideal){
actual_crop=in_actual;
ideal_crop=in_ideal;
}
public int get_actual_crop(){return (1) ;}
public int get_ideal_crop()(return (2) ;}
}
class Lot_size{
private int length;
private int width;
private Crop_assessment crop= (3) ;
public void set(int 1,int W,int a,int i){
length=1;
width=W;
crop.set(a,i);
}
public int get_area(){return length*width;}
public int get_data()freturn (4) ;}
public int get_data2()(return (5) ;}
}
第10题
试题三(共15分)
阅读以下说明和C函数,填补C函数中的空缺(1)~(6),将解答写在答题纸的对应栏内。
【说明】
函数numberOfwords (char message[])的功能是计算存储在message字符数组中的一段英文语句中的单词数目,输出每个单词(单词长度超过20时仅输出其前20个字母),并计算每个英文字母出现的次数(即频数),字母计数时不区分大小写。
假设英文语句中的单词合乎规范(此处不考虑单词的正确性),单词不缩写或省略,即不会出现类似don't形式的词,单词之后都为空格或标点符号。
函数中判定单词的规则是:
(1)一个英文字母串是单词;
(2) 一个数字串是单词;
(3)表示名词所有格的撇号(')与对应的单词看作是一个单词。
除上述规则外,其他情况概不考虑。
例如,句子“The 1990's witnessed many changes in people's concepts ofconservation”中有10个单词,输出如下:
The
1990's
witnessed
many
changes
in
people's
concepts
of
conservation
函数numberOfijvords中用到的部分标淮库函数如下所述。
【C函数】
int numberOfwords (char message[])
{
char wordbuffer[21],i=0; /*i用作wordbuffer的下标*/
(1) pstr;
int ps[26]={0); /*ps[0]用于表示字母'A'或'a'的频数*/
/*ps[1]用于表示字母'B'或'b'的频数,依此类推*/
int wordcounter=0;
pstr=message;
while (*pstr){
if((2)(*pstr)){/*调用函数判断是否为一个单词的开头字符*/
i=0;
do{/*将一个单词的字符逐个存入wordbuffer[],并对字母计数*/
wordbuffer[i++]=*pstr;
if(isalpha(*pstr)){
if (3) (*pstr))ps[*pstr-'a']++;
else ps[*pstr-'A']++;
}
(4) ; /*pstr指向下一字符*/
}while (i<20&&(isalnum(*pstr)||*pstr=='\"));
if (i>=20) /*处理超长单词(含名词所有格形式)*/
while (isalnum(*pstr)||*pstr=='\"){pstr++;}
(5) ='\0';/*设置暂存在wordbuffepstrr中的单词结尾*/
wordcounter++; /*单词计数*/
puts(wordbuffer); /*输出单词*/
}
(6); /*pstr指向下一字符*/
}
retum wordcounter;
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!