//多项式的综合运算 public bool Multinomial_Compute(out MultinomialC out1) { out1 = null; if (Fm.Length == 0) { Error = "括号中无内容"; return false; } if (Fm[0] == '-') { try { S_Number.PushStack(new MultinomialC()); } catch (Exception) { Error = "内存空间不足"; return false; } } while (Seek < Fm.Length) { if (IsOperator(Fm[Seek])) { if (!Operator_Compute(Fm[Seek++])) return false; } else if (IsNumber(Fm[Seek]) || IsLetter(Fm[Seek])) { if (Seek > 0 && Fm[Seek - 1] == ')') if (!Operator_Compute('*')) return false; Double d = 1; if (IsNumber(Fm[Seek])) { String s; if (!Cut_Double2(out s)) return false; d = Double.Parse(s); } if (Seek < Fm.Length && IsLetter(Fm[Seek])) { MultinomialB b; if (!ConstructMultinomialB(out b)) return false; b.coefficient = d; try { S_Number.PushStack(new MultinomialC(b)); } catch (Exception) { Error = "内存空间不足"; return false; } } else { try { S_Number.PushStack(new MultinomialC(new MultinomialB(d))); } catch (Exception) { Error = "内存空间不足"; return false; } } } else if (Fm[Seek] == '(') { if (Seek > 0) { if (IsNumber(Fm[Seek - 1]) || IsLetter(Fm[Seek - 1]) || Fm[Seek - 1] == ')') { if (!Operator_Compute('*')) return false; } } String s = Cut_Bracket(); Formula f; Double d; try { f = new Formula(s, 0); if (f.Number_Compute(out d)) S_Number.PushStack(new MultinomialC(new MultinomialB(d))); else { MultinomialC c = new MultinomialC(s); if (!c.Multinomial_Compute(out c)) return false; S_Number.PushStack(c); } } catch (Exception) { Error = "内存空间不足"; return false; } } else if (IsESC(Fm[Seek])) Seek++; else { Error = "存在非法字符:\"" + Fm[Seek] + "\""; return false; } } Object data; if (!S_Number.PopStack(out data)) { Error = "存在多余的运算符"; return false; } out1 = (MultinomialC)data; Object O; while (S_Operator.PopStack(out O)) { if (!S_Number.PopStack(out data)) { Error = "存在多余的运算符"; return false; } if (!Cell_Compute((Char)O, (MultinomialC)data, out1, out out1)) return false; } if (!S_Number.IsEmpty()) { Error = "缺少运算符"; return false; } for (int i = 0; i < out1.count; i++) if (Double.IsInfinity(out1.Buf[i].coefficient)) { Error = "数值太大,已溢出"; return false; } return true; }
//计算游标当前位置处的指数 private bool Degree(out Double out1) { out1 = 1; Seek++; if (Seek >= Fm.Length) { Error = "\"^\"后面没有数字"; return false; } while (Seek < Fm.Length) { if (Fm[Seek] != '^' && IsOperator(Fm[Seek]) || IsLetter(Fm[Seek])) break; else if (Fm[Seek] == '^') Seek++; else if (IsNumber(Fm[Seek])) { String s; if (!Cut_Double2(out s)) return false; out1 *= Double.Parse(s); } else if (Fm[Seek] == '(') { String s = Cut_Bracket(); Double d; Formula f1; try { f1 = new Formula(s, 0); } catch (Exception) { Error = "内存空间不足"; return false; } if (f1.Number_Compute(out d)) out1 *= d; else { if (IncludeLetter(s)) Error = "多项式不能作为指数"; return false; } } else if (IsESC(Fm[Seek])) Seek++; else { Error = "存在非法字符:\"" + Fm[Seek] + "\""; return false; } } return true; }
//带括号检查、括号替换和出错信息输出功能的多项式综合运算 public static bool Multinomial_Compute(String si, out String out1) { out1 = ""; if (si.Length == 0) { MessageBox.Show("请输入一个算式", "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } Formula f; try { f = new Formula(si, 0); } catch (Exception) { Error = "内存空间不足"; return false; } if (!f.Check_Bracket()) { MessageBox.Show("多项式非法:\n\n" + Error, "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } f.Formula_Adjust(); Double d; if (f.Number_Compute(out d)) { out1 = Formula.Number_Adjust(d); return true; } MultinomialC c; try { c = new MultinomialC(f.formula); } catch (Exception) { Error = "内存空间不足"; return false; } Error = null; if (!c.Multinomial_Compute(out c)) { if (Error == null) MessageBox.Show("多项式非法", "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); else MessageBox.Show("多项式非法:\n\n" + Error, "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } out1 = c.ToString(); return true; }
//计算自己的算式,不检查和改变括号,以便提高递归的效率 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 Cut_Bracket(out Double out1, out Double out2) { String s1 = ""; out1 = out2 = 0; Seek++; while (Seek < Fm.Length) { if (Fm[Seek] == '(') s1 += "(" + Cut_Bracket() + ")"; else if (Fm[Seek] == ',') { Formula f1; try { f1 = new Formula(s1, angle_manner); } catch (Exception) { Error = "内存空间不足"; return false; } if (!f1.Number_Compute(out out1)) return false; s1 = ""; Seek++; } else if (Fm[Seek] == ')') { Formula f1; try { f1 = new Formula(s1, angle_manner); } catch (Exception) { Error = "内存空间不足"; return false; } if (!f1.Number_Compute(out out2)) return false; break; } else s1 += Fm[Seek++]; } Seek++; return true; }
//计算一个算式不输出出错信息 public static bool Number_Compute2(String si, out Double out1) { out1 = 0; if (si.Length == 0) return false; Formula f1; try { f1 = new Formula(si, 0); } catch (Exception) { Error = "内存空间不足"; return false; } if (!f1.Check_Bracket()) return false; f1.Formula_Adjust(); if (!f1.Number_Compute(out out1)) return false; return true; }
//计算一个用字符串表示的算式并用消息框给出出错信息 public static bool Number_Compute(String si, int angle_manner, out Double out1) { out1 = 0; if (si.Length == 0) { MessageBox.Show("请输入一个算式", "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } Formula f1; try { f1 = new Formula(si, angle_manner); } catch (Exception) { Error = "内存空间不足"; return false; } if (!f1.Check_Bracket()) { MessageBox.Show("算式非法:\n\n" + Error, "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } f1.Formula_Adjust(); Error = null; if (!f1.Number_Compute(out out1)) { if (Error == null) MessageBox.Show("算式非法", "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); else MessageBox.Show("算式非法:\n\n" + Error, "无法计算", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } return true; }