public void E131() { FixedCapacityStackOfStrings stack = new FixedCapacityStackOfStrings(100); //string[] file = File.ReadAllText(@"C:\Users\itmin_000\Dropbox\Code\TestProject\External\Algorithm\BagsQueuesStacks\more tobe.txt").Split(' '); string[] file = "it was - the best - of times - - - it was - the - -".Split(' '); for (int i = 0; i < file.Length; i++) { if (file[i] != "-") stack.Push(file[i]); else if (!stack.IsEmpty()) Debug.WriteLine(stack.Pop() + " "); } Debug.WriteLine(string.Format("{0} left on stack.", stack.Size)); }
public void E1311() { FixedCapacityStackOfStrings vals = InfixToPostfix(); FixedCapacityStackOfStrings exp1 = new FixedCapacityStackOfStrings(32); FixedCapacityStackOfStrings exp2 = new FixedCapacityStackOfStrings(32); //**-65-43+21 while (!vals.IsEmpty()) { string val = vals.Pop(); exp1.Push(val); Debug.Write(val); } while (!exp1.IsEmpty()) { string next = exp1.Pop(); if (next == "+") { exp2.Push((int.Parse(exp2.Pop()) + int.Parse(exp2.Pop())).ToString()); } else if (next == "-") { int op2 = int.Parse(exp2.Pop()); int op1 = int.Parse(exp2.Pop()); exp2.Push((op1 - op2).ToString()); } else if (next == "*") { exp2.Push((int.Parse(exp2.Pop()) * int.Parse(exp2.Pop())).ToString()); } else if (next == "/") { exp2.Push((int.Parse(exp2.Pop()) / int.Parse(exp2.Pop())).ToString()); } else { exp2.Push(next); } } Assert.AreEqual("3", exp2.Pop()); }
private static FixedCapacityStackOfStrings InfixToPostfix() { FixedCapacityStackOfStrings vals = new FixedCapacityStackOfStrings(100); FixedCapacityStackOfStrings ops = new FixedCapacityStackOfStrings(100); string[] input = "( ( 1 + 2 ) * ( ( 3 - 4 ) * ( 5 - 6 ) ) )".Split(' '); for (int i = 0; i < input.Length; i++) { if (input[i] == "(") { } else if (input[i] == "+") { ops.Push("+"); } else if (input[i] == "-") { ops.Push("-"); } else if (input[i] == "*") { ops.Push("*"); } else if (input[i] == "/") { ops.Push("/"); } else if (input[i] == ")") { vals.Push(ops.Pop()); } else { vals.Push(input[i]); } } return vals; }
private static FixedCapacityStackOfStrings CopyStack(FixedCapacityStackOfStrings stack) { FixedCapacityStackOfStrings s = new FixedCapacityStackOfStrings(stack.Size); foreach (var item in stack) { s.Push(item); Debug.Write(item); } Debug.WriteLine(""); return s; }
public void E139() { string[] input = "1 + 2 ) * 3 - 4 ) * 5 - 6 ) )".Split(' '); FixedCapacityStackOfStrings exp = new FixedCapacityStackOfStrings(32); FixedCapacityStackOfStrings assistStack = new FixedCapacityStackOfStrings(100); for (int i = 0; i < input.Length; i++) { if (input[i] == ")") { while (!exp.IsEmpty()) { assistStack.Push(exp.Pop()); } exp.Push("("); while (!assistStack.IsEmpty()) { exp.Push(assistStack.Pop()); } exp.Push(")"); } else { exp.Push(input[i]); } } foreach (var item in exp) { Debug.Write(item); } }
public void E134() { //string input = "[()]{}{[()()]()}"; string input = "[(])"; FixedCapacityStackOfStrings stack = new FixedCapacityStackOfStrings(input.Length / 2); if (input.Length % 2 != 0) { Debug.WriteLine("false"); return; } for (int i = 0; i < input.Length; i++) { if (input[i] == '{') { stack.Push("{"); } else if (input[i] == '[') { stack.Push("["); } else if (input[i] == '(') { stack.Push("("); } else if (input[i] == '}') { if (stack.Pop() != "{") { Debug.WriteLine("false"); return; } } else if (input[i] == ']') { if (stack.Pop() != "[") { Debug.WriteLine("false"); return; } } else if (input[i] == ')') { if (stack.Pop() != "(") { Debug.WriteLine("false"); return; } } } Debug.WriteLine("true"); }
public void E1312() { //string[] input = "( ( 1 + 2 ) * ( ( 3 - 4 ) * ( 5 - 6 ) ) )".Split(' '); string[] input = "1 2 3 4 5".Split(' '); FixedCapacityStackOfStrings stack = new FixedCapacityStackOfStrings(input.Length); for (int i = 0; i < input.Length; i++) { stack.Push(input[i]); Debug.Write(input[i]); } Debug.WriteLine(""); foreach (var item in CopyStack(stack)) { Debug.Write(item); } Debug.WriteLine(""); }