2016-10-22 167 views
0

即時通訊工作在涉及堆棧和隊列的C#程序。該隊列將輸入字符串排入隊列中,並在堆棧操作同時在隊列輸入上完成時將其出隊。堆棧爲空...爲什麼?

現在在checkMatch()中發生了什麼,該程序給出了一個異常錯誤,說棧是空的。 我使用了調試/步驟,我發現堆棧在checkMatch函數中真的是空的,我不明白爲什麼。 我在C++中完成了相同的程序來測試它,在C++中我根本沒有得到這個錯誤,事實上,我得到了我想要的輸出。

但經過一整天的長期研究,並嘗試了一堆東西,包括淺拷貝,克隆等等。當程序進入checkMatch函數時,我仍然無法讓堆棧包含某些東西。在我的調試活動期間,ive意識到堆棧和輸入在checkMatch函數中得到了重置,因此我收到了這個堆棧空的異常。

這裏是代碼:

public static void loadtheInput(Queue<string> input) 
{ 
    input.Enqueue("A"); 
    input.Enqueue("B"); 
    input.Enqueue("C"); 
    input.Enqueue("D"); 
} 

public static void printtheLine(StreamWriter DisplayOutTxt, Queue<string> sameMatch, Stack<string> stack, Queue<string> input, string operations) 
{ 
    string returnMatched = ""; 
    string returnStack = ""; 
    string returnInput = ""; 

    if (stack.Count == 0) //if stack is empty, printtheLine so DisplayOutTxt the table header 
    { 
     stack.Push("A"); 
     stack.Push("C"); 
    } 
    returnMatched = printQueue(matched); 
    returnStack = printStack(stack); 
    returnInput = printQueue(input); 
} 

public static string printStack(Stack<string> stack) 
{ 
    string DisplayOutTxt = ""; 
    while (stack.Count > 0) 
    { 
     DisplayOutTxt += stack.Peek(); 
     stack.Pop(); 
    } 
    return DisplayOutTxt; 
} 

private static string printQueue(Queue<string> queue) 
{ 
    string DisplayOutTxt = ""; 

    if (queue.Count == 0) //if the queue is empty 
    { 
     DisplayOutTxt = " "; //set DisplayOutTxt to a space 
    } 
    else 
    { 
     while (queue.Count > 0) //queue not empty 
     { 
      DisplayOutTxt += queue.Peek(); //concat front of queue to DisplayOutTxt 
      queue.Dequeue(); //dequeue the front string 
     } 
    } 
    return DisplayOutTxt; 
} 

public static void checkMatch(StreamWriter DisplayOutTxt, Queue<string> sameMatch, Stack<string> stack, Queue<string> input, ref string operations) 
{ 
    printtheLine(DisplayOutTxt, sameMatch, stack, input, operations); //print line of DisplayOutTxt 

    //here is where i start facing the problem. stack (and input) are both empty once they step into this checkMatch function! 
    //I think its a reference issue, but i just cant figure out what to do after everything Ive tried 

    if (stack.Peek() == input.Peek()) //if the next stuff in stack and input match each other 
    { 
     // some code is here 
    } 
} 

static int Main() 
{ 
    StreamWriter DisplayOutTxt = new StreamWriter("output.txt"); 

    Queue<string> sameMatch = new Queue<string>(); 
    Stack<string> stack = new Stack<string>(); 
    Queue<string> input = new Queue<string>(); 

    string operations = ""; 
    loadtheInput(input); //load input into input queue and load all productions into parse table 

    while (input.Count > 0) //while input vector is not empty 
    { 
     checkMatch(DisplayOutTxt, sameMatch, stack, input, ref operations); //call function to check for sameMatch stuff 
    } 
    DisplayOutTxt.Flush(); 
    DisplayOutTxt.Close(); 
    return 0; 
} 

heres一些調試/步距我沒有來確定被輸入的checkMatch函數時的棧計數的圖像

heres異常錯誤圖像

回答

1

在您的printStack函數中,您正在清除堆棧。循環瀏覽並彈出每個項目。

參考here如何打印堆疊物品而不彈出它們。

在C#中,Stack參數將是一個引用類型,所以在函數中修改它會改變原始堆棧。但是,隨着堆棧實現IEnumerable,您可以枚舉項目而不修改原始項目。

你可以使用類似這樣

public static string printStack(IEnumerable<string> stack) 
{ 
    string DisplayOutTxt = ""; 

    foreach (var obj in stack) 
    { 
     DisplayOutTxt += obj; 
    } 

    return DisplayOutTxt; 
} 

但更容易是做

returnStack = string.Join("", stack); 
+0

我有一種感覺,它與一些循環呢!順便說一句,我檢查了你發佈的鏈接,我看他們是如何做到的butu我不知道如何將它應用到printStack()我有。我是否刪除了彈出窗口?我已經這樣做了,但這並沒有解決這個問題。我可能改變順序嗎? – masterofcatastrophe

+0

returnStack,我是否將它替換爲返回DisplayOutTxt? – masterofcatastrophe

+0

不,你不需要一個方法在IEnumerable集合中將所有項目連接在一起,你可以使用string.Join()方法爲你做這個,空字符串是分隔符。基本上你只是實現了.NET框架中已經存在的東西。 –