《计算机程序设计(C++)》第10周编程作业
1 定义一个带重载构造函数的日期类(20分)
题目内容:
定义一个带重载构造函数的日期类Date,数据成员有年、月、日;成员函数包括:一个带参数的构造函数Date(int,int,int),一个不带参数的构造函数(设置日期为1900年1月1日),一个按“年-月-日”格式显示日期的函数,一个对数据成员赋值的函数void init(int,int,int)。
主函数中对类的测试要求:
- 分别使用两个不同的重载构造函数创建两个日期类对象(必须为d1,d2,d2初始值为2100-12-12);
- 按“年-月-日”格式分别显示两个对象的值;
- 输入数据,用init函数为d1赋值; 2.按“年-月-日”格式显示对象d1的值;。
输入格式:
给d1赋值的数据
输出格式:
d1的默认值
d2的初始值
d1赋值后的值
输入样例:
2011 4 29
输出样例:
1900-1-1 2100-12-12 2011-4-29
时间限制:500ms 内存限制:32000kb
#include<iostream>
using namespace std;
class Data
{
private:
int Year, Mon, Day;
public:
Data()
{
Year = 1900;
Mon = 1;
Day = 1;
}
Data(int y, int m, int d)
{
Year = y;
Mon = m;
Day = d;
}
void init(int y, int m, int d)
{
Year = y;
Mon = m;
Day = d;
}
void show()
{
cout << Year << '-' << Mon << '-' << Day << endl;
}
};
int main() {
Data d1, d2(2100, 12, 12);
d1.show();
d2.show();
int y, m, d;
cin >> y >> m >> d;
d1.init(y, m, d);
d1.show();
return 0;
}
2 动态生成Person类的对象(20分)
题目内容:
编写Person类,数据成员为姓名(20字符长度)、年龄(int)和性别(char)。
编写无参数的构造函数,其中姓名赋值为“XXX”,年龄0,性别’m’; 编写析构函数,在其中输出字符串“Now destroying the instance of Person”; 编写Register成员函数,为数据成员赋值; 编写showme成员函数,显示姓名、年龄和性别。编写主函数: 用Person类创建2个指针,p1和 p2; 用new创建两个Person对象,分别将指针赋值给p1,p2;
用showme成员函数显示p1,p2所指对象的值; 再输入一组“姓名、年龄和性别”值,用成员函数Register为p1的成员赋值;
将p1所指对象的值赋值给p2所指对象; 用showme显示p1、p2所指对象的值。 删除动态对象。输入格式:
为p1的成员赋值时使用的数据
输出格式:
person1和person2的默认值
person1和person2的赋值后的值
析构函数输出的信息
输入样例:
Bobs 24 m
输出样例:
person1:XXX 0 m
person2:XXX 0 m
person1:Bobs 24 m
person2:Bobs 24 m
Now destroying the instance of Person
Now destroying the instance of Person时间限制:500ms 内存限制:32000kb
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
char Name[20], Sex[2];
int Age;
public:
Person()
{
strcpy(Name, "XXX");
strcpy(Sex, "m");
Age = 0;
}
~Person() { cout << "Now destroying the instance of Person" << endl; }
void Register(char name[], int age, char sex[]);
void showme() { cout << Name << ' ' << Age << ' ' << Sex << endl; }
};
void Person::Register(char name[], int age, char sex[])
{
strcpy(Name, name);
strcpy(Sex, sex);
Age = age;
}
int main() {
Person* p1, * p2;
p1 = new Person;
p2 = new Person;
cout << "person1:";
p1->showme();
cout << "person2:";
p2->showme();
char name[20], sex[2];
int age;
cin >> name >> age >> sex;
p1->Register(name, age, sex);
*p2 = *p1;
cout << "person1:";
p1->showme();
cout << "person2:";
p2->showme();
delete p1;
delete p2;
return 0;
}
3 设计带构造函数的Dog类(20分)
题目内容:
设计一个Dog类,包含name、age、sex和weight等属性,在有参数的构造函数中对数据成员进行初始化。
公有成员函数有:GetName()、GetAge()、GetSex()和GetWeight()可获取名字、年龄、性别和体重。编写成员函数speak() 显示狗的叫声。编写主函数,输入狗的名字、年龄、性别和体重;声明Dog对象并用输入的数据通过构造函数初始化对象,通过成员函数获取狗的属性并显示出来。
输入格式:
狗的信息
输出格式:
狗的信息,外加叫声
输入样例:
Tom 4 m 2.4
输出样例:
Tom
4
m
2.4
Arf!Arf!
时间限制:500ms 内存限制:32000kb
#include<iostream>
#include<cstring>
using namespace std;
class Dog
{
private:
char Name[20], Sex[2];
int Age;
float Weight;
public:
Dog(char name[], int age, char sex[], float weight)
{
strcpy(Name, name);
strcpy(Sex, sex);
Age = age;
Weight = weight;
}
void GetName() { cout << Name << endl; }
void GetAge() { cout << Age << endl; }
void GetSex() { cout << Sex << endl; }
void GetWeight() { cout << Weight << endl; }
void speak() { cout << "Arf!Arf!" << endl; }
};
int main() {
char name[20], sex[2];
int age;
float weight;
cin >> name >> age >> sex >> weight;
Dog d1(name, age, sex, weight);
d1.GetName();
d1.GetAge();
d1.GetSex();
d1.GetWeight();
d1.speak();
return 0;
}
4 设计并测试一个椭圆类(20分)
题目内容:
设计并测试一个名为Ellipse的椭圆类,其属性为圆心坐标及长半轴和短半轴的长度。设计一个构造函数(Ellipse(int,int,double,double))对这些属性进行初始化,并通过成员函数计算出椭圆的面积(double
Area())。S(椭圆面积)=PI(圆周率)×a(长半轴)×b(短半轴) 其中PI取3.14
输入格式:
圆心坐标、长半轴和短半轴的长度
输出格式:
椭圆的面积
输入样例:
1 1 1 2
输出样例:
6.28
时间限制:500ms 内存限制:32000kb
#include<iostream>
using namespace std;
class Ellipse
{
private:
int X, Y;
double L, S;
public:
Ellipse(int x, int y, double l, double s)
{
X = x;
Y = y;
L = l;
S = s;
}
double Area() { return 3.14 * L * S; }
};
int main() {
int x, y;
double a, b;
cin >> x >> y >> a >> b;
Ellipse e1(x, y, a, b);
cout << e1.Area() << endl;
return 0;
}
5 设计一个多功能的MyTime类(20分)
题目内容:
设计一个多功能的MyTime类,设计多个重载的构造函数,可以设置时间,进行时间的加减运算,按各种可能的格式(24小时制、12小时制)输出时间。
注意:
(1)请考虑设置的时间的合理性(时0-23; 分0-59;秒0-59)。
(2)12小时制中,12:00:00前为AM, 12:00:00及以后为PM
(3)加减运算的加数、减数是一个时间的长度,单位为“时、分、秒”
(4)构造函数:没参数时,设置时间为0时 0分 0秒;有参数时,设置成给定的时、分、秒。
在主函数中(1)声明两个对象t1,t2,并通过构造函数初始化它们(t2初始化为为8:10:30)
(2)显示按12、14小时制显示t1、t2的时间。
(3)再设置t1的时间,数据由用户输入。
(4)再输入待加减的时间。
(5)t1加输入的时间,并按12小时和24小时制显示。
(6)t2减输入的时间,并按12小时和24小时制显示。
输入格式:
第一行为t1的时间,第二行为待加减的时间
输出格式:
显示按12、14小时制显示t1、t2的初始时间
t1加输入的待加减的时间按12小时和24小时制显示。
t2减输入的待加减的时间按12小时和24小时制显示
输入样例:
11 30 30
5 15 20
输出样例:
00:00:00 AM
00:00:00
08:10:30 AM
08:10:30
04:45:50 PM
16:45:50
02:55:10 AM
02:55:10
时间限制:500ms内存限制:32000kb
#include<iostream>
using namespace std;
class MyTime
{
private:
int H, M, S;
public:
MyTime()
{
H = 0;
M = 0;
S = 0;
}
MyTime(int h, int m, int s)
{
H = (0 <= h && h <= 23) ? h : 0;
M = (0 <= m && m <= 59) ? m : 0;
S = (0 <= s && s <= 59) ? s : 0;
}
void set(int h, int m, int s)
{
H = (0 <= h && h <= 23) ? h : 0;
M = (0 <= m && m <= 59) ? m : 0;
S = (0 <= s && s <= 59) ? s : 0;
}
void addtime(int h, int m, int s);
void cuttime(int h, int m, int s);
void show_12();
void show_24();
};
void MyTime::addtime(int h, int m, int s)
{
if ((S + s) > 59)
{
S = S + s - 60;
M++;
}
else
S = S + s;
if ((M + m) > 59)
{
M = M + m - 60;
H++;
}
else
M = M + m;
H = ((H + h) > 23) ? (H + h - 24) : (H + h);
}
void MyTime::cuttime(int h, int m, int s)
{
if ((S - s) < 0)
{
S = S - s + 60;
M--;
}
else
S = S - s;
if ((M - m) < 0)
{
M = M - m + 60;
H--;
}
else
M = M - m;
H = ((H - h) < 0) ? (H - h + 24) : (H - h);
}
void MyTime::show_12()
{
if (H < 10)
cout << '0' << H << ':';
else if (H < 13)
cout << H << ':';
else if (H < 22)
cout << '0' << H - 12 << ':';
else
cout << H - 12 << ':';
if (M < 10)
cout << '0' << M << ':';
else
cout << M << ':';
if (S < 10)
cout << '0' << S;
else
cout << S;
if (H < 12)
cout << " AM" << endl;
else
cout << " PM" << endl;
}
void MyTime::show_24()
{
if (H < 10)
cout << '0' << H << ':';
else
cout << H << ':';
if (M < 10)
cout << '0' << M << ':';
else
cout << M << ':';
if (S < 10)
cout << '0' << S << endl;
else
cout << S << endl;
}
int main() {
MyTime t1, t2(8, 10, 30);
t1.show_12();
t1.show_24();
t2.show_12();
t2.show_24();
int h, m, s;
cin >> h >> m >> s;
t1.set(h, m, s);
cin >> h >> m >> s;
t1.addtime(h, m, s);
t1.show_12();
t1.show_24();
t2.cuttime(h, m, s);
t2.show_12();
t2.show_24();
return 0;
}