应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,此工程包含有一个源程序文件proj2.cpp。其中定义了Base1类、Base2类和Derived类。
Base1是一个抽象类,其类体中声明了纯虚函数Show。Base2类的构造函数负责动态分配一个字符数组,并将形参指向的字符串复制到该数组中,复制功能要求通过调用strcpy函数来实现。Derived类以公有继承方式继承Base1类,以私有继承方式继承Base2类。在Derived类的构造函数的成员初始化列表中调用Base类的构造函数。
请在程序中的横线处填写适当的代码,然后删除横线,以完成Base1、Base2和Derived类的功能。此程序的正确输出结果应为:
I'm a derived class.
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
// proj2.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Base1 {
public:
// ********found******** 下列语句需要声明纯虚函数Show
______;
};
class Base2 {
protected:
char * _p;
Base2 (const char * s)
{
_p = new char[strlen (s) +i];
// ********found******** 下列语句将形参指向的字符串常量复制到该类的字符数组中
}
~Base2() {delete [] _p;}
};
// ********found******** Derived类公有继承Base1,私有继承Base2类
class Derived:______{
public:
// ********found******** 以下构造函数调用Base2类构造函数
Derived (const char * s):______
{ }
void Show()
{cout << _p << endl;}
};
int main()
{
Base1 * pb = new Derived('I'm a derived class.');
pb -> Show();
delete pb;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开prog3下的工程prog3,其中包含了类TaxCalculator(“个税计算器”)和主函数main的定义。创建“个税计算器”需要接收税率表信息和起征额信息。在main函数中,通过两个数组创建了如下的税率表:
下标
适用收入段下限(lower_limits)
适用税率(rates)
说明:适用的收入段为
0
0
5
大于0小于等于500
1
500
10
大于500小于等于2000
2
2000
15
大于2000小于等于5000
3
5000
20
大于5000小于等于20000
4
20000
25
大于20000小于等于40000
5
40000
30
大于40000小于等于60000
6
60000
35
大于60000小于等于80000
7
80000
40
大于80000小于等于100000
8
100000
45
大于100000
利用这个税率表创建“个税计算器”时,假定起征额为2000元(即不超过2000元的所得不征收个人所得税)。请补充完成计算应纳个人所得税额的成员函数getTaxPayable,其中的参数income为月收入。此程序的正确输出结果应为:
月收入为800元时应缴纳个人所得税0元
月收入为1800元时应缴纳个人所得税0元
月收入为2800元时应缴纳个人所得税55元
月收入为3800元时应缴纳个人所得税155元
月收入为4800元时应缴纳个人所得税295元
月收入为5800元时应缴纳个人所得税455元
注意:只能在函数getTaxPayable中的“// ********333********”和“// ********666********”之间填入若干语句,不要改动程序中的其他内容。
//TaxCalculator.h
#include <iostream>
#include <iomanip>
using namespace std;
class TaxCalculator{
public:
TaxCalculator (double the_limits[], double the_rates[], int the_length, double the_threshold)
: lower_limits (new double[the_length]), rates(new double[the_length]),
list_len (the_length), threshold(the_threshold) {
for(int i = 0; i < list_len; i++){
lower_limits[i] = the_limits[i];
rates[i] = the_rates[i];
}
}
~TaxCalculator() {delete[]lower_limits; delete[] rates;]
double getTaxPayable (double income)const; //返回指定月收入的应纳个人所得税额
void showTaxPayable (double income) const; //显示指定月收入的应纳个人所得税额
private:
double * lower limits; //适用收入段下限
double * rates; //适用税率
int list_len; //税率表项数
double threshold; //起征额
};
void writeToFile(const char * path);
//TaxCalcnlator.cpp
#include 'TaxCalculator.h'
double TaxCalculator::getTaxPayable(double income) const {
double taxable = income-threshold;
//应纳税工资额
double tax_payable = 0.0;
//应纳个人所得税额
int i=list len-1;
while(i >= 0){
// ********333********
// ********666********
--i;
}
return taxpayable;
}
void TaxCalculator::showTaxPayable (double income) const {
cout << '月收入为' << setw(6) << income << '元时应缴纳个人所得税'
<<setw(4) << getTaxPayable (income) << '元' << endl;
}
//main.cpp
#include 'TaxCalculator.h'
int main(){
double limits[] = {0.0, 500.0, 2000.0, 5000.0, 20000.0, 40000.0, 60000.0, 80000.0, 100000.0};
double rates[] = {0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45};
TaxCalculator calc (limits,rates,9,2000.0);
calc.showTaxPayable(800.0);
calc.showTaxPayable(1800.0);
calc.showTaxPayable(2800.0);
calc.showTaxPayable(3800.0);
calc.showTaxPayable(4800.0);
calc.showTaxPayable(5800.0);
writeToFile(' ');
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)完成构造函数,设置数组元素的个数为0,请在注释//********1********之后添加语句。
(2)完成函数AddMember(int n),如果参数n包含在类的数据成员数组中,则返回0,否则把数据写入数组,然后返回1,请在注释//********2********之后添加语句。
(3)完成函数DelMember(int n),如果变量在数据中,则删除该变量,并且返回1,如果不存在则返回0,请在注释//********3********之后添加语句。
(4)完成成员函数isInclude(int n)的定义,该函数检查参数n是否在类的数据成员数组elems中,如果在返回1,否则返回0。请在注释//********4********之后添加语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include<iostream.h>
#define MAX_LENGTH 500
class CArray
{
public:
CArray ()
{
//********1********
}
int AddMember(int n)
{
for (int i=0; i<length; i++)
{
//********2********
if(Array[i] = n)
return 0;
}
Array [length++] = n;
return 1;
}
int DelMember(int n)
{
int i;
for(i=0; i<length; i++)
{
if(Array[i] == n)
{
break;
}
}
//********3********
if()
{
for(; i<length-1; i++)
{
Array[i]=Array [i+1];
}
length--;
return 1;
}
return 0;
}
int isInclude(int n)
{
for (int i=0; i<length; i++)
{
//********4********
if()
{
return 1;
}
}
return 0;
}
private:
int Array [MAX_LENGTH];
int length; //用来记录数组的个数
};
int main ()
{
CArray obj;
obj .AddMember (1);
obj.AddMember (3);
obj.AddMember (8);
obj.AddMember (23);
cout<<obj.is Include (8)<<endl;
cout<<obj, isInclude (11)<<endl;
obj.DelMember (3);
obj.DelMember (8);
cout<<obj.isInclude (2)<<endl;
cout<<obj.isInclude (1)<<endl;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,此工程包含一个源程序文件proj2.cpp。其中定义了Score类。
Score是一个用于管理考试成绩的类。其中,数据成员_s指向存储成绩的数组,_n表示成绩的个数;成员函数Sort使用冒泡排序法将全部成绩按升序进行排列。
请在程序中的横线处填写适当的代码,然后删除横线,以实现Score类的成员函数Sort。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
// proj2.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Score{
public:
Score(double * s, int n) : _s(s), _n(n) {}
double GetScore(int i) const{return_s[i];}
void Sort();
private:
double *_s;
int _n;
};
void Score::Sort()
{
// ********found********
for (int i = 0; i < _n-1; ______)
// ********found********
for (int j =______; j > i; j--)
if (_s[j] < _s[j -i])
{// 交换_s[j]和_s[j-1]
double t = _s[j];
// ********found********
______;
// ********found********
______;
}
}
int main()
{
const int NUM = 10;
double s[NUM];
srand(time(0));
for (int i=0; i<NUM; i++)
s[i] = double (rand())/RAND_MAX * 100;
Score ss(s, NUM);
ss.Sort();
for (int j = 0; j<NUM; j ++)
cout << ss.GetScore(j) << endl;
return 0;
}
应用题
使用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++6.0打开下的源程序文件2.cpp。阅读下列函数说明和代码,完成空出部分的程序。实现函数sort(int A[],int n),用冒泡法对数组数据进行排序。所谓冒泡法,就是每次把相邻的两个数交换,较大的数交换到后面。这样下标从0到n-1的数与其后面相邻的数交换,可以把最大的数交换到数组的末端。进行n次下标从0到n-1的交换,则数组变成有序的,并且按由大到小排序。
注意:不能修改和删除程序的其他部分,也不能修改程序的结构。
试题程序:
#include<iostream.h>
#define N 10
void sort(int A[N],int n)
{
}
int main()
{
int A[N]={1,50,17,69,87,42,90,-6,0,-11);
sort(A,10);
for(int i=0;i<sizeof(A)/sizeof(int);i++)
{
cout<<A[i]<<'';
}
cout<<endl;
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开proj2下的工程proj2,该工程中包含一个程序文件main. epp,其中有日期类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]. compareName(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('李四', true, Date(1973, 5, 30))
};
const int size=sizeof(staff)/sizeof(staff[0]);
int i;
cout<<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;
}
应用题 请使用VC6或使用[答题]菜单打开proj3下的工程proj3,其中声明的CDeepCopy是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数operator=,以实现深层复制。
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out. dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//CDeepCopy. h
#icloude<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;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。此程序的功能是将out1.txt文件中的内容输出到屏幕与文件中。输出如下:
李一 1.78m 21
王一 1.65m 23
out2.txt文件的内容如下:
李一 1.78m 21
王一 1.65m 23
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)以输入方式打开文件out1.txt,请在注释//********1********后添加适当的语句。
(2)以输出方式打开文件out2.txt,请在注释//********2********后添加适当的语句。
(3)从文件中获得一个字符,判断是否结束,如果结束则退出输出。请在注释//********3********后添加适当的语句。
(4)把获得的字符输出到文件中,请在注释//********4********后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
char ch;
fstream infile, outfile;
//********1********
infile.open ('out1.txt');
if(!infile)
{
cout << 'out1.txt文件不能打开'<<endl;
abort();
}
//********2********
outfile.open('out2.txt');
if(!outfile)
{
cout <<'out2.txt文件不能打开' <<endl;
abort();
}
//********3********
while()
{
cout<<ch;
//********4********
}
cout <<endl;
infile.close();
outfile.close();
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp,请编写一个函数int huiwen(int n),用于求解所有不超过200的n值,其中n的平方是具有对称性质的回文数(回文数是指一个数从左向右读与从右向左读是一样的,例如:34543和1234321都是回文数)。求解的基本思想是:首先将n的平方分解成数字保存在数组中,然后将分解后的数字倒过来再组成新的整数,比较该整数是否与n的平方相等。
注意:请勿修改主函数main和其他函数中的任何内容,只在横线处编写适当代码,也不要删除或移动“//****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
int huiwen(int n)
{
int arr[16], sqr, rqs=0, k=1;
sqr =n*n;
for(int i=1; sqr!=0; i++)
{
//******** found********
______;
sqr/ =10;
}
for(;i>1; i--)
{
rqs+ =arr[i-1]* k;
//******** found********
______;
}
//******** found********
if(______)
return n;
else
return 0;
}
int main ()
{
int count =0;
cout <<'The number are: '<<endl;
for(int i=10; i<200; i++)
if(huiwen(i)) cout << ++count << '\t'<<i<<'\t'<<i* i<<endl;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln,其中定义了多个类,但类不能达到输出要求,按照定义的输出修改函数,使得输出如下:
TestClass3
TestClass2
TestClass1
其中定义的类并不完整,按照要求完成下列操作,将类的定义补充完整。
(1)类TestClass2公共继承于TestClass1,请在注释//********1********后添加适当的语句。
(2)print函数为虚函数,请在注释//********2********后添加适当的语句。
(3)修改语句,使得p指定对象obj3,请在注释//********3********后添加适当的语句。
(4)修改语句,利用p调用print()函数,完成输出信息,注释//********4********后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
#include<iostream>
using namespace std;
class TestClass1
{
public:
void print(){
cout<<'TestClasS1'<<endl:
}
};
//********1********
Class TestClass2
{
public:
//********2********
void print()
{
cout<<'TestClass2'<<endl;
}
};
class TestClass3:public TestClass2
{
public:
void print()
{
cout<<'TestClass3'<<endl;
}
};
int main()
{
TestClass2 obj2;
TestClass3 obj3;
TestClass1* p;
obj2.print();
obj3.print();
//********3********
p=obj3;
//********4********
p.print();
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp,请编写一个函数int huiwen(int n),用于求解所有不超过200的n值,其中n的平方是具有对称性质的回文数(回文数是指一个数从左向右读与从右向左读是一样的,例如:34543和1234321都是回文数)。求解的基本思想是:首先将n的平方分解成数字保存在数组中,然后将分解后的数字倒过来再组成新的整数,比较该整数是否与n的平方相等。
注意:请勿修改主函数main和其他函数中的任何内容,只在横线处编写适当代码,也不要删除或移动“//****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
int huiwen(int n)
{
int arr[16], sqr, rqs=0, k=1;
sqr =n*n;
for(int i=1; sqr!=0; i++)
{
//******** found********
______;
sqr/ =10;
}
for(;i>1; i--)
{
rqs+ =arr[i-1]* k;
//******** found********
______;
}
//******** found********
if(______)
return n;
else
return 0;
}
int main ()
{
int count =0;
cout <<'The number are: '<<endl;
for(int i=10; i<200; i++)
if(huiwen(i)) cout << ++count << '\t'<<i<<'\t'<<i* i<<endl;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开prog3下的工程prog3,其中包含了类Polynomial(“多项式”)的定义。
形如5x4+3.4x2-7x+2的代数式称为多项式,其中的5为4次项系数,3.4为2次项系数,-7为1次项系数,2为0次项(常数项)系数。此例缺3次项,意味着3次项系数为0,即省略了0x3。在Polynomial中,多项式的各个系数存储在一个名为coef的数组中。例如,对于上面的多项式,保存在coef[0]、coef[1]...coef[4]中的系数依次为:2.0、-7.0、3.4、0.0、5.0,也即对于i次项,其系数就保存在coef[i]中。成员函数getValue计算多项式的值,多项式中x的值是由参数指定的。
请补充完成文件:Polynomial.cpp中成员函数getValue的定义。此程序的正确输出结果应为:
Value of p1 when x=2.0:59.8
Value of p2 when x=3.0:226.8
注意:只在函数getValue的“// *******333*******”和“// *******666*******”之间填入若干语句,不要改动程序中的其他内容。
//Polynomiac.h
#include <iostream>
using namespace std;
class Polynomial{ //“多项式”类
public:
Polynomial (double coef[], int hum):coef (new double[num]), num_of_terms(num) {
for (int i = 0; i < num_of_terms; i ++)
this -> coef[i] = coef[i];
}
~Polynomial() {delete[] coef;}
//返回指定次数项的系数
double getCoefficient (int power) const {return coef[power];}
//返回在x等于指定值时多项式的值
double getValue (double x) const;
private:
//系数数组,coef[0]为0次项(常数项)系数,coef[1]为1次项系数,coef[2]为2次项(平方项)系数,余类推。
double * coef;
int num_of_terms;
};
void writeToFile(const char * path);
// Polymomial.cpp
#include 'Polynomial.h'
double Polynomial::getValue (double x) const {
//多项式的值value为各次项的累加和
double value = coef[0];
// ********333********
// ********666********
return value;
}
//main.cpp
#include 'Polynomial.h'
int main() {
double p1[] = {5.0, 3.4, -4.0, 8.0}, p2[] = {0.0, -5.4, 0.0, 3.0, 2.0};
Polynomial poly1 (p1, sizeof(p1)/sizeof (double)), poly2 (p2,sizeof(p2)/sizeof (double));
cout << 'Value of p1 when x = 2.0:' << poly1.getValue(2.0) << endl;
cout << 'Value of p2 when x = 3.0:' << poly2.getValue(3.0) << endl;
writeToFile(' ');
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类MyPoint和表示矩形的类MyRectangle;程序应当显示:
(0,2)(2,2)(2,0)(0,0)4
但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:
(1)在“// **1** ****found****”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对up_left和down_right进行初始化。
(2)在“// **2** ****found****”的下方是成员函数getDownLeft的定义中的一条语句。函数getDownLeft返回用MyPoint对象表示的矩形的左下角。
(3)在“// **3** ****found****”的下方是成员函数area的定义,它返回矩形的面积。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found*****”。
// proj3.cpp
#include <iostream>
using namespace std;
class MyPoint{ //表示平面坐标系中的点的类
double x;
double y;
public:
MyPoint (double x, double y) {this->x=x; this->y=y;}
double getX() const {return x;}
double getY() const {return y;}
void show () const {cout << '(' <<x <<',' <<y<<')' ;}
};
class MyRectangle //表示矩形的类
MyPoint up_left; //矩形的左上角顶点
MyPoint downright; //矩形的右下角顶点
public:
MyRectangle (MyPoint upleft, MyPoint downright);
MyPoint getUpLeft() const {return up_left;} //返回左上角坐标
MyPoint getDownRight() const {return down_right;} //返回右下角坐标
MyPoint getUpRight() const; //返回右上角坐标
MyPoint getDownLeft() const; //返回左下角坐标
double area() const; //返回矩形的面积
};
// **1** *******found*********
MyRectangle::MyRectangle (______):
up_left (p1), down_right (p2) {}
MyPoint MyRectangle::getUpRight()const
{
return MyPoint (down_right.getX(), up_left.getY());
}
MyPoint MyRectangle::getDownLeft()const
{
// **2** *******found*******
return MyPoint(______);
}
// **3** *******found*******
______ area() const
{
return (getUpLeft().getX()-getDownRight().getX ())*(getDownRight().getY()-getUpLeft().getY());
}
int main()
{
MyRectangle r (MyPoint (0, 2), MyPoint(2,0));
r.getUpLeft().show();
r.getUpRight().show();
r.getDownRight().show();
r.getDownLeft().show();
cout <<r.area() <<endl;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)对文件以追加的方式打开文件。请在注释//********1********后添加适当的语句。
(2)定义m、n为类TestClass的公有int型数据成员,请在注释//********2********后添加适当的语句。
(3)定义p为类TestClass的数据成员指针,并指向类TestClass数据成员m,请在注释//********3********后添加适当的语句。
(4)定义p指向类TestClass数据成员n,请在注释//********4********后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include<iostream.h>
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;
void WriteFile(int x)
{
ofstream out1;
//********1********
out1.open('modi3.txt',);
out1<<x<<' ';
out1.close();
}
void ClearFiie()
{
ofstream out1;
out1.open('modi3.txt');
out1.close();
}
class TestClass
{
public:
void disp()
{
cout<<'m='<<m<<endl;
WriteFile(m);
cout<<'n='<<n<<endl:
WriteFile(n);
}
//********2********
};
void main()
{
//********3********
ClearFile();
TestClass a;
a.*p=30;
//********4********
a.*p=45;
a.disp();
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类MyPoint和表示矩形的类MyRectangle;程序应当显示:
(0,2)(2,2)(2,0)(0,0)4
但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:
(1)在“// **1** ****found****”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对up_left和down_right进行初始化。
(2)在“// **2** ****found****”的下方是成员函数getDownLeft的定义中的一条语句。函数getDownLeft返回用MyPoint对象表示的矩形的左下角。
(3)在“// **3** ****found****”的下方是成员函数area的定义,它返回矩形的面积。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found*****”。
// proj3.cpp
#include <iostream>
using namespace std;
class MyPoint{ //表示平面坐标系中的点的类
double x;
double y;
public:
MyPoint (double x, double y) {this->x=x; this->y=y;}
double getX() const {return x;}
double getY() const {return y;}
void show () const {cout << '(' <<x <<',' <<y<<')' ;}
};
class MyRectangle //表示矩形的类
MyPoint up_left; //矩形的左上角顶点
MyPoint downright; //矩形的右下角顶点
public:
MyRectangle (MyPoint upleft, MyPoint downright);
MyPoint getUpLeft() const {return up_left;} //返回左上角坐标
MyPoint getDownRight() const {return down_right;} //返回右下角坐标
MyPoint getUpRight() const; //返回右上角坐标
MyPoint getDownLeft() const; //返回左下角坐标
double area() const; //返回矩形的面积
};
// **1** *******found*********
MyRectangle::MyRectangle (______):
up_left (p1), down_right (p2) {}
MyPoint MyRectangle::getUpRight()const
{
return MyPoint (down_right.getX(), up_left.getY());
}
MyPoint MyRectangle::getDownLeft()const
{
// **2** *******found*******
return MyPoint(______);
}
// **3** *******found*******
______ area() const
{
return (getUpLeft().getX()-getDownRight().getX ())*(getDownRight().getY()-getUpLeft().getY());
}
int main()
{
MyRectangle r (MyPoint (0, 2), MyPoint(2,0));
r.getUpLeft().show();
r.getUpRight().show();
r.getDownRight().show();
r.getDownLeft().show();
cout <<r.area() <<endl;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换教室前:
学号:0789 姓名:张三 班级:062113 教室:521
学号:0513 姓名:李四 班级:062113 教室:521
改换教室后:
学号:0789 姓名:张三 班级:062113 教室:311
学号:0513 姓名:李四 班级:062113 教室:311
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Class{ //“班级”类
public:
Class(const char * id, const char * room) {
strcpy(class_id, id);
// *******found*******
______
}
const char * getClassID() const {return class_id;} //返回班号
// *******found*******
const char * getClassroom() const {______} //返回所在教室房号
void changeRoomTo(const char * new_room) { //改换到另一个指定房号的教室
strcpy (classroom, new_room);
}
private:
char class_id[20]; //班号
char classroom[20]; //所在教室房号
};
class Student{ //“学生”类
char my_id[10]; //学号
char my_name[20]; //姓名
Class my_class; //所在教室
public:
// **********found**********
Student (const char * the_id, const char * the_name, Class the_class):______{
strcpy (my_id, the_id);
strcpy (my_name, the_name);
}
const char * getID() const {return my_id;}
const char * getName() const {return my_name;}
Class getClass() const {return my_class;}
};
void showStudent (Student * stu) {
cout << '学号:' << stu -> getID() << ' ';
cout << '姓名:' << stu -> getName() << ' ';
cout << '班级:' << stu -> getClass().getClassID() << ' ';
cout << '教室:' << stu -> getClass().getClassroom() << endl;
}
int main() {
Class cla('062113','521');
Student Zhang ('0789', '张三', cla), Li('0513', '李四', cla);
cout << '改换教室前:' << endl;
showStudent (Zhang);
showStudent (Li);
//062113班的教室由521改换到311
// **********found**********
cout << '改换教室后:' << endl;
showStudent (Zhang);
showStudent (Li);
return 0;
}
应用题
请打开考生文件夹下的解决方案文件proj3,此工程中包含一个源程序文件proj3.cpp,补充编制C++程序proj3.cpp,其功能是读取文本文件in.dat中的全部内容,将文本存放到doc类的对象myDoc中。然后将myDoc中的字符序列反转,并输出到文件out.dat中。文件in.dat的长度不大于1000字节。
要求:
补充编制的内容写在“//**********333**********”与“//**********66666**********”两行之间。实现将myDoc中的字符序列反转,并将反转后的序列在屏幕上输出。不得修改程序的其他部分。
注意:程序最后已将结果输出到文件out.dat中,输出函数writeToFile已经给出并且调用。
//proj3.cpp
#include<iostream>
#include<fstream>
#include<estring>
using namespace std;
class doc
{
private:
char *str;//文本字符串首地址
int length;//文本字符个数
public:
//构造函数,读取文件内容,用于初始化新对象,filename是文件名字符串首地址
doc(char *filename);
void reverse();//将字符序列反转
~doc();
void writeToFile(char *filename):
};
doc::doc(char *filename)
{
ifstream myFile(filename);
int len=1001,tmp;
str=new char[len];
length=0;
while((tmp=myFile.get())!=EOF)
{
str[length++]=tmp;
}
str[length]='\0';
myFile.close();
}
void doc::reverse(){
//将数组str中的length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个
//字符交换……
//*************333*************
//*************666*************
}
doc::~doc()
{
delete[]str;
}
void doc::writeToFile(char *filename)
{
ofstream outFile(filename);
outFile<<str;
outFile.close();
}
void main()
{
doc myDoc('in.dat');
myDoc.reverse();
myDoc.writeToFile('out.dat');
}
应用题 请使用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;
}
应用题
使用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;
}
