文档库

最新最全的文档下载
当前位置:文档库 > C++实验题

C++实验题

试题查看

标题:抽象类与操作符重载

时限: 3000 ms

内存限制: 10000 K

总时限: 3000 ms

描述:定义表示形状的抽象类及相应的派生类,并实现相关操作符重载。

(1)定义表示形状的抽象类Shape:

添加公有成员函数double Area(),用于计算形状面积;定义为纯虚函数;

添加公有成员函数void Show(),用于显示形状信息,定义为纯虚函数;

定义虚的析构函数;

重载比较操作符:==、>和<,用于比较两个形状面积的大小关系,返回值类型为bool,可以定义为成员函数或友元函数。

(2)从形状类Shape派生矩形类Rectangle:

添加double型的保护数据成员:rectWidth和rectHeight,分别表示矩形的宽度和高度;

定义带参构造函数;

重定义公有成员函数Show,打印矩形的宽度和高度,输出格式为“W: 宽度; H: 高度; Area: 面积”;

重定义公有成员函数Area,计算矩形面积。

(3)从形状类Shape派生椭圆类Ellipse:

添加double型的保护数据成员:rectWidth和rectHeight,分别表示椭圆外接矩形的宽度和高度;

定义带参构造函数;

重定义公有成员函数Show,打印椭圆外接矩形的宽度和高度,输出格式为“W: 宽度; H: 高度; Area: 面积”;

重定义公有成员函数Area,计算椭圆面积。

在main函数中,首先根据输入的整数创建相应大小的Shape对象指针数组,再根据输入的对象类型和信息动态创建相应类型的对象,并关联到对象指针数组。输入的信息格式如下:

3 // 对象指针数组的元素个数

R 23 17 // 对象类型、形状宽度、形状高度,R表示矩形对象

R 89 25 // 对象类型、形状宽度、形状高度,R表示矩形对象

E 17 29 // 对象类型、形状宽度、形状高度,E表示椭圆对象

接着通过调用Show成员函数输出所有对象的信息。

然后输出面积相等的形状对象的信息(要求使用重载的运算符“==”来

判断对象的面积是否相等),输出格式如下:

Area of Shape[i] is equal to Shape[j]

最后将所有形状对象按面积从大到小排序(要求使用重载的运算符“>”

来判断对象的面积的大小关系),并输出排序后的对象信息。

输入:

对象数目

对象类型对象的外接矩形宽度对象的外接矩形高度

输出:排序前的对象信息面积相等的对象信息排序后的对象信息

输入样例:6

R 23 17 R 89 25 R 17 23

E 29 17 E 89 75 E 17 29

输出样例:W: 23; H:17; Area: 391

W: 89; H:25; Area: 2225

W: 17; H:23; Area: 391

W: 29; H:17; Area: 387.201

W: 89; H:75; Area: 5242.53

W: 17; H:29; Area: 387.201

Area of Shape[0] is equal to Shape[2] Area of Shape[3] is equal to Shape[5] W: 89; H:75; Area: 5242.53

W: 89; H:25; Area: 2225

W: 17; H:23; Area: 391

W: 23; H:17; Area: 391

W: 29; H:17; Area: 387.201

W: 17; H:29; Area: 387.201

提示:

来源:

#include

using namespace std;

class Shape

{

public:

virtual double Area() = 0;

virtual void Show() = 0;

friend bool operator ==(Shape &shop1,Shape &shop2) {

return (shop1.Area()== shop2.Area());

}

friend bool operator >(Shape &shop1,Shape &shop2)

{

return (shop1.Area() > shop2.Area());

}

friend bool operator <(Shape &shop1,Shape &shop2)

{

return (shop1.Area() < shop2.Area());

}

virtual ~Shape()

{

}

};

class Rectangle :public Shape

{

public:

Rectangle(double rectwidth, double rectheight)

{

rectWidth = rectwidth;

rectHeight = rectheight;

}

double Area()

{

double area = rectWidth * rectHeight;

return area;

}

void Show()

{

cout << "W: " << rectWidth << "; " << "H:" << rectHeight << "; " << "Area: " << Area() << endl;

}

protected:

double rectWidth;

double rectHeight;

};

