public string[] ConvertToPostfixNotation(string input) { List<string> operators = new List<string>(standart_operators); List<string> outputSeparated = new List<string>(); Stack<string> stack = new Stack<string>(); foreach (string c in Separate(input)) { if (operators.Contains(c)) { if (stack.Count > 0 && !c.Equals("(")) { if (c.Equals(")")) { string s = stack.Pop(); while (!s.Equals("(")) { outputSeparated.Add(s); s = stack.Pop(); } } else if (GetPriority(c) > GetPriority(stack.Peek())) stack.Push(c); else { while (stack.Count > 0 && GetPriority(c) <= GetPriority(stack.Peek())) outputSeparated.Add(stack.Pop()); stack.Push(c); } } else stack.Push(c); } else outputSeparated.Add(c); } if (stack.Count > 0) foreach (string c in stack) outputSeparated.Add(c); return outputSeparated.ToArray(); }
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 doSmth(string s) //{ // bool reallyDoingSmth = false; // double temp = 0; // for (int i = 1; i < s.Length; i++) // { // if (s[i] == '/'){ // temp = Convert.ToDouble(s.Substring(0, i)) / Convert.ToDouble(s.Substring(i + 1)); // reallyDoingSmth = true; // break; // } // if (s[i] == '*') { // temp = Convert.ToDouble(s.Substring(0, i)) * Convert.ToDouble(s.Substring(i + 1)); // reallyDoingSmth = true; // break; // } // if (s[i] == '+') // { // temp = Convert.ToDouble(s.Substring(0, i)) + Convert.ToDouble(s.Substring(i + 1)); // reallyDoingSmth = true; // break; // } // if (s[i] == '-') { // temp = Convert.ToDouble(s.Substring(0, i)) - Convert.ToDouble(s.Substring(i + 1)); // reallyDoingSmth = true; // break; // } // } // smth = false; // if (reallyDoingSmth) // { // return temp.ToString(); // } // else // { // return s; // } //} //private double doSmthToo(string s) //{ // List<double> tempNum = new List<double>(); // List<char> tempOper = new List<char>(); // double temp = 0; // for (int i = 0; i < s.Length; i++) // { // if ((s[i].Equals('-')) || (s[i].Equals('+')) || (s[i].Equals('*')) || (s[i].Equals('/'))) // { // tempNum.Add(Convert.ToDouble(s.Substring(0, i))); // tempOper.Add(s.Substring(i, 1).ToCharArray()[0]); // } // } // for (int i = 0; i < tempNum.Count; i++) // { // if (tempOper[i].Equals('-')) // { // temp -= tempNum[i]; // } // if (tempOper[i].Equals('+')) // { // temp += tempNum[i]; // } // if (tempOper[i].Equals('*')) // { // temp *= tempNum[i]; // } // if (tempOper[i].Equals('/')) // { // temp /= tempNum[i]; // } // } // return temp; //} private string toUnarMinus(string input) { List<string> number = new List<string>(standart_number); for (int i = 0; i < input.Length; i++) { if (input[i].Equals('-')) { try { if (!number.Contains(input[i - 1].ToString())) { input = input.Remove(i, 1); input = input.Insert(i, "@"); } } catch { input = input.Remove(0, 1); input = input.Insert(0, "@"); } } } return input; }