首页 > 代码库 > waiting time-计算等待时间之后的时刻

waiting time-计算等待时间之后的时刻

waiting time-计算等待时间之后的时刻,按24小时制计算,允许重复计算:

//waiting time
#include<iostream>
using namespace std;

void get_time(int& hours,int& minutes);
void convert_time(int& currenthours,int& currentminutes,int& waithours,int& waitminutes);
//void show_time();

int main()
{
    int currenthours,currentminutes;
    int waithours,waitminutes;
    char ans;
    
    do{
        cout<<"Current time,";
        get_time(currenthours,currentminutes);
        cout<<"Wait time,";
        get_time(waithours,waitminutes);
        convert_time(currenthours,currentminutes,waithours,waitminutes);
    
        cout<<"Do you want again?";
        cin>>ans;
    }while(‘y‘ == ans || ‘Y‘ == ans);
    
    return 0;
}

void get_time(int& hours,int& minutes)
{
    char b;
    cout<<"Enter the 24 hours time like 11:11 :\n";
    cin>>hours>>b>>minutes;
}

void convert_time(int& currenthours,int& currentminutes,int& waithours,int& waitminutes)
{
    int hours,minutes;
    if(currenthours + waithours < 24)
        {
            hours = currenthours+waithours;
            if(currentminutes + waitminutes < 60)
                minutes = currentminutes + waitminutes;
            else 
                {
                    hours += 1;
                    minutes =(currentminutes + waitminutes) - 60;
                }
        }
    else 
        {
            hours = (currenthours+waithours) - 24 + currenthours;
            
            if(currentminutes + waitminutes < 60)
                minutes = currentminutes + waitminutes;
            else 
                {
                    hours += 1;
                    minutes =(currentminutes + waitminutes) - 60;
                }
        }
    
    cout<<"After the waiting time,the time is "<<hours<<":"<<minutes<<endl;    
    
    return;
    
}

结果:

Current time,Enter the 24 hours time like 11:11 :
12:30
Wait time,Enter the 24 hours time like 11:11 :
15:40
After the waiting time,the time is 16:10
Do you want again?y
Current time,Enter the 24 hours time like 11:11 :
8:30
Wait time,Enter the 24 hours time like 11:11 :
15:10
After the waiting time,the time is 23:40
Do you want again?


waiting time-计算等待时间之后的时刻