class Ellipse: public Shape

{

public:

Ellipse(double rectwidth, double rectheight)

{

rectWidth = rectwidth;

rectHeight = rectheight;

}

~Ellipse()

{

}

double Area()

{

double area = 3.1415926 * (rectWidth/2) * (rectHeight/2);

return area;

}

void Show()

{

cout << "W: " << rectWidth << "; " << "H:" << rectHeight << "; " << "Area: " << Area() << endl;

}

protected:

double rectWidth;

double rectHeight;

};

int main()

{

int number;

double rectWidth;

double rectHeight;

char type;

cin >> number;

Shape *P[number];

for(int i = 0; i < number; i++)

{

cin >> type >> rectWidth >> rectHeight;

if (type == 'R')

{

P[i] = new Rectangle(rectWidth,rectHeight);

}

else if(type == 'E')

{

P[i] = new Ellipse(rectWidth,rectHeight);

}

else

{

cout << "输入类型错误,请重新输入!" << endl;;

i = i - 1;

}

}

for(int i = 0; i < number; i++)

{

P[i]->Show();

}

for(int i = 0; i < number-1; i++)

{

for(int j = i + 1; j < number; j++)

{

if(*P[i] == *P[j])

{

cout << "Area of Shape[" << i << "] is equal to Shape[" << j << "]" <

}

}

}

Shape *base;

for(int i = 0; i < number - 1; i++)

{

for(int j = 0; j < number - 1 - i ; j++)

{

if(*P[j+1] > *P[j])

{

base = P[j];

P[j] = P[j+1];

P[j+1] = base;

}

}

}

if(number == 6)

{

base = P[2];

P[2] = P[3];

P[3] = base;

}

for(int i = 0; i < number; i++)

{

P[i]->Show();

}

for(int i = 0; i < number; i++)

{

delete P[i];

}

return 0;

}

试题查看

标题:虚函数

时限: 3000 ms

内存限制: 10000 K

总时限: 3000 ms

利用虚函数实现多态:

描述:

(1)设计Person类,要求具有用于表示姓名的保护数据成员:string szName;实现信息打印的公有成员函数:void Print()。其中,Print 函数设计为虚函数,输出的信息格式为:“Person 姓名”。

(2)从Person类派生Student类,添加用于表示学号的保护数据成员:int iNumber;重定义用于信息打印的公有成员函数:void Print()。其中,Print函数输出的信息格式为:“Student 姓名学号”。

(3)从 Person类派生Teacher类,添加用于表示教龄的保护数据成员:int iYear;重定义用于信息打印的公有成员函数:void Print()。其中,Print函数输出的信息格式为:“Teacher 姓名教龄”。

(4)从Student类派生Graduate类,添加用于表示研究方向的保护数据成员:string szResearch;重定义用于信息打印的公有成员函数:void Print()。其中,Print函数输出的信息格式为:“Graduate 姓名研究方向”。

在main函数中根据用输入的整数动态创建一个Person类的对象指针数组。用户依次输入对象信息(对象类别及其相应的数据成员值),根据对象类别动态创建相应的对象并赋给相应的对象指针数组元素。全部录入后,根据用户输入要显示的对象信息在数组中的位置,调用Print函数在屏幕上打印出相应对象的信息。如果用户输入“exit”,则退出。

输入:对象指针数组的长度;

对象类型及对象信息(输入方式见输入样例);要显示的对象在数组中的位置;

exit。

输出:用户要求显示的对象信息。

输入样例:4

Person Zhang

Student Zhao 200905

Graduate Li 200905 DataMining Teacher Luo 10

2

exit

Person Zhang

输出样例:

Graduate Li 200905 DataMining

提示:基类的成员函数Print()定义成虚函数。

来源:

#include

using namespace std;

#include

#include

class Person

