我的逻辑有问题吗?

时间:2015-11-20 06:55:15

标签: c++ algorithm c++11 logic

我正在尝试实施电梯算法Shortest-Seek-Time-First。

这是我确定要插入哪个队列的逻辑,以及当前的方向。但是我遇到问题,例如,如果我在位置4的前进方向并请求移动到2 ...使用SSTF向一个方向移动,因为它向前移动,所以假设将2插入反向队列并且继续前进,直到前锋q为空。

string direction,requestIsIn,currDStr;
bool success= true;

cout << "Do the EnqRequest function."<<endl;
cout <<endl<<endl;
cout <<"Enter a New Track Number"<<endl;
cin >> newTrackNum;

if ( newTrackNum >= 0 && newTrackNum <= 9 )
{
    item.SetSerialNum(serialNumber);
    item.SetTrackRequest(newTrackNum);

    if ( currDir )
    {
        pRequest=forward.peek();
    }
    else
    {
        pRequest=reverse.peek();
    }

    if ( currP >= item.GetTrackRequest() && ! reverse.PQueueIsEmpty() )
    {
        direction="Reverse";
        item.SetDirection( false );
    }
    else if ( currP < item.GetTrackRequest() && ! forward.PQueueIsEmpty() )
    {
        item.SetDirection( true );
        direction="Forward";
    }
    else
    {
        if (item.GetDirection())
        {
            direction="Forward";
        }
        else
        {
            direction="REVERSE";
        }
    }

    if ( pRequest.GetDirection() && item.GetDirection() )
    {
        if ( item.GetTrackRequest() > pRequest.GetTrackRequest() )
        {
            requestIsIn="Forward Queue";

            cout<<"Inserting in forward queue"<<endl;
            serialNumber++;

            item.SetSerialNum(serialNumber);
            forward.PQueueInsertMin(item, success);

            cout<<"1st case forward"<<endl;
        }
        else if ( item.GetTrackRequest()== pRequest.GetTrackRequest() )
        {
            //&& item.GetSerialNum()>pRequest.GetSerialNum()

            requestIsIn="Forward Queue";

            cout<<"Inserting in forward queue"<<endl;
            serialNumber++;

            item.SetSerialNum(serialNumber);
            forward.PQueueInsertMin(item, success);

            cout<<"2nd case forward"<<endl;
        }
        else if ( item.GetTrackRequest() < pRequest.GetTrackRequest() && ! forward.PQueueIsEmpty() )
        {
            requestIsIn="Forward Queue";

            cout<<"Inserting in forward queue"<<endl;
            serialNumber++;

            item.SetSerialNum(serialNumber);
            forward.PQueueInsertMin(item, success);

            cout<<"1st case forward"<<endl;
        }
        else
        {
            requestIsIn="Reverse Queue";

            cout<<"Inserting in Reverse queue"<<endl;
            serialNumber++;
            item.SetSerialNum(serialNumber);

            reverse.PQueueInsert(item,success );

            cout<<"3rd case forward"<<endl;
        }
    }
    else
    {
        if ( item.GetTrackRequest() < pRequest.GetTrackRequest() )
        {
            requestIsIn="Reverse Queue";

            cout<<"Inserting in Reverse Queue"<<endl;
            serialNumber++;

            item.SetSerialNum(serialNumber);

            reverse.PQueueInsert(item, success);
            cout<<"1st case Reverse"<<endl;
        }
        else if ( item.GetTrackRequest() == pRequest.GetTrackRequest() && item.GetSerialNum() > pRequest.GetSerialNum() )
        {
            requestIsIn="Reverse Queue";

            cout<<"Inserting in Reverse Queue"<<endl;
            serialNumber++;

            item.SetSerialNum(serialNumber);

            reverse.PQueueInsert(item, success);
            cout<<"2nd case Reverse"<<endl;
        }
        else
        {
            requestIsIn="Forward Queue";

            cout<<"Inserting in Forward queue"<<endl;
            serialNumber++;
            item.SetSerialNum(serialNumber);

            forward.PQueueInsertMin(item, success);
            cout<<"3rd case Reverse"<<endl;
        }
    }

    cout <<"EnqRequest   "<<newTrackNum<<endl;
    cout << endl << endl;
    cout <<"Current Position: .............."<<currP<<endl;
    cout <<"Current Direction: ............."<<direction<<endl;
    cout <<"Request was Enqueued in the: ..."<<requestIsIn<<endl;
    cout <<"Number of Request is: ......"<<serialNumber<<endl;
    cout <<"==================================="<<endl;

0 个答案:

没有答案