Exemple #1
0
 private double GetResult()
 {
     var stack = new Stack<string>();
     //обрабатываем массив 
     foreach (var str in _polishNotation)
     {
         if (str == null)
             break;
         if (GetPriority(str) == -1)
             stack.Push(str);
         else
             if (str == "*" || str == "/" || str == "+" || str == "-" || str == "^")
             {
                 double first = 0, second = 0;
                 double.TryParse(stack.Pop(), out second);
                 double.TryParse(stack.Pop(), out first);
                 switch (str)
                 {
                     case "+":
                         stack.Push((first + second).ToString(CultureInfo.InvariantCulture));
                         break;
                     case "-":
                         stack.Push((first - second).ToString(CultureInfo.InvariantCulture));
                         break;
                     case "*":
                         stack.Push((first * second).ToString(CultureInfo.InvariantCulture));
                         break;
                     case "/":
                         stack.Push((first / second).ToString(CultureInfo.InvariantCulture));
                         break;
                     case "^":
                         stack.Push(Math.Pow(first, second).ToString(CultureInfo.InvariantCulture));
                         break;
                 }
             }
         //Console.WriteLine(stack.top());
     }
     double result;
     if ((stack.GetSize() != 1) || !double.TryParse(stack.Top(), out result))
     {
         //Console.WriteLine("Incorrect Input");
         throw new IndexOutOfRangeException();
     }
     return result;
 }