gpt4 book ai didi

c++ - 如何让定时器在后台保持运行

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:22 26 4
gpt4 key购买 nike

我正在为我的俱乐部编写程序。要求用户在 2 分钟内回答 10 个问题。我正在为每个问题使用函数,并在用户回答完当前问题后调用下一个函数。如何在显示问题的同时在顶部显示剩余时间?

我试过循环来控制我的时间。但它不会在顶部显示剩余时间并同时显示问题,因为循环需要执行直到条件为假

Question1()
{
countdown();
cout<<"Question 1 out of 10"<<endl<<endl;
cout<<" Where Is The First Indoor Bowling Lane Built? "<<endl;
cout<<"A. New York City"<<endl;
cout<<"B. Berlin"<<endl;
cout<<"C. Ohio"<<endl;
cout<<"D. Japan"<<endl;
cout<<"Your answer: ";
cin>>ans1;
Question2();
}
void countdown()
{
while(timer>=0)
{
cout<<"Time remaining: "<<timer<<endl;
Sleep(1000);
timer--;
system("cls");
}
}

预计时间会在我的问题上进行倒计时,但问题只会在设定的时间结束前显示。

最佳答案

我回想起 MS-DOS 的日子,记得有一个函数 _kbhit<conio.h> 提供在 Windows 中。它不是可移植的,但也许你不在乎。这是我编写的一个函数,将使用 _kbhit轮询按键。它很粗糙,但也许这就是您要找的东西。

int GetInputWithTimeout(int count, int timeout)
{
int answer = -1;
const int poll_rate = 10;
int poll_cycle = 0, last_timeout = 0;
do {
// poll for keypress and store answer if valid
if (_kbhit())
{
int input = _getch();
while (_kbhit()) _getch(); // discard control sequences
input = tolower(input);
if (input >= 'a' && input < 'a' + count)
{
answer = input - 'a';
}
}

// display time remaining if changed
if (last_timeout != timeout) {
last_timeout = timeout;
std::cout << "\rTime remaining: " << timeout << " " << std::flush;
}

// perform small sleeps for a potentially wildly inaccurate, but responsive delay
if (timeout > 0)
{
Sleep(1000 / poll_rate);
poll_cycle = (poll_cycle + 1) % poll_rate;
if (poll_cycle == 0) --timeout;
}
} while (timeout > 0 && answer == -1);
std::cout << std::endl;
return answer;
}

因此您可以使用此函数,传递所需数量的测验输入。建议的用法是这样的:

struct Question {
std::string question;
std::vector<std::string> answers;
int correct_answer;
int timeout_seconds = 10;
};

bool AskQuestion(int number, const Question& question)
{
std::cout << "Question " << number << ": " << question.question << std::endl;
int count = static_cast<int>(question.answers.size());
for (int i = 0; i < count; i++)
{
std::cout << " " << char('A' + i) << ": " << question.answers[i] << std::endl;
}
int answer = GetInputWithTimeout(count, question.timeout_seconds);
return answer == question.correct_answer;
}

您会注意到我在结构中表示了一个测验问题。因此,您可以像这样设置和运行整个测验:

int main()
{
std::vector<Question> questions = {
{
"Where Is The First Indoor Bowling Lane Built?",
{
"New York City",
"Berlin",
"Ohio",
"Japan"
},
0, // I have no idea, so just guessed it's new york
},
{
"What is the airspeed velocity of an unladen swallow?",
{
"10km/h",
"20km/h",
"30km/h",
"40km/h",
"What do you mean -- an african or european swallow?"
},
4,
},
};

int num_questions = static_cast<int>(questions.size());
int num_correct = 0;
for (int q = 0; q < num_questions; q++)
{
num_correct += AskQuestion(q, questions[q]);
std::cout << std::endl;
}

std::cout << "Final score: " << num_correct << " of " << num_questions << std::endl;
return 0;
}

呃,糟糕,我有点为你写了整个程序。它并不漂亮,但希望它至少能教给你一些东西。

关于c++ - 如何让定时器在后台保持运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com