2016-04-27 80 views
1

所以我想创建一个更大的项目的概念证明。我目前正在进行定时测验,只有1个问题,而你有10秒的时间来回答。正在执行其他命令时正在运行一个计时器C++

什么我真的问

  • 我知道我可以做

    “CIN < < VAR” 读取用户输入或 “无功= _getch()”

  • 我可以通过做一个计时器

    clock_t timer;

    timer = clock();

    //代码

    计时器=时钟() - 吨;

  • 但你怎么把这一切都放在一起?你可以让计时器在请求输入时运行吗?看起来好像不是这样,因为C++逐行执行每个部分,并在继续之前等待完成。但必须有办法!以下是我已经想出了...

    bool Question(int Correct) { 
        int Answer = 0; 
        cin >> Answer; 
        if (Answer == Correct) { 
         return true; 
        } 
        else { 
         return false; 
        } 
    } 
    
    int main() { 
    
    cout << "1 + 1 is: "; 
    
    clock_t Timer; 
    Timer = clock(); 
    
    bool Is_Correct = Question(2); 
    
    Timer = clock() - Timer; 
    
    cout << "You Answered: "; 
    
    if (Is_Correct) { 
        cout << "Correct!"; 
    } 
    else { 
        cout << "Wrong!"; 
    } 
    
    cout << "\nAnd by the way, you answered the question with " << 10 - (Timer/CLOCKS_PER_SEC) << " Seconds to Spare.\n"; 
    
    cin.get(); 
    cin.get(); 
    
    return 0; 
    } 
    

很抱歉的间距,它被认为是有点混乱。

+0

作为一个开始,这是非常类似C!尝试使用''标题来实现类似时间的工具。 – DeiDei

+0

@alf对不起,我忘了提及它将只是一个窗口。有没有办法告诉exe文件,如果一个命令(cin)在10秒内没有被回答,它应该只是“继续前进”? – Dosisod

+0

如果您正在尝试制作更好的车轮,为什么不正确使用它并使用带有可视化的代码分析工具。在HPC社区,调优和分析工具(TAU)http://www.cs.uoregon.edu/research/tau/home.php非常受欢迎。它可能看起来像全面的仪表和分析是矫枉过正,1)它会给你你想要的时间信息,加上更详细的时间,2)知道如何使用这种工具是很好的,3)你可能无法准确找到你在寻找什么,但是你可能会发现一些关于这个过程中性能瓶颈的有趣的事情 – Matt

回答

0

时钟在程序等待输入时继续运行,所以计时输入操作没有问题(即看看它花了多长时间)。但是,如果您想在10秒钟内终止输入操作,那么您必须使用特定于平台的方式,例如键盘轮询。所有标准的C++输入操作都是阻塞的。因此,只有标准的C++,你可以尝试将输入操作放在它自己的执行线程中,推断异步,这就是线程的用途。但是,你会发现在等待输入时无法终止该线程,并且(获得创意)无法向其发布虚假输入。

尽管如此,关于

你可以有一个计时器运行,而它的要求输入?

如果你的意思是有时间显示,为什么,这是没有问题的。

你可以把它放在它自己的线程中。但是,如果你想每行输入多个单个字符,那么你可能必须使用系统特定的方法,因为使用标准库的文本显示修改功能(其实质上由ASCII \b\r控制)没有办法做到这一点,我能想到食堂的每一次文本光标位置显示的时间发生变化,举例如下:

#include <atomic> 
#include <chrono> 
#include <exception> // std::terminate 
#include <iostream> 
#include <iomanip>  // std::setw 
#include <thread> 
using namespace std; 

auto fail(string const& s) 
    -> bool 
{ cerr << "!" << s << "\n"; terminate(); } 

auto int_from(istream& stream) 
    -> int 
{ 
    int x; 
    stream >> x || fail("Input operation failed"); 
    return x; 
} 

namespace g { 
    atomic<bool> input_completed; 
} // namespace g 

void count_presentation(string const& prompt) 
{ 
    for(int n = 1; not g::input_completed; ++n) 
    { 
     string const count_display = to_string(n); 
     string const s = count_display + prompt.substr(count_display.length()); 
     cout << "\r" << s << flush; 

     using namespace std::chrono_literals; 
     this_thread::sleep_for(100ms); 
    } 
} 

auto main() 
    -> int 
{ 
    string const prompt = string(20, ' ') + "What's 6*7? "; 
    auto counter = thread(count_presentation, ref(prompt)); 
    const int x = int_from(cin); 
    g::input_completed = true; 
    counter.join(); 
    cout << x << "\n"; 
} 
+0

那么在Windows中呢? C++ 11 – Dosisod

+0

@Dosisod:如果你的意思是,在Windows中有什么系统特定的手段?然后,Windows有一个难以使用的正确控制台窗口API。但是你也可以在Windows中使用ncurses库。更简单一点,如果你的编译器支持它,还有一个叫做'conio.h'的非标准头支持键盘轮询。对于GUI,Windows提供了定时器消息和硬线程定时器。 –

+0

谢谢你的帮助!我发现一个线程,可以等待键盘输入,并仍然运行计时器:http://stackoverflow.com/questions/14192230/wait-for-input-for-a-certain-time – Dosisod

0

我发现这个Clock In C++ Console?。这很酷,它回答你的问题。作者使用gotoxy()移动到控制台输出的开头并覆盖当前时间。不过我不推荐using namespace std;的例程,所以我编辑了代码。祝你好运与你的积分

#include <iostream> 
#include <ctime> 
#include <windows.h> 

// to move back to the beginning and output again 
void gotoxy (int x, int y) 
{ 
    COORD coord; // coordinates 
    coord.X = x; coord.Y = y; // X and Y coordinates 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates 
} 

int main() 
{ 
    std::time_t now; 
    while (true) 
    { 
     gotoxy (0,0); 
     now = std::time(0); 
     std::cout << "The time is now: " << std::ctime(&now); 
     Sleep (20); 
    } 
    std::cin.get(); 
    return 0; 
} 
0

下面是使用<chrono>中的实用程序的示例。您必须有权访问C++ 11。

你可以很容易地使用这个模型来适应你的代码。

#include <iostream> 
#include <chrono> 

using namespace std::chrono; 

int main() 
{ 
    int num; 

    // start timing 
    auto start = steady_clock::now(); 

    std::cin >> num; 

    // stop timing 
    auto stop = steady_clock::now(); 

    // stop - start will give you the time that passed 
    std::cout << "It took " 
       << duration_cast<milliseconds>(stop - start).count() 
       << " milliseconds!\n"; 

    std::cin.get(); 
} 

这将打印您在开始声明和停止声明执行之间的所有时间。
duration_cast中,您可以使用seconds,microseconds等。

+0

是的,所以我可以花费你输入一个输入的时间。但是有没有办法让程序停止等待输入,只是说“时间到了!” ? – Dosisod

相关问题