我正在处理涉及堆栈和队列的ac#program。队列使输入字符串入队,同时在此期间对来自队列的输入执行堆栈操作时将其出队。
现在发生的事情是在checkMatch()中,程序给出了一个异常错误,指出堆栈为空。我已经使用了调试/调试功能,但我发现在checkMatch函数中,堆栈确实是空的,我也不明白为什么。我已经在c ++中完成了相同的程序来对其进行测试,而在c ++中我根本没有收到此错误,实际上,我得到了我想要的输出。
但是经过一整天的长时间研究,并尝试了一堆东西,包括浅拷贝,克隆等。当程序进入checkMatch函数时,我似乎仍然无法使堆栈包含某些东西。在调试活动中,我知道在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;
}
继承人一些调试/步距的图像我没有以确定由被输入的checkMatch功能时的栈计数
继承人异常错误图像
在您的printStack函数中,您正在清除堆栈。通过循环并弹出每个项目。
请参阅此处,如何在不弹出堆栈项目的情况下打印它们。
在C#中,Stack参数将是引用类型,因此在函数中对其进行修改将使原始Stack发生变化。但是,随着Stack实现IEnumerable,您可以枚举项目,而无需修改原始项目。
你可以用这样的东西
public static string printStack(IEnumerable<string> stack)
{
string DisplayOutTxt = "";
foreach (var obj in stack)
{
DisplayOutTxt += obj;
}
return DisplayOutTxt;
}
但是要做起来要容易得多
returnStack = string.Join("", stack);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句