嘿,所以我正在使用 windows GetTickCount() 和 STL 处理秒表类,但是在将 Stopwatch(int DecNumb) 构造函数实现到重载 Stopwatch(int DecNumb, char command[]) 时遇到了问题后一个构造函数中的“准确度”数据类型设置不正确。
(它似乎回到了 unsigned long int 560345 或其他东西的前值......)
这是我用来测试它的 class 和 main() 命令:
class Stopwatch
{
protected:
int accuracy;
unsigned long int initial_ilu;
unsigned long int current_ilu;
long float output_fl;
vector<long float> times;
public:
Stopwatch(int DecNumb) { // so accuracy*10 isn't 0
accuracy = 1;
for(;DecNumb>0;DecNumb--) // the Tick count will
accuracy = accuracy*10;}; // diveded by accuracy (each 0 in this number moves the decimal over once)
Stopwatch(int aDecNumb, char command[]) {Stopwatch::Stopwatch(aDecNumb);
if(command = "start") Stopwatch::Start();};
void Start(){initial_ilu = GetTickCount()/*/accuracy*/;};
long float ElapsedTime()
{
current_ilu = GetTickCount()/*/accuracy*/;
output_fl = (current_ilu - initial_ilu)/accuracy;
return output_fl;
};
void Wait(long float seconds)
{
for(unsigned long int waitTime = GetTickCount() + (seconds*accuracy);
waitTime > GetTickCount();) {}
// stay stuck in for loop until time specified is up
};
void SaveTime(){times.push_back(GetTickCount()/*/accuracy*/);};
long float GetTime(int location){if(times.size()<location+1) return times[location+1];
else return -1;};
};
这是 main()
int main()
{
Stopwatch myStopwatch(3,"start");
for(;;)
{
myStopwatch.Wait(2);
cout << myStopwatch.ElapsedTime() << endl;
}
return 0;
}
为什么精度不能保持在我在构造函数中设置的值?谢谢!=) 哦,欢迎对我的代码提供任何其他反馈!(我比较新)