static ParsedFormula Parse(string formula, string variantName) { FormulaParser parser = new FormulaParser(); parser.Formula = formula; parser.Variant = variantName; IBxUnitConvert root = parser.Parse(); return((root == null) ? null : new ParsedFormula(root)); }
static public void Parse(string s, string variant) { FormulaParser.s_instance.Formula = s; FormulaParser.s_instance.Variant = variant; int nCurPos = 0; IBxUnitConvert root = FormulaParser.s_instance.Parse(null, null, ref nCurPos); double d = root.Calc(1.2); int a = 0; }
public IBxUnitConvert Parse(IBxUnitConvert left, IFEBinary mid, ref int pos) { IBxUnitConvert param = ParseOneParam(ref pos); if (param == null) { if (mid != null) { throw new Exception("操作符后未发现公式元素!"); } else { return(left); } } else { IFEBinary curOperator = ParseOneBinary(ref pos); if (curOperator == null) /*|| (curOperator.ElementType == EFormulaElementType.rightBrace)*/ { if (mid == null) { return(param); } else { return(new BUParsedItem(left, mid, param)); } } else { if (mid == null) { return(Parse(param, curOperator, ref pos)); } else { if (mid.Grade >= curOperator.Grade) { return(Parse(new BUParsedItem(left, mid, param), curOperator, ref pos)); } else { IBxUnitConvert right = Parse(param, curOperator, ref pos); if (right == null) { throw new Exception(string.Format("符号{0}之后的部分错误!", mid.ToString())); } return(new BUParsedItem(left, mid, right)); } } } } }
static bool ContainsVariant(IBxUnitConvert item) { if (item == null) { return(false); } if (item is BUParsedVariant) { return(true); } if (item is BUParsedItem) { if (ContainsVariant(((BUParsedItem)item).left)) { return(true); } return(ContainsVariant(((BUParsedItem)item).right)); } return(false); }
protected IBxUnitConvert ParseOneParam(ref int curPos) { //去掉空格 while ((curPos < length) && (formula[curPos] == ' ')) { curPos++; } if (curPos >= length) { return(null); } //如果是 "(" string temp = BUOperators.s_leftBrace.ToString(); if (string.Compare(formula, curPos, temp, 0, temp.Length, true) == 0) { curPos += temp.Length; return(Parse(null, null, ref curPos)); } //如果是自变量 if (string.Compare(formula, curPos, m_sVariant, 0, m_sVariant.Length, true) == 0) { curPos += m_sVariant.Length; return(new BUParsedVariant()); } //如果是数字 string sDigit = formula.EatDigit(curPos); if (sDigit != null) { double d; if (!double.TryParse(sDigit, NumberStyles.Any, CultureInfo.InvariantCulture, out d)) { throw new Exception(string.Format("分析数据错误{0}", sDigit)); } curPos += sDigit.Length; return(new BUParsedNumber(d)); } //判断是否是负号"-" bool bIsMinus; int nEat = StringDigitParse.EatSignum(formula, curPos, out bIsMinus); if (nEat > 0) { curPos += nEat; IBxUnitConvert next = ParseOneParam(ref curPos); if (next != null) { if (bIsMinus) { return(new BUParsedItem(new BUParsedNumber(-1), BUOperators.s_binarys[2], next)); } else { return(next); } } } return(null); }
public ParsedFormula(IBxUnitConvert root) { m_rootItem = root; }
public ParsedFormula() { m_rootItem = null; }
public BUParsedItem(IBxUnitConvert leftParam, IFEBinary midOperater, IBxUnitConvert rightParam) { left = leftParam; mid = midOperater; right = rightParam; }
static IBxUnitConvert Inverse(IBxUnitConvert root) { BUParsedVariant var = new BUParsedVariant(); return(_Inverse(var, root)); }
static IBxUnitConvert _Inverse(IBxUnitConvert Y, IBxUnitConvert X) { if (X is BUParsedVariant) { return(Y); } if (!(X is BUParsedItem)) { throw new Exception("等式中无自变量!\n"); } BUParsedItem xItem = (BUParsedItem)X; //自变量包含在X的左侧 if (ContainsVariant(xItem.left)) { if (ContainsVariant(xItem.right)) { throw new Exception("目前不能反转超过一个自变量的等式!\n"); } X = xItem.left; if (xItem.mid == BUOperators.s_binaryAdd) { Y = Contact(Y, BUOperators.s_binarySubtract, xItem.right); } else if (xItem.mid == BUOperators.s_binarySubtract) { Y = Contact(Y, BUOperators.s_binaryAdd, xItem.right); } else if (xItem.mid == BUOperators.s_binaryMultiply) { Y = Contact(Y, BUOperators.s_binaryDivide, xItem.right); } else if (xItem.mid == BUOperators.s_binaryDivide) { Y = Contact(Y, BUOperators.s_binaryMultiply, xItem.right); } else { throw new Exception(); } } else //自变量包含在X的右侧 { if (!ContainsVariant(xItem.right)) { throw new Exception("等式中无自变量!\n"); } X = xItem.right; if (xItem.mid == BUOperators.s_binaryAdd) { Y = Contact(Y, BUOperators.s_binarySubtract, xItem.left); } else if (xItem.mid == BUOperators.s_binarySubtract) { Y = Contact(xItem.left, BUOperators.s_binarySubtract, Y); } else if (xItem.mid == BUOperators.s_binaryMultiply) { Y = Contact(Y, BUOperators.s_binaryDivide, xItem.left); } else if (xItem.mid == BUOperators.s_binaryDivide) { Y = Contact(xItem.left, BUOperators.s_binaryDivide, Y); } else { throw new Exception(); } } return(_Inverse(Y, X)); }
static IBxUnitConvert Contact(IBxUnitConvert left, IFEBinary mid, IBxUnitConvert right) { return(new BUParsedItem(left, mid, right)); }