2012-03-28 102 views
0

嗨我有一個家庭作業,我需要在兩個小時內實現兩條單線道的交叉口。我需要調整階段,以便每個隊列理想地少於5輛車,9也是可以接受的。實現與鏈接列表堆棧的交集。

除了一些事情,所有的作品都是我的階段實施的方式不對,我似乎無法解決問題。我能得到的絕對最好是一個隊列是0或1,另一個隊列是40+。我似乎無法將他們兩人都置於9以下。我已將問題歸結於階段檢查,但無法想出解決問題的方法。我知道我希望稍微傾向Q1,因爲汽車的到達速度比Q2快一點。

在此先感謝您的幫助。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

using namespace std; 

struct Node { 
    int data; 
    Node *next; 
}; 

class Queue { 
private:       
    Node *listpointer; 
public:       
    Queue(); 
    ~Queue(); 
    void push(int newthing); 
    void pop(); 
    int top(); 
    bool isEmpty(); 
    int queueCount(); 
    void Queue::popTwo(); 
    bool Queue::twoOrMore(); 
}; 

Queue::Queue() { 
//constructor 
    listpointer = NULL; 
} 

Queue::~Queue() { 
//destructor 

} 

void Queue::push(int newthing) { 
//place the new thing on top of the Queue 
    Node *temp; 
    temp = new Node;    
    temp->data = newthing; 
    temp->next = listpointer; 
    listpointer = temp; 
} 

void Queue::pop() { 
//remove top item from the Queue 
    Node *p; 
    p = listpointer; 
    if (listpointer != NULL) { 
     listpointer = listpointer->next; 
     delete p; 
    } 
} 

int Queue::top() { 
//return the value of the top item 
    return listpointer->data; 
} 

bool Queue::isEmpty() { 
//returns true if the Queue is empty 
    if (listpointer == NULL) { 
     return true; 
    } 
    return false; 
} 

int Queue::queueCount() { 
    Node *temp; 
    int count = 0; 
    temp = listpointer; 
    while (temp != NULL) { 
     ++count; 
     temp = temp->next; 
    } 
    return count; 
    delete temp; 
} 

void Queue::popTwo() { 
// remove the 2 top items from the stack 
    Node *p; 
    p = listpointer; 
    if (listpointer != NULL) { 
     listpointer = listpointer->next; 
     delete p; 
    } 
    p = listpointer; 
    if (listpointer != NULL) { 
     listpointer = listpointer->next; 
     delete p;     
    } 
} 

bool Queue::twoOrMore() { 
// return true if the stack has at least two items 
    if(listpointer==NULL || listpointer->next==NULL) return false; 
    else return true; 
} 

//implement/copy your queue structure and functions above 
//then, declare two instances: 
//Queue Q1, Q2; 
//if you want, make a separate function to change the 
//signals between the queues (either green or red) 
//When the signal changes, one queue only is allowed to delete elements 

Queue Q1, Q2; 

int Q1phase = 30; //initial attempt 
int Q2phase = 60; //initial attempt 
const int Q1arrive = 18; //fixed 
const int Q2arrive = 22; //fixed 
const int leave_rate = 10; //fixed, one car leaves either queue every 10 seconds 

int car_id=0; 
int clock=0; 
bool Q1_green, Q2_green; //indicates which queue is opened, only one at a time 

