2 回答
TA贡献1862条经验 获得超6个赞
#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;
}
TA贡献2080条经验 获得超4个赞
#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;
}
- 2 回答
- 0 关注
- 741 浏览
添加回答
举报