洗车队列问题

时间:2014-12-17 09:47:09

标签: c++ queue

我正在研究“汽车排队”或“洗车”问题的变化,我必须输入汽车需要洗多长时间,汽车进入洗车队列的几率给定时刻,然后接收作为输出的洗车数量,在一天结束时排队的车辆数量,以及总等待时间。我仍然是c ++的新手,并且在过去的几个月里一直在广泛使用它。很可能我的队列实现代码有一个错误,我没有抓到,但我相信错误是主要的。目前,我为每个循环添加了输出,因此我可以观察代码运行,并遵循它。它似乎接受了一辆汽车,然后马上完成洗车。然后,当没有汽车时,它只是在洗涤和等待循环之间交替。任何帮助将不胜感激。

#include <assert>
#include <iostream>
#include <fstream>
#include <ostream>
#include <ctime>
#include <cstdlib>
using namespace std;

class Node
  {
    private:
     int item;
Node * next;
public:
    Node ();
    Node (const int& anItem);
    Node (const int& anItem, Node * nextNodePtr);
    void setItem (const int& anItem);
    void setNext (Node * nextNodePtr);
    int getItem ();
    Node* getNext();
   };
class LinkedQueue 
  { 
    private:  
       Node* backPtr; 
       Node* frontPtr; 
    public: 
       bool isEmpty(); 
       bool enqueue(int& newEntry); 
       bool dequeue(); 
       int peekFront() const;
       LinkedQueue();
       ~LinkedQueue();
       LinkedQueue(const LinkedQueue& aQueue); 
   };
int main ()
{   
    srand(1);
    int timeForWash;
    int minute;
    int timeEnteredQueue;
    int carsWashed;
    int totalQueueMin;
    int timeLeftOnCar;
    int probOfArrival;
    int carsInQueue = 0;

    LinkedQueue carQueue;

    cout << "Enter time to wash one car:" << endl;
    cin >> timeForWash;
    cout << endl;
    cout << "Enter probability of arrival per minute" << endl;
    cin >> probOfArrival;

    carsWashed = 0;
    totalQueueMin = 0;
    timeLeftOnCar = 0;
    for (minute = 1; minute <= 20; ++minute)
    {
        if (rand()%100 <= probOfArrival)
            {
                carQueue.enqueue(minute);
                carsInQueue++;
                cout << "Queued" << endl;
            }

        if ((timeLeftOnCar == 0) && ( !carQueue.isEmpty()))
        {
            timeEnteredQueue = carQueue.peekFront();
            carQueue.dequeue();
            totalQueueMin = totalQueueMin + (minute - timeEnteredQueue);
            ++carsWashed;
            carsInQueue--;
            timeLeftOnCar = timeForWash;
            cout << "Finish" << endl;
            }
            if (timeLeftOnCar =! 0)
            {
                timeLeftOnCar -= 1;
                cout << "Washing" << endl;
            }
        if ((timeLeftOnCar == 0) && ( carQueue.isEmpty()))
        {
            cout << "Waiting" << endl;
        }
    }
    cout << carsWashed << endl;
    cout << totalQueueMin << endl;
    cout << carsInQueue << endl;
    system("pause");
    return(0);
  };
//Implementation
Node:: Node() : next (nullptr)
{
} // default
Node:: Node (const int& anItem) : item(anItem), next(nullptr)
{
}

Node:: Node (const int& anItem, Node * nextNodePtr) : item(anItem), next(nextNodePtr)
{
}

void Node:: setItem (const int& anItem)
{
    item = anItem;
}

void Node:: setNext (Node * nextNodePtr)
{
    next = nextNodePtr;
}

int Node:: getItem ()
{
    return item;
}

Node * Node:: getNext ()
{
    return next;
}

bool LinkedQueue::enqueue(int& newEntry) 
    { 
     Node* newNodePtr = new Node(newEntry); 
     //Insert the new node 
     if (isEmpty()) 
     frontPtr = newNodePtr; // The queue was empty 
     else
     backPtr->setNext(newNodePtr); // The queue was not empty 
     backPtr = newNodePtr; // New node is at back 
     return true; 
} // end enqueue
bool LinkedQueue::dequeue() 
{ 
    bool result = false; 
    if (!isEmpty()) 
     { 
     // Queue is not empty; remove front 
     Node* nodeToDeletePtr = frontPtr; 
     if (frontPtr == backPtr) 
     { // Special case: one node in queue 
     frontPtr = nullptr; 
     backPtr = nullptr; 
     } 
     else
     frontPtr = frontPtr->getNext(); 
     // Return deleted node to system 
     nodeToDeletePtr->setNext( nullptr); 
     delete nodeToDeletePtr; 
     nodeToDeletePtr = nullptr; 
     result = true; 
     } // end if 
     return result; 
} // end dequeue
bool LinkedQueue:: isEmpty()
{
    return (frontPtr == nullptr );
}
int LinkedQueue:: peekFront() const
{
    return frontPtr->getItem();
} 
LinkedQueue::LinkedQueue() 
{
    frontPtr = nullptr;
    backPtr = nullptr;
} 
LinkedQueue::LinkedQueue( const LinkedQueue& aQueue) : 
frontPtr(aQueue.frontPtr) 
{ 
} // end copy constructor  
LinkedQueue::~LinkedQueue() 
{ 
} // end destructor  

1 个答案:

答案 0 :(得分:2)

timeLeftOnCar =! 0timeLeftOnCar = !0相同,即timeLeftOnCar = 1,始终为真。

您需要timeLeftOnCar != 0timeLeftOnCar > 0