操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。程序中位于每个“//ERROR ****found****”下一行的语句有错误,请加以改正。改正后程序的输出结果应为:
Thp value is 5
The value is 10
There are 2 objects.
There are 1 objects.
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int value)
{
// ERROR ********** found**********
this.value = value;
count ++;
}
// ERROR ********** found**********
void ~MyClass()
{
count--;
}
static int getCount () { return count; }
int getValue() { return value; }
private:
int value;
static int count;
};
// ERROR ********** found**********
static int MyClass::count = 0;
int main()
{
MyClass* p = new MyClass(5);
MyClass* q = newMyClass(10);
cout << 'The value is' << p -> getValue() << endl;
cout << 'The value is' << q -> getValue() << endl;
cout << 'There are' << MyClass::getCount() << ' objects.' << endl;
delete p;
cout << 'There are' <' MyClass::getCount() << 'objects.' << endl;
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的CDeepCopy是一个用于表示动态数组的类。请编写其中的复制构造函数。
要求:
补充编制的内容写在“//********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(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 ( 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 ('');
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,此工程中包含程序文件main.cpp,其中有ElectricFan(“电风扇”)类和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
品牌:清风牌,电源:关,风速:0
品牌:清风牌,电源:开,风速:3
品牌:清风牌,电源:关,风速:0
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class ElectricFan { //“电扇”类
char * brand;
int intensity; //风速:0-关机,1-弱,2-中,3-强
public:
ElectricFan(const char * the_brand): intensity(0) {
brand = new char[strlen (the_brand) +1];
strcpy(brand, the brand);
}
~ElectricFan() {delete[] brand;}
// ERROR *******found*******
const char * theBrand() const {return * brand;} //返回电扇品牌
int theIntensity() const {return intensity;}
//返回风速
bool isOn() const {return intensity>0;}
//返回电源开关状态
// ERROR *******found*******
void turnOff() {intensity=1;} //关电扇
void setIntensity (int inten) {
//开电扇并设置风速
// ERROR *******found*******
if (intensity >= 1 intensity <= 3)
intensity = inten;
}
void show() {
cout << '品牌:' << theBrand() << '牌'
<< ',电源:' << (isOn())? '开':'关')
<< ',风速:' << theIntensity() << endl;
}
};
int main() {
ElectricFan fan ('清风');
fan.show();
fan.setIntensity(3);
fan.show();
fan.turnOff();
fan.show();
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The value is:10
注意:只修改注释“// ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class Member {
// ERROR *******found*******
private:
Member(int val):value(val) {}
int value;
};
class MyClass {
Member _m;
public:
// ERROR *******found*******
MyClass(int val) {}
int GetValue() const {return _m.value;}
};
int main()
{
MyClass *obj = new MyClass (10);
// ERROR *******found*******
下面语句输出obj指向类中的value值
cout << 'The value is:' << obj.GetValue() << endl;
delete obj;
return 0;
}
操作题请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件proj3。此工程中包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类MyPoint和表示三角形的类MyTriangle;程序应当显示:6.828432但程序中有缺少部分,请按照以下提示,把缺失部分补充完整:(1)在“//**1******found****”的下方是构造函数的定义,它用参数提供的3个顶点对point1、point2和point3进行初始化。(2)在“//**2******found****”的下方是成员函数perimeter的定义,该函数返回三角形的周长。(3)在“//**3******found****”的下方是成员函数area的定义中的一条语句。函数area返回三角形的面积。方法是:若a、b、c为三角形的3个边长,并令s=,则三角形的面积A为。注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。//proj3.cpp#include<iostream>#include<cmath>usingnamespacestd;classMyPoint{//表示平面坐标系中的点的类doublex;doubley;public:MyPoint(doublex,doubley){this->x=x;this->y=y;}doublegetX()const{returnx;}doublegetY()const{returny;}voidshow()const{cout<<'('<<x<<','<<y<<')';}};classMyTriangle{//表示三角形的类MyPointpoint1;//三角形的第一个顶点MyPointpoint2;//三角形的第二个顶点MyPointpoint3;//三角形的第三个顶点public:MyTriangle(MyPointp1,MyPointp2,MyPointp3);doubleperimeter()const;//返回三角形的周长doublearea()const;//返回三角形的面积};//**1************found**********MyTriangle::MyTriangle(MyPointp1,MyPointp2,MyPointp3):______{}doubledistance(MyPointp1,MyPointp2)//返回两点之间的距离{returnsqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));}//**2************found**********doubleMyTriangle______{returndistance(point1,point2)+distance(point2,point3)+distance(point3,point1);}doubleMyTriangle::area()const{//**3************found**********doubles=______;//使用perimeter函数returnsqrt(s*(s-distance(point1,point2))*(s-distance(point2,point3))*(s-distance(point3,point1)));}intmain(){MyTriangletri(MyPoint(0,2),MyPoint(2,0),MyPoint(0,0));cout<<tri.perimeter()<<endl<<tri.area()<<endl;return0;}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。此工程中包含源程序文件main.cpp,其中有类TVSet(“电视机”)和主函数main的定义。程序中位于每个“//ERROR ********found********”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
规格:29英寸,电源:开,频道:5,音量:18
规格:29英寸,电源:关,频道:-1,音量:-1
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace Std;
class TVSet { //'电视机'类
const int size;
int channel; //频道
int volume; //音量
bool on; //电源开关:true表示开,false表示关
public:
TVSet (int size): size (size), channel(0), on(false)
// ERROR ********* found*********
{}
int getSize () const { return size; }
//返回电视机规格
bool isOn()const{ return on;} //返回电源开关状态
//返回当前音量,关机情况下返回-1
int getVolume () const
{ return isOn()? volume : -1;}
//返回当前频道,关机情况下返回-1
int getChannel () const
{ return isOn()? channel: -1; }
void turnOnOff() { on=! on;} //将电源在“开”和“关”之间转换
void setChannelTo (int chan) { //设置频道(关机情况下无效)
if(isOn() chan >=0 chan< =99)
// ERROR ********* found*********
;
}
// ERROR ********* found*********
void setVolumeTo (int vol)const { //设置音量(关机情况下无效)
if(isOn() vol>=0 vol <=20)
volume = vol;
}
void show_state() {
cout << '规格:' <<getSize() << '英寸'<< ',电源:' << (isOn ()? '开' : '关')<< ',频道:' << getChannel ()<< ',音量:' << getVolume () << endl;
}
};
int main() {
TVSet tv (29);
tv.turnOnOff ();
tv. setChannelTo (5);
tv.show_state();
tv.turnOnOff ();
tv.show_state();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。此工程定义了StopWatch(秒表)类,用于表示时、分、秒信息,有构造函数StopWatch()、设置时间函数reset(),并且重载了前置和后置++运算符,实现增加1秒的功能。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
00:00:00
00:01:00
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <iomanip>
using namespace std;
class StopWatch //'秒表'类
{
int hours; //小时
int minutes; //分钟
int seconds; //秒
public:
StopWatch(): hours(0), minutes (0), seconds(0){}
void reset () {hours =minutes =seconds =0;}
StopWatch operator++(int) //后置++
{
StopWatch old=* this;
++ (* this);
return old;
}
//前进1秒
StopWatch operator ++ () //前置++
{
//ERROR ********* found*********
if(seconds ++ ==60)
{
seconds =0; minutes ++;
if(minutes ==60)
{
minutes =0;
hours ++;
}
}
// ERROR ********* found*********
return this;
}
friend void show(StopWatch);
};
void show (StopWatch watch)
{
cout << setfill ('0');
cout << setw(2) <<watch.hours <<':'
<< setw (2) << watch.minutes << ':'
<< setw (2) << watch.seconds << endl;
}
int main ()
{
StopWatch sw;
show (sw);
for (int i=0; i<59; i++) sw++;
// ERROR ********* found*********
show (sw++);
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,该工程含有一个源程序文件proj1.cpp。其中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The value is 10
注意:只修改注释“// ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
int value;
public:
// ERROR ******found******
void MyClass(int val) : value(val) {}
int GetValue() const {return value;}
void SetValue(int val);
};
// ERROR ******found******
inline void SetValue (int val)
{value = val;}
int main()
{
MyClass obj(0);
obj.SetValue(10);
// ERROR ********found******* 下列语句功能是输出obj的成员value的值
cout << 'The value is' << obj.value << endl;
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,其中有矩形类Rectangle、函数show和主函数main的定义。程序中位于每个“//ERROR ****found****”下一行的语句有错误,请加以改正。改正后程序的输出结果应该是:
Upper left=(1,8),down right=(5,2),area=24.
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cmath>
using namespace std;
class Rectangle{
double x1, y1; //左上角坐标
double x2, y2; //右下角坐标
public:
// ERROR ********** found**********
Rectangle (double x1, y1; double x2, y2){
this ->x1=x1;
this ->y1=y1;
this ->x2=x2;
this ->y2=y2;
}
double getX1 ()const{ return x1; }
double getY1 ()const{ return y1; }
double getX2 ()const{ return x2; }
double getY2 ()const{ return y2; }
double getHeight () const { return fabs(y1-y2);}
double getWidth () const { return fabs(x1-x2);}
double area () const { return getHeight()* getWidth(); }
};
// ERROR ********** found**********
void show (Rectangle r) const{
cout <<'Upper left = (';
// ERROR ********** found**********
cout << r.x1 <<', ' << r.y1 <<'), down right = (' <<r.x2 <<', ' <<r.y2;
cout << '), area =' << r. area () << '. ' << endl;
}
int main(){
Rectangle r1 (1, 8, 5, 2);
show(r1);
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。此工程中包含源程序文件main.cpp,其中有类TVSet(“电视机”)和主函数main的定义。程序中位于每个“//ERROR ********found********”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
规格:29英寸,电源:开,频道:5,音量:18
规格:29英寸,电源:关,频道:-1,音量:-1
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace Std;
class TVSet { //'电视机'类
const int size;
int channel; //频道
int volume; //音量
bool on; //电源开关:true表示开,false表示关
public:
TVSet (int size): size (size), channel(0), on(false)
// ERROR ********* found*********
{}
int getSize () const { return size; }
//返回电视机规格
bool isOn()const{ return on;} //返回电源开关状态
//返回当前音量,关机情况下返回-1
int getVolume () const
{ return isOn()? volume : -1;}
//返回当前频道,关机情况下返回-1
int getChannel () const
{ return isOn()? channel: -1; }
void turnOnOff() { on=! on;} //将电源在“开”和“关”之间转换
void setChannelTo (int chan) { //设置频道(关机情况下无效)
if(isOn() chan >=0 chan< =99)
// ERROR ********* found*********
;
}
// ERROR ********* found*********
void setVolumeTo (int vol)const { //设置音量(关机情况下无效)
if(isOn() vol>=0 vol <=20)
volume = vol;
}
void show_state() {
cout << '规格:' <<getSize() << '英寸'<< ',电源:' << (isOn ()? '开' : '关')<< ',频道:' << getChannel ()<< ',音量:' << getVolume () << endl;
}
};
int main() {
TVSet tv (29);
tv.turnOnOff ();
tv. setChannelTo (5);
tv.show_state();
tv.turnOnOff ();
tv.show_state();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//**********found**********
vehicle (int maxspeed,intweight):______
~vehicle(){};
int getMaxSpeed(){return MaxSpeed;}
int getWeight(){return Weight;}
};
//**********found**********
class bicycle:______public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}
int getHeight(){return Height;};
};
//**********found**********
class motorcar:______public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed,int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}
int getSeatNum(){return SeatNum;};
};
//**********found**********
class motorcycle:______
{
public:
motorcycle(int maxspeed,intweight,int height):vehicle (maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}
};
void main ()
{
motorcycle a(80,150,100);
cout<<a.getMaxSpeed()<<endl;
cout<<a.getWeight()<<endl;
cout<<a.getHeight()<<endl;
cout<<a.getSeatNum()<<endl;
}
操作题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的Matrix是一个用于表示矩阵的类。其成员函数transpose的功能是实现矩阵的转置运算。将矩阵A的行列互换,所得到的矩阵称为A的转置,记做AT。例如,若有3×3矩阵则A的转置为请编写成员函数transpose,以实现矩阵转置功能。要求:补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Matrix.h#include<iostream>#include<iomanip>usingnamespacestd;constintM=18;constintN=18;classMatrix{intarray[M][N];public:Matrix(){}intgetElement(inti,intj)const{returnarray[i][j];}voidsetElement(inti,intj,intvalue){array[i][j]=value;}voidtranspose();voidshow(constchar*s)const{cout<<endl<<s;for(inti=0;i<M;i++){cout<<endl;for(intj=0;j<N;j++)cout<<setw(4)<<array[i][j];}}};voidreadFromFile(constchar*,Matrix);voidwriteToFile(char*,constMatrix);//main.cpp#include<fstream>#include'Matrix.h'voidreadFromFile(constchar*filename,Matrixm){ifstreaminfile(filename);if(!infile){cerr<<'无法读取输入数据文件!\n';return;}intd;for(inti=0;i<M;i++)for(intj=0;j<N;j++){infile>>d;m.setElement(i,j,d);}}voidMatrix::transpose(){//********333********//********666********}intmain(){Matrixm;readFromFile('',m);m.show('Beforetranspose:');m.transpose();m.show('Aftertranspose:');writeToFile('',m);return0;}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Initial times are
0 d:0 h:0 m:59 s
After one second times are
0 d:0 h:1 m:0 S
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Clock
{
public:
Clock(unsigned long i = 0);
void set(unsigned long i = 0);
void print() const;
void tick(); //时间前进一秒
Clock operator ++();
private:
unsigned long total_sec, seconds, minutes, hours, days;
};
Clock::Clock(unsigned long i)
: total_sec(i), seconds(i % 60),
minutes((i/60) % 60),
hours((i/3600) % 24),
days(i/86400) {}
void Clock::set (unsigned long i)
{
total_sec = i;
seconds = i % 60;
minutes = (i/60) % 60;
hours = (i/3600) % 60;
days = i/86400;
}
// ERROR *******found*******
void Clock::print()
{
cout << days << 'd:' << hours << 'h:'
<< minutes << 'm:' << seconds << 's' << endl;
}
void Clock::tick ()
{
// ERROR *******found*******
set (total_sec ++);
}
Clock Clock::operator ++()
{
tick();
// ERROR *******found*******
return this;
}
int main()
{
Clock ck(59);
cout << 'Initial times are' << endl;
ck.print();
++ ck;
cout << 'After one second times are' << endl;
ck.print();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件proj3,其中该工程中包含定义了用于表示姓名的抽象类Name、表示“先名后姓”的姓名类Name1(名、姓之间用空格隔开)和表示“先姓后名”的姓名类Name2(姓、名之间用逗号隔开);程序应当显示:
John Smith
Smith, John
但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:
(1)在“//**1** ****found****”的下方是函数show中的一个语句,它按先名后姓的格式输出姓名。
(2)在“//**2** ****found****”的下方是函数getWord中的一个语句,它把一个字符序列复制到head所指向的字符空间中,复制从start所指向的字符开始,共复制endstart个字符。
(3)在“//**3** ****found****”的下方是函数createName中的语句,它根据指针p的值决定返回何种对象:如果p为空,直接返回一个Name1对象,否则直接返回一个Name2对象。注意:返回的Name1或Name2对象必须是动态对象,返回的实际是指向它的指针。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。填写的内容必须在一行中完成,否则评分将产生错误。
//proj3.cpp
#include <iostream>
using namespace std;
class Name{
protected:
char * surname; //姓
char * firstname; //名
public:
~Name(){ delete[] surname; delete[] firstname; }
virtual void show() =0;
};
class Name1:public Name{
public:
Name1(const char * name);
//**1** **********found**********
void show() {______;}
};
class Name2:public Name {
public:
Name2 (const char * name);
void show ()
{ cout << surname <<',' <<firstname; }
};
char * getWord (const char * start, const char * end)
{
char * head = new char [ end - start + 1];
//**2** **********found**********
for (int i = 0; i<end - start; i ++) ______;
head[ end - start] ='\0';
return head;
}
Name1::Name1 (const char * name)
{
char * p=strchr(name,');
firstname = getWord (name, p);
surname =new char[ strlen (p)];
strcpy (surname, p+1);
}
Name2::Name2 (const char * name)
{
char * p =strchr (name,',');
surname =getWord (name, p);
firstname =new char[ strlen (p) ];
strcpy (firstname, p+1 );
}
Name * createName(const char * s)
{
char * p=strchr (s,',');
//**3** ********** found**********
if (p) ______;
}
int main ()
{
Name * n;
n = createName ('John Smith');
n->show(); cout <<endl;
delete n;
n = createName ('Smith, John');
n->show(); cout <<endl;
delete n;
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;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。完成fun()函数,其功能是:求出M行N列二维数组每行元素中的最小值,并计算它们的和值。和值通过形参传回主函数输出。注意:不能修改程序的其他部分,只能修改fun()函数。
#include<iostream.h>
#define M 2
#define N 4
void fun(int a[M][N],int *sum)
{
}
void main()
{
int X[M][N]={7,6,5,2,4,2,8,3);
int s;
fun(x,s);
cout<<s<<endl;
return;
}
应用题 使用VC++6.0打开下的源程序文件2.cpp。请完成以下两个函数。
(1)fun1(int n)求出n的阶乘,必须使用递归调用。
(2)fun2(int n)求出n的阶乘,不能使用递归调用。如果n小于1则返回0。
注意:不能修改函数的其他部分。
试题程序:
#include<iostream.h>
//必须使用递归
int funl(int n)
{
}
//不能使用递归
int fun2(int n)
{
}
void main()
{
int i;
cout<<'请输入一个整数:'<<endl;
cin>>i;
cout<<'输入数字的阶乘是:'<<fun1(i)<<endl;
cout<<'输入数字的阶乘是:'<<fun2(i)<<endl;
return;
}
应用题
使用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或使用【答题】菜单打开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++6.0打开下的源程序文件2.cpp。完成fun函数,使其功能为将两个按小到大排序的数组a和b,复制合并成一个有序整数序列c,其中形参n和m分别是数组a和b的元素个数。
注意:不能修改程序的其他部分,只能修改fun函数。
试题程序:
#include<iostream.h>
void fun(int a[], int n, int b[], int m, int*c)
{
}
void main()
{
int A[]={3, 5, 7, 9, 11, 18, 21};
int B[]={6, 15, 19, 21, 39);
int C[25], i;
for(i=0; i<25; i++)C[i]=0;
cout<<'A[]=';
for(i=0; i<7; i++)
cout<<A[i]<<',';
cout<<endl;
cout<<'B[]=';
for(i=0; i<5; i++)
cout<<B[i]<<',';
cout<<endl;
fun(A, 7, B, 5, C);
cout<<'C[]=';
for(i=0; i<12; i++)
cout<<C[i]<<',';
cout<<endl;
return;
}
