//多项式除以一个数值 private bool Div(MultinomialC a, MultinomialC b, out MultinomialC out1) { out1 = null; try { out1 = new MultinomialC(a); } catch (Exception) { Error = "内存空间不足"; return false; } if (b.count == 0) { Error = "0不能做除数"; return false; } if (!b.IsNumber()) { Error = "多项式不能做除数"; return false; } if (out1.count == 0) return true; if (out1.IsNumber()) { out1.Buf[0].coefficient /= b.Buf[0].coefficient; return true; } Double n = (int)b.Buf[0].coefficient; for (int i = 0; i < out1.count; i++) out1.Buf[i].coefficient /= n; return true; }
//多项式的指数运算 private bool Pow(MultinomialC a, MultinomialC b, out MultinomialC out1) { out1 = null; if (b.count == 0) { if (a.count == 0) { MessageBox.Show("不能计算0的0次幂"); return false; } try { out1 = new MultinomialC(new MultinomialB(1)); } catch (Exception) { Error = "内存空间不足"; return false; } return true; } if (!b.IsNumber()) { Error = "指数不能是多项式"; return false; } if (a.count == 0) { try { out1 = new MultinomialC(); } catch (Exception) { Error = "内存空间不足"; return false; } return true; } if (a.IsNumber()) { try { out1 = new MultinomialC(new MultinomialB(Math.Pow(a.Buf[0].coefficient, b.Buf[0].coefficient))); } catch (Exception) { Error = "内存空间不足"; return false; } if (Double.IsNaN(out1.Buf[0].coefficient)) return false; return true; } int n = (int)b.Buf[0].coefficient; if (n < 0) { Error = "多项式不能计算负数的幂"; return false; } try { out1 = new MultinomialC(a); } catch (Exception) { Error = "内存空间不足"; return false; } for (int i = 1; i < n; i++) out1 = MUL(out1, a); return true; }
//两个多项式的减法 private static MultinomialC Sub(MultinomialC a, MultinomialC b) { MultinomialC c; MultinomialC d; try { c = new MultinomialC(a); d = new MultinomialC(b); } catch (Exception) { throw; } for (int i = 0; i < d.count; i++) { d.Buf[i].coefficient *= -1; c.Insert(d.Buf[i]); } return c; }
//多项式的单目运算 private bool Cell_Compute(Char O, MultinomialC a, MultinomialC b, out MultinomialC out1) { out1 = null; switch (O) { case '+': out1 = Add(a, b); break; case '-': out1 = Sub(a, b); break; case '*': out1 = MUL(a, b); break; case 'N': out1 = MUL(a, b); break; case '^': if (!Pow(a, b, out out1)) return false; break; case '/': if (!Div(a, b, out out1)) return false; break; default: Error = "存在非法的运算符:" + O + ""; return false; } return true; }
//多项式的综合运算 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; }
//带括号检查、括号替换和出错信息输出功能的多项式综合运算 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 static MultinomialC MUL(MultinomialC a, MultinomialC b) { MultinomialC c; try { c = new MultinomialC(); } catch (Exception) { throw; } for (int i = 0; i < a.count; i++) for (int j = 0; j < b.count; j++) c.Insert(MultinomialB.MUL(a.Buf[i], b.Buf[j])); return c; }
//两个多项式的加法 public static MultinomialC Add(MultinomialC a, MultinomialC b) { MultinomialC c; try { c = new MultinomialC(a); } catch (Exception) { throw; } for (int i = 0; i < b.count; i++) c.Insert(b.Buf[i]); return c; }
//复制构造函数 public MultinomialC(MultinomialC a) { BM = a.BM; M = a.M; count = a.count; try { Buf = new MultinomialB[M]; for (int i = 0; i < count; i++) Buf[i] = new MultinomialB(a.Buf[i]); } catch (Exception) { throw; } }