用C++实现时间的 加减运算 .

用C++实现时间的 加减运算 .


烙印99
浏览 742回答 2
2回答

阿波罗的战车

#include <iostream>using namespace std;struct Time{int hour;int minute;int second;};bool AddTime(Time&desTime, const Time &scrTime);bool SubtractTime(Time &desTime, const Time &scrTime);int main(){Time&nbsp;t1;Time t2;t1.hour = 23;t1.minute = 40;t1.second = 50;t2.hour = 10;t2.minute = 30;t2.second = 59;if ( !AddTime( t1, t2 ) ){cout<< "小时: " << t1.hour <<endl<< "分钟: " << t1.minute <<endl<< "秒: " << t1.second <<endl;}else{cout<< "你的输入的时间非法." <<endl;}t1.hour = 23;t1.minute = 40;t1.second = 50;cout<< endl;if ( !SubtractTime( t1, t2 ) ){cout<< "小时: " << t1.hour <<endl<< "分钟: " << t1.minute <<endl<< "秒: " << t1.second <<endl;}else{cout<< "你的输入的时间非法." <<endl;}return 0;}bool AddTime(Time &desTime, const Time &scrTime){if( desTime.hour >= 0 && desTime.hour <= 23 &&desTime.minute >= 0 && desTime.minute <= 59 &&desTime.second >= 0 && desTime.second <= 59 &&scrTime.hour >= 0 && scrTime.hour <= 23 &&scrTime.minute >= 0 && scrTime.minute <= 59 &&scrTime.second >= 0 && scrTime.second <= 59 ){desTime.second += scrTime.second ;int n = 0;if( desTime.second >= 60 ){n = 1;desTime.second -= 60;}desTime.minute += scrTime.minute + n;n = 0;if( desTime.minute >= 60 ){n = 1;desTime.minute -= 60;}desTime.hour += scrTime.hour + n;if( desTime.hour >= 24 ){desTime.hour -= 24;}return 0;}return 1;}bool SubtractTime(Time &desTime, const Time &scrTime){if( desTime.hour >= 0 && desTime.hour <= 23 &&desTime.minute >= 0 && desTime.minute <= 59 &&desTime.second >= 0 && desTime.second <= 59 &&scrTime.hour >= 0 && scrTime.hour <= 23 &&scrTime.minute >= 0 && scrTime.minute <= 59 &&scrTime.second >= 0 && scrTime.second <= 59 ){int n = 0;if( desTime.second < scrTime.second ){n = 1;desTime.second += 60;}desTime.second -= scrTime.second ;desTime.minute -= n;n = 0;if( desTime.minute < scrTime.minute ){n = 1;desTime.minute += 60;}desTime.minute -= scrTime.minute;desTime.hour -= n;if( desTime.hour < scrTime.hour ){return 1;}desTime.hour -= scrTime.hour;return 0;}return 1;}

犯罪嫌疑人X

#include <iostream>using namespace std;struct Time{int hour;int minute;int second;};bool AddTime(Time&desTime, const Time &scrTime);bool SubtractTime(Time &desTime, const Time &scrTime);int main(){Time t1;Time t2;t1.hour = 23;t1.minute = 40;t1.second = 50;t2.hour = 10;t2.minute = 30;t2.second = 59;if ( !AddTime( t1, t2 ) ){cout<< "小时: " << t1.hour <<endl<< "分钟: " << t1.minute <<endl<< "秒: " << t1.second <<endl;}else{cout<< "你的输入的时间非法." <<endl;}t1.hour = 23;t1.minute = 40;t1.second = 50;cout<< endl;if ( !SubtractTime( t1, t2 ) ){cout<< "小时: " << t1.hour <<endl<< "分钟: " << t1.minute <<endl<< "秒: " << t1.second <<endl;}else{cout<< "你的输入的时间非法." <<endl;}return 0;}bool AddTime(Time &desTime, const Time &scrTime){if( desTime.hour >= 0 && desTime.hour <= 23 &&desTime.minute >= 0 && desTime.minute <= 59 &&desTime.second >= 0 && desTime.second <= 59 &&scrTime.hour >= 0 && scrTime.hour <= 23 &&scrTime.minute >= 0 && scrTime.minute <= 59 &&scrTime.second >= 0 && scrTime.second <= 59 ){desTime.second += scrTime.second ;int n = 0;if( desTime.second >= 60 ){n = 1;desTime.second -= 60;}desTime.minute += scrTime.minute + n;n = 0;if( desTime.minute >= 60 ){n = 1;desTime.minute -= 60;}desTime.hour += scrTime.hour + n;if( desTime.hour >= 24 ){desTime.hour -= 24;}return 0;}return 1;}bool SubtractTime(Time &desTime, const Time &scrTime){if( desTime.hour >= 0 && desTime.hour <= 23 &&desTime.minute >= 0 && desTime.minute <= 59 &&desTime.second >= 0 && desTime.second <= 59 &&scrTime.hour >= 0 && scrTime.hour <= 23 &&scrTime.minute >= 0 && scrTime.minute <= 59 &&scrTime.second >= 0 && scrTime.second <= 59 ){int n = 0;if( desTime.second < scrTime.second ){n = 1;desTime.second += 60;}desTime.second -= scrTime.second ;desTime.minute -= n;n = 0;if( desTime.minute < scrTime.minute ){n = 1;desTime.minute += 60;}desTime.minute -= scrTime.minute;desTime.hour -= n;if( desTime.hour < scrTime.hour ){return 1;}desTime.hour -= scrTime.hour;return 0;}return 1;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP