2016-10-22 170 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框架中已经存在的东西。 –