示例#1
0
 public bool ApplyModifiers(Stack stack, Modifier mod)
 {
     //This is used to perform the mathematical modifiers.
     int firstValue = stack.Pop();
     Program.form.listBox1.Items.Add("FirstValue is removed from stack.");
     int secondValue = stack.Pop();
     Program.form.listBox1.Items.Add("SecondValue is removed from stack.");
     if (mod == Modifier.Add){
         Push(firstValue+secondValue);
         Program.form.listBox1.Items.Add("Pushed " + (firstValue + secondValue) + " to stack.");
     }
     else if (mod == Modifier.Divide){
         if(secondValue == 0){
             Program.form.listBox1.Items.Add("Can't devide by zero.");
             return false;
         }
         Push(firstValue/secondValue);
         Program.form.listBox1.Items.Add("Pushed " + (firstValue / secondValue) + " to stack.");
     }
     else if (mod == Modifier.Multiply){
         Push(firstValue*secondValue);
         Program.form.listBox1.Items.Add("Pushed " + (firstValue * secondValue) + " to stack.");
     }
     else if (mod == Modifier.Subtract){
         Push(firstValue-secondValue);
         Program.form.listBox1.Items.Add("Pushed " + (firstValue - secondValue) + " to stack.");
     }
     else{
         Program.form.listBox1.Items.Add("Unknown modifier: " + mod); //Just to be sure.
         return false;
     }
     return true;
 }
示例#2
0
        //Use my Stack class.
        public ListStack(Stack b)
        {
            listStack = new List<int>();    //Constructor for the List.
            stackPointer = 0;

            if (b != null)
            {
                int pointer2;
                int i = 0;

                int[] array = new int[10];

                pointer2 = b.GetCount();
                if (pointer2 > 0)
                {
                    i = 1;
                }

                while (pointer2 > 0)
                {
                    array[i] = b.Pop();
                    pointer2--;
                    i++;
                }
                i--;
                while (i > 0)
                {
                    this.Push(array[i]);
                    i--;
                }
            }
        }
示例#3
0
        public ArrayStack(Stack b)
        {
            arrayStack = new int[10];   //Tis the the Array where the Values will be stored. Max 10 items.
            Pointer = 0;    //A pointer to keep track of where to Pop or Push the next Value.

            if (b != null)
            {
                int pointer2;
                int i = 0;

                int[] array = new int[10];

                pointer2 = b.GetCount();
                if (pointer2 > 0)
                {
                    i = 1;
                }

                while (pointer2 > 0)
                {
                    array[i] = b.Pop();
                    pointer2--;
                    i++;
                }
                i--;
                while (i > 0)
                {
                    this.Push(array[i]);
                    i--;
                }
            }
        }
示例#4
0
        public MyListStack(Stack b)
        {
            linkedListStack = new LinkedList<int>();
            Pointer = 0;

            if (b != null)
            {
                int pointer2;
                int i = 0;

                int[] array = new int[10];

                pointer2 = b.GetCount();
                if (pointer2 > 0)
                {
                    i = 1;
                }

                while (pointer2 > 0)
                {
                    array[i] = b.Pop();
                    pointer2--;
                    i++;
                }
                i--;
                while (i > 0)
                {
                    this.Push(array[i]);
                    i--;
                }
            }
        }
示例#5
0
 public void EmptyStack(Stack stack)
 {
     //To clear the stack just Pop every item in the stack.
     int max = stack.GetCount();
     for (int i = 0;i < max; i++){
         stack.Pop();
     }
 }