首页 \ 问答 \ 堆栈是空的......为什么?(Stack is empty …Why?)

堆栈是空的......为什么?(Stack is empty …Why?)

我正在处理涉及堆栈和队列的ac#程序。 队列将输入字符串排队,并在同一时间对队列输入执行堆栈操作时将其出列。

现在发生的事情是在checkMatch()中,程序给出了一个异常错误,表明Stack是空的。 我已经使用了调试/步骤,我在checkMatch函数中看到Stack真的是空的,我不明白为什么。 我用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函数时的堆栈计数

继承人异常错误图像


im working on a c#program that involved stack and queue. the queue Enqueues the input string, and dequeues it as the stack operations are done on the input from the queue in the mean time.

now what is happening is in the checkMatch(), the program gives an exception error that says the Stack is empty. Ive used the debugging/step over and I see the Stack is truly empty in the checkMatch function, and I dont understand why. Ive done the same program in c++ to test it out, and in c++ i dont get this error at all, in fact, I get the output I desire.

but after a long research throughout the day, and trying a bunch of things, including shallow copy, cloning, etc.. i still cant seem to get the stack to contain something by the time the program enters the checkMatch function. during my debugging activity, ive realized that the stack and input are getting reset in the checkMatch function, hence why I am receiving this stack empty exception.

here is the code:

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 an image of some debugging/stepover i did to determine the stack count by the time the checkMatch function was entered

heres the exception error image


原文:https://stackoverflow.com/questions/40190364
更新时间:2023-09-16 07:09

最满意答案

您应该为要显示的项目类型创建DataTemplate。 在该DataTemplate中,您使用DataTrigger来控制项目在外观上的变化,具体取决于对象的属性。


You should create a DataTemplate for the type of the item you're displaying. In that DataTemplate, you use a DataTrigger to control how the item varies in appearance depending upon a property of the object.

相关问答

更多
  • 嗯,这不是一个完整的答案,但它符合我的需要。 我不需要深入到多边形的数据,因为这些值不是数据值,它们是计算值。 它们不需要在我的“数据层”中,它们可以在我的adorner类中计算值。 我把它完成了 public String TestingLabel { get { return "test"; } } 在我的AxisSelectionAdorners类中。 然后像这样绑定xaml: {Binding TestingLabel, RelativeSource={RelativeSource Ancesto ...
  • 您需要在绑定上使用IValueConverter。 BackgroundColor="{Binding Path=IsRound, Converter={StaticResource BoolToFillColorConverter}}" public class BoolToFillColorConverter: IValueConverter { public object Convert(object value, Type targetType, object parameter, Syste ...
  • 尝试在类Person中实现INotifyPropertyChanged: public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string _name; private string _firstName; public string Name { get { ...
  • 您可能没有在类中实现INotifyPropertyChanged,并且可能在绑定后分配值。 如果您尝试使用Snoop http://snoopwpf.codeplex.com/,您可以找到确切的问题。 You may not be implementing INotifyPropertyChanged in the classes and may be assigning the value after the binding. If you try Snoop http://snoopwpf.codepl ...
  • 我不认为StyleSelector侦听PropertyChange通知,因此当IsInLiveMode属性发生更改时,它不会重新运行。 将您的样式DataTrigger基于IsInLiveMode ,并且只要引发属性更改通知,它就会被重新评估。