The time machine-时间机器计算差值(二十四小时内):输入hours、minutes,1代表AM,0代表PM.

//The time machine-时间机器计算差值(二十四小时内)
#include<iostream>

int computeDifference(int startHours, int startMinutes, bool startIsAM,int endHours,   int endMinutes,   bool endIsAM);

int main()
{
    using namespace std;
    int startHours,startMinutes,endHours,endMinutes;
    bool startIsAM,endIsAM;
    int total_minutes;
    
    cout<<"Please inout the start time (startHours,startMinutes,startIsAM(1:AM;0:PM)):\n";
    cin>>startHours>>startMinutes>>startIsAM;
    cout<<"Please inout the end time (endHours,endMinutes,endIsAM(1:AM;0:PM)):\n";
    cin>>endHours>>endMinutes>>endIsAM;
    
    total_minutes = computeDifference(startHours,startMinutes,startIsAM,endHours,endMinutes,endIsAM);
    
    cout<<"The difference value of the two time is "<<total_minutes<<" minutes!"<<endl;
    
    return 0;
}

int computeDifference(int startHours,int startMinutes,bool startIsAM,int endHours,int endMinutes,bool endIsAM)
{
    using namespace std;
    int total_minutes;
    if(true == startIsAM && false == endIsAM)
        {
            if(startMinutes <= endMinutes)
                total_minutes = (endHours - startHours)*60 + (endMinutes - startMinutes);
            else
                total_minutes = (endHours -1 - startHours)*60 + (endMinutes + 60 - startMinutes);
        }
    else if(true == startIsAM && true == endIsAM)
        {
            if(startHours < endHours)
                {
                    if(startMinutes <= endMinutes)
                        total_minutes = (endHours - startHours)*60 + (endMinutes - startMinutes);
                    else
                        total_minutes = (endHours -1 - startHours)*60 + (endMinutes + 60 - startMinutes); 
                }
            else if(startHours > endHours)
                {
                    if(startMinutes >= endMinutes)
                        total_minutes =24*60 - (startHours - endHours)*60 + (startMinutes - endMinutes);
                    else
                        total_minutes =24*60 - (startHours -1 - endHours)*60 + (startMinutes + 60 - endMinutes); 
                }
            else if(startHours == endHours)
                {
                    if(startMinutes <= endMinutes)
                        total_minutes = (endHours - startHours)*60 + (endMinutes - startMinutes);
                    else
                        total_minutes = 24 * 60 - (startMinutes - endMinutes);
                }
        }
    else if(false == startIsAM && false == endIsAM)
        {
            if(startHours < endHours)
                {
                    if(startMinutes <= endMinutes)
                        total_minutes = (endHours - startHours)*60 + (endMinutes - startMinutes);
                    else
                        total_minutes = (endHours -1 - startHours)*60 + (endMinutes + 60 - startMinutes); 
                }
            else if(startHours > endHours)
                {
                    if(startMinutes >= endMinutes)
                        total_minutes =24*60 - (startHours - endHours)*60 - (startMinutes - endMinutes);
                    else
                        total_minutes =24*60 - (startHours -1 - endHours)*60 - (startMinutes + 60 - endMinutes); 
                } 
            else if(startHours == endHours)
                {
                    if(startMinutes <= endMinutes)
                        total_minutes = (endHours - startHours)*60 + (endMinutes - startMinutes);
                    else
                        total_minutes = 24 * 60 - (startMinutes - endMinutes);
                }
        }
    else if(false == startIsAM && true == endIsAM)
        {
            if(startMinutes >= endMinutes)
                total_minutes =24*60 - (startHours - endHours)*60 - (startMinutes - endMinutes);
            else
                total_minutes =24*60 - (startHours -1 - endHours)*60 - (startMinutes + 60 - endMinutes);
        }
        
    return total_minutes;
}

结果:

Please inout the start time (startHours,startMinutes,startIsAM(1:AM;0:PM)):
11 59 1
Please inout the end time (endHours,endMinutes,endIsAM(1:AM;0:PM)):
12 01 0
The difference value of the two time is 2 minutes!

Please inout the start time (startHours,startMinutes,startIsAM(1:AM;0:PM)):
11 59 1
Please inout the end time (endHours,endMinutes,endIsAM(1:AM;0:PM)):
11 58 1
The difference value of the two time is 1439 minutes!