//计算自己的算式,不检查和改变括号,以便提高递归的效率 public bool Number_Compute(out Double out1) { out1 = 0; if (Fm.Length <= 0) { Error = "括号中无算式"; return false; } if (Fm[Seek] == '-' || Fm[Seek] == '+') S_Number.PushStack(0.0); while (Seek < Fm.Length) { if (IsNumber(Fm[Seek])) { String s; if (!Cut_Double2(out s)) return false; S_Number.PushStack(Double.Parse(s)); } else if (IsOperator(Seek)) { if (Fm[Seek] == '√') if (Seek <= 0 || !(Fm[Seek - 1] == ')' || IsNumber(Fm[Seek - 1]) || IsLetter(Fm[Seek - 1]))) S_Number.PushStack(2.0); if (!Operator_Compute(Fm[Seek++])) return false; } else if (IsLetter(Fm[Seek])) { if (Seek > 0) if (IsNumber(Fm[Seek - 1]) || IsLetter(Fm[Seek - 1]) || Fm[Seek - 1] == '²' || Fm[Seek - 1] == '³') { if (!Operator_Compute('N')) return false; } String s; if (Cut_KeyWord(out s)) { Double d1 = 1; if (s == "e" || s == "pi") { if (s == "e") d1 = Math.E; else d1 = Math.PI; } else { Stack S_String; try { S_String = new Stack(5); } catch (Exception) { Error = "内存空间不足"; return false; } S_String.PushStack(s); while (Seek < Fm.Length) { if (IsOperator(Seek) || Fm[Seek] == '!') { Error = "存在多余的运算符\"" + Fm[Seek] + "\""; return false; } else if (!IsLetter(Fm[Seek])) break; if (!Cut_KeyWord(out s)) return false; if (s == "e" || s == "pi") break; S_String.PushStack(s); } bool HaveData = false; if (Seek < Fm.Length && Fm[Seek] == '(') { Object O1; S_String.GetTopData(out O1); s = (String)O1; if (s == "log" || s == "root") { HaveData = true; S_String.PopStack(out O1); Double d2, d3; if (!Cut_Bracket(out d2, out d3)) return false; if (s == "log") { d1 = Math.Log(d3, d2); if (Double.IsNaN(d1)) { Error = "在求对数时遇到语法错误"; return false; } } else if (!Root(d2, d3, out d1)) return false; } } if (s != "e" && s != "pi") s = ""; while (Seek < Fm.Length) { if (Fm[Seek] == '(') s += "(" + Cut_Bracket() + ")"; else if (Fm[Seek] == 'E') { s += Fm[Seek++]; if (Seek < Fm.Length) s += Fm[Seek++]; else { Error = "\"E\"后无数字"; return false; } } else if (Fm[Seek] == '√' || PRI(Fm[Seek]) < PRI('^')) break; else if (IsLetter(Fm[Seek])) { if (Fm[Seek] == 'e') s += Fm[Seek++]; else if (Fm[Seek] == 'P' || Fm[Seek] == 'p') { s += Fm[Seek++]; if (Seek >= Fm.Length) { Error = "存在非法字符\"" + Fm[Seek - 1] + "\""; return false; } else if (Fm[Seek] != 'i' && Fm[Seek] != 'I') { Error = "存在非法字串\"" + Fm[Seek - 1] + Fm[Seek] + "\""; return false; } else s += Fm[Seek++]; } else break; } else s += Fm[Seek++]; } if (s.Length == 0) { if (!HaveData) { Error = "缺少操作数"; return false; } } else { Formula f = new Formula(s, angle_manner); if (HaveData) f.S_Number.PushStack(d1); if (!f.Number_Compute(out d1)) return false; } Object O2; while (!S_String.IsEmpty()) { S_String.PopStack(out O2); if (!Cell_Compute((String)O2, d1, out d1)) return false; } } S_Number.PushStack(d1); } else { Error = "存在非法字串:\"" + s + "\""; return false; } } else if (Fm[Seek] == '(') { Double d1 = 1; if (Seek > 0 && !IsOperator(Seek - 1)) { if (IsNumber(Fm[Seek - 1]) || IsLetter(Fm[Seek - 1]) || Fm[Seek - 1] == ')') { if (!Operator_Compute('*')) return false; } } Formula f1; try { f1 = new Formula(Cut_Bracket(), angle_manner); } catch (Exception) { Error = "内存空间不足"; return false; } if (!f1.Number_Compute(out d1)) return false; S_Number.PushStack(d1); } else if (Fm[Seek] == '!') { Object o1; if (!S_Number.PopStack(out o1)) { Error = "存在多余运算符\"!\""; return false; } Double a1; if (!Factorial((Double)o1, out a1)) return false; S_Number.PushStack(a1); Seek++; } else if (IsESC(Fm[Seek])) Seek++; else { Error = "存在非法字符:\"" + Fm[Seek] + "\""; return false; } } Object data, sy; if (!S_Number.PopStack(out data)) { Error = "存在多余的运算符"; return false; } out1 = (Double)data; while (S_Operator.PopStack(out sy)) { if (!S_Number.PopStack(out data)) { Error = "存在多余的运算符\"" + (Char)sy + "\""; return false; } if (!Cell_Compute((Char)sy, (Double)data, out1, out out1)) return false; } if (!S_Number.IsEmpty()) { Error = "缺少运算符"; return false; } if (Double.IsNaN(out1)) return false; if (Double.IsInfinity(out1)) { Error = "数值太大,已溢出"; out1 = 0; return false; } return true; }
//检查括号是否配对 public bool Check_Bracket() { Stack stack; try { stack = new Stack(10); } catch (Exception) { Error = "内存空间不足"; return false; } Object o1; for (int i = 0; i < Fm.Length; i++) { if (Fm[i] == '(' || Fm[i] == '[' || Fm[i] == '{') stack.PushStack(Fm[i]); else if (Fm[i] == ')') { if (!stack.PopStack(out o1)) { Error = "存在多余的\")\""; return false; } if ((Char)o1 != '(') { Error = "\")\"不配对"; return false; } } else if (Fm[i] == ']') { if (!stack.PopStack(out o1)) { Error = "存在多余的\"]\""; return false; } if ((Char)o1 != '[') { Error = "\"]\"不配对"; return false; } } else if (Fm[i] == '}') { if (!stack.PopStack(out o1)) { Error = "存在多余的\"}\""; return false; } if ((Char)o1 != '{') { Error = "\"}\"不配对"; return false; } } } if (!stack.IsEmpty()) { Error = "括号不配对"; return false; } return true; }