简答题 C++
题号 #2607261

请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,该工程中包含程序文件main.epp,其中有类Door(“门”)和主函数main的定义。程序中位于每个“//ERROR****found ****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
打开503号门…门是锁着的,打不开。
打开503号门的锁…锁开了。
打开503号门…门打开了。
打开503号门…门是开着的,无须再开门。
锁上503号门…先关门...门锁上了。
注意:只修改每个“//ERROR***********found **********”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class Door{
int Bum;//门号
bool closed;//true表示门关着
bool locked://true表示门锁着
public:
Door(int nHm){
//ERROR***********found***********
nun=this一>ann:
closed=locked=true:
}
bool isClosed()const{rehlrn closed;}
//门关着时返回true,否则返回false
bool isOpened()const{return!closed;}
//门开着时返回true,否则返回false
b001 isLocked()const{retum locked;}
//门锁着时返回true,否则返回false
bool isUnlocked()const{return!locked;}
//门未锁时返回true,否则返回false
void open(){  //开门
cout<<endl<<”打开”<<nnm<<”号门...”;
//ERROR***********found***********
if(closed)
cout<<”门是开着的,无须再开门。”;
else if(10cked)
cout<<”门是锁着的,打不开。”;
else{
closed=false;
eout<<”门打开了。”;
}
}
void close(){//关门
cout<<endl<<”关上”<<num<<”号门…”;
if(closed)
cout<<”门是关着的,无须再关门。”;
else{
closed=true;
cout<<”门关上了.";
}
}
//ERROR************found************
void lock()const{//锁门
cout<<endl<<”锁上”<<num<<”号门…”;
if(locked)
cout<<”门是锁着的,无须再锁门。”;
else{
if(!closed){
cout<<”先关门...”;
closed=true;
}
locked=true:
cout<<”门锁上了。”;
}
}
wild unlock(){//开锁
cout<<endl<<”开”<<num<<”号门的锁...”;
if(!locked)
cout<<”门没有上锁,无须再开锁。”;
else{
locked=false;
cout<<”锁开了。”;
}
};
int main(){
Door door(503);
door.open();
door.unlock();
door.open();
door.open();
floor.10ck();
return 0;
}

进入在线模考

参考答案

暂无

答案解析

(1)this—>num=num;
(2)if(!closed)
(3)void lock(){

你可能感兴趣的试题

1 请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为:
按姓名排序
排序前:
张三   男  出生日期:1978年4月20日
王五   女  出生日期:1965年8月3日
杨六   女  出生日期:1965年9月5日
李四   男  出生日期:1973年5月30日
排序后:
李四   男  出生日期:1973年5月30日
王五   女  出生日期:1965年8月3日
杨六   女  出生日期:1965年9月5日
张三   男  出生日期:1978年4月20日
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
class Date{//日期类
int year,month,day;//年、月、日
public:
Date(int year,int month,int day):year(year),month(month),day(day){}
int getYear()const{return year;}
int getMonth()const{return month;}
int getDay()const{return day;}
};
class Person{  //人员类
char name[14];//姓名
bool is—male;//性别,为true时表示男性
Date birth—date;//出生日期
public:
Person(char*name,bool is—male,Date birth_date)
//***************found***************
:——————
{
{
strcpy(this一>name,name);
}
const char * getName()const{return name;}
bool isMale()const{return is—male;}
Date getBirthdate()const{return birth—date;}
//利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于
int compareName(const Person&p)const{
//***************found*****************
————}
void show(){
cout<<endl:
cout<<name<<”<<(is_male?”男”:”女”)<<”<<”出生日期:”<<birth—date.getYear()<<”年”//显示出生年
//**************found**************
__________________________//显示出生月
<< “birth—date.getDay()<<”日”;//显示出生日
}
};
void sortByName(Person ps[],int size){
//将人员数组按姓名排列为升序
for(int i=0;i<size一1;i++){
//采用选择排序算法
int m=i;
for(int j=i+1;j<size;j++)
if(ps[j].eompareName(ps[m])<0)
m=j;
if(m>i){
Person P=ps[m];
ps[m]=ps[i];
ps[i]=p;
}
}
}
int main(){
Person staff[]={
Person(”张三”,true,Date(1978,4,20)),
Person(”王五”,false,Date(1965,8,3)),
Person(”杨六”,false,Date(1965,9,5)),
Person(”李四”,tme,Date(1973,5,30))
};
const int size=sizeof(staff)/sizeof(staff[0]);
int i;
eout<<endl<<”按姓名排序”;
cout<<endl<<”排序前:”;
for(i=0;i<size;i++)staff[i].show();
sortByName(staff,size);
cout<<endl<<endl<<”排序后:”;
for(i_0;i<size;i++)staff[i].show();
cout<<endl:
return 0;
{
2

请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
2  3 4 5  27 28  31  66 75
2 3 4 5  6 27 28 31 56 75
2 3 4 5  6  19 27 28 31  66 75
3 4 5 6  19 27 28 31  66 75
3 4 5 6  19 27 28 31  66 75
要求:
补充编制的内容写在“//***********333***********”与“//***********666***********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//IntegorSet.h
#ifndef INTEGERSET
#define INTEGERSET
#include<iostream>
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class IntegerSet{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;  //用于记录集合中元素个数的计数器
puhlic:
IntegerSet():counter(0){}
//创建一个空集合
IntegerSet(int data[],int size);
//利用数组提供的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remeve(int element);
//删除集合中指定的元素
int getCount()const{return counter;}
//返回集合中元素的个数
int getElement(int i)const{retum elem[i];}//返回集合中指定的元素
void show()const;
};
void WriteToFile(char*);
#endif
//main.cpp
#include”IntegerSet.h”
#include<inmanip>
IntegerSet::IntegerSet(int data[],int size):counter(0){
for(int i=0;i<size;i++)
add(data[i]);
}
}
void IntegerSet::add(int element){
int j;
//从后往前寻找第一个小于等于element的元素
for(j=counter;j>0;j-)
if(element>=elem[j一1])break;
//如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回
if(j>0)
if(element==elem[j-1])return;
//如果找到的是小于element的元素,j就是要添加的位置
//该元素及其后面的元素依次后移,腾出插入位置
for(int k=counter;k>j;k一)
elem[k]=elem[k一1];
elem[j]=element;//将element插入到该位置
counter++;    //计数器加l
}
void IntegerSet::remove(int element){
//***************333***************
//***************666***************
void IntegerSet::show()const{
for(int i=0;i<getCount();i++)
cout<<setw(4)<<getElement(i);
cout<<endl:
}
int main(){
int d[]={5,28,2,4,5,3,2,75,27,66,31};
IntegerSet S(d,11);S.show();
S.add(6);    s.show();
S.add(19);    S.show();
S.remove(2);  s.show();
S.add(4);    S.show();
writeToFile(””);
return 0;
}

A.算法就是程序
B.设计算法时只需要考虑数据结构的设计
C.设计算法时只需要考虑结果的可靠性
D.以上三种说法都不对
找试题 · 找答案 · 找资料 在线模考