应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,其中有整数栈类IntList、顺序栈类SeqList和链接栈类LinkList的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
4 6 3 1 8
4 6 3 1 8
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动//“****found****”。
#include <iostream>
using namespace std;
class IntStack { //整数栈类
public:
virtual void push(int) = 0; //入栈
virtual int pop() = 0;
//出栈并返回出栈元素
virtual int topElement() const = 0;
//返回栈顶元素,但不出栈
virtual bool isEmpty() const=0;
//判断是否栈空
};
class SeqStack: public IntStack{int data[100]; //存放栈元素的数组
int top; //栈顶元素的小标
public:
// ********found********
SeqStack():______{} //把top初始化为-1表示栈空
void push (int n) {data[++top] = n;}
// **********found**********
int pop() {return______;}
int topElement() const {return data[top];}
bool isEmpty() const {return top == -1;}
};
struct Node{
int data;
Node * next;
};
class LinkStack: public IntStack{
Node * top;
public:
// **********found**********
LinkStack():______{} //把top初始化为NULL表示栈空
void push (int n) {
Node * p = new Node;
p -> data = n;
// **********found**********
______
top = p;
}
int pop(){
int d=top->data;;
top=top->next;
return d;
}
int topElement() const {return top -> data;}
bool isEmpty() const {return top == NULL;}
};
void pushData(IntStack st){
st.push(8);
st.push(1);
st.push(3);
st.push(6);
st.push(4);
}
void popData(IntStack st){
while(! st.isEmpty())
cout << st.pop() << '';
}
int main(){
SeqStack st1; pushData(st1); popData(st1);
cout << endl;
LinkStack st2; pushData(st2); popData(st2);
cout << endl;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
每卖出一个瓜,则计算瓜的重量,还要计算所有卖出瓜的总重量以及总个数,同时允许退货,请按照以下的操作,把类补充完整
(1)定义类Cmelon的私有静态数据成员float型变量totalweight和int型变量totalNo,请在注释////********1********后添加适当的语句。
(2)完成类Cmelon的带一个float型变量w的构造函数,并把这个w加到totalweight中,并且totalNo自加。请在注释//********2********后添加适当的语句。
(3)在析构函数中,在totalweight中减去weight,然后totalNo自减,请在注释//********3********后添加适当的语句。
(4)完成静态成员变量的初始化为0,请在注释//********4********后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include<iostream.h>
class Cmelon
{
private:
float weight;
//********1********
static int totalNo;
public:
Cmelon(float w)
{
//********2********
totalweight+= w;
totalNo++;
}
~Cmelon()
{
//********3********
totalweight -= weight;
}
void display()
{
cout << 'Sell a melon with '<< weight << 'kg' << endl;
cout << 'Total sell number: ' << totalNo << endl;
cout << 'Total sell weight: '<< totalweight <<'kg' <<endl << endl;
}
};
//********4********
float Cmelon::totalweight = 0.0;
int main()
{
Cmelon melon1(1.2);
melon1.display();
Cmelon melon2(2.3);
melon2.display();
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程proj3,其中声明的CDeepCopy是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数operator=,以实现深层复制。
要求:
补充编制的内容写在“// ********333********”与“// ********666********”之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//CDeepCopy.h
#include <iostream>
#include <string>
using namespace std;
class CDeepCopy
{
public:
int n; //动态数组的元素个数
int *p; //动态数组首地址
CDeepCopy(int);
~CDeepCopy();
CDeepCopy operator = (const CDeepCopy r); //赋值运算符函数
};
void writeToFile(char *);
//main.cpp
#include 'CDeepCopy.h'
CDeepCopy::~CDeepCopy() {delete[] p;}
CDeepCopy::CDeepCopy(int k) {
n=k; p=new int[n]; } //构造函数实现
CDeepCopy CDeepCopy::operator = (const CDeepCopy r) //赋值运算符函数实现
{
// ********333********
// ********666********
}
int main()
{
CDeepCopy a(2),d(3);
a.p[0] = 1; d.p[0] = 666;
//对象a,d数组元算的赋值
{
CDeepCopy b(3);
a.p[0] = 88; b = a;
//调用赋值运算符函数
cout << b.p[0];
//显示内层局部对象的数组元素
}
cout << d.p[0];
//显示d数组元算a.p[0]的值
cout << 'd fade away; \n';
cout << a.p[0];
//显示a数组元素a.p[0]的值
writeToFile(' ');
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motocycle类。要求将vehicle作为虚基类,避免二义性问题。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
A vehicle is running!
A vehicle has stopped!
A bicycle is running!
A bicycle has stopped!
A motorcar is running!
A motocycle is running!
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
vehicle(): MaxSpeed(0), Weight(0){}
vehicle(int max_speed, int weight): MaxSpeed (max_speed ), Weight
(weight){}
//********** found**********
______Run()
{
cout << 'Avehicle is running!' << endl;
}
//********** found**********
______Stop()
{
cout << 'A vehicle has stopped!' << endl;
}
};
class bicycle : virtual public vehicle
{
private:
int Height;
public:
bicycle(): Height(0){}
bicycle(int max_speed, int weight,int height)
:vehicle (max_speed, weight), Height(height){};
void Run () {cout << 'A bicycle is running!' << endl; }
void Stop() {cout << 'A bicycle has stopped!' << endl; }
};
class motorcar : virtual public vehicle
{
private:
int SeatNum;
public:
motorcar(): SeatNum(0) {}
motorcar (int max_speed, intweight, int seat_num)
//********** found**********
:______{}
void Run() {cout << 'A motorcar is running!' << endl; }
void Stop () {cout << 'A motorcar has stopped!' << endl; }
};
//********** found**********
class motorcycle: ______
{
public:
motorcycle(){}
motorcycle (int max_speed, int weight, int height, int seet_num): bicycle(max_speed, weight, height), motorcar (max_speed, weight, seet_num){};
~motorcycle () {};
void Run () {cout << 'A motorcycle is running!' << endl; }
void Stop() {cout << 'A motorcycle has stopped!' << endl; }
};
int main()
{
vehicle * ptr;
vehicle a;
bicycle b;
motorcar c;
motorcycle d;
a.Run(); a. Stop();
b.Run(); b. Stop();
ptr = c; ptr->Run();
ptr = d; ptr->Run();
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。函数sum(int n)计算在n范围内,能被7和11整除的所有整数的和(包括n在内)。
注意:不能修改程序的其他部分,只能补充sum()函数。
#include <iostream.h>
double sum(int n)
{
}
void main()
{
cout<<sum (80) <<endl;
cout<<sum(200) <<endl;
cout<<sum(300)<<endl;
return;
}
应用题 使用VC++6.0打开下的源程序文件3.cpp。其中定义的类不完整,按要求完成下列操作,将类的定义补充完整。
(1)在类TC中定义name为字符串类型,age为int型,请在注释1之后添加语句。
(2)设置类TC0的基类为TC类的定义,请在注释2后添加语句。
(3)在类TC的派生类TC0的公有成员中定义析构函数TC0,请在注释3后添加语句。
(4)设置类TC1的基类为TC类的定义,请在注释4后添加语句。
程序输出结果为
TC class constructor
TC0 class constructor
TC on class constructor
TC1 class constructor
TC1 class constructor
TC class constructor
TC0 class constructor
TC class constructor
注意:增加或者修改代码的位置已经用符号表示出来,请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
class TC
{
//********1********
public:
TC(){cout<<'TC class constructor'<<endl;}~
TC(){cout<<'TC class constructor'<<endl;}
};
//********2********
{
char*departmert;
int level;
public:
TC0()
{cout<<'TC0 class constructor'<<endl;}
//********3********
{cout<<'TC0 class constructor'<<endl;}
};
//********4********
{
char*major;
float salary;
public:
TC1(){cout<<'TC1 class constructor'<<endl;}~
TC1(){cout<<'TC1 class constructor'<<endl;}
};
void main()
{
TC0 s1;
TC1 t1;
}
应用题 请使用VC6或使用[答题] 菜单打开proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3. h、proj3. cpp、writeToFile. obj。补充完成重载赋值运算符函数,完成深复制功能。
屏幕上输出的正确结果应该是:
Hello!
Happy new year!
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”两行之间。不得修改程序的其他部分。
注意:
程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out. dat中。输出函数writeToFile已经编译为obj文件。
//proj3. h
#include<iostream>
#include<iomanip>
using namespace std;
class MiniString
{
public:
friend ostream operator<<(ostream output, const MiniString s)//重载流插入运算符
{ output<<s. sPtr; return output; }
friend istream operator>>(istream input, MiniString s)//重载流提取运算符
{ char temp[100];//用于输入的临时数组
temp[0]='\0';//初始为空字符串
input>> setw(100)>>temp;
int inLen=strlen(temp);//输入字符串长度
if(inLen !=0)
{
s. length=inLen;//赋长度
if(s. sPtr!=0)delete[]s. sPtr;//避免内存泄漏
s. sPtr=new char[s. length+1];
strcpy(s. sPtr, temp);//如果S不是空指针,则复制内容
}
else s. sPtr[0]='\0';//如果s是空指针,则为空字符串
return input;
}
void modString(const char*string2)//更改字符串内容
{
if(string2 !=0)//如果string2不是空指针,则复制内容
{
if(strlen(string2)!=length)
{
length=strlen(string2);
delete[]sPtr;
sPtr=new char[1ength+1];//分配内存
}
strcpy(sPtr, string2);
}
else sPtr[0]='\0';//如果string2是空指针,则为空字符串
}
MiniString operator=(const MiniString otherString);
MiniString(const char*S=''): length((s !=0) ? strlen(s): 0)//构造函数
{
sPtr=0;
if(length!=0)
setString(s);
}
~MiniString()//析构函数
{delete[]sPtr;}
private:
int length;//字符串长度
char*sPtr;//指向字符串起始位置
void setString(const char*string2)//辅助函数
{
sPtr=new char[strlen(string2)+1];//分配内存
if(string2!=0) strcpy(sPtr, string2);//如果string2不是空指针,则复制内容
else sPtr[0]='\0';//如果string2是空指针,则为空字符串
}
};
//proj3. cpp
#include<iostream>
#include<iomanip>
using namespace std;
#include'proj3. h'
MiniString MiniString:: operator=(const MiniString otherString)
{//重载赋值运算符函数。提示:可以调用辅助函数setString
//*************333**********
//*************666**********
}
int main()
{
MiniString strl('Hello!'), str2;
void writeToFile(const char*);
str2=strl;//使用重载的赋值运算符
str2. modString('Happy new year!');
cout<<strl<<'\n';
cout<<str2<<'\n';
writeToFile(' ');
return 0;
}
应用题 请使用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 66 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文件,并且在本程序中调用。
//IntegerSet. h
#ifndef INTEGERSET
#define INTEGERSET
#include<iostream>
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class IntegerSet {
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;//用于记录集合中元素个数的计数器
public:
IntegerSet(): counter(0) {}
//创建一个空集合
IntegerSet(int data[], int size);
//利用数组提供的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remove(int element);
//删除集合中指定的元素
int getCount()const {return counter;}
//返回集合中元素的个数
int getElement(int i)const {return elem[i];}//返回集合中指定的元素
void show()const;
};
void WriteToFile(char*);
#endif
//main. cpp
#include'IntegerSet. h'
#include<iomanip>
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++;//计数器加1
}
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;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中包含了日期类Date、人员类Person及排序函数sortByAge和主函数main的定义。其中Person的成员函数compareAge的功能是:将当前Person对象和参数Person对象进行比较,若前者的年龄大于后者的年龄,则返回一个正数;若前者的年龄小于后者的年龄,则返回一个负数;若年龄相同则返回0。注意,出生日期越大,年龄越小。请编写成员函数compareAge。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
按年龄排序
排序前:
张三 男 出生日期:1978年4月20日
王五 女 出生日期:1965年6月3日
杨六 女 出生日期:1965年9月5日
李四 男 出生日期:1973年5月30日
排序后:
张三 男 出生日期:1978年4月20日
李四 男 出生日期:1973年5月30日
杨六 女 出生日期:1965年9月5日
王五 女 出生日期:1965年6月3日
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//Person.h
#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]; //姓名
boom is_male; //性别,为true时表示男性
Date birth_date; //出生日期
public:
Person (char * name, bool is_male, Date birth_date);
const char * getName () const { return name; }
bool isMale()const{ return is_male; }
Date getBirthdate () const { return birth_date; }
int compareAge (const Person p) const;
void show () const;
};
void sortByAge(Personps[], int size);
void writeToFile(char * );
//main.cpp
#include'Person.h'
Person::Person(char * name, bool is_male, Date birth_date): is_male (is_male), birth_date (birth_date) {
strcpy(this->name, name);
}
int Person::compareAge(const Person p) const {
//******** 333********
//******** 666********
}
void Person::show () const {
cout << endl;
cout << name << ' //显示姓名
<< (is_male? '男' : '女') //显示性别('男'或'女')
<<'出生日期:' //显示出生日期
<<birth_date.getYear () << '年'
<<birth_date.getMonth() <<'月'
<<birth_date.getDay() <<'日';
}
void sortByAge(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].compareAge (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, 6, 3)),
Person('杨六', false, Date (1965, 9, 5)),
Person ('李四', true, Date (1973, 5, 30))
};
const int size = sizeof (staff)/sizeof(staff[0]);
int i;
cout << '按年龄排序' << endl <<'排序前:';
for(i=0; i<size; i++) staff[i]. show ();
sortByAge (staff, size);
cout << endl << endl << '排序后:';
for(i=0; i<size; i++) staff[i]. show ();
cout << endl;
writeToFile ('');
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成proj3.h,重载复合赋值运算符+=。
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include <iostream>
#include <iomanip>
using namespace std;
class MiniString
{public:
friend ostream operator << (ostream output, const MiniString s )
//重载流插入运算符
{output << s.sPtr; return output;
}
friend istream operator >> ( istream input, MiniString s )
//重载流提取运算符
{char temp [100]; //用于输入的临时数组
temp[0] = '\0';
input >> setw (100) >> temp;
int inLen = strlen(temp); //输入字符串长度
if( inLen!= 0)
{
s.length = inLen; //赋长度
if( s.sPtr! = 0) delete []s.sPtr; //避免内存泄漏
s.sPtr = new char [s.length + 1];
strcpy( s.sPtr, temp );
//如果s不是空指针,则复制内容
}
else s.sPtr [0] = '\0';
//如果s是空指针,则为空字符串
return input;
}
MiniString ( const char * s=''): length (( s!= 0 )? strlen(s) : 0 ){ setString(s); }
~MiniString() { delete [] sPtr; }//析构函数
//************* 333***********
//+=运算符重载
//************* 666***********
private:
int length; //字符串长度
char * sPtr; //指向字符串起始位置
void setString( const char * string2 ) //辅助函数
{
sPtr = new char [length + 1];
//分配内存
if ( string2 ! = 0 )
strcpy( sPtr, string2 );
//如果string2不是空指针,则复制内容
else sPtr [0] = '\0';
//如果string2是空指针,则为空字符串
}
};
//proj3.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include 'proj3.h'
int main ()
{
MiniString str1 ('World'), str2 ('Hello');
void writeToFile (char * );
str2 + = str1; //使用重载的+=运算符
cout << str2 << '\n';
writeToFile ('');
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
//矩阵基础类,一个抽象类
class MatrixBase {
int rows, cols;
public:
MatrixBase(int rows, int cols): rows(rows), cols(cols) {}
int getRows()const {return rows;}//矩阵行数
int getCols()const {return cols;}//矩阵列数
virtual double getElement(int r, int c)const=0;//取第i个元素的值
void show()const {//分行显示矩阵中所有元素
for(int i=0; i<rows; i++) {
cout<<endl;
for(int i=0; j<cols; j++)
//**********found**********
cout<<______<<' ';
}
}
};
//矩阵类
class Matrix: public MatrixBase {
double*val;
public:
//**********found**********
Matrix(int rows, int cols, double m[]=NULL):______ {
//**********found**********
val=______;
for(int i=0; i<rows*cols; i++)
val[i]=(m==NULL?0.0: m[i]);
}
~Matrix() {delete[]val;}
double getElement(int r. int c)const {return val[r*getCols()+c];}
};
//单位阵(主对角线元素都是1,其余元素都是0的方阵)类
class UnitMatrix: public MatrixBase {
public:
UnitMatrix(int rows): MatrixBase(rows, rows) {}
//单位阵行数列数相同
double getElement(int r, int c)const {
//**********found**********
if(______)return 1.0;
return 0.0;
}
};
int main() {
MatrixBase*m;
double d[][5]= {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7}};
m=new Matrix(3, 5,(double*)d);
m->show();
delete m;
cout<<endl;
m=new UnitMatrix(6);
m->show();
delete m;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类Office的定义,还有主函数main的定义。请在程序中“// ****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
办公室房间号:308
办公室长度:5.6
办公室宽度:4.8
办公室面积:26.88
办公室所属部门:会计科
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class Room{ //“房间”类
int room_no; //房间号
double length; //房间长度(m)
double width; //房间宽度(m)
public:
Room(int the_room_no, double the_length, double the_width):room_no (the_room_no), length(the_length), width (the_width) {}
int theRoomNo()const{return room_no;}
//返回房间号
double theLength()const {return length;} //返回房间长度
double theWidth()const {return width;} //返回房间宽度
// **********found**********
double theArea()const{______} //返回房间面积(矩形面积)
};
class Office: public Room{ //“办公室”类
char * depart; //所属部门
public:
Office (int the_room_no, double the_length, double the_width, const char * the_depart)
// **********found**********
:______{
depart = new char[strlen(the_depart) +1];
strcpy(______);
}
~Office() {delete [] depart;}
const char * theDepartment() const{return depart;} //返回所属部门
};
int main(){
// **********found**********
Office ______;
cout << '办公室房间号:' << an_office.theRoomNo() << endl
<< '办公室长度:' << an_office.theLength() << endl
<< '办公室宽度:' << an_office.theWidth() << endl
<< '办公室面积:' << an_office.theArea() << endl
<< '办公室所属部门:' << an_office.theDepartment() << endl;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。函数IsPalindromes (cha* string)实现的功能是判定给定的字符串是否构成回文字符串,如果是则返回1,否则返回0。
如:1234554321或者1234321都认为是回文字符串。
如果串为空或一个字母时,均认为是回文字符串。
注意:不能修改程序的其他部分,只能补充IsPalindromes()函数。
#include <iostream.h>
#define MAXLEN 1024
bool IsPalindromes(char*string)
{
}
void main()
{
char str[MAXLEN];
cout<<'请输入一行文字'<<endl;
cin.getline(str,MAXLEN);
cout<<IsPalindromes(str)<<endl;
return;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。此程序的运行结果为:
In CDerive's display().b=1
In CDerive2's display().b=2
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义函数display()为无值型纯虚函数。请在注释//********1********之后添加适当的语句。
(2)建立类CDerive的构造函数,请在注释//********2********女之后添加适当的语句。
(3)完成类CDerive2成员函数diaplay()的定义。请在注释//********3********之后添加适当的语句。
(4)定义类Derive1的对象指针d1,类CDerive2的对象指针d2。其初始化值分别为1和2。请在注释//********4********之后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include <iostream>
using namespace std;
class CBase
{
public:
CBase(int i) {b=i;}
//********1********
protected:
int b;
};
class CDerive : public CBase
{
public:
//********2********
void display()
{
cout<<' In CDerive's display().'<<' b= '<<b<<endl;
}
};
class CDerive2: public CBase
{
public:
CDerive2(int i) :CBase(i){}
//********3********
void func(CBase *obj)
{
obj->display();
}
void main()
{
//********4********
func(d1);
func(d2);
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)完成默认构造函数TestClass的定义,使得TestClass对象的类型为int,默认值为a=0,b=0,c=0,请在注释//********1********后添加适当的语句。
(2)定义类的私有成员变量,X、Y、Z类型为int,请在注释//********2********后添加适当的语句。
(3)定义类TestClass的数据成员count声明为静态整数型数据成员,请在注释//********3********后添加适当的语句。
(4)在构造函数中实现用count表示类TestClass被实现对象的次数。请在注释//********4********后添加适当的语句。
本程序的输出结果为:
The point is(1,1,1)
There are 3 point objects
The point is(1,2,3)
There are 3 point objects
The point is(0,0,0)
There are 3 point objects
注意:除在指定的位置添加语句之外,请不要改动程序的其他部分。
#include<iostream>
using namespace std;
class TestClass
{
public:
//********1********
{
X=a;
Y=b;
Z=c;
//********4********
}
void Display()
{
cout<<'The point is ('<<x<<','<<y<<','<<z<<')'<<endl;
cout<<'There are'<<count<<'point objects.'<<endl;
}
private:
//********2********
//********3********
};
int TestClass::count=0;
int main()
{
TestClass p1(1,1,1),p2(1,2,3),p3(0,0,0);
p1.Display();
p2.Display();
p3.Display();
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开prog2下的工程prog2。此工程中包含一个程序文件main.cpp,其中有“部门”类Department和“职工”类Staff的定义,还有主函数main的定义。在主函数中定义了两个“职工”对象,他们属于同一部门。程序展示,当该部门改换办公室后,这两个人的办公室也同时得到改变。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换办公室前:
职工号:0789 姓名:张三 部门:人事处 办公室:521
职工号:0513 姓名:李四 部门:人事处 办公室:521
改换办公室后:
职工号:0789 姓名:张三 部门:人事处 办公室:311
职工号:0513 姓名:李四 部门:人事处 办公室:311
注意:只在横线处填写适当的代码,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Department{ //“部门”类
public:
Department (const char * name, const char * office) {
strcpy(this->name, name);
// ********found********
______
}
const char * getName() const {return name;} //返回部门名称
// ********found********
const char * getOffice() const {______} //返回办公室房号
void changeOfficeTo (const char * office) { //改换为指定房号的另一个办公室
strcpy (this -> office, office);
}
private:
char name[20]; //部门名称
char office[20]; //部门所在办公室房号
};
class Staff{ //“职工”类
public:
// **********found**********
Staff (const char * my_id, const char * my_name, Department my_dept):______{
strcpy (this -> staff_id, my_id);
strcpy (this -> name, my_name);
}
const char * getID() const {return staff id;}
const char * getName() const {return name;}
Department getDepartment() const {return dept;}
private:
char staff_id[10]; //职工号
char name[20]; //姓名
Department dept; //所在部门
};
void showStaff(Staff staff) {
cout << '职工号:' << staff.getID() << ' ';
cout << '姓名:' << staff.getName() << ' ';
cout << '部门:' << staff.getDepartment().getName() << ' ';
cout << '办公室:' << staff.getDepartment().getOffice() << endl;
}
int main() {
Department dept ('人事处', '521');
Staff Zhang ('0789','张三',dept), Li ('0513','李四',dept);
cout << '改换办公室前:' << endl;
showStaff (Zhang);
showStaff (Li);
//人事处办公室由521搬到311
// *******found*******
______
cout << '改换办公室后:' << endl;
showStaff (Zhang);
showStaff (Li);
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程proj3,其中声明IntSet是一个用于表示正整数集合的类。IntSet的成员函数Intersection的功能是求当前集合与另一个集合的交集。请完成成员函数Intersection。在main函数中给出了一组测试数据,此时程序的输出应该是:
求交集前:
1 2 3 5 8 10
2 8 9 11 30 56 67
求交集后:
1 2 3 5 8 10
2 8 9 11 30 56 67
2 8
要求:
补充编制的内容写在“// *******333*******”与“// *******666*******”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Intset.h
#include <iostream>
using namespace std;
const int Max=100;
class IntSet
{
public:
IntSet()
//构造一个空集合
{
end = -1;
}
IntSet (int a[], int size) //构造一个包含数组a中size个元素的集合
{
if (size >= Max)
end = Max - 1;
else
end = size - 1;
for (int i = 0; i <= end; i ++)
element[i] = a[i];
}
bool IsMemberOf (int a)
//判断a是否为集合中的一个元素
{
for (int i = 0; i <= end; i ++)
if (element[i] == a)
return true;
return false;
}
int GetEnd() {return end;}
//返回最后一个元素的下标
int GetElement (int i) {return element[i];}
//返回下标为i的元素
IntSet Intersection (IntSet set);
//求当前集合与集合set的交
void Print ()
//输出集合中的所有元素
{
for(int i=0;i<=end;i++)
if((i+1)% 20==0)
cout << element[i] << endl;
else
cout << element[i] << '';
cout << endl;
}
private:
int element[Max];
int end;
};
void writeToFile (const char *);
//main.cpp
#include 'IntSet.h'
IntSet IntSet::Intersection(IntSet set)
{
int a[Max],size=0;
// *******333*******
// *******666*******
return IntSet(a,size);
}
int main()
{
int a[] = {1,2,3,5,8,10};
int b[] = {2,8,9,11,30,56,67};
IntSet set1 (a, 6), set2 (b, 7), set3;
cout << '求交集前:' << endl;
set1.Print();
set2.Print();
set3.Print();
set3 = set1.Intersection (set2);
cout << endl << '求交集后:' << endl;
set1.Print();
set2.Print();
set3.Print();
writeToFile (' ');
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。sum(int n)计算所有n的因子之和(不包括1和自身)。注意:不能修改程序的其他部分,只能补充sum()函数。
#include <iostream.h>
int sum(int n)
{
}
void main()
{
cout<<sum(10)<<endl;
cout<<sum(200)<<endl;
cout<<sum(400)<<endl;
return;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给myArray。类intArray重载了“=”运算符。程序中给出了一个测试数据文件input,不超过300个的整数。程序的输出是:
10
11
13
16
20
要求:
补充编制的内容写在“// ********333********”与“// ********666********”之间。实现重载赋值运算符函数,并将赋值结果在屏幕输出。格式不限。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//intArray.h
class intArray
{
private:
int * array;
int length;
public:
intArray(char * filename);
intArray();
intArray operator = (const intArray src);
~intArray();
void show();
};
void writeToFile(const char * path);
//main.cpp
#include <iostream>
#include <fstream>
#include <cstring>
#include 'intArray.h'
using namespace std;
intArray::intArray()
{
length = 10;
array = new int[length];
}
intArray::intArray (char * filename)
{
ifstream myFile(filename);
array = new int[300];
length = 0;
while(myFile >> array[length ++])
length --;
myFile.close();
}
intArray intArray::operator = (const intArray src)
{
if (array!= NULL) delete [] array;
length = src.length;
array = new int[length];
// *************333***********
// *************666***********
return * this;
}
intArray::~intArray()
{
delete [] array;
}
void intArray::show()
{
int step=0;
for(int i=0; i<length; i=i+step)
{
cout <<array[i] <<endl;
step ++;
}
}
void main()
{
intArray * arrayP = new intArray ('input.dat');
intArray myArray;
myArray = * arrayP;
(* arrayP).show();
delete arrayP;
writeToFile(' ');
}
应用题
请打开考生文件夹下的解决方案文件proj3,其中包含主程序文件main.cpp和用户定义的头文件Arry.h,整个程序包含有XArray类的定义和main主函数的定义。请把主程序文件中的XArray类的成员函数sum()的定义补充完整,补充的内容填写在'//**********333**********'与'//**********666**********'两行之间。经修改后运行程序,得到的输出为:
10
d=43
注意:只允许在'//**********333**********'和//**********666**********'两行之间填写内容,不允许修改其他任何地方的内容。
//Array.h
#include<iostream>
#include<cstdlib>
using namespace std;
class XArray {//数组类
int *a;
int size;
public:
XArray(int b[],int len):size(len)//构造函数
{
if(size<2){cout<<'参数不合适'<<endl;exit(1);}
a=new int[size];
for(int i=0;i<size;i++)a[i]=b[i];
}
int sum();//返回数组a[size]中的最大值与最小值之和
int length()eonst{return size;}//返回数组长度
~XArray()t delete[]a;}
};
void writeToFile(const char*);//不用考虑此语句的作用
//main.cpp
#include 'Array.h'
//返回数组a[size]中的最大值与最小值之和
int XArray::sum(){//补充函数体
//**********333**********
//**********666**********
}
void main(){
int s1[10]={23,15,19,13,26,33,18,30,20,10};
XArray x(s1,10);
int d=x.sum();
cout<<x.length()<<endl;
cout<<'d='<<d<<endl:
writeToFile('c:\\test\\');//不用考虑此语句的作用
}
