2016-04-27 35 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

相關問題