我在描述问题时遇到了困难,所以如果我的问题标题看起来很奇怪,请原谅。如何在长变量中保留小时,分钟和秒钟?
我正在上课。
我于这些变量: 私人的,_ticks:记住
// 1 _ticks = 1/100 of a second
// 0 _ticks = 00:00:00.00 i.e. 12:00am
// a time is stored as a number of ticks since midnight
// for example 1234567 ticks would be 3:25:45.67am
long _ticks;
// the following static fields might come in handy
// 8,643,999 _ticks = 23:59:59.99 i.e. 11:59:59.99pm
static const long _lastTickOfTheDay = 8639999;
// 4,320,000 _ticks = 12:00:00.00 i.e 12pm i.e. noon
static const long _noon = 4320000;
// _ticks per second;
static const long _ticksPerSecond = 100;
// _ticks per minute;
static const long _ticksPerMinute = 6000;
// _ticks per hour;
static const long _ticksPerHour = 360000;
// _ticks per day
static const long _ticksPerDay = 8640000;
就这样,我提出的功能设置与小时,分钟,秒和毫秒的时间。 设置与所有这些变量的时间是相当简单:
void MyTime::SetTime(int newHrs, int newMins, int newSecs, int newMilisecs)
{
this->_ticks = (newHrs * _ticksPerHour) + (newMins * _ticksPerMinute)
+ (newSecs * _ticksPerSecond) + (newMilisecs);
}
接下来,我需要设置几个小时,分,秒的时间,同时保持了毫秒。如何做到这一点的数学无法回避,这与我所能做到的一样。正如你所看到的,没有多少:
// Hours, Minutes, Seconds
void MyTime::SetTime(int newHours, int newMinutes, int newSeconds)
{
// Take the ticks apart and put them back together
int oldTime = _ticks;
int newTime = (newHours * _ticksPerHour) + (newMinutes * _ticksPerMinute)
+ (newSeconds * _ticksPerSecond);
}
'_ticksPerSecond'为100,但允许传递毫秒数。例如,2秒和567毫秒给出2 * 100 + 567 = 767'个刻度,这由您的规模,等于7秒和67毫秒。改变这也可以是使用'60 * _ticksPerSecond','60 * _ticksPerMinute'等等的一个很好的教训,而不是仅仅是6000,360000等。它允许你只改变一个基于它而改变的那个,并且使得很明显,你正在使用60秒/分钟等。 – chris
这是一个很好的观点......这是可能的我的老师(因为这是一个家庭作业,我正在努力)作出了一个定义错误,或者我需要做比预期更多的数学...... –
@ Hobo42:评论使它清楚地表明一个嘀嗒和一个毫秒是完全不同的。 –