public decimal result(string input) { input = correction(input); input = toUnarMinus(input); List<string> operators = new List<string>(standart_operators); Stack<string> stack = new Stack<string>(); Queue<string> queue = new Queue<string>(ConvertToPostfixNotation(input)); string str = queue.Dequeue(); while (queue.Count >= 0) { if (!operators.Contains(str)) { try { stack.Push(str); str = queue.Dequeue(); } catch { stack.Clear(); stack.Push("Unparsable"); break; } } else { decimal summ = 0; switch (str) { case "+": { try { decimal a = Convert.ToDecimal(stack.Pop()); decimal b = Convert.ToDecimal(stack.Pop()); summ = a + b; break; } catch { return Convert.ToDecimal("asdasd"); break; } } case "-": { try { decimal a = Convert.ToDecimal(stack.Pop()); decimal b = Convert.ToDecimal(stack.Pop()); summ = b - a; break; } catch { return Convert.ToDecimal("asdasd"); break; } } case "*": { try { decimal a = Convert.ToDecimal(stack.Pop()); decimal b = Convert.ToDecimal(stack.Pop()); summ = a * b; break; } catch { return Convert.ToDecimal("asdasd"); break; } } case "/": { try { decimal a = Convert.ToDecimal(stack.Pop()); decimal b = Convert.ToDecimal(stack.Pop()); summ = b / a; break; } catch { return Convert.ToDecimal("asdasd"); break; } } case "^": { try { decimal a = Convert.ToDecimal(stack.Pop()); decimal b = Convert.ToDecimal(stack.Pop()); summ = Convert.ToDecimal(Math.Pow(Convert.ToDouble(b), Convert.ToDouble(a))); break; } catch { return Convert.ToDecimal("asdasd"); break; } } case "@": { try { decimal a = Convert.ToDecimal(stack.Pop()); summ = -a; break; } catch { break; } } } stack.Push(summ.ToString()); if (queue.Count > 0) str = queue.Dequeue(); else break; } } if (stack.ToArray().Length > 1) { string s = "sasdasd"; decimal b = Convert.ToDecimal(s); return b; } else { return Convert.ToDecimal(stack.Pop()); } }
private string startCalculating() { if (expression.Equals(Constant.VOID) || expression.Length <= 0) { return(Constant.VOID); } string[] tokens = letsGOTokenize(expression); showArray(tokens, "tokens"); string token, previous = Constant.VOID; foreach (string el in tokens) { token = el; Console.WriteLine("\n"); showArray(numbers.ToArray(), "Numbers Before"); showArray(operations.ToArray(), "Operations Before"); // is Delimiter if (isDelimiter(token)) { switch (token) { case Constant.OPEN_BRACKET: tryInsertNumericFactor(previous); operations.Push(Constant.OPEN_BRACKET); break; case Constant.CLOSE_BRACKET: while (operations.Count != 0 && !operations.Peek().Equals(Constant.OPEN_BRACKET)) { calculate(); } operations.Pop(); break; } } // is Operation or Function if (isOperation(token) || isFunction(token)) { if (operations.Count == 0) { if (token.Equals(Constant.MINUS) && isUnaryMinus(previous)) { token = Constant.UNARY_MINUS; } else if (isFunction(token) && !token.Equals(Constant.FACTORIAL)) { tryInsertNumericFactor(previous); } } else { // operations not empty if (isFunction(token) && !token.Equals(Constant.FACTORIAL)) { tryInsertNumericFactor(previous); } else if (token.Equals(Constant.MINUS) && isUnaryMinus(previous)) { token = Constant.UNARY_MINUS; } if (!previous.Equals(Constant.EXPONENT) && !token.Equals(Constant.UNARY_MINUS)) { while (operations.Count != 0 && getPriority(token) <= getPriority(operations.Peek())) { calculate(); } } } operations.Push(token); } // is Number if (isNumber(token)) { tryInsertNumericFactor(previous); if (token.Equals(Constant.PI)) { token = String.Concat(Math.PI); } else if (token.Equals(Constant.E)) { token = String.Concat(Math.E); } if (isContainsFunction(token)) { int len = getFunctionLength(token); String number = token.Substring(0, token.Length - len); String function = token.Substring(token.Length - len); numbers.Push(number); while (operations.Count != 0 && getPriority(Constant.MULTIPLY) <= getPriority(operations.Peek())) { calculate(); } operations.Push(Constant.MULTIPLY); operations.Push(function); token = function; } else { numbers.Push(token); } } previous = token; Console.WriteLine("\n"); showArray(numbers.ToArray(), "Numbers After"); showArray(operations.ToArray(), "Operations After"); } while (operations.Count != 0) { calculate(); } Console.WriteLine("\n"); showArray(numbers.ToArray(), "Numbers End"); showArray(operations.ToArray(), "Operations End"); return(numbers.Pop()); }