如何在使用C ++创建的测验程序中插入倒数计时器? [英] How to insert a countdown timer in a quiz program that is created using c++?
问题描述
所有带有5分的10个问题都必须在时间内回答.因此,应该显示每个问题n剩余时间所消耗的时间.有人可以帮忙吗?
All the 10 questions with 5 marks need to be answered within time. so the time consumed for each question n remaining time should be displayed. can anybody help?
推荐答案
便携式C ++解决方案是使用 boost.chrono .
A portable C++ solution would be to use chrono::steady_clock
to measure time. This is available in C++11 in the header <chrono>
, but may well be available to older compilers in TR1 in <tr1/chrono>
or boost.chrono.
稳定时钟总是以尽可能均匀"的速率前进,这是在多任务多线程平台上的重要考虑因素.稳定时钟也独立于任何类型的挂钟",例如系统时钟(可以随时任意操纵).
The steady clock always advances at a rate "as uniform as possible", which is an important consideration on a multi-tasking multi-threaded platform. The steady clock is also independent of any sort of "wall clock", like the system clock (which may be arbitrarily manipulated at any time).
(注意:如果您的实现中没有 steady_clock
,请查找 monotonic_clock
.)
(Note: if steady_clock
isn't in your implementation, look for monotonic_clock
.)
< chrono>
类型使用起来有点麻烦,因此这里有一段示例代码,它返回稳定的时间戳(或者更确切地说,是您喜欢的任何时钟的时间戳,例如 high_resolution_clock
):
The <chrono>
types are a bit fiddly to use, so here is a sample piece of code that returns a steady timestamp (or rather, a timestamp from whichever clock you like, e.g. the high_resolution_clock
):
template <typename Clock>
long long int clockTick(int multiple = 1000)
{
typedef typename Clock::period period;
return (Clock::now().time_since_epoch().count() * period::num * multiple) / period::den;
}
typedef std::chrono::monotonic_clock myclock; // old
typedef std::chrono::steady_clock yourclock; // C++11
用法:
long long int timestamp_ms = clockTick<myclock>(); // milliseconds by default
long long int timestamp_s = clockTick<yourclock>(1); // seconds
long long int timestamp_us = clockTick<myclock>(1000000); // microseconds
这篇关于如何在使用C ++创建的测验程序中插入倒数计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!