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); } }
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); } }
public void LoadArguments(Type[] args) { if (args.Length == 0 || stack.Count == 0) { return; } arguments = new TValue[args.Length]; for (int i = args.Length - 1; i >= 0; i--) { arguments[i] = stack.Pop(); } }
//Написать программу, которая определяет, является ли введенная скобочная последовательность правильной. //Примеры правильных скобочных выражений: (), ([])(), { }(), ([{}]), //неправильных — )(, ()) ({), (, ])}), ([(]) для скобок [, (, {. //Например: (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"); }
public void Pop_EmptyStack_ThrowsExcepion() { Assert.That(() => _stack.Pop(), Throws.InvalidOperationException); }
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(); }
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); }