C++时钟不工作

C++ Clock Not Working

本文关键字:工作 时钟 C++      更新时间:2023-10-16

我想做一个简单的程序,用户必须输入一个提示数字(1-4)。继续的条件是基于在用户选择的级别定义的时间内输入数字,当然,输入正确的数字。问题是程序会识别数字是否正确,但是您可以根据需要花费多长时间,并且不会停止游戏。这个"15分钟"的程序本应是一次对C++的深入研究,但它已经变成了一个周末的项目,令人困惑的时钟算法和混乱。以下是完整的源代码。任何帮助都会很棒...

编辑:这是在线C++模拟器的链接,因此您可以在当前状态下测试该程序...

#include <iostream>
#include <time.h>
#include <ctime>
#include <unistd.h>

using namespace std;
int main() {
    //input
    string numberGuessed;
    int intNumberGuessed;
    int score = 0;
    int actualNumber;
    int gameOver = 0;
    //Level Select Variables
    char level = {0};
    string input = " ";
    double timeForInput;

    //initialize random seed
    srand (time(NULL));

    //Generates random number
    actualNumber = rand() % 4 + 1;
    //LEVEL SELECTOUR
    cout << "Select A Level: 'e' 'm', or 'h':  ";
        getline(cin, input);
        if (input.length() == 1) {
            level = input[0];
        }
        if (level == 'e') {
            cout << "You Have Selected Easy..." << endl;
            cout<< "You Have .5 Second to Enter" << endl;
            timeForInput = 5;
        } else if(level == 'm'){
            cout << "You Have Selected Medium..." << endl;
            cout<< "You Have .2 Seconds to Enter" << endl;
            timeForInput = 2;
        } else if(level == 'h'){
            cout << "You Have Selected HARD!!!" << endl;
            cout<< "You ONLY Have .1 Seconds to Enter" << endl;
            timeForInput = 1;
        } else {
            cout << "You LOSE! GOOD DAY SIR!" << endl;
        }
        //Instructions and Countdown
        cout<<"Press The Number and Hit Enter in The Amount of Time Provided"<<endl;
        sleep(1);
        cout<<"3"<<endl;
        sleep(1);
        cout<<"2"<<endl;
        sleep(1);
        cout<<"1"<<endl;
        sleep(1);
        cout<<"GO!"<<endl;
        cout<<"--------------------------------------------------------------------------------"<<endl;
        cout<< "Enter the Numbers As They Appear:"<<endl;
        cout<<endl;
        cout<<endl;
        cout<<endl;
        sleep(1);
        double duration = 0.0;


        do {
        //Procedere For Each Round
        clock_t start;
        clock_t finish;

        start = clock();
        finish = clock();
        double delay = (double)(finish-start);

        //Clock
            start = clock();
            cout<<"The number is:  "<< actualNumber<<endl;
            getline(cin, numberGuessed);
            intNumberGuessed = stoi(numberGuessed);
            finish = clock();
            double elapsed = (double)(finish-start);
            elapsed-=delay;
            duration = elapsed/CLOCKS_PER_SEC;
            cout<<duration<<endl;
            //Test User's input
            if((intNumberGuessed == actualNumber) && (duration <= (timeForInput/10))){
                score += 1;
                gameOver = 0;
            } else if ((intNumberGuessed != actualNumber) || (duration >= (timeForInput/10))) {
                gameOver = 1;
            }
            //Reset Number
           actualNumber = rand() % 4 + 1;
        } while (gameOver != 1);

        cout<<"You Failed!"<<endl;
        sleep(1);
        cout<<"Your Score Was:  "<<score<<endl;
        return 0;
}

在标准中,指定clock()以返回进程使用的近似处理器时间。特别是,这意味着表达式(finish-start)产生的持续时间不一定等于已过去的挂钟时间量。例如,如果您测量四个线程咀嚼 CPU 1 秒,那么您应该得到大约 4 秒的结果。

这与程序相关的方式是,仅等待输入或休眠的程序不使用任何处理器时间,因此(finish-start)的结果将为零。

#include <iostream>
#include <chrono>   // std::chrono::seconds, milliseconds
#include <thread>   // std::this_thread::sleep_for
#include <ctime>    // std::clock()
int main() {
    auto start_processor_usage = std::clock();
    auto start_wall_clock = std::chrono::steady_clock::now();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    auto finish_processor_usage = std::clock();
    auto finish_wall_clock = std::chrono::steady_clock::now();
    std::cout << "CPU usage: " << (finish_processor_usage - start_processor_usage) / CLOCKS_PER_SEC << 'n';
    std::cout << "Wall clock: " << (finish_wall_clock - start_wall_clock) / std::chrono::milliseconds(1) << 'n';
}

上面的程序应该输出如下内容:

CPU usage: 0
Wall clock: 1000

请注意,虽然 *nix 平台通常正确实现clock()以返回处理器使用情况,但 Windows 不会。在 Windows 上clock()返回挂钟时间。在Windows和其他平台之间切换时,您需要牢记这一点。


好的,当然。使用 <random> 的随机整数的语法是什么?- 诺吉吉

#include <random>
int main() {
  // initialize with random seed
  std::random_device r;
  std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
  std::mt19937 engine(seed);
  // define distribution: improved version of "% 4 + 1"
  auto one_to_four = std::uniform_int_distribution<>(1, 4);
  // generate number
  auto actualNumber = one_to_four(engine);
}

我还发现 clock() 没有给出我想要的结果。所以我只是写了一个简单的 Timer 类来使用 Windows 上的 QueryPerformanceCounter 和 Linux 上的 clock_gettime 进行持续时间计算

试试这个:https://github.com/AndsonYe/Timer

:)