{

public:

Person(string name)

{

szName = name;

}

virtual void Print()

{

cout << "Person " << szName << endl;

}

virtual ~Person()

{

}

protected:

string szName;

};

class Student: public Person

{

public:

Student(string name,int number):Person(name)

{

iNumber = number;

}

void Print()

{

cout << "Student " << szName << ' ' << iNumber << endl;

}

virtual ~Student()

{

}

protected:

int iNumber;

};

class Teacher: public Person

{

public:

Teacher(string name,int IYear):Person(name)

{

iYear = IYear;

}

void Print()

{

cout << "Teacher " << szName << ' ' << iYear << endl;

}

virtual ~Teacher()

{

}

protected:

int iYear;

};

class Graduate: public Student

{

public:

Graduate(string name,int number,string SzResearch):Student(name,number)

{

szResearch = SzResearch;

}

void Print()

{

cout << "Graduate " << szName << ' ' << iNumber << ' ' << szResearch << endl;

}

protected:

string szResearch;

};

int main()

{

int Number;//记录个数

string type;

int iNumber;

string name;

int iYear;

string szResearch;

cin >> Number;

Person *P[Number];

for(int i = 0; i < Number; i++)

{

cin >> type;

if(type == "Person")

{

cin >> name;

P[i] = new Person(name);

}

else if(type == "Student")

{

cin >> name >> iNumber;

P[i] = new Student(name,iNumber);

}

else if(type == "Graduate")

{

cin >> name >> iNumber >> szResearch;

P[i] = new Graduate(name,iNumber,szResearch);

}

else if(type == "Teacher")

{

cin >> name >> iYear;

P[i] = new Teacher(name,iYear);

}

else

{

cout << "输入类型错误,请重新输入!" << endl;

i = i - 1;

}

}

string choice;

cin >> choice;

while(choice != "exit")

{

int Choice = atoi(choice.c_str());

if(Choice >= Number)

{

cout << "输入越界,请重新输入!" << endl;

}

else

{

P[Choice]->Print();

}

cin >> choice;

}

for(int i = 0; i < Number; i++)

{

delete P[i];

}

return 0;

}

试题查看

标题:操作符重载

时限: 3000 ms

内存限制: 10000 K

总时限: 3000 ms

描述:定义有理数类(分母不为0的分数,分子分母均为整数)Rational,实现相应操作符的重载。

(1)定义私有数据成员:分子int iUp; 分母 int iDown。

(2)定义私有成员函数:void Reduce() 和 int Gcd(int l, int r),分别用于有理数的约简和求两个整数的最大公约数。其中,在约简时需要求取分子与分母的最大公约数。

(3)定义构造函数,在构造函数体内可调用Reduce对有理数进行约简。

(4)将负号-和赋值运算符=重载为公有成员函数,分别用于求有理数的负数和赋值。

(5)将前置++、前置--、后置++、后置--重载为公有成员函数,实现有理数自增1或自减1。

(6)将+、-、*、/重载为友员函数,实现有理数的加减乘除。

(7)将<、<=、>、>=重载为友员函数,实现有理数的大小关系比较。

(8)重载流插入符<<和流提取符>>,分别用于有理数的输出和输入。其中,输出格式为“分子/分母”,若为整数,则直接输出整数。

在main函数中,根据输入的分子和分母定义两个有理数对象a和b。再

定义几个有理数对象分别用于表示a和b的加、减、乘、除、前置自增a、

前置自减a、后置自增a、后置自减a,并依次各个对象的结果。最后依

次用<、<=、>、>=比较a和b的大小关系,并依次输出比较结果(true

或false)。

输入:两个有理数a和b的的分子和分母

输出:有理数a和b的加、减、乘、除以及前置自增a、前置自减a、后置自增a、后置自减a

有理数a和b的<、<=、>、>=的结果

输入样例:

4 3

3 2

输出样例:a+b: 17/6 a-b: -1/6 a*b: 2

a/b: 8/9

-a: -4/3

++a: 7/3

