操作题 请使用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或使用【答题】菜单打开prog1下的工程prog1。此工程中包含程序文件main.cpp,其中有类Score(“成绩”)和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
学号:12345678 课程:英语 总评成绩:85
注意:只修改每个“// ERROR ****found****”下的一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Score{
public:
Score (const char * the_course, const char * the_id, int the_normal, int the_midterm, int the_end_of_term}
: course(the_course), normal(the_normal), midterm(the_midterm), end_of_term(the_end_of_term) {
// ERROR *******found*******
strcpy (the_id, student_id);
}
const char * getCourse() const {return course;} //返回课程名称
// ERROR *******found*******
const char * getID() const {return student_id;} //返回学号
int getNormal() const {return normal;} //返回平时成绩
int getMidterm() const {return midterm;}
//返回期中考试成绩
int getEndOfTerm() const {return end_of_term;} //返回期考试成绩
int getFinal() const; //返回总评成绩
private:
const char * course; //课程名称
char student_id[12]; //学号
int normal; //平时成绩
int midterm; //期中考试成绩
int end_of_term; //期考试成绩
};
//总评成绩中平时成绩占20%,期中考试占30%,期考试占50%,最后结果四舍五入为一个整数
// ERROR *******found*******
int getFinal() const {
return normal * 0.2 + midterm * 0.3 + end_of_terms 0.5 + 0.5;
}
int main() {
char English[] = '英语';
Score score (English, '12345678',68,83,92);
cout << '学号:' << score.getID() << ' ';
cout << '课程:' << score.getCourse() << ' ';
cout << '总评成绩:' << score.getFinal() << endl;
return 0;
}
操作题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了Shape类和Point类。Shape类表示抽象的形状,其成员函数draw声明了显示形状的接口。Point是Shape的派生类,表示平面直角坐标系中的点,其成员函数draw用于在屏幕上显示Point对象;成员函数distance用于计算两个点之间的距离。提示:在平面直角坐标系中,点(x1,y2)和点(x2,y2)之间的距离为:d=;标准库函数spn用于求平方根。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:(3,0)(0,4)Distance=5注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。#include<iostream>#include<math.h>usingnamespacestd;classShape{public://**********found**********______virtual~Shape(){}};classPoint:publicShape{public:Point(doublex,doubley):x_(x),y_(y){}virtualvoiddraw()const;//**********found**********doubledistance(______)const{returnsqrt((x_-pt.x_)*(x_-pt.x_)+(y_-pt.y_)*(y_-pt.y_));}private://**********found**********______};voidPoint::draw()const{//**********found**********cout<<'('<<______<<')'<<endl;}intmain(){Point*pt1=newPoint(3,0);Point*pt2=newPoint(0,4);Shape*s=pt1;s->draw();s=pt2;s->draw();cout<<'Distance='<<pt1->distance(*pt2)<<endl;deletept1;deletept2;return0;}
操作题 请使用VC6或使用【答题】菜单打开prog1下的工程prog1。此工程中包含程序文件main.cpp,其中有类Score(“成绩”)和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
学号:12345678 课程:英语 总评成绩:85
注意:只修改每个“// ERROR ****found****”下的一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Score{
public:
Score (const char * the_course, const char * the_id, int the_normal, int the_midterm, int the_end_of_term}
: course(the_course), normal(the_normal), midterm(the_midterm), end_of_term(the_end_of_term) {
// ERROR *******found*******
strcpy (the_id, student_id);
}
const char * getCourse() const {return course;} //返回课程名称
// ERROR *******found*******
const char * getID() const {return student_id;} //返回学号
int getNormal() const {return normal;} //返回平时成绩
int getMidterm() const {return midterm;}
//返回期中考试成绩
int getEndOfTerm() const {return end_of_term;} //返回期考试成绩
int getFinal() const; //返回总评成绩
private:
const char * course; //课程名称
char student_id[12]; //学号
int normal; //平时成绩
int midterm; //期中考试成绩
int end_of_term; //期考试成绩
};
//总评成绩中平时成绩占20%,期中考试占30%,期考试占50%,最后结果四舍五入为一个整数
// ERROR *******found*******
int getFinal() const {
return normal * 0.2 + midterm * 0.3 + end_of_terms 0.5 + 0.5;
}
int main() {
char English[] = '英语';
Score score (English, '12345678',68,83,92);
cout << '学号:' << score.getID() << ' ';
cout << '课程:' << score.getCourse() << ' ';
cout << '总评成绩:' << score.getFinal() << endl;
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“// ERROR ****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
There is a white dog named Hoho.
There is a black dog named Haha.
There is a motley dog named Hihi.
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
//狗的颜色:黑、白、黄、褐、花、其他
enum DOGCOLOR {BLACK, WHITE, YELLOW, BROWN, PIEBALD, OTHER};
class Dog{ //狗类
DOGCOLOR color;
char name[20];
static int count;
public:
Dog (char name[], DOGCOLOR color){
strcpy(this->name,name);
// ERROR *******found*******
strcpy (this -> color, color);
}
DOGCOLOR getColor() const {return color;}
// ERROR *******found*******
const char * getName() const {return * name;}
const char * getColorString() const {
switch(color) {
case BLACK: return 'black';
case WHITE: return 'white';
case YELLOW: return 'yellow';
case BROWN: return 'brown';
case PIEBALD: return 'piebald';
}
return 'motley';
}
void show() const {
cout << 'There is a' << getColorString() << 'dog named' << name << '.' << endl;
}
};
int main() {
// ERROR *******found*******
Dog dog1('Hoho', WHITE), dog2('Haha', BLACK); dog3('Hihi', OTHER);
dog1.show();
dog2.show();
dog3.show();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The value of member objects is 8
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
class Member
{
public:
Member(int x) {val =x;}
int GetData() {return val;}
private:
//ERROR ******** found********
int val =0;
};
class MyClass
{
public:
// ERROR ******** found********
MyClass(int x) { data=x; }
void Print()
// ERROR ******** found********
{ cout <<'The value of member object is' <<data.val <<endl;}
private:
Member data;
};
int main()
{
MyClass obj(8);
obj.Print();
return 0;
}
操作题
请打开考生文件夹下的解决方案文件proj1,此工程中包含程序文件main.epp,其中有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>=1intensity<=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****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Base:Good Luck!
Derived:Good Luck!
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Base
{
// ERROR ******** found********
private:
char* msg;
public:
Base(char* str)
{
// ERROR ******** found********
msg=new char[strlen(str)];
strcpy (msg, str);
cout << 'Base: ' <<msg << endl;
}
// ERROR ******** found********
~Base() { delete msg; }
};
class Derived:public Base
{
public:
Derived (char* str):Base(str) {}
void Show () { cout <<'Derived:' <<msg << endl; }
};
int main ()
{
Derived obj ('Good Luck! ');
obj.Show();
return 0;
}
操作题 请使用菜单命令或直接使用VC6打开下的工程proj1,其中有“MP3播放器”类MP3Player和主函数main的定义。程序中位于每个//ERROR **********found**********下的语句行有错误,请加以更正。更正后程序的输出应该是:
天籁-1
黑色
注意:只能修改每个//ERROR ***********found***********下的那一行,不要改动程序中的其他内容。
#include<iostream>
#include<iostream>
using namespace std;
class MP3Player { //“MP3播放器”类
char*type;//型号
char*color;//颜色
public:
//ERROR *********found*********
MP3Player(const char*ty=NULL, const char*co) {
//ERROR *********found*********
if(ty=NULL)
type=NULL;
else{
type=new ehar[stden(ty)+1];
strcpy(type, ty);
}
if(co==NULL)
color=NULL;
else{
color=new char[strlen(co)+1];
strcpy(color, co);
}
}
~MP3Player(){
if(type) delete[]type;
//ERROR *********found*********
if(color) delete color;
}
const char *getType()const {return type;}
const char *getColor()const {return color;}
};
int main(){
MP3Piayer myplayer(“天籁-1”, “黑色”);
cout<<myplayer.getType()<<endl;
cout<<myplayer.getColor()<<endl;
return 0;
}
操作题
使用VC++2010打开考生文件夹下“proj1”文件夹中的工程proj1.sln,该程序运行时有错,请改正其中的错误,使程序正常运行,输出的结果为:
Constructor,i=0,
Destructor
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iestream.h>
class CObj
{
int i;
public:
CObj ();
void display();
~CObj ();
};
//******error******
CObj:CObj ()
{
cout << 'Constructor' << ',';
i=0;
}
//******error******
CObj:display ()
{
cout << 'i=' << i << ', '<<endl;
}
//******error******
CObj:CObj ()
{
cout << 'Destructor' << endl;
}
void main()
{
CObj a;
a.display();
}
操作题 请使用菜单命令或直接使用VC6打开下的工程proj1,其中有“MP3播放器”类MP3Player和主函数main的定义。程序中位于每个//ERROR **********found**********下的语句行有错误,请加以更正。更正后程序的输出应该是:
天籁-1
黑色
注意:只能修改每个//ERROR ***********found***********下的那一行,不要改动程序中的其他内容。
#include<iostream>
#include<iostream>
using namespace std;
class MP3Player { //“MP3播放器”类
char*type;//型号
char*color;//颜色
public:
//ERROR *********found*********
MP3Player(const char*ty=NULL, const char*co) {
//ERROR *********found*********
if(ty=NULL)
type=NULL;
else{
type=new ehar[stden(ty)+1];
strcpy(type, ty);
}
if(co==NULL)
color=NULL;
else{
color=new char[strlen(co)+1];
strcpy(color, co);
}
}
~MP3Player(){
if(type) delete[]type;
//ERROR *********found*********
if(color) delete color;
}
const char *getType()const {return type;}
const char *getColor()const {return color;}
};
int main(){
MP3Piayer myplayer(“天籁-1”, “黑色”);
cout<<myplayer.getType()<<endl;
cout<<myplayer.getColor()<<endl;
return 0;
}
操作题 使用VC6打开proj1下的工程proj1,其中有“沙发”类Sofa和主函数main的定义。程序中位于每个//ERROR**********found**********下的语句行有错误,请加以更正。更正后程序的输出应该是:
座位数:3
颜色:红色
注意:只能修改每个//ERROR**********found**********下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class Sofa{ //“沙发”类
int seats; //座位数
char color[10]; //颜色
public:
//ERROR******found******
Sofa(int s,const char*co)
{
//ERROR******found******
if(co=NULL)
color[0]='\0';
else
strcpy(color,co);
}
//ERROR******found******
const char*getSeats()const{return seats;}
const char*getColor()const{return color;}
};
int main(){
Sofa safa(3);
cout<<'座位数:'<<sara.getSeats()<<endl;
cout<<'颜色:'<<sara.getColor()<<endl;
return 0;
}
操作题 使用VC++6.0打开下的源程序文件1.cpp,该程序运行时有错,请改正错误,使得程序正确执行,并且输出以下语句:
TC1: 0
TC2
注意:不要改动main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在//******error******的下面。
试题程序:
#include<iostream.h>
struct TC1
{
TC1(int i=0)
{
m_i=i;
}
void print()
{
cout<<'TC1:'<<m_i<<endl;
}
int m_i;
};
class TC2
{
public:
TC2()
{
}
void print()
{
cout<<'TC2'<<endl;
}
//********error********
private:
~TC2()
{
}
};
int main()
{
//********error********
TC1 obj1();
//********error********
TC2 obj2();
obj1.print();
obj2.print();
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有类Foo和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
X=a
Y=42
注意:只修改每个“// ERROR *******found*******”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Foo {
public:
Foo(char x) {x_ = x;}
char getX() const {return x_;}
public:
static int y_;
private:
char x_;
};
// ERROR *******found*******
int Foo.y_ = 42;
int main(int argc, char * argv[])
{
// ERROR *******found*******
Foo f;
// ERROR *******found*******
cout << 'X =' << f.x_ << endl;
cout << 'Y =' << f.y_ << endl;
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****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Constructor called.
The value is 10
Copy constructor called.
The value is 10
Destructor called.
Destructor called.
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
public:
// ERROR *******found*******
MyClass(int i)
{value = i; cout << 'Constructor called.' << endl;}
// ERROR *******found*******
MyClass(const MyClass p)
{
value = p.value;
cout << 'Copy constructor called.' << endl;
}
void Print()
{cout << 'The value is' << value << endl;}
// ERROR *******found*******
void ~MyClass()
{cout << 'Destructor called.' << endl;}
private:
int value;
};
int main()
{
MyClass obj1;
obj1.Print();
MyClass obj2(obj1);
obj2.Print();
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,此工程包含有一个源程序文件proj1.cpp。其中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
(4,4)
注意:只修改注释“// ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class Point{
public:
// ERROR *******found*******
Point(double x, double y) _x(x), _y(y) {}
double GetX() const {return _x;}
double GetY() const {return _y;}
// ERROR *******found*******
void Move (double xOff, double yOff) const
{_x + = xOff; _y + = yOff;}
protected:
double _x, _y;
};
int main()
{
Point pt(1.5, 2.5);
pt.Move(2.5, 1.5);
// ERROR *******found******** 以下语句输出pt成员_x和_y的值
cout << '(' << pt._x << ',' << pt._y << ')'<< endl;
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“// ERROR ****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
书名:C++语句程序设计 总页数:299
已把“C++语言程序设计”翻到第50页
已把“C++语言程序设计”翻到第51页
已把“C++语言程序设计”翻到第52页
已把“C++语言程序设计”翻到第51页
已把书合上。
当前页:0
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Book{
char * title;
int num_pages; //页数
int cur_page; //当前打开页面的页码,0表示书未打开
public:
// ERROR *******found*******
Book(const char * theTitle, int pages) num_pages(pages)
{
title = new char[strlen(theTitle) +1];
strcpy(title,theTitle);
cout << endl << '书名' << title
<< '总页数:' << num_pages;
}
~Book() {delete []title;}
bool isClosed() const {return cur_page == 0;} //书合上时返回true,否则返回false
bool isOpen() const {return! isClosed ();} //书打开时返回true,否则返回false
int numOfPages() const {return num_pages;} //返回书的页数
int currentPage() const {return cur_page;} //返回打开页面的页码
// ERROR ********found********
void openAtPage(int page_no)const { //把书翻到指定页
cout << endl;
if (page_no <1 || page_no >num_pages) {
cout << '无法翻到第' << cur_page << '页。';
close ();
}
else {
cur_page = page_no;
cout << '已把'' << title << ''翻到第' << cur_page << '页';
}
}
void openAtPrevPage() {openAtPage(cur_page -1);} //把书翻到上一页
void openAtNextPage() {openAtPage(cur_page +1);} //把书翻到下一页
void close() { //把书合上
cout << endl;
if (isClosed())
cout << '书是合上的。';
else{
// ERROR *******found*******
num_pages = 0;
cout << '已把书合上。';
}
cout << endl;
}
};
int main(){
Book book('C++语言程序设计',299);
book.openAtPage(50);
book.openAtNextPage();
book.openAtNextPage();
book.openAtPrevPage();
book.close();
cout << '当前页:' << book.currentPage() << endl;
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
value = 63
number = 1
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class MyClass {
int * p;
const int N;
public:
// ERROR *******found*******
MyClass(int val):N = 1
{
p = new int;
*p = val;
}
// ERROR *******found*******
~MyClass() {delete *p;}
friend void print (MyClass obj);
};
// ERROR *******found*******
void MyClass::print (MyClass obj)
{
cout << 'value =' << * (obj.p) << endl;
cout << 'number =' << obj.N << endl;
}
int main()
{
MyClass obj(63);
print(obj);
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;
}