码迷,mamicode.com
首页 > 其他好文 > 详细

The time machine-时间机器计算差值(二十四小时内)

时间:2014-08-31 10:37:31      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:the time machine-时间机器计算差值(二十四小时内)

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!


The time machine-时间机器计算差值(二十四小时内)

标签:the time machine-时间机器计算差值(二十四小时内)

原文地址:http://9320314.blog.51cto.com/9310314/1546946

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!