# Operational Questions of Shandong University of Science and Technology on May 18, 2020

Keywords: less Attribute Programming

## Operational Questions of Shandong University of Science and Technology on May 18, 2020

**Title 1: **Square, Rectangle, Cube Two

Description

Give the length of Square, Rectangle, Cube, Cuboid and find the area.
Squares and cubes have equal sides, so you only need to store one side length.
A rectangle stores two side lengths.
A cube needs to store three edge lengths
Designs a class of Shape for polymorphic implementation to seek (table) area.
Please read carefullyAppend.ccCode, and designed the square, rectangle, cube, cube derivation relationship so that the main() function can run and get the correct output.

Input

Enter the first integer n to indicate that there are n graphics behind it.Each graphic takes up one line of input.SQR means square, CUB means cube, followed by only one side length; RCT means rectangle, followed by length and width; CBD means cube, followed by length, width and height.

Output

From the second line of input, each line outputs a graphic (table) area string.

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

Title given code

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];
}

Range

#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];
}

**Title 2: ** Animals want to eat

Description

Define the Animal class with only one pure virtual function eat.

Define four classes, Dog, Cat, Sheep, Chicken, which are subclasses of Animal.

Each class overloads the eat method, and the output of each method is shown as an example.

Input

A series of integers in the range of 0 to 3.

Output

Each input corresponds to one line of output, with 0, 1, 2, 3 representing Dog, Cat, Sheep, Chicken, respectively.

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.

Title given function

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;
}

Range

#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;
}

**Title 3: ** Small Airplane Warfare

Description

Airplane Battle is a fun little game. As a programming maniac, I don't want to write a fun game myself. How can I say that?

Now, use C++ to write a small prototype of an airplane war game, including at least the following:

1. FlyThing class: The parent class of enemy and warplane, is an abstract class.

(1) It has a name attribute and a location attribute, where the name is a string and the location is a coordinate in two-dimensional space, representing the location on the screen.

(2) pure virtual function void fly(), which simulates the flight action of an aircraft.

(3) void show(): Display the name and location of the aircraft.The format is: "\$at x y", where \$is the name of the aircraft and X and y are the horizontal and vertical coordinates.

(4) other necessary functions.

1. EnemyPlane class: Enemy class, a subclass of FlyThing.Its Fly() function adds 1 to the plane's ordinate.

2. MyPlane class: The fighter class, which is a subclass of FlyThing.Its Fly() function flies according to the input.Specifically:

(1) If A or a is entered, the horizontal coordinates are minus 1;

(2) If S or s is input, the ordinate coordinates are added 1;

(3) If W or W is input, the ordinate coordinates are minus 1;

(4) If D or D is entered, then the horizontal coordinates are added by 1.

Input

Line 1 N is a positive integer, followed by line N, each containing a string and two integers are the name of the enemy aircraft and the horizontal and vertical coordinates, respectively.

Line N+2 is a positive integer M, after which line M is a character, one of a, s, d, w, for the flight of a fighter.

Output

See example~

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

Title given code

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;
}

Range

#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;
}

**Q4: ** Operator overload: Distance between points

Description

Defines a Point class with two properties of type double representing the horizontal and vertical coordinates of a point.Overrides its subtraction operator to calculate the distance between two points.

Input

Enter the coordinates of a series of points, each of which takes up one row.

Output

See example.

Sample Input

20 20
30 30
40 40
50 50

Sample Output

14.1421
28.2843
42.4264
56.5685

Title given code

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

Range

#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;
}

**Title 5: ** 12-hour time class output

Description

Encapsulates a time class Time for time processing related functions, supports 24-hour and 12-hour systems, and supports the following operations:

1. Time::Time() parameterless construction method.
2. Time:: Time (int, int) construction method: Pass time, seconds, three parameters to construct the object.
3. Time:: Time (const T&) copy construction method.
Time::hour(): The number of hours to return time;
Time::minute(): The number of minutes to return time;
Time::second(): The number of seconds to return the time.
5. Member write function:
Time::hour(int): The number of hours a parameter modifies a Time;
Time::minute(int): The number of minutes a parameter modifies a Time;
Time::second(int): The number of seconds a parameter modifies a Time.
6. Overall object reading and writing method:
Time:: setTime (int, int) method: The number of seconds in seconds in seconds when three parameters are passed to modify the Time object.This method returns the modified object.
Time:: setTime (const T&) method: The number of seconds and minutes in which a parameter is passed to modify the Time object.This method returns the modified object.
Time::getTime() method: Returns a reference to the object itself.In fact, t.getTime() is t.
Only the Time::getTime() method in the Time class is really redundant and will be used when combining or inheriting relationships.
7. Time::inputTime() method: A time-and-second value that modifies a Time object by reading data from standard input in a format.This method returns the modified object.
8. Time::showTime() method: output "hh:mm:ss", less than two to be preceded by 0."Time error" is output if the object is not a valid time.
9. Time::showTime12Hour() Method: Output 12-hour time: hh:m m:ss a.m. in the morning and hh:m m:ss p.m. in the afternoon."Time error" is output if the object is not a valid time.Note: This function only displays 12-hour time, does not modify the data members of the object, and the object still stores 24-hour time.
The 12-hour system represents each period in sequence of numbers 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.
00:00-00:59 for 24 hours is 12:00 a.m. -12:59 a.m. for 12 hours.
1:00-11:59 for 24 hours is 1:00 a.m. -11:59 a.m. for 12 hours.
12:00-12:59 for 24 hours is 12:00 p.m. -12:59 p.m. for 12 hours;
13:00-23:59 for 24-hour systems is 1:00 p.m. -11:59 p.m. for 12-hour systems.
You designed a time class, Time, to make the main() function work correctly.
See the format for function callsAppend.cc.
Append.ccThe main() function has been given.The main() function is slightly more cumbersome and is only used for various calls to the test object.