int main(int argc, char **argv) { 
    //if(argc!=3) {printf("needs: Q1phase Q2phase\n"); exit(0); } 
    //Q1phase=atoi(argv[1]); 
    //Q2phase=atoi(argv[2]); 
    if(Q1phase < 30 || Q2phase < 30) {printf("Minimum time for each queue to be closed is 30 seconds\n"); exit(0);} 
    clock = 0; 
    car_id = 0; 
    Q1_green = true; 
    Q2_green = false; 
    int length_Q1, length_Q2; 
    length_Q1 = 0; 
    length_Q2 = 0; 

    while (clock < 7200) { 
     clock++; 
     if (clock % Q1arrive == 0) { 
      car_id++; 
      //car_id join Q1 
      Q1.push(car_id); 
      length_Q1 = Q1.queueCount(); 
     } 
     if (clock % Q2arrive == 0) { 
      car_id++; 
      //or car_id join Q2 
      Q2.push(car_id); 
      length_Q2 = Q2.queueCount(); 
     } 

     if ((clock % Q1phase == 0) || (clock % Q2phase == 0)) { 
      if (Q1_green == true) { 
       Q1_green = false; 
       Q2_green = true; 
      } else { 
       Q1_green = true; 
       Q2_green = false; 
      } 
     } 

     if (clock % leave_rate == 0) { 
      if (Q1_green == true) { 
       Q1.pop(); 
       length_Q1 = Q1.queueCount(); 
      } 

      if (Q2_green == true) { 
       Q2.pop(); 
       length_Q2 = Q2.queueCount(); 
      } 
     } 

    //ChangeSignal();//every second, check if it is time to change signals (phasing is important!) 
    //After the signal change: 
    //verify which queue is opened 
    //either Q1 or Q2 will have the chance to delete one element (Q.Leave()) 
    // 
    printf("at time %d:\nthe current length of Q1 is %d\n",clock,length_Q1); 
    printf("the current length of Q2 is %d\n", length_Q2); 
    //at the end of the simulation, both queues should have few cars 
    } 
} 
+0

你有跟蹤如何隊列隨時間變化,以及如何燈隨時間而變化?如果不一定如何解決問題,這可能會給出線索。 – 2012-03-28 00:21:31

+1

您的程序中有一大塊代碼是重新創建已經使用C++語言的數據結構。這是作業的必要部分嗎?即是模擬交叉點,還是製作隊列數據結構,還是兩者的作業?重新設計這個輪子會讓程序的主要目的分散注意力,同時也會使您浪費時間來調試隊列數據結構,而不是模擬邏輯。家庭作業是一個阻力,所以不要花費不必要的時間。 – Kaz 2012-03-28 00:24:40

+0

我追蹤了燈光隨着時間的推移如何變化,但它仍然讓我感到困惑。它真的是模擬真正簡單的交集並創建隊列數據結構。所以大部分都是必要的,但它也是由講師作爲模板給出的。我認爲這兩個階段是不必要的,但我很確定他們是必需的。 – RedFred 2012-03-28 00:30:03

回答

0

您的總抵達率超過了休假率,所以汽車將不得不積壓。

總抵達率是1/22 + 1/18 =~ 0.1010汽車/秒。這超過了每秒0.1汽車的休假率。

燈光每30秒更換一次(Q1phase),因爲Q2phaseQ1phase的倍數。所以基本上這些隊列有相同的工作週期。

汽車從每個隊列中排出總費用的一半:一個隊列爲0.05,另一隊列爲0.05(1/20)。

請注意,休假率爲1/20小於1/18。所以1/18到來的隊列將會積壓。 1/20的離開率大於1/22,所以具有1/22到達率的隊列不會積壓。

這個微小的差異並不是很微小!到達率超過休假率或不超過休假率的情況存在差異。


哦,這裏是如何計算汽車在拖後隊列:

到達率:1/18。 退出率:1/20(1/10的一半) 總時間:7200秒:

7200 *(1/18) - 7200 *(1/20)== ????

:)

+0

好吧,我明白這一點,但我可以改變的唯一變量是Q1phase和Q2phase。 – RedFred 2012-03-28 00:54:13

+0

排隊有時是違反直覺的。 **整體**抵達率和休假率並不能預測40輛汽車的積壓。但是你必須瞭解它的方式是,一個隊列中的快速處理不會給你一個**信用額**,可以用於其他隊列中的待辦事項。當一個隊列沒有汽車在等待時,這對另一個隊列沒有任何幫助。 – Kaz 2012-03-28 01:03:23

+0

換句話說,這個問題可能會導致積壓只有7輛車。爲什麼?因爲這是* system * arrrival和leave rate的差異乘以7200.所以我理解你的任務:操縱階段,試圖讓隊列降低到接近整個系統的速率預測值。祝你好運! – Kaz 2012-03-28 01:04:36