예제 #1
0
        // Explained in Stack class
        public override double?GetValue(int stackLength = -1)
        {
            // if no count given, set length as count
            if (stackLength == -1)
            {
                stackLength = getLength();
            }

            if (stackLength == 0)
            {
                return(null);
            }
            else if (stackLength == 1)
            {
                return(item);
            }
            else
            {
                return(next.GetValue(stackLength - 1));
            }
        }
예제 #2
0
        /// <summary>
        /// Calculates last 2 values from stack and adds result to stack
        /// </summary>
        /// <param name="value"></param>
        public void Operator(string value)
        {
            int listBoxCount = listBoxValues.Count;

            if (listBoxCount >= 2)
            {
                // doubles used in calculation
                double?a, b;
                switch (array)
                {
                case "ArrayStack":
                    b = arrayStack.GetValue();
                    arrayStack.Remove();
                    a = arrayStack.GetValue();
                    arrayStack.Remove();
                    break;

                case "ListStack":
                    b = listStack.GetValue();
                    listStack.Remove();
                    a = listStack.GetValue();
                    listStack.Remove();
                    break;

                case "MyListStack":
                    b = myListStack.GetValue();
                    System.Windows.Forms.MessageBox.Show("b - " + b.ToString());
                    a = myListStack.GetValue();
                    System.Windows.Forms.MessageBox.Show("a - " + a.ToString());
                    myListStack.Remove();
                    myListStack.Remove();
                    break;

                default:
                    a = 1;
                    b = 1;
                    break;
                }

                switch (value)
                {
                case "/":
                    lastResult = a / b;
                    break;

                case "*":
                    lastResult = a * b;
                    break;

                case "-":
                    lastResult = a - b;
                    break;

                case "+":
                    lastResult = a + b;
                    break;

                default:
                    lastResult = 0;
                    break;
                }

                // Remove last to values from list
                listBoxValues.RemoveAt(listBoxCount - 1);
                listBoxValues.RemoveAt(listBoxCount - 2);
                AddToStack(true);
            }
        }