1

我需要运行一个while循环并在有输入时接受输入。我对 C++ 并不陌生,但这个障碍非常困难。由于 NDA(这个学校项目显然是一些秘密的东西),我只能向您展示测试用例。

我一直在寻找解决问题的稻草;尝试捕捉、cin.get、cin.peek、if(cin.peek){}。如果有人能指出我正确的方向,我将不胜感激!

该程序不是时间关键的,但需要以固定间隔调用函数。代码的可移植性、while-cin-combination 或类似的东西并不重要;该代码只能在至少具有双核处理器的 Windows 7 或 Windows 8 PC 上运行。

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int input = 0;
    int pastTime, nowTime;
    pastTime = nowTime = time(0);

    cin >> input;
    while(input != -1)
    {
        if(input == 1)
        {
            cout << "Entered 1" << endl;
            //To be done instead of the two 'elses', 
            //bypassing interval-dependant code
        }
        else if(input == 2)
        {
            cout << "Entered 2" << endl;
            //To be done instead of the interval-dependant code
        }
        else if(pastTime == (nowTime - 5))
        {
            cout << "Nothing entered." << endl;
            //Needs to be done with a fixed interval.
        }
        nowTime = time(0);
        cin >> input;
    }
return 0;
}

解决方案是,基于 om James Beilby 的链接:

// This program is based on counter.cpp from Boost\lib\thread\tutorial

#include <boost/thread/thread.hpp>
#include <iostream>
#include <ctime>

int timeNow = time(0);
int timePast = time(0);

void fct_one()
{
    while(1) //keeps running all the time
    {
        if(timePast == (timeNow - 3)) // only executed once every three seconds
        {
            //do some stuff
            timePast = time(0);
        }
        timeNow = time(0); // time is continuously updated 
    }
}

void fct_two()
{
    int input = 0;
    int timeTemp = time(0);
    while(1) //keeps running all the time
    {
        std::cin >> input; // cin blocking for input
        if(input == 1)
        {
            //do some stuff
        }
        if(input == 2)
        {
            //do some stuff
        }
        if(input == -1)
        {
            std::cout << "Program is done. ";
            system("pause");
            exit(1);
        }
    }
}

int main()
{
    boost::thread_group threads;
    threads.create_thread(&fct_one)
    threads.create_thread(&fct_two);
    threads.join_all();
    return 0;
}
4

2 回答 2

0

我会将读取输入cin与执行默认超时功能完全分开。您将需要一个后台线程之类的东西,它根据时间间隔执行默认功能。要处理前两种情况,您需要通知线程跳过下一次执行(如果确实有必要),只需调用您想要的任何函数或什么都不做。

于 2012-11-07T19:23:06.867 回答
0

简单的答案是将在某个时间间隔运行的代码放在另一个线程上。由于您已经注意到这是 Windows,您可以使用Timer Queue

从例行程序开始,以开始和停止依赖时间的工作:

HANDLE Start(HANDLE hTimerQueue)
{
    DWORD timerMS = 5000; /* every 5 seconds */
    HANDLE hTimer;
    if (!CreateTimerQueueTimer(&hTimer, 
          hTimerQueue,
          (WAITORTIMERCALLBACK)timerWork,
          /*lpParam*/NULL,
          /*start in ___ ms:*/0,
          /*run every __ ms:*/timerMS,
          /*flags*/0))
    {
        return NULL;
    }

    return hTimer;
}

BOOLEAN Stop(HANDLE hTimerQueue, HANDLE hTimer)
{
    if (!DeleteTimerQueueTimer(hTimerQueue,
        hTimer,
        /*wait for our timer to complete*/INVALID_HANDLE_VALUE))
    {
        return FALSE;
    }

    return TRUE;
}

然后将依赖时间的工作放入它自己的回调中:

VOID CALLBACK timerWork(PVOID lpParam, BOOLEAN TimerOrWaitFired /*ignored*/)
{
    for (int ii = 0; ii < 10; ++ii) {
        std::cout << "timer work: " << ii << std::endl;
        Sleep(250);
    }
}

最后,将这些集成到您的工作流程中:

int main(int argc, char* argv[])
{
    HANDLE hTimerQueue = CreateTimerQueue(hTimerQueue);
    if (NULL == hTimerQueue) return -1;
    HANDLE hTimer = Start(hTimerQueue);
    if (NULL == hTimer) return -1;

    /* our timed callback is now running in the background */
    int input = 0;
    std::cin >> input;
    while(input != -1)
    {
        if(input == 1)
        {
            if (Stop(hTimerQueue, hTimer)) {
                std::cout << "Entered 1" << std::endl;
                if (NULL == (hTimer = Start(hTimerQueue))) return -2;
            }
        }
        else if(input == 2)
        {
            if (Stop(hTimerQueue, hTimer)) {
                std::cout << "Entered 2" << std::endl;
                if (NULL == (hTimer = Start(hTimerQueue))) return -2;
            }
        }

        std::cin >> input;
    }

    DeleteTimerQueue(hTimerQueue);
    return 0;
}
于 2012-11-07T19:42:29.987 回答