Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- /// <summary>
- /// Простая реализация стека на основе массива.
- /// </summary>
- /// <typeparam name="T">Тип элемента</typeparam>
- public sealed class CustomStack<T> : IEnumerable<T>
- {
- public CustomStack() : this(0)
- {
- }
- public CustomStack(Int32 capacity)
- {
- arr = new T[capacity];
- lastIndex = -1;
- }
- public T Peek()
- {
- if (lastIndex > -1)
- return arr[lastIndex];
- else
- throw new InvalidOperationException("Stack empty.");
- }
- public void Push(T value)
- {
- lastIndex++;
- if (arr.Length < lastIndex + 1)
- this.Capacity = lastIndex > 0 ? lastIndex * 2 : 1;
- arr[lastIndex] = value;
- }
- public T Pop()
- {
- if (lastIndex > -1)
- {
- lastIndex--;
- return arr[lastIndex + 1];
- }
- else
- throw new InvalidOperationException("Stack empty.");
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return new CustomStackEnumerator(this);
- }
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- return new CustomStackEnumerator(this);
- }
- public int Count
- {
- get
- {
- return lastIndex + 1;
- }
- }
- public int Capacity
- {
- get
- {
- return arr.Length;
- }
- set
- {
- if (value >= Count)
- Array.Resize<T>(ref arr, value);
- else
- throw new ArgumentOutOfRangeException("Capacity was less than the current size.");
- }
- }
- private T[] arr;
- private int lastIndex;
- private struct CustomStackEnumerator : IEnumerator<T>, IEnumerator
- {
- public CustomStackEnumerator(CustomStack<T> stack)
- {
- this.stack = stack;
- this.index = -1;
- }
- T IEnumerator<T>.Current
- {
- get
- {
- return stack.arr[index];
- }
- }
- object IEnumerator.Current
- {
- get
- {
- return stack.arr[index];
- }
- }
- public bool MoveNext()
- {
- index++;
- return index < stack.Count;
- }
- public void Reset()
- {
- index = -1;
- }
- void IDisposable.Dispose()
- {
- }
- private CustomStack<T> stack;
- private int index;
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- public static class SimpleCalculator
- {
- public static double CalculateString(string expression)
- {
- translatorStack = new Stack<string>();
- interpreterStack = new Stack<Double>();
- translatorStack.Push(EmptyOperation);
- foreach (string s in (expression + ' ' + EmptyOperation).Split(' '))
- {
- double value;
- if (Double.TryParse(s, out value))
- {
- interpreterStack.Push(value);
- }
- else
- {
- string lastOperationInStack = translatorStack.Peek();
- transitionTable[VerticalIndexesMap.IndexOf(lastOperationInStack), HorizontalIndexesMap.IndexOf(s)](s);
- }
- }
- return interpreterStack.Peek();
- }
- private const string EmptyOperation = "$";
- private const string OpeningBracket = "(";
- private const string Plus = "+";
- private const string Minus = "-";
- private const string Multiplication = "*";
- private const string Division = "/";
- private const string ClosingBracket = ")";
- private static Dictionary<String, Func<double, double, double>> arithmeticalFunctions = new Dictionary<string, Func<double, double, double>>()
- {
- {Plus, (a, b) => a + b},
- {Minus, (a, b) => a - b},
- {Multiplication, (a, b) => a * b},
- {Division, (a, b) => a / b}
- };
- private static Stack<String> translatorStack;
- private static Stack<Double> interpreterStack;
- private static Action<string> f1 = (string inputOperation) =>
- {
- translatorStack.Push(inputOperation);
- };
- private static Action<String> f2 = (string inputOperation) =>
- {
- string lastOperationInStack = translatorStack.Pop();
- double operand2 = interpreterStack.Pop();
- double operand1 = interpreterStack.Pop();
- double result = arithmeticalFunctions[lastOperationInStack](operand1, operand2);
- interpreterStack.Push(result);
- translatorStack.Push(inputOperation);
- };
- private static Action<String> f3 = (string inputOperation) =>
- {
- translatorStack.Pop();
- };
- private static Action<String> f4 = (string inputOperation) =>
- {
- string lastOperationInStack = translatorStack.Pop();
- double operand2 = interpreterStack.Pop();
- double operand1 = interpreterStack.Pop();
- double result = arithmeticalFunctions[lastOperationInStack](operand1, operand2);
- interpreterStack.Push(result);
- lastOperationInStack = translatorStack.Peek();
- transitionTable[VerticalIndexesMap.IndexOf(lastOperationInStack), HorizontalIndexesMap.IndexOf(inputOperation)](inputOperation);
- };
- private static Action<String> f5 = (string inputOperation) =>
- {
- throw new Exception("Invalid input string.");
- };
- private static Action<String> f6 = (string inputOperation) =>
- {
- };
- private const string HorizontalIndexesMap = EmptyOperation + OpeningBracket + Plus + Minus + Multiplication + Division + ClosingBracket;
- private const string VerticalIndexesMap = EmptyOperation + OpeningBracket + Plus + Minus + Multiplication + Division;
- private static readonly Action<String>[,] transitionTable = new Action<String>[,]
- {
- {f6,f1,f1,f1,f1,f1,f5},
- {f5,f1,f1,f1,f1,f1,f3},
- {f4,f1,f2,f2,f1,f1,f4},
- {f4,f1,f2,f2,f1,f1,f4},
- {f4,f1,f4,f4,f2,f2,f4},
- {f4,f1,f4,f4,f2,f2,f4}
- };
- }
- public class Program
- {
- public static void Main(String[] args)
- {
- SimpleCalculator.CalculateString("1 + 2");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement