/// <summary> /// 计算 /// </summary> /// <param name="ope">运算符</param> /// <param name="numList">参数列表</param> public static CalNum Calculate(CalOpe ope, List<CalNum> numList) { CalNum result = null; try { switch (ope.tag) { //下面是运算符 case "+": result = Cal_Plus(numList); break; case "-": result = Cal_Minus(numList); break; case "*": result = Cal_Multiply(numList); break; case "/": result = Cal_Divide(numList); break; case "Mod": case "mod": result = Cal_Mod(numList); break; case "^": result = Cal_Pow(numList); break; case "Exp": case "exp": result = Cal_Exp(numList); break; case "ln": result = Cal_Ln(numList); break; case "log": result = Cal_Log(numList); break; case "sqrt": result = Cal_Sqrt(numList); break; case "sin": result = Cal_Sin(numList); break; case "cos": result = Cal_Cos(numList); break; case "tan": result = Cal_Tan(numList); break; case "asin": result = Cal_Asin(numList); break; case "acos": result = Cal_Acos(numList); break; case "atan": result = Cal_Atan(numList); break; case "sinh": result = Cal_Sinh(numList); break; case "cosh": result = Cal_Cosh(numList); break; case "tanh": result = Cal_Tanh(numList); break; case "nCr": //组合 case "ncr": result = Cal_nCr(numList); break; case "nAr": //排列 case "nar": result = Cal_nAr(numList); break; case "!": result = Cal_Fac(numList); break; case "%": result = Cal_Per(numList); break; case "度": result = Cal_Du(numList); break; case "toDegree": result = Cal_Degree(numList); break; case "BIN": //二进制 result = Cal_B(numList); break; case "OCT": //八进制 result = Cal_O(numList); break; case "HEX": //十六进制 result = Cal_H(numList); break; case "toB": //二进制 result = Cal_ToB(numList); break; case "toO": //八进制 result = Cal_ToO(numList); break; case "toH": //十六进制 result = Cal_ToH(numList); break; //下面是常数 case "PI": case "pi": result = new CalNum(Math.PI); break; case "e": result = new CalNum(Math.E); break; case "rnd": Random rnd = new Random(); result = new CalNum(rnd.NextDouble()); break; //下面是函数 case "avg": result = average(numList); break; case "sum": result = sum(numList); break; case "conv": result = UnitConversion(numList); break; case "s": result = SwatchStandardVariance(numList); break; case "dms": result = dms(numList); break; //下面是逻辑运算 case "and": case "And": result = and(numList); break; case "or": case "Or": result = or(numList); break; case "xor": case "Xor": result = xor(numList); break; case "not": case "Not": result = not(numList); break; //下面是位移运算符 case ">>": case "Rsh": result = Rsh(numList); break; case "<<": case "Lsh": result = Lsh(numList); break; //其他运算符 case "sgn": case "Sgn": case "Sign": case "sign": result = sgn(numList); break; case "int": case "Int": result = Int(numList); break; case "Abs": case "abs": result = abs(numList); break; case "Rnd": result = Rnd(numList); break; //分段函数 case "subfuns": MakeException("该函数不能这样使用"); break; //变量或用户自定义函数 default: result = GetVariableOrCustomFunResultValue(ope.tag, numList); break; } } catch (Exception ex) { throw (new Exception("在'" + ope.tag + "'附近可能存在错误:" + ex.Message)); } if (result == null) { throw (new Exception("在'" + ope.tag + "'附近可能存在错误")); } if (result.stringValue == "非数字") { throw (new Exception("'" + ope.tag + "'运算的计算结果非数字")); } return result; }
/// <summary> /// 构造函数 /// </summary> public CalVal(CalOpe ope) { this.ope = ope; this.type = ValType.Operator; this.level = ope.level; }
/// <summary> /// 插入一个运算因子 /// </summary> private void Insert(CalOpe ope, int pos) { this.valList.Insert(pos, new CalVal(ope)); }