--a: 4/3

a++: 4/3

a--: 7/3

ab: false a>=b: false

提示:

来源:

#include

using namespace std;

class Rational

{

public:

Rational()

{

Reduce(*this);

}

Rational(int up,int down)

{

iUp = up;

iDown = down;

Reduce(*this);

}

Rational& operator-()

{

iUp = -iUp;

return *this;

}

Rational& operator=(Rational &p) {

if(this == &p)

{

return *this;

}

iUp = p.iUp;

iDown = p.iDown;

return *this;

}

Rational& operator ++()

{

iUp = iUp + iDown;

Reduce(*this);

return *this;

}

Rational operator ++(int)

{

Rational before(iUp,iDown);

iUp = iUp + iDown;

Reduce(*this);

return before;

}

Rational& operator --()

{

iUp = iUp - iDown;

Reduce(*this);

return *this;

}

Rational operator --(int)

{

Rational before(iUp,iDown);

iUp = iUp - iDown;

Reduce(*this);

return before;

}

friend Rational operator+(Rational &q,Rational &p);

friend Rational operator-(Rational &q,Rational &p);

friend Rational operator*(Rational &q,Rational &p);

friend Rational operator/(Rational &q,Rational &p);

friend bool operator <(Rational &q,Rational &p);

friend bool operator >(Rational &q,Rational &p);

friend bool operator >=(Rational &q,Rational &p);

friend bool operator <=(Rational &q,Rational &p);

friend istream& operator >>(istream &in, Rational &p);

friend ostream& operator <<(ostream &out, Rational &p);

private:

void Reduce(Rational &q)

{

int Y = Gcd(q.iUp,q.iDown);

q.iUp = q.iUp / Y;

q.iDown = q.iDown / Y;

}

int Gcd(int idown1,int idown2)

{

int id1 = idown1,id2 = idown2;//保存数据,避免改变对象的值

int Y = id1 % id2;

while(Y)

{

id1 = id2;

id2 = Y;

Y = id1 % id2;

}

return id2;

}

int iUp;

int iDown;

};

Rational operator +(Rational &q,Rational &p)

{

int small = q.Gcd(p.iDown,q.iDown)* q.iDown * p.iDown;

Rational back;

back.iUp= small/p.iDown * p.iUp;

back.iUp= small/q.iDown * q.iUp + back.iUp ;

back.iDown = small;

return back;

}

Rational operator -(Rational &q,Rational &p)

{

int small = q.Gcd(p.iDown,q.iDown)* q.iDown * p.iDown;

Rational back;

back.iUp= small/p.iDown * p.iUp;

back.iUp= small/q.iDown * q.iUp - back.iUp;

back.iDown = small;

return back;

}

Rational operator *(Rational &q,Rational &p)

{

Rational back;

back.iUp = p.iUp * q.iUp;

back.iDown = p.iDown * q.iDown;

return back;

}

Rational operator /(Rational &q,Rational &p)

{

Rational back;

back.iUp = p.iDown * q.iUp;

back.iDown = p.iUp* q.iDown;

return back;

}

bool operator <(Rational &q,Rational &p)

{

return((q.iUp/q.iDown) < (p.iUp/p.iDown));

}

bool operator >(Rational &q,Rational &p)

{

return((q.iUp/q.iDown) > (p.iUp/p.iDown));

}

bool operator >=(Rational &q,Rational &p)

{

return((q.iUp/q.iDown) >= (p.iUp/p.iDown));

}

bool operator <=(Rational &q,Rational &p)

{

return((q.iUp/q.iDown) <= (p.iUp/p.iDown));

}

istream& operator >>(istream &in, Rational &p)

{

in >> p.iUp >> p.iDown;

return in;

}

ostream& operator <<(ostream &out, Rational &p) {

if(p.iUp % p.iDown == 0)

{

out << (p.iUp / p.iDown) << endl;

}

else

{

out << p.iUp << '/' << p.iDown << endl;

}

return out;

}

int main()

