Subtract time in minutes and seconds

Title Description: Define a time class, minutes and seconds are its two private member data. Enter a start time and an end time (start time is earlier than the end time), through operator overloading-(minus sign), calculate how many seconds between these two times. Note: These two times are within the same hour, and a 60-minute, 60-second time division is used, which is from 00: 00-59: 59.

Input format: The test input contains several test cases, and each test case occupies one line. Each test case includes four numbers, each number is separated by a space, each number is composed of two digits, the first number and the second number represent the start time in minutes and seconds, the third The number and the fourth number represent the end time in minutes and seconds, 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 seconds 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 second;
public:
    void set(int m, int s) {
        minute = m;
        second=s;
    }

    friend int operator-(Time, Time);

};

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


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/105448947