用c ++实现一个计时器

时间:2015-06-08 06:50:44

标签: c++

我目前正在用c ++制作游戏。我有一个计时器功能:

void timer(int &s , int &m , bool a)
{
   while (a)
   {
      Sleep(1000);
      if (s%60)
        m+=1;
      s+=1;
   }
}

以下是游戏的功能:

void game()
{
   int s = 0 , m = 0;
   char a[]="Computers";
   char b[10];
   timer(s,m,true);
   while (strcmpi(a,b)!=0)
   {
      cout<<"Guess the word:";
      gets(b);
   }
   timer(s,m,false);
   cout<<"You got it correct!\n";
   cout<<"Time taken : "<<m<<':'<<s<<endl;
}

当我运行程序时,没有任何反应。我猜测计时器正在运行,并且不允许game()中的while循环执行。

所以基本上我试图找出用户正确猜测单词的时间。

我该如何克服这个问题?

提前致谢:)

4 个答案:

答案 0 :(得分:5)

您可以使用计时功能:

#include <chrono>

using namespace chrono;

auto a = high_resolution_clock::now();

... what you want to measure ...

auto b = high_resolution_clock::now();

cout << "took " << duration_cast<seconds>(b - a).count() << << " seconds" <<  endl;

答案 1 :(得分:3)

您的代码将在while(true)上屏蔽。请考虑使用std::chrono

void game()
{
   int s = 0 , m = 0;
   char a[]="Computers";
   char b[10];
   auto start = chrono::steady_clock::now();
   while (strcmpi(a,b)!=0)
   {
      cout<<"Guess the word:";
      gets(b);
   }
   auto time_elapsed = chrono::steady_clock::now() - start;
   cout<<"You got it correct!\n";
   cout<<"Time taken : " << std::chrono::duration_cast<std::chrono::seconds>
                         (time_elapsed ).count() << " s"<<endl;
}

答案 2 :(得分:3)

如果你想要的只是时间,那就扔掉你的计时器。询问用户启动时的时间,存储时间,并从用户完成时间开始减去开始时间。

void game()
{
   int s = 0 , m = 0;
   char a[]="Computers";
   char b[10];
   time_t start = time(NULL); <<<< Modification here
   while (strcmpi(a,b)!=0)
   {
      cout<<"Guess the word:";
      gets(b);
   }
   cout<<"You got it correct!\n";
   time _t taken = time(NULL) - start;
   m = taken / 60; <<<< Modification here
   s = taken % 60; <<<< Modification here
   cout<<"Time taken : "<< m <<':'<<s<<endl;
}

如果转让允许,请考虑将char s a和b替换为string s a和b。用户不能溢出字符串,并以char数组和gets的方式对程序造成严重破坏。

void game()
{
   int s = 0 , m = 0;
   std::string a ="Computers"; <<<< Modification here
   std::string b; <<<< Modification here
   time_t start = time(NULL); 
   do
   {
      cout<<"Guess the word:";
      cin>> b; <<<< Modification here
   } while (a != b); <<<< Modification here. No sense testing b before you read b
   cout<<"You got it correct!\n";
   time _t taken = time(NULL) - start;
   m = taken / 60; 
   s = taken % 60; 
   cout<<"Time taken : "<< m <<':'<<s<<endl;
}

答案 3 :(得分:1)

您可以使用time.h头文件来测量经过的时间。您的代码可以像这样:

    exportSession = [[AVAssetExportSession alloc]
                                              initWithAsset:songAsset
                                              presetName:AVAssetExportPresetAppleM4A];
        exportSession.outputFileType = @"com.apple.m4a-audio";
    filePath = [[docDir stringByAppendingPathComponent:fileName] stringByAppendingPathExtension:@"m4a.mov"]
        exportSession.outputURL = [NSURL fileURLWithPath:filePath];
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            if (exportSession.status == AVAssetExportSessionStatusCompleted) {
                NSString *newPath = [NSString stringWithFormat:@"%@/%@", documentDirectory, [[[filePath lastPathComponent] substringToIndex:[[filePath lastPathComponent] length]-5] stringByAppendingString:@"a"]];
                [[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
                NSError *error = nil;
                [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:newPath error:&error];
            }
            if (exportSession.status == AVAssetExportSessionStatusCancelled) {
            }
            if (exportSession.status == AVAssetExportSessionStatusExporting) {
            }
            if (exportSession.status == AVAssetExportSessionStatusFailed) {
                          NSLog(@"erros:%@",exportSession.error);
            }
            if (exportSession.status == AVAssetExportSessionStatusUnknown) {
            }
            if (exportSession.status == AVAssetExportSessionStatusWaiting) {
            }
        }];

}

有关time.h头文件的详细信息,请参阅此link