首页 > 代码库 > The time machine-时间机器计算差值(二十四小时内)
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-时间机器计算差值(二十四小时内)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。