山东科技大学2020年5月18日作业题解

山东科技大学2020年5月18日作业题解

**题目一:**正方形、长方形、立方体 之二

Description

给出正方形(Square)、长方形(Rectangle)、立方体(Cube)、长方体(Cuboid)的边长,求(表)面积。
正方形、立方体各边相等的,因此只需存储一条边长。
长方形需存储两条边长。
长方体需存储三条边长
设计一个基类Shape,用于多态的实现求求(表)面积。
请仔细阅读append.cc代码,并设计好正方形、长方形、立方体、长方体派生关系,使main()函数能够运行并得到正确的输出。

Input

输入第一个整数n,表示后面有n个图形。每个图形占用一行输入。SQR表示正方形、CUB表示立方体,后面只输入一个边长;RCT表示长方形,后面输入长和宽;CBD表示长方体,后面输入长宽高。

Output

从输入的第二行开始,每行对应输出一个图形的(表)面积字符串。

Sample Input

6
RCT 3 5
SQR 3
CUB 3
CBD 3 4 5
CBD 1 2 1
RCT 3 1

Sample Output

15
9
54
94
10
3

题目给定代码

int main()
{
    int n, l, w, h;
    cin >> n;
    Shape *sp[100];
    string name;
    for(int i = 0; i < n; i++)
    {
        cin >> name;
        if(name == "SQR")
        {
            cin >> l;
            sp[i] = new Square(l);
        }
        if(name == "RCT")
        {
            cin >> l >> w;
            sp[i] = new Rectangle(l, w);
        }
        if(name == "CUB")
        {
            cin >> l;
            sp[i] = new Cube(l);
        }
        if(name == "CBD")
        {
            cin >> l >> w >> h;
            sp[i] = new Cuboid(l, w, h);
        }
    }
    for(int i = 0; i < n; i++)
        cout << sp[i]->area() << endl;
    for(int i = 0; i < n; i++)
        delete sp[i];
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Shape{
	public:
		virtual int area() = 0;
};
class Square : public Shape{
	private:
		int r;
	public:
		Square(int r_r){
			r = r_r;
		}
		int area(){
			return r * r;
		}
};
class Cube : public Shape{
	private:
		int r;
	public:
		Cube(int r_r){
			r = r_r;
		}
		int area(){
			return r * r * 6;
		}
};
class Rectangle : public Shape{
	private:
		int l, r;
	public:
		Rectangle(int l_l, int r_r){
			l = l_l, r = r_r;
		}
		int area(){
			return l * r;
		}
};
class Cuboid : public Shape{
	private:
		int l, r, h;
	public:
		Cuboid(int l_l, int r_r, int h_h){
			l = l_l, r = r_r, h = h_h;
		}
		int area(){
			return (l * r + l * h + r * h) * 2;
		}
};

int main()
{
    int n, l, w, h;
    cin >> n;
    Shape *sp[100];
    string name;
    for(int i = 0; i < n; i++)
    {
        cin >> name;
        if(name == "SQR")
        {
            cin >> l;
            sp[i] = new Square(l);
        }
        if(name == "RCT")
        {
            cin >> l >> w;
            sp[i] = new Rectangle(l, w);
        }
        if(name == "CUB")
        {
            cin >> l;
            sp[i] = new Cube(l);
        }
        if(name == "CBD")
        {
            cin >> l >> w >> h;
            sp[i] = new Cuboid(l, w, h);
        }
    }
    for(int i = 0; i < n; i++)
        cout << sp[i]->area() << endl;
    for(int i = 0; i < n; i++)
        delete sp[i];
}

**题目二:**动物要吃饭

Description

定义Animal类,只有一个纯虚函数eat。

定义Dog、Cat、Sheep、Chicken四个类,它们都是Animal的子类。

每个类重载eat方法,每个方法输出的内容见样例。

Input

一系列0~3之内的整数。

Output

每个输入对应一行输出,0、1、2、3分别代表Dog、Cat、Sheep、Chicken。

Sample Input

0
1
2
3
2
3
1
0

Sample Output

Dog eats bone.
Cat eats fish.
Sheep eats grass.
Chicken eats worm.
Sheep eats grass.
Chicken eats worm.
Cat eats fish.
Dog eats bone.

题目给定函数

int main()
{
    int c;
    vector<Animal*> animals;
    vector<Animal*>::iterator itr;
    while(cin>>c)
    {
        switch(c)
        {
        case 0 :
            animals.push_back(new Dog());
            break;
        case 1:
            animals.push_back(new Cat());
            break;
        case 2:
            animals.push_back(new Sheep());
            break;
        case 3:
            animals.push_back(new Chicken());
        }
    }
    for (itr = animals.begin(); itr != animals.end(); itr++)
        (*itr)->eat();
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Animal{
	public:
		virtual void eat() = 0;
};
class Dog : public Animal{
	public:
		void eat(){
			printf("Dog eats bone.\n");
		}
};
class Cat : public Animal{
	public:
		void eat(){
			printf("Cat eats fish.\n");
		}
};
class Sheep : public Animal{
	public:
		void eat(){
			printf("Sheep eats grass.\n");
		}
};
class Chicken : public Animal{
	public:
		void eat(){
			printf("Chicken eats worm.\n");
		}
};

int main()
{
    int c;
    vector<Animal*> animals;
    vector<Animal*>::iterator itr;
    while(cin>>c)
    {
        switch(c)
        {
        case 0 :
            animals.push_back(new Dog());
            break;
        case 1:
            animals.push_back(new Cat());
            break;
        case 2:
            animals.push_back(new Sheep());
            break;
        case 3:
            animals.push_back(new Chicken());
        }
    }
    for (itr = animals.begin(); itr != animals.end(); itr++)
        (*itr)->eat();
    return 0;
}

**第三题:**小型飞机大战

Description

飞机大战这个小游戏很好玩,作为一名编程狂人,自己不去写个好玩的游戏,怎么能说得过去呢?

现在,请使用C++来编写一个小的飞机大战游戏雏形,至少包括如下类:

  1. FlyThing类:敌机和战机的父类,是一个抽象类。

(1)拥有名字属性和位置属性,其中名字是一个字符串,位置是二维空间下的坐标,代表了在屏幕上的位置。

(2)纯虚函数void fly(),模拟飞机的飞行动作。

(3)void show():显示飞机的名字和位置。格式为:“$ at x y”,其中$为飞机名字,x和y是横坐标和纵坐标。

(4)其他必要的函数。

  1. EnemyPlane类:敌机类,是FlyThing的子类。其Fly()函数实现将飞机的纵坐标加1。

  2. MyPlane类:战机类,是FlyThing的子类。其Fly()函数根据输入实现飞行。具体是:

(1)如果输入A或a,则横坐标减1;

(2)如果输入S或s,则纵坐标加1;

(3)如果输入W或w,则纵坐标减1;

(4)如果输入D或d,则横坐标加1。

Input

第1行N是一个正整数,之后N行,每行包括1个字符串、2个整数分别是敌机名字和横、纵坐标。

第N+2行是一个正整数M,之后M行每行是一个字符,为a、s、d、w之一,用于战机的飞行。

Output

见样例~

Sample Input

3
enemy1 10 0
enemy2 20 20
enemy3 30 10
4
a
s
w
d

Sample Output

WUDI at 50 50
enemy1 at 10 0
enemy2 at 20 20
enemy3 at 30 10
WUDI at 49 50
enemy1 at 10 1
enemy2 at 20 21
enemy3 at 30 11
WUDI at 49 51
enemy1 at 10 2
enemy2 at 20 22
enemy3 at 30 12
WUDI at 49 50
enemy1 at 10 3
enemy2 at 20 23
enemy3 at 30 13
WUDI at 50 50
enemy1 at 10 4
enemy2 at 20 24
enemy3 at 30 14

题目给定代码

int main()
{
    FlyThing **planes;
    int numOfEnemplanes, i, x, y, numOfMoves, j;
    string str;
    cin>>numOfEnemplanes;
    planes = new FlyThing*[numOfEnemplanes + 1];
    planes[0] = new MyPlane("WUDI", 50, 50);
    for(i = 1; i <= numOfEnemplanes; i++)
    {
        cin>>str>>x>>y;
        planes[i] = new EnemyPlane(str, x, y);
    }
 
    for (j = 0; j < numOfEnemplanes + 1; j++)
    {
        planes[j]->show();
    }
 
    cin>>numOfMoves;
    for (i = 0; i < numOfMoves; i++)
    {
        for (j = 0; j < numOfEnemplanes + 1; j++)
        {
            planes[j]->fly();
            planes[j]->show();
        }
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class FlyThing {
protected:
	int locate_x, locate_y;
	string name;
public:
	virtual void fly() = 0;
	void show() {
		cout << name << " at " << locate_x << " " << locate_y << "\n";
	}
	FlyThing(string s, int x, int y) {
		name = s, locate_x = x, locate_y = y;
	}
};
class EnemyPlane : public FlyThing {
public:
	void fly() {
		++locate_y;
	}
	EnemyPlane(string s, int x, int y) : FlyThing(s, x, y) {

	}
};
class MyPlane : public FlyThing {
public:
	MyPlane(string s, int x, int y) : FlyThing(s, x, y) {

	}
	void fly() {
		char move;
		getchar(), move = getchar();
		if (move == 'a') {
			--locate_x;
		}
		else if (move == 's') {
			++locate_y;
		}
		else if (move == 'w') {
			--locate_y;
		}
		else {
			++locate_x;
		}
	}
};

int main()
{
	FlyThing** planes;
	int numOfEnemplanes, i, x, y, numOfMoves, j;
	string str;
	cin >> numOfEnemplanes;
	planes = new FlyThing * [numOfEnemplanes + 1];
	planes[0] = new MyPlane("WUDI", 50, 50);
	for (i = 1; i <= numOfEnemplanes; i++)
	{
		cin >> str >> x >> y;
		planes[i] = new EnemyPlane(str, x, y);
	}

	for (j = 0; j < numOfEnemplanes + 1; j++)
	{
		planes[j]->show();
	}

	cin >> numOfMoves;
	for (i = 0; i < numOfMoves; i++)
	{
		for (j = 0; j < numOfEnemplanes + 1; j++)
		{
			planes[j]->fly();
			planes[j]->show();
		}
	}
	return 0;
}

**第四题:**运算符重载:点之间的距离

Description

定义Point类,有2个double类型的属性表示点的横坐标和纵坐标。重载其减法运算符,用于计算两个点之间的距离。

Input

输入一系列点的坐标,每个点的坐标占一行。

Output

见样例。

Sample Input

20 20
30 30
40 40
50 50

Sample Output

14.1421
28.2843
42.4264
56.5685

题目给定代码

int main()
{
    Point p1(10,10);
    double x, y;
    while(cin>>x>>y)
    {
        Point p2(x, y);
        cout<<p2 - p1<<endl;
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Point {
private:
	double x_, y_;
public:
	friend double operator-(Point p1, Point p2) {
		return sqrt((p1.x_ - p2.x_) * (p1.x_ - p2.x_) + (p1.y_ - p2.y_) * (p1.y_ - p2.y_));
	}
	Point(double x_x, double y_y) {
		x_ = x_x, y_ = y_y;
	}
};

int main()
{
	Point p1(10, 10);
	double x, y;
	while (cin >> x >> y)
	{
		Point p2(x, y);
		cout << p2 - p1 << endl;
	}
	return 0;
}

**第五题:**时间类的12小时制输出

Description

封装一个时间类Time,用于时间处理的相关功能,支持24小时制和12小时制,支持以下操作:

  1. Time::Time()无参构造方法。
  2. Time::Time(int,int,int)构造方法:传递时分秒的三个参数构造对象。
  3. Time::Time(const T&)拷贝构造方法。
  4. 成员读函数:
    Time::hour() :返回Time的小时数;
    Time::minute():返回Time的分钟数;
    Time::second():返回Time的秒数。
  5. 成员写函数:
    Time::hour(int) :传参修改Time的小时数;
    Time::minute(int):传参修改Time的分钟数;
    Time::second(int):传参修改Time的秒数。
  6. 对象整体读写方法:
    Time::setTime(int,int,int)方法:传递时分秒三个参数修改Time对象的时分秒数。该方法返回修改后的对象。
    Time::setTime(const T&)方法:传递一个参数修改Time对象的时分秒数。该方法返回修改后的对象。
    Time::getTime()方法:返回对象自身的引用。其实,t.getTime()即t。
    仅在Time类中的Time::getTime()方法实在是多余,在组合或者继承关系时才会有机会用到。
  7. Time::inputTime()方法:按格式从标准输入读取数据修改Time对象的时分秒数值。该方法返回修改后的对象。
  8. Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。如果对象不是合法的时间,则输出“Time error”。
  9. Time::showTime12Hour()方法:输出12小时制的时间:上午输出“hh:mm:ss a.m.”、下午输出“hh:mm:ss p.m.”。如果对象不是合法的时间,则输出“Time error”。注意:该函数仅显示12小时制时间,并不修改对象的数据成员,对象依然存储24小时制时间。
    12小时制以数字12、1、2、3、4、5、6、7、8、9、10、11依次序表示每个时段的。
    24小时制的00:00~00:59,是12小时制的12:00 a.m.~12:59 a.m.;
    24小时制的1:00~11:59是十二小时制的1:00 a.m.~11:59 a.m.。
    24小时制的12:00~12:59,是12小时制的12:00 p.m.~12:59 p.m.;
    24小时制的13:00~23:59是十二小时制的1:00 p.m.~11:59 p.m.。
    你设计一个时间类Time,使得main()函数能够正确运行。
    函数调用格式见append.cc。
    append.cc中已给出main()函数。main()函数内容稍微繁复,仅为测试对象的各种调用情况。

Input

输入的第一个整数n,表示有n组测试数据,每组3个整数:hh,mm,ss,分别表示时、分、秒,其值都在int范围内。

Output

开始部分为由main()函数产生的固定输出,用于测试对象的某些方法的调用情况。输出“Test data output :”之后为测试数据对应的输出:
每组测试数据对应一组输出,奇数行的输入对应输出24小时制时间“hh:mm:ss”,偶数行的输入对应输出12小时制时间:上午输出“hh:mm:ss a.m.”、下午输出“hh:mm:ss p.m.”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“Time error”。格式见sample。

Sample Input

6
0 0 1
0 59 59
1 1 60
23 0 0
23 59 59
24 1 0

Sample Output

Constant test output :
11:59:58 a.m.
12:00:01 p.m.
11:59:58
12:00:01

Test data output :
00:00:01
12:59:59 a.m.
Time error
11:00:00 p.m.
23:59:59
Time error

题目给定代码

int main()
{
    cout<<"Constant test output :"<<endl;
    const Time c(11, 59, 58);
    const Time cc(12, 0, 1);
    c.showTime12Hour();
    cc.showTime12Hour();
    c.showTime();
    cc.showTime();
 
    cout<<"\nTest data output :"<<endl;
    Time t;
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        if(i % 4 == 0)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            Time tt(hour, minute, second);
            tt.showTime12Hour();
        }
        if(i % 4 == 1)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            t.setTime(hour, minute, second).showTime();
        }
        if(i % 4 == 2)
            t.inputTime().showTime12Hour();
        if(i % 4 == 3)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            t.hour(hour);
            t.minute(minute);
            t.second(second);
            t.showTime();
        }
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Time {
private:
    int Time_hour, Time_minute, Time_second;
public:
    Time() {

    }
    Time(int h, int m, int s) {
        Time_hour = h, Time_minute = m, Time_second = s;
    }
    Time(Time &T) {
        Time_hour = T.Time_hour, Time_minute = T.Time_minute, Time_second = T.Time_second;
    }
    int hour(){
        return Time_hour;
    }
    int minute() {
        return Time_minute;
    }
    int second() {
        return Time_second;
    }
    void hour(int h) {
        Time_hour = h;
    }
    void minute(int m) {
        Time_minute = m;
    }
    void second(int s) {
        Time_second = s;
    }
    Time& setTime(int h, int m, int s) {
        Time_hour = h, Time_second = s, Time_minute = m;
        return *this;
    }
    Time& setTime(Time& T) {
        Time_hour = T.Time_hour, Time_minute = T.Time_minute, Time_second;
        return *this;
    }
    Time& getTime() {
        return *this;
    }
    Time& inputTime() {
        cin >> Time_hour >> Time_minute >> Time_second;
        return *this;
    }
    const void showTime() const {
        if (Time_hour >= 24 || Time_minute >= 60 || Time_second >= 60 || Time_hour < 0 || Time_minute < 0 || Time_second < 0) {
            cout << "Time error" << endl;
        }
        else {
            cout << setw(2) << setfill('0') << Time_hour << ":" << setw(2) << setfill('0') << Time_minute << ":" << setw(2) << setfill('0') << Time_second << endl;
        }
    }
    void showTime12Hour()const {
        if (Time_hour >= 24 || Time_minute >= 60 || Time_second >= 60 || Time_hour < 0 || Time_minute < 0 || Time_second < 0) {
            cout << "Time error" << endl;
        }
        else{
            if (Time_hour == 12) {
                cout << setw(2) << setfill('0') << 12 << ":" << setw(2) << setfill('0') << Time_minute << ":" << setw(2) << setfill('0') << Time_second << " p.m." << endl;
            }
            else if (Time_hour == 0) {
                cout << setw(2) << setfill('0') << 12 << ":" << setw(2) << setfill('0') << Time_minute << ":" << setw(2) << setfill('0') << Time_second << " a.m." << endl;
            }               
            else if (Time_hour > 12 && Time_hour < 24) {
                cout << setw(2) << setfill('0') << Time_hour - 12 << ":" << setw(2) << setfill('0') << Time_minute << ":" << setw(2) << setfill('0') << Time_second << " p.m." << endl;
            }              
            else {
                cout << setw(2) << setfill('0') << Time_hour << ":" << setw(2) << setfill('0') << Time_minute << ":" << setw(2) << setfill('0') << Time_second << " a.m." << endl;
            }              
        }
    }
};

int main()
{
    cout << "Constant test output :" << endl;
    const Time c(11, 59, 58);
    const Time cc(12, 0, 1);
    c.showTime12Hour();
    cc.showTime12Hour();
    c.showTime();
    cc.showTime();

    cout << "\nTest data output :" << endl;
    Time t;
    int cases;
    cin >> cases;
    for (int i = 1; i <= cases; ++i)
    {
        if (i % 4 == 0)
        {
            int hour, minute, second;
            cin >> hour >> minute >> second;
            Time tt(hour, minute, second);
            tt.showTime12Hour();
        }
        if (i % 4 == 1)
        {
            int hour, minute, second;
            cin >> hour >> minute >> second;
            t.setTime(hour, minute, second).showTime();
        }
        if (i % 4 == 2)
            t.inputTime().showTime12Hour();
        if (i % 4 == 3)
        {
            int hour, minute, second;
            cin >> hour >> minute >> second;
            t.hour(hour);
            t.minute(minute);
            t.second(second);
            t.showTime();
        }
    }
}

**第六题:**时间类的静态成员计数

Description

封装一个时间类Time,用于时间处理的相关功能,支持以下操作:

  1. Time::Time()无参构造方法。
  2. Time::Time(int,int,int)构造方法:传递时分秒的三个参数构造对象。
  3. Time::Time(const T&)拷贝构造方法。
  4. 对象整体读写方法:
    Time::setTime(int,int,int)方法:传递时分秒三个参数修改Time对象的时分秒数。该方法返回修改后的对象。
    Time::setTime(const T&)方法:传递一个参数修改Time对象的时分秒数。该方法返回修改后的对象。
    Time::getTime()方法:返回对象自身的引用。其实,t.getTime()即t。
    仅在Time类中的Time::getTime()方法实在是多余,在组合或者继承关系时才会有机会用到。
  5. Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。如果对象不是合法的时间,则输出“Time error”。
  6. 静态成员方法:
    Time::getNumber()方法:返回程序中已创建的Time对象总数。
    Time::displayNumber()方法:输出程序中已创建的Time对象总数。
    注意:在用Time对象传递参数时应传对象的引用而不是直接传对象,返回对象时同样返回引用,以免产生多余的对象拷贝。多余的拷贝构造会引起对象计数的错误。
    你设计一个时间类Time,使得main()函数能够正确运行。
    函数调用格式见append.cc。
    append.cc中已给出main()函数。main()函数内容稍微繁复,仅为测试对象的各种调用情况。

Input

输入为多行,每行为一组测试数据,每组3个整数:hh,mm,ss,分别表示时、分、秒,其值都在int范围内。

Output

开始部分为由main()函数产生的固定输出,用于测试对象的某些方法的调用情况。输出“Test data output :”之后为测试数据对应的输出:
每组测试数据对应一组输出“hh:mm:ss”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“Time error”。格式见sample。
最后一行输出一个整数n,表示有n组测试数据输入。

Sample Input

0 0 1
0 59 59
1 1 60
23 0 0
23 59 59
24 1 0

Sample Output

Static member test output :
Now, There is 0 object of Time.
Now, There is 1 object of Time.
There was a call to the copy constructor : 0,0,0
Now, There is 2 object of Time.
Now, There is 3 object of Time.
There was a call to the copy constructor : 1,2,3
Now, There is 4 object of Time.

Test data output :
00:00:01
00:59:59
Time error
23:00:00
23:59:59
Time error
6

题目给定代码

int main()
{
    cout<<"Static member test output :"<<endl;
    Time::displayNumber();
    Time t;
    t.displayNumber();
    Time tt(t);
    tt.displayNumber();
    Time ttt(1, 2, 3);
    ttt.displayNumber();
    Time tttt(ttt.getTime());
    tttt.displayNumber();
    int non_cases = Time::getNumber();
 
    cout<<"\nTest data output :"<<endl;
    int hour, minute, second;
    while(cin>>hour>>minute>>second)
    {
        Time t;
        t.setTime(hour, minute, second).showTime();
    }
    cout<<t.getNumber() - non_cases<<endl;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Time {
private:
    int hour, minute, second;
    static int count;
public:
    Time() {
        hour = 0, minute = 0, second = 0;
        ++count;
    }
    Time(int h, int m, int s) {
        hour = h, minute = m, second = s;
        ++count;
    }
    Time(const Time& T) {
        hour = T.hour, minute = T.minute, second = T.second;
        ++count;
        cout << "There was a call to the copy constructor : " << hour << "," << minute << "," << second << "\n";
    }
    Time& setTime(int h, int m, int s) {
        hour = h, minute = m, second = s;
        return *this;
    }
    Time& setTime(Time& T) {
        hour = T.hour, minute = T.minute, second = T.second;
        return *this;
    }
    Time getTime() {
        return *this;
    }
    void showTime() {
        if (hour >= 24 || minute >= 60 || second >= 60 || hour < 0 || minute < 0 || second < 0) {
            cout << "Time error" << endl;
        }
        else {
            cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl;
        }
    }
    static void displayNumber() {
        cout << "Now, There is " << count << " object of Time.\n";
    }
    static int getNumber() {
        return count;
    }
};
int Time::count = 0;

int main()
{
    cout << "Static member test output :" << endl;
    Time::displayNumber();
    Time t;
    t.displayNumber();
    Time tt(t);
    tt.displayNumber();
    Time ttt(1, 2, 3);
    ttt.displayNumber();
    Time tttt(ttt.getTime());
    tttt.displayNumber();
    int non_cases = Time::getNumber();

    cout << "\nTest data output :" << endl;
    int hour, minute, second;
    while (cin >> hour >> minute >> second)
    {
        Time t;
        t.setTime(hour, minute, second).showTime();
    }
    cout << t.getNumber() - non_cases << endl;
}

**第七题:**时间类的加、减法赋值运算

Description

封装一个时间类Time,在类上重载以下运算符,使得main()函数能够正确运行。

  1. Time::Time()无参构造方法。
  2. Time::inputTime()方法:按格式从标准输入读取数据修改Time对象的时分秒数值。该方法返回修改后的对象。
  3. Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。如果对象不是合法的时间,则输出“Time error”。
  4. 运算符
    加法赋值运算符“+=”和减法赋值运算符“-=”:把一个整数m加到Time对象自身,并且仅对合法的时间操作,不会产生不合法的时间,比如:
    若原时间对象为“00:00:00”,减去2后的对象为“23:59:58”;
    若原时间对象为“23:59:59”,加上1后的对象为“00:00:00”;
    若原时间对象为“24:60:60”,加减后的对象仍为“24:60:60”
    函数调用格式见append.cc。
    append.cc中已给出main()函数

Input

输入的第一个整数n,表示有n组测试数据,每组4个整数,前三个整数为:hh,mm,ss,分别表示时、分、秒,其值都在int范围内,最后一个整数为m。

Output

每个输入对应两行输出,分别为时间“hh,mm,ss”加上m秒和减去m秒后的值。错误的时间输出“Time error”

Sample Input

6
0 0 1 2
0 59 59 1
1 1 60 10
23 0 0 60
23 59 59 100
24 1 0 3

Sample Output

00:00:03
23:59:59
01:00:00
00:59:58
Time error
Time error
23:01:00
22:59:00
00:01:39
23:58:19
Time error
Time error

题目给定代码

int main()
{
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        Time t;
        t.inputTime();
        Time tt(t);
        int num;
        cin>>num;
        t += num;
        t.showTime();
        tt -= num;
        tt.showTime();
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Time {
private:
    int hour, minute, second;
public:
    Time() {

    }
    Time& inputTime() {
        cin >> hour >> minute >> second;
        return *this;
    }
    void showTime() {
        if (hour >= 24 || minute >= 60 || second >= 60 || hour < 0 || minute < 0 || second < 0) {
            cout << "Time error" << endl;
        }
        else {
            cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl;
        }
    }
    Time operator += (int a){
        if (hour >= 24 || minute >= 60 || second >= 60 || hour < 0 || minute < 0 || second < 0) {
        
        }
        else{
            second += a;
            while (second >= 60){
                second -= 60; 
                ++minute;
            }
            while (minute >= 60){
                minute -= 60; 
                ++ hour;
            }
            hour %= 24;
            return *this;
        }
    }
    Time operator -= (int a){
        if (hour >= 24 || minute >= 60 || second >= 60 || hour < 0 || minute < 0 || second < 0) {
        
        }
        else{
            second -= a;
            while (second < 0){
                second += 60; 
                --minute;
            }
            while (minute < 0){
                minute += 60; 
                --hour;
            }
            while (hour < 0){
                hour += 24;
            }
            return *this;
        }
    }  
};

int main()
{
    int cases;
    cin >> cases;
    for (int i = 1; i <= cases; ++i)
    {
        Time t;
        t.inputTime();
        Time tt(t);
        int num;
        cin >> num;
        t += num;
        t.showTime();
        tt -= num;
        tt.showTime();
    }
}

猜你喜欢

转载自blog.csdn.net/a1351937368/article/details/106247342