将倒数计时器添加到数学程序测验中

Add a countdown timer to a math program quiz

本文关键字:程序 倒数 计时器 添加      更新时间:2023-10-16

我正在尝试在此程序中添加倒数计时器。我希望计时器在提出第一个数学事实问题时开始,并且在到期后,我希望该程序给出等级。如果可能的话,在C 中执行什么代码?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int main(int args, char* argv[])
{
    int i;
    int result;
    int solution;
    char fact;
    bool done = false;
    int correct = 0;
    int count = 0;
    do {
        try {
            cout << "Enter (m)ultiplication or "
                 << "(a)ddition." << endl;  /*or (s)ubstraction. */
            cin >> fact;
            while (!cin)
                throw fact;
            if (fact != 'A')
                if (fact != 'a')
                    if (fact != 'M')
                        if (fact != 'm')
                            while (!cin)
                                throw fact;
            cout << "Now, enter the number of the fact that
                 you would like to do." << endl;
            cin >> i;
            int wrong = 0;
            int score = 0;
            int j = 0;
            while (!cin)
                throw i;
            switch (fact) {
            case 'm':
            case 'M':
                while (j < 13) {
                    cout << "What's " << i << " x " << j << "?" << endl;
                    cin >> result;
                    while (!cin)
                        throw result;
                    solution = i * j;
                    if (result == solution) {
                        cout << "Great Job!  That is the correct answer for the problem "
                             << i << " x " << j << "." << endl;
                        cout << endl;
                        cout << endl;
                        cout << endl;
                        score++;
                        j++;
                        cout << endl;
                    }
                    if (result != solution) {
                        cout << "Oh no!  " << result << " is NOT the correct answer for "
                             << i << " x " << j << "." << endl;
                        wrong = wrong + 1;
                        count++;
                    }
                    if (count == 3) {
                        cout << "The correct answer is " << i * j << "." << endl;
                        j++;
                        wrong = wrong - 3;
                        count = 0;
                    }
                    if (count == 1) {
                        cout << endl;
                        count--;
                        wrong = wrong - 1;
                    }
                    if (count == 2) {
                        cout << endl;
                        count--;
                        wrong = wrong - 2;
                    }
                }
            case 'a':
            case 'A':
                while (j < 13) {
                    cout << "What's " << i << " + " << j << "?" << endl;
                    cin >> result;
                    while (!cin)
                        throw result;
                    solution = i + j;
                    if (result == solution) {
                        cout << "Great Job!  That is the correct answer for the problem "
                             << i << " + " << j << "." << endl;
                        cout << endl;
                        cout << endl;
                        cout << endl;
                        score++;
                        j++;
                        cout << endl;
                    }
                    if (result != solution) {
                        cout << "Oh no!  " << result << " is NOT the correct answer for "
                             << i << " + " << j << "." << endl;
                        wrong = wrong + 1;
                        count++;
                    }
                    if (count == 3) {
                        cout << "The correct answer is " << i + j << "." << endl;
                        j++;
                        wrong = wrong - 3;
                        count = 0;
                    }
                    if (count == 1) {
                        cout << endl;
                        count--;
                        wrong = wrong - 1;
                    }
                    if (count == 2) {
                        cout << endl;
                        count--;
                        wrong = wrong - 2;
                    }
                }
                if (j == 13) {
                    system("pause");
                    correct = score - wrong;
                    score = (correct * 100) / 13;
                }
                if (score >= 80) {
                    cout << "Excellent!!!!!" << endl;
                    cout << "You scored " << score << "%." << endl;
                    cout << "You got " << correct << " out of 13 correct." << endl;
                    cout << "Keep up the good work." << endl;
                } else if (score >= 70) {
                    cout << "Congratulations!!!!!" << endl
                         cout << "You scored " << score << "%." << endl;
                    cout << "You got " << correct << " out of 13 correct." << endl;
                    cout << "Let's see if we can score even higher next time." << endl;
                } else {
                    cout << "You scored below 70 which means that you may need some"
                         << " more practice." << endl;
                    cout << "You scored " << score << "%." << endl;
                    cout << "You got " << correct << " out of 13 correct." << endl;
                    cout << "You might want to try the " << i << " facts again."
                         << "  Goodluck!!!!!" << endl;
                }
            }
        } catch (char fact) {
            cout << "Invalid input.  You can only enter (m)ultiplication or"
                 << " (a)ddition.  Please try again." << endl;
            cin.clear();
            cin.ignore(100, 'n');
        } catch (int i) {
            cout << "Invalid input0.  You can only enter a
                 number here.  Please try again." << endl;
            cin.clear();
            cin.ignore(100, 'n');
        } catch (...) {
            cout << "Invalid input2.  You can only enter a number here.
                 Please try again." << endl;
            cin.clear();
            cin.ignore(100, 'n');
        }
    } while (!done);
    return 0;
}

任务很难,但是如果您敢尝试,我建议以两个步骤进行操作:

实施不准确的解决方案:对用户的查询之间检查计时器到期。

如果剩下一些时间,则提出下一个问题,否则会显示统计信息。因此,尽管计时器用完了,但程序始终在最后一个问题上等待用户输入。不是测验的样子,而是从。

开始的好举动

方法:在开始测验保存当前时间之前,在每个问题之前,请在保存的时间和当前一个问题之间进行三角洲,并与时间限制进行比较。用Chrono(从C 11开始)的示例,以Oldschool Clock

为例

添加中等问题中断

此部分需要功能,该功能将等待用户输入不长,而不是指定的时间。因此,您不必使用std :: cin()您需要计算剩余时间(时间限制在cur时间和开始时间之间的时间限制)并致电某种cin_with_timeout(time_left)。

最困难的是实现CIN_WITH_TIMEOUT(),它需要多线程和线程同步的可靠知识。可以在这里找到伟大的灵感,但这是开始思考而不是完整解决方案的方向。