【问题标题】:Add a countdown timer to a math program quiz在数学程序测验中添加倒数计时器
【发布时间】:2017-05-09 13:06:27
【问题描述】:

我正在尝试向该程序添加倒数计时器。我希望计时器在第一个数学事实问题被问到时启动,并且在到期时我希望程序给出分数。如果可能,用 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;
}

【问题讨论】:

  • int i; 是它开始出错的地方。
  • 有可能,但也不是小事。你在这里拥有的大部分东西都必须被替换。考虑研究多线程编程。

标签: c++


【解决方案1】:

任务比较艰巨,但如果你敢尝试,我建议分两步完成:

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

如果还剩一些时间,则询问下一个问题,否则显示统计信息。因此,尽管计时器已用完,但程序始终等待用户输入最后一个问题。不是测验的具体内容,而是一个好的开始。

方法:在开始测验之前保存当前时间,在每个问题之前将保存时间与当前时间之间的增量与时间限制进行​​比较。 chrono 示例(从 C++11 开始),oldschool 示例clock

添加中间问题中断

这部分需要功能,它将等待用户输入的时间不超过指定的时间。因此,您需要计算剩余时间量(时间限制减去当前时间和开始时间之间的增量)并调用某种 cin_with_timeout(time_left),而不是使用 std::cin()。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    相关资源
    最近更新 更多