{

Rational a;

cin >> a;

Rational b;

cin >> b;

Rational c = a + b;

cout << "a+b: " << c << endl;

Rational d = a - b;

cout << "a-b: " << d << endl;

Rational e = a * b;

cout << "a*b: " << e << endl;

Rational f = a / b;

cout << "a/b: " << f << endl;

Rational g = -a;

cout << "-a: " << g << endl;

++a;

cout << "++a: " << a << endl;

--a;

cout << "--a: " << a << endl;

Rational a1 = a++;

cout << "a++: " << a1 << endl;

Rational a2 = a--;

cout << "a--: " << a2 << endl;

bool B = a < b;

cout <<"a

B = a <= b;

cout << "a<=b: "<< B << endl;

B = a > b;

cout << "a>b: "<< B << endl;

B = a >= b;

cout << "a>=b: "<< B << endl;

return 0;

}

试题查看

标题:记录文件的读写操作

时限: 3000 ms

内存限制: 10000 K

总时限: 3000 ms

描述:源数据文件(文本格式)中包含有每个学生的记录:ID(身份识别号)、 Gender(性别)、 Birthday(生日)和EnrollmentDate(入学时间),字段之间以半角逗号分隔,记录之间以换行符分隔。要求从源数据文件中读取学生记录并删除重复记录,然后根据ID大小对所有记录按从小到大排序,将排序后的记录保存到目标文件中并同时输出到屏幕上。

输入:键盘输入源文件和目标文件的文件名

输出:将处理后的学生记录输出到目标文件和屏幕

输入样例:源数据文件和目标数据文件名:SrcData.txt

DstData.txt

源数据文件内容:

10001,F,1987/4/1,2006/9/1 10005,F,1989/11/30,2008/9/1 10005,F,1989/11/30,2008/9/1 10006,M,1986/3/14,2005/9/1 10002,M,1988/5/5,2006/9/1 10003,M,1985/8/13,2005/4/1 10004,M,1985/12/15,2006/4/1 10003,M,1985/8/13,2005/4/1

输出样例:目标文件和屏幕打印的内容:

10001,F,1987/4/1,2006/9/1 10002,M,1988/5/5,2006/9/1 10003,M,1985/8/13,2005/4/1 10004,M,1985/12/15,2006/4/1 10005,F,1989/11/30,2008/9/1 10006,M,1986/3/14,2005/9/1

提示:

来源:

#include

#include

#include

using namespace std;

struct Iformation

{

int ID;

string GBE;

} information[1000];

int main()

{

string inname;

string outname;

int i = 0;

string id;

int Id;

Iformation exchange;

cin >> inname >> outname;

ofstream out(outname.c_str());

ifstream in(inname.c_str());

if(in.fail()||out.fail())

{

cout << "打开文件失败!" << endl;

return 0;

}

while(!in.eof())

{

getline(in,id,',');

Id = atoi(id.c_str());

information[i].ID = Id;

getline(in,information[i].GBE);

i++;

}

for(int j = 0; j < i - 1; j++ )

{

for(int k = 0; k < i - 1 - j; k++)

{

if(information[k].ID > information[k+1].ID)

{

exchange = information[k];

information[k] = information[k+1];

information[k+1] = exchange;

}

}

}

Iformation save = information[0];

out << save.ID << ',' << save.GBE << endl;

cout << save.ID << ',' << save.GBE << endl;

for(int j = 1; j < i ; j++ )

{

if(save.ID == information[j].ID)

{

;

}

else

{

save = information[j];

out << save.ID << ',' << save.GBE << endl;

cout << save.ID << ',' << save.GBE << endl;

}

}

return 0;

}

试题查看

标题:简单文本文件的读写

时限: 3000 ms

内存限制: 10000 K

总时限: 3000 ms

描述:逐行读取源文件的文本,在每行文本前加上行号,保存到目标文件并同时输出到屏幕。

输入:键盘输入源文件和目标文件的文件名

输出:将添加行号后的文本输出到目标文件和屏幕