计算机二级
请使用“答题”菜单或使用VC6菜单打开考生文件夹proj2下的工程 proj2。此工程包含一个程序文件 main.cpp,其中有日期类Date、人员 类Person以及排序函数sortByAge和主函数main的定义。请在程序中的横 线处填写适当的代码,然后删除横线,以实现该程序。此程序的正确 输出结果应为:
排序前:
张三 男 出生日期:1978年4月20日
王五 女 出生日期:1965年8月3日
杨六 女 出生日期:1965年9月5日
李四 男 出生日期:1973年5月30日
排序后:
张三 男 出生日期:1978年4月20日
李四 男 出生日期:1973年5月30日
杨六 女 出生日期:1965年9月5日
王五 女 出生日期:1965年8月3日
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“//**********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, _________);
}
const char * getName()const{ return name; }
bool isMale()const{ return is_male; }
Date getBirthdate()const{ return birth_date; }
int compareAge(const Person &p)const{
//比较两个人的年龄,返回正数、0或负数,分别表示大于、等于和小于
int n;
n = p.birth_data.getYear() - birth_data.getYear();
if( n != 0 ) return n ;
//**********found**********
___________________________
if( n != 0 ) return n ;
return p.birth_data.getDay() - birth_data.getDay();
}
void show(){
cout<<endl;
cout<<name<<' '; //显示姓名
//**********found**********
<< ___________________ //显示性别(“男”或“女”,双引号内不含空格)
<<"出生日期" //显示出生日期
<<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){
//**********found**********
Person p = ______________________;
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("李四", 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;
return 0;
}请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程文件proj3,其中声明了CDeepCopy类,它是一个用于表示动态数组的类。请编写其中的复制构造函数。
要求:
补充编制的内容写在//********333********与//********666********两行之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
部分源程序如下:
/**********code.c**********/
# include "CDeepCopy.h"
CDeepCopy :: ~CDeepCopy() { delete[] p ;}
CDeepCopy:: CDeepCopy(int k) { n = k; p = new int[n]; } //构造函数实现
CDeepCopy:: CDeepCopy(const CDeepCopy& r) //复制构造函数
{
/*******333*******/
/*******666*******/
}
int main()
{
CDeepCopy a(2),d(3);
a.p[0] = 1; d.p[0] = 666 //对象 a,d 数组元素的赋值
{
CDeepCopy b(a);
a.p[0] = 88 ;
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("k:\k01\61010001\");
return 0
}
结果文件 out.dat 内容如下:
5222 d fade away:
66
/**********code.c**********/请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程文件proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数 add将一个元素添加到集合中,成员函数remove从集合中删除指定的元素(如果集合中存在该元素),成员函数 filter去除集合中的所有负整数。请编写这个filter函数。此程序的正确输出结果应为:
5 28 2 -4 5 3 2 -75 27 66 31
5 28 2 -4 5 3 2 -75 27 66 31 6
5 28 2 -4 5 3 2 -75 27 66 31 6 -19
5 28 2 -4 5 3 -75 27 66 31 6 -19 4
5 28 2 -4 5 3 -75 27 66 31 6 -19 4
5 28 2 5 3 27 66 31 6 4
要求:
补充编制的内容写在//********333********与//********666******** 两行之间。不得修改程序的其他部分。
注意:相关文件包括:main.cpp、Integers.h。
程序最后已经调用WriteToFile函数,使用另一组不同的测试数据, 将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译 为obj文件。
部分源程序如下:
/**********code.c**********/
# include "Integers.h"
# include <iomanip>
Integers::Integers(int data[], int size): counter(0){
for(int i=0; i<size; i++) add(data[i]);
}
void Integers::add(int element){
if(counter<MAXELEMENTS) elemK[counter++] = element;
}
void Integers::remove(int element){
int j;
for(j = counter - 1; j >= 0; j --)
if(elem[j] == element) break;
for(int i = j; i<counter - 1; i++) elem[i] = elem[i+1];
counter -- ;
}
void Integers::filter(){
//**********333**********
//**********666**********
}
void Integers::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};
Integers 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();
s.filter();
s.show();
writeToFile("k:\k01\61010001\");
return 0;
}
/**********code.c**********/结果文件out.dat内容如下:
0,27,-2,9,67,62 ,
若变量a的定义为“int a=8;”,则下列逻辑表达式中其值为false的 是( )