// ============ ВЫПОЛНЕНИЕ ДЕЙСТВИЯ НАД ДВУМЯ ЧИСЛАМИ ============ public static void Calculate(MyStack <float> numbers, MyStack <string> symbols) { float b = numbers.Pop(); float a = numbers.Pop(); string sym = symbols.Pop(); float result = 0; switch (sym) { case "+": result = a + b; break; case "-": result = a - b; break; case "*": result = a * b; break; case "/": result = a / b; break; } numbers.Push(result); }
// ============ ПОДСЧЕТ ЗНАЧЕНИЯ ВЫРАЖЕНИЯ ============ public static void SolveProblem(string[] problem) { MyStack <float> numbers = new MyStack <float>(); MyStack <string> symbols = new MyStack <string>(); foreach (var el in problem) { float number; bool success = float.TryParse(el, out number); if (success) { numbers.Push(number); } else if (symbols.IsEmpty) { symbols.Push(el); } else if (el == "*" || el == "/") { if (symbols.Peek() == "+" || symbols.Peek() == "-") { symbols.Push(el); } else { while (!symbols.IsEmpty && symbols.Peek() != "+" && symbols.Peek() != "-") { Calculate(numbers, symbols); if (symbols.IsEmpty) { symbols.Push(el); break; } else if (symbols.Peek() == "+" || symbols.Peek() == "-") { symbols.Push(el); break; } } } } else { while (!symbols.IsEmpty) { Calculate(numbers, symbols); if (symbols.IsEmpty) { symbols.Push(el); break; } } } } while (!symbols.IsEmpty) { Calculate(numbers, symbols); } Console.WriteLine($"Result: {numbers.Pop()}"); }