[C++/PTA] Calculation time subtraction
Topic requirements
Title description: Define a time class, hour and minute are its two private member data. Enter a start time and an end time (the start time is earlier than the end time), and use the operator overload - (minus sign) to calculate how many minutes are between the two times. Explanation: These two times are within the same day, and use the 24-hour timing formula, that is, from 00:00-23:59.
Input format: The test input contains several test cases, each test case occupies a line. Each test case includes four numbers, separated by spaces, each number is composed of two digits, the first number and the second number represent the hour and minute of the starting time, the third number and the fourth The numbers represent the hours and minutes of the end time, respectively. When the input of a test case is 00 00 00 00, the input ends, and the corresponding result is not output.
Output format: output one line for each test case. Just output a number, indicating the number of minutes between the two.
Input example:
12 11 12 58
00 13 16 00
09 07 23 59
00 00 00 00
Sample output:
47
947
892
problem solving ideas
Define a class Time
named with the following private member variables:
hour
: Hourminute
: minute
and the following public member functions:
- Constructor: parameters for hours and minutes can be passed in.
- Friend function
operator-
: Overload-
the operator to calculateTime
the time difference between two objects of type (unit: minutes).
In operator-
the function , use the idea of time judgment to calculate the difference in minutes between two times.
Judging the relationship between the minutes of the two Time
objects , if p.minute
is q.minute
smaller or equal to , it means that the whole point has not been crossed, and the time difference is directly calculated by the difference between the hours and minutes. Otherwise, it means that p
the add 1 to the hour and add 60 to the minute, and then q
add it to the difference between the hour and minute to get the time difference.
In main()
the function , use the loop and cin
to read multiple sets of time data.
If the four numbers read are all 0, exit the loop. Otherwise, create two objects of Time
type p1
and p2
and pass the entered hours and minutes as parameters.
p2
Assign p1
the result of subtracting the time difference to a variable difference
, and finally difference
output .
the code
#include<iostream>
using namespace std;
// 声明类 Time
class Time {
private:
int hour; // 小时数
int minute; // 分钟数
public:
// 构造函数,默认参数值为 0
Time(int h = 0, int m = 0) {
hour = h;
minute = m;
}
// 友元函数 operator- 的声明
friend int operator- (Time p, Time q);
};
// 定义友元函数 operator-
int operator- (Time q, Time p) {
int c = 0; // 定义时间差变量
int d = 0; // 定义小时差变量
if (p.minute < q.minute || p.minute == q.minute) {
// 如果第一个时间比第二个时间早或相等
d = q.hour - p.hour; // 计算小时差
c = q.minute - p.minute + 60 * d; // 计算时间差(单位:分钟)
return c; // 返回时间差
}
else {
// 如果第一个时间比第二个时间晚
d = q.hour - p.hour - 1; // 计算小时差
c = 60 + q.minute - p.minute + 60 * d; // 计算时间差(单位:分钟)
return c; // 返回时间差
}
}
// 主函数
int main() {
int a, b, c, d; // 定义输入的四个整数
while (cin >> a >> b >> c >> d) {
// 循环读取多组数据
if (a == 0 && b == 0 && c == 0 && d == 0) // 如果读到的数据均为 0
break; // 跳出循环
int difference; // 定义时间差变量
Time p1(a, b); // 创建 Time 类型对象 p1,并传入小时数和分钟数作为参数
Time p2(c, d); // 创建 Time 类型对象 p2,并传入小时数和分钟数作为参数
difference = p2 - p1; // 计算 p2 与 p1 的时间差,并赋值给 difference
cout << difference << endl; // 输出时间差(并换行)
}
return 0; // 返回 0,代表程序顺利结束
}
Summarize
This topic mainly examines 运算符重载
and友元函数的使用方法
I am Qiu said , see you next time.