Input

The first integer n entered represents N sets of test data, each set of three integers: hh,mm,ss, representing hours, minutes, and seconds, with values in the int range.

Output

The starting section is the fixed output generated by the main() function to test the invocation of some methods on the object.After the output "Test data output:", the output corresponding to the test data is:
Each set of test data corresponds to a set of outputs, the input of odd rows corresponds to the output 24-hour time "hh:m m:ss", the input of even rows corresponds to the output 12-hour time: hh:m m:ss a.m. in the morning, hh:m m:ss p.m. in the afternoon, and less than two outputs need to be preceded by 0.If the time entered is not valid, "Time error" is output.See sample for format.

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

Title given code

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();
}
}
}

Range

#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();
}
}
}

**Title 6: ** Static member count for time class

Description

Encapsulates a time-class Time for time processing-related functions, supporting the following operations:

1. Time::Time() parameterless construction method.
2. Time:: Time (int, int) construction method: Pass time, seconds, three parameters to construct the object.
3. Time:: Time (const T&) copy construction method.
4. Overall object reading and writing method:
Time:: setTime (int, int) method: The number of seconds in seconds in seconds when three parameters are passed to modify the Time object.This method returns the modified object.
Time:: setTime (const T&) method: The number of seconds and minutes in which a parameter is passed to modify the Time object.This method returns the modified object.
Time::getTime() method: Returns a reference to the object itself.In fact, t.getTime() is t.
Only the Time::getTime() method in the Time class is really redundant and will be used when combining or inheriting relationships.
5. Time::showTime() method: output "hh:mm:ss", less than two to be preceded by 0."Time error" is output if the object is not a valid time.
6. Static member method:
Time::getNumber() method: Returns the total number of Time objects created in the program.
Time::displayNumber() method: The total number of Time objects created in the output program.
Note: When passing parameters with a Time object, the reference to the object should be passed instead of the object directly. When returning the object, the reference should also be returned in order to avoid creating an extra copy of the object.Excess copy construction can cause an object count error.
You designed a time class, Time, to make the main() function work correctly.
See the format for function callsAppend.cc.
Append.ccThe main() function has been given.The main() function is slightly more cumbersome and is only used for various calls to the test object.

Input

The input is a multi-line, one set of test data for each behavior, each set of three integers: hh,mm,ss, representing hours, minutes, and seconds, with values in the int range.

Output

The starting section is the fixed output generated by the main() function to test the invocation of some methods on the object.After the output "Test data output:", the output corresponding to the test data is:
Each set of test data corresponds to a set of output "hh:mm:ss", less than two outputs need to be preceded by 0.If the time entered is not valid, "Time error" is output.See sample for format.
The last line outputs an integer n, meaning there are N sets of test data inputs.

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

Title given code

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;
}

Range

#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;
}

**Title 7: ** Addition and subtraction assignment of time classes

Description

Encapsulates a time class, Time, and overloads the following operators on the class to make the main() function work correctly.

1. Time::Time() parameterless construction method.
2. Time::inputTime() method: A time-and-second value that modifies a Time object by reading data from standard input in a format.This method returns the modified object.
3. Time::showTime() method: output "hh:mm:ss", less than two to be preceded by 0."Time error" is output if the object is not a valid time.
4. operator
Addition assignment operator'+='and subtraction assignment operator'-=': adding an integer m to the Time object itself and only for legitimate time operations does not result in illegal time, such as:
If the original time object is "00:00:00", the object after subtracting 2 is "23:59:58";
If the original time object is "23:59:59", the object after adding 1 is "00:00:00";
If the original time object is "24:60:60", the object after adding or subtracting is still "24:60:60"
See the format for function callsAppend.cc.
Append.ccThe main() function has been given

Input

The first integer n entered represents N sets of test data, each with 4 integers. The first three integers are hh, m m, ss, representing hours, minutes, and seconds. The values are in the int range, and the last integer is m.

Output

Each input corresponds to two lines of output, the time "hh, m m, ss" plus m seconds and minus m seconds.Error time output "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

Title given code

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();
}
}

Range

#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();
}
}

Posted by hmvrulz on Wed, 20 May 2020 10:35:07 -0700