1

whileループを実行し、入力があるときはいつでもそのループへの入力を受け入れる必要があります。私はC++に不慣れではありませんが、このハードルは非常に困難です。NDA(この学校のプロジェクトは明らかにいくつかの秘密のものです)のために、私はあなたにテストケースしか見せることができません。

私は問題を解決しようとしているストローを把握してきました。catch、cin.get、cin.peek、if(cin.peek){}を試してください。誰かが私を正しい方向に向けることができれば、私はとても感謝しています!

プログラムはタイムクリティカルではありませんが、関数は一定の間隔で呼び出す必要があります。コードが移植可能である必要はなく、while-cinの組み合わせなどである必要はありません。コードは、少なくともデュアルコアプロセッサを搭載したWindows7またはWindows8PCでのみ実行されます。

#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;
}

解決策は、JamesBeilbyのリンクに基づいています。

// 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読み取り入力をデフォルトのタイムアウト機能から完全に分離して実行します。時間間隔に基づいてデフォルトの機能を実行するバックグラウンドスレッドのようなものが必要になります。最初の2つのケースを処理するには、スレッドが次の実行をスキップするように通知し(これが本当に必要な場合)、必要な関数を呼び出すか、何もしない必要があります。

于 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 に答える