Beispiel #1
0
        public static void IEnumerable_Testing <TStack>()
            where TStack : IStack <int>, new()
        {
            {             // push only
                int[]        values = { 0, 1, 2, 3, 4, 5, };
                IStack <int> stack  = new TStack();
                values.Stepper(i => stack.Push(i));
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(stack) == values.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                values.Stepper(i => set.Add(i));
                foreach (int i in stack)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
            {             // push + pop
                int[]        values         = { 0, 1, 2, 3, 4, 5, };
                int[]        expectedValues = { 0, 1, 2, 3, };
                IStack <int> stack          = new TStack();
                values.Stepper(i => stack.Push(i));
                stack.Pop();
                stack.Pop();
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(stack) == expectedValues.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                expectedValues.Stepper(i => set.Add(i));
                foreach (int i in stack)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
            {             // push + pop
                int[]        values = { 0, 1, 2, 3, 4, 5, };
                IStack <int> stack  = new TStack();
                values.Stepper(i => stack.Push(i));
                values.Stepper(i =>
                {
                    stack.Pop();
                    stack.Push(i);
                });
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(stack) == values.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                values.Stepper(i => set.Add(i));
                foreach (int i in stack)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
        }
        //Написать программу, которая определяет, является ли введенная скобочная последовательность правильной.
        //Примеры правильных скобочных выражений: (), ([])(), { }(), ([{}]),
        //неправильных — )(, ()) ({), (, ])}), ([(]) для скобок [, (, {.
        //Например: (2+(2*2)) или [2/{5*(4+7)}]
        static void BracketsValidator(string input)
        {
            TStack <char> sequence = new TStack <char>();
            bool          validate = true;

            char[] openBracketsPull  = { '[', '{', '(' };
            char[] closeBracketsPull = { ']', '}', ')' };

            for (int i = 0; i < input.Length; i++)
            {
                if (openBracketsPull.Contains(input[i]))
                {
                    sequence.Push(input[i]);
                }
                else if (closeBracketsPull.Contains(input[i]))
                {
                    try
                    {
                        char toCompare = sequence.Pop();
                        if (Array.IndexOf(openBracketsPull, toCompare) != (Array.IndexOf(closeBracketsPull, input[i])))
                        {
                            validate = false;
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        validate = false;
                        break;
                    }
                }
            }
            Console.WriteLine($"Input: {input}");
            Console.WriteLine("{0}", validate ? "Test passed" : "Test failed");
        }
Beispiel #3
0
 public static void Stepper_Testing <TStack>()
     where TStack : IStack <int>, new()
 {
     {             // push only
         int[]        values = { 0, 1, 2, 3, 4, 5, };
         IStack <int> stack  = new TStack();
         values.Stepper(i => stack.Push(i));
         Assert.IsTrue(stack.Count == values.Length);
         ISet <int> set = SetHashLinked.New <int>();
         values.Stepper(i => set.Add(i));
         stack.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
     {             // push + pop
         int[]        values         = { 0, 1, 2, 3, 4, 5, };
         int[]        expectedValues = { 0, 1, 2, 3, };
         IStack <int> stack          = new TStack();
         values.Stepper(i => stack.Push(i));
         stack.Pop();
         stack.Pop();
         Assert.IsTrue(stack.Count == expectedValues.Length);
         ISet <int> set = SetHashLinked.New <int>();
         expectedValues.Stepper(i => set.Add(i));
         stack.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
     {             // push + pop
         int[]        values = { 0, 1, 2, 3, 4, 5, };
         IStack <int> stack  = new TStack();
         values.Stepper(i => stack.Push(i));
         values.Stepper(i =>
         {
             stack.Pop();
             stack.Push(i);
         });
         Assert.IsTrue(stack.Count == values.Length);
         ISet <int> set = SetHashLinked.New <int>();
         values.Stepper(i => set.Add(i));
         stack.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
 }
Beispiel #4
0
        public void Run()
        {
            Procedure p = FindProcedure("System.Void local.Assembly.Main::EntryPoint()");

            TStack stack = new TStack();
            Frame  frame = new Frame(this, stack);

            Interpreter i = new Interpreter(this);

            i.ExecuteProcedure(frame, p);
        }
        //Реализовать перевод из десятичной в двоичную систему счисления с использованием стека.
        static void ConvertToBinary(int number)
        {
            TStack <int> binary = new TStack <int>();

            while (number >= 2)
            {
                binary.Push(number % 2);
                number /= 2;
            }
            binary.Push(number);
            binary.PrintStackData();
        }
Beispiel #6
0
        private TValue[] Execute(TStack stack, String name)
        {
            Procedure p = package.FindProcedure(name);

            Frame frame = new Frame(package, stack);

            frame.LoadArguments(p.GetArguments());

            ExecuteProcedure(frame, p);

            return(frame.Arguments);
        }
            public Task <int> RunAsync <TStack>() where TStack : Stack, new()
            {
                try
                {
                    var stack = new TStack();
                    // Stack doesn't call RegisterOutputs, so we register them on its behalf.
                    stack.RegisterPropertyOutputs();
                    RegisterTask("User program code.", stack.Outputs.DataTask);
                }
                catch (Exception ex)
                {
                    return(HandleExceptionAsync(ex));
                }

                return(WhileRunningAsync());
            }
Beispiel #8
0
 public void SetUp()
 {
     _stack = new TStack <string>();
 }
Beispiel #9
0
        public static void Main(string[] args)
        {
            var s = new TStack <int>();

            s.Pop();
        }
        //*Реализовать алгоритм перевода из инфиксной записи арифметического выражения в постфиксную
        static void ReversePolishNotationAlgorithm(string input)
        {
            Console.WriteLine($"Input: {input}");
            Console.Write("Result: ");
            TStack <char> operatorStack = new TStack <char>();
            bool          operatorCheck;
            char          toCheckWith;

            for (int i = 0; i < input.Length; i++)
            {
                operatorCheck = false;
                if (input[i] == ' ')
                {
                    continue;
                }
                if (IsOperator(input[i]))
                {
                    while (operatorStack.Count > 0 && !operatorCheck)
                    {
                        toCheckWith = operatorStack.Pop();
                        if (input[i] == ')')
                        {
                            if (toCheckWith != '(')
                            {
                                Console.Write($"{toCheckWith} ");
                                continue;
                            }
                        }
                        else if (input[i] == '(')
                        {
                            operatorStack.Push(toCheckWith);
                            operatorStack.Push(input[i]);
                            operatorCheck = true;
                            continue;
                        }
                        if (toCheckWith == '(')
                        {
                            operatorCheck = true;
                            if (input[i] != ')')
                            {
                                operatorStack.Push(toCheckWith);
                                operatorStack.Push(input[i]);
                            }
                            continue;
                        }
                        operatorCheck = PriorityCheck(input[i], toCheckWith);
                        if (!operatorCheck)
                        {
                            Console.Write($"{toCheckWith} ");
                        }
                        else
                        {
                            operatorStack.Push(toCheckWith);
                            operatorStack.Push(input[i]);
                        }
                    }
                    if (operatorStack.Count == 0 && input[i] != ')')
                    {
                        operatorStack.Push(input[i]);
                    }
                }
                else
                {
                    Console.Write($"{input[i]} ");
                }
            }
            while (operatorStack.Count != 0)
            {
                Console.Write(operatorStack.Pop() + " ");
            }
            Console.WriteLine();
        }
Beispiel #11
0
        private int DoCALL_LOC(Frame frame, Instruction ili)
        {
            bool isConstructor = ili.Operand.As <string>().IsConstructor();

            int    argCount;
            TStack argsStack = new TStack();

            if (ili.Operand.As <string>().IsNoArgFunc())
            {
                argCount = 0;
            }
            else
            {
                Type[] pargs = TypeHelper.GetArguments(ili.Operand.As <string>().ExtractArguments());
                argCount = pargs.Length;
            }

            TValue[] argRefs = new TValue[argCount];
            TValue[] args    = new TValue[argCount];

            if (isConstructor)
            {
                throw new NotImplementedException();
            }

            for (int i = argCount - 1; i >= (isConstructor ? 1 : 0); i--)
            {
                argRefs[i] = frame.Pop();
                args[i]    = frame.ResolveRef(argRefs[i]);

                if (argRefs[i].IsRef)
                {
                    args[i].MakeValueRef();
                }
            }

            for (int i = 0; i < argCount; i++)
            {
                argsStack.Push(args[i]);
            }

            args = Execute(argsStack, ili.Operand.As <string>());

            while (argsStack.Count > 0)
            {
                TValue val = argsStack.Pop();
                if (val.IsArgRef)
                {
                    frame.Push(args[val.Index]);
                }
                else
                {
                    frame.Push(val);
                }
            }

            for (int i = argCount - 1; i >= 0; i--)
            {
                frame.AssignRef(argRefs[i], args[i]);
            }

            if (isConstructor)
            {
                frame.Push(frame.Locals[255]);
            }

            return(0);
        }
Beispiel #12
0
 public Frame(Package package, TStack stack)
 {
     this.package = package;
     this.stack   = stack;
     this.locals  = new TValue[256];
 }