Calculation time subtraction

Title Description: Define a time class, hours and minutes 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 between these two times. Note: These two times are within the same day, and the 24-hour time division is used, which is from 00: 00-23: 59.

Input format: The test input contains several test cases, and each test case occupies one 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 start time, the third number and the fourth The numbers represent the hours and minutes of the end time, respectively. When reading 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.

Sample input:

12 11 12 58

00 13 16 00

09 07 23 59

00 00 00 00

Sample output:

47

947

892

#include <iostream>

using namespace std;

class Time {
private:
    int minute;
    int hour;
public:
    void set(int h, int m) {
        minute = m;
        hour = h;
    }

    friend int operator-(Time, Time);

};

int operator-(Time t2, Time t1) {
    return (t2.hour - t1.hour) * 60 + t2.minute - t1.minute;
}


int main() {
    int a, b, c, d;
    Time t1, t2;
    cin >> a >> b >> c >> d;
    do {
        t1.set(a, b);
        t2.set(c, d);
        cout << (t2 - t1) << endl;
        cin >> a >> b >> c >> d;
    } while (!(a == 0 && b == 0 && c == 0 && d == 0));

}

Published 163 original articles · praised 18 · visits 7683

Guess you like

Origin blog.csdn.net/xcdq_aaa/article/details/105448841