/// <summary> /// a+b -> a+b /// a*b -> ?? cas particulier /// a+b*c -> a+(b*c) /// a*b+c*d -> (a*b)+(c*d) /// </summary> /// <param name="execResult"></param> /// <param name="exprCalculation"></param> /// <param name="listExprExecBase"></param> /// <param name="listCalcAdd"></param> /// <returns></returns> private bool BuildCalculationExpressionWithPriority(ExecResult execResult, ExprCalculation exprCalculation, List <ExpressionExecBase> listExprExecBase, out ExprExecCalcAdd mainCalcAdd) { // init the main additions/sub mainCalcAdd = new ExprExecCalcAdd(); //bool currOperatorIsAdd = true; CalcOperatorTypeWF calcOperatorTypeWF = CalcOperatorTypeWF.Nothing; ExprExecCalcMul exprExecCalcMul = null; int pos = 0; // scan operators while (true) { // no more operator or operand if (pos >= exprCalculation.ListOperator.Count) { // the current calc operation is an add? if (calcOperatorTypeWF == CalcOperatorTypeWF.Plus) { // the current calc operation is an add/sub; add the last operand mainCalcAdd.AddOperandNumber(listExprExecBase[pos]); return(true); } else { // the current calc operation is an mul/div exprExecCalcMul.AddOperandNumber(listExprExecBase[pos]); return(true); } } // todo: sur?? il y a un operande de plus que les opérateurs if (pos >= listExprExecBase.Count) { break; } // analyze the current operator: its an add/sous or a mul/div CalcOperatorTypeWF calcOperatorTypeWFOut; ExprExecCalcMul exprExecCalcMulOut; if (!BuildCalcExprProcessOperator(execResult, exprCalculation, listExprExecBase, mainCalcAdd, pos, calcOperatorTypeWF, exprExecCalcMul, out calcOperatorTypeWFOut, out exprExecCalcMulOut)) { return(false); } // the workflow is updated by the function calcOperatorTypeWF = calcOperatorTypeWFOut; exprExecCalcMul = exprExecCalcMulOut; // goto next operator/operand pos++; } return(true); }
/// <summary> /// analyze the current operator: its an add/sous or a mul/div. /// /// </summary> /// <param name="execResult"></param> /// <param name="exprCalculation"></param> /// <returns></returns> private bool BuildCalcExprProcessOperator(ExecResult execResult, ExprCalculation exprCalculation, List <ExpressionExecBase> listExprExecBase, ExprExecCalcAdd mainCalcAdd, int pos, CalcOperatorTypeWF calcOperatorTypeWFIn, ExprExecCalcMul exprExecCalcMulIn, out CalcOperatorTypeWF calcOperatorTypeWFOut, out ExprExecCalcMul exprExecCalcMulOut) { exprExecCalcMulOut = null; if (exprCalculation.ListOperator[pos].Operator == OperatorCalculationCode.Plus || exprCalculation.ListOperator[pos].Operator == OperatorCalculationCode.Minus) { // manage the previous case, exp: 2*3+4 -> (2*3)+4 finish the 2*3 mul expr and continue the addition if (calcOperatorTypeWFIn == CalcOperatorTypeWF.Mul) { // add the last operand to the mul expr exprExecCalcMulIn.AddOperandNumber(listExprExecBase[pos]); exprExecCalcMulOut = exprExecCalcMulIn; // now its an addition expression calcOperatorTypeWFOut = CalcOperatorTypeWF.Plus; //mainCalcAdd.AddOperandNumber(listExprExecBase[pos]); mainCalcAdd.ListOperator.Add(exprCalculation.ListOperator[pos]); return(true); } // the operator is a + (addition) calcOperatorTypeWFOut = CalcOperatorTypeWF.Plus; // save the current operand value and the current operator mainCalcAdd.AddOperandNumber(listExprExecBase[pos]); mainCalcAdd.ListOperator.Add(exprCalculation.ListOperator[pos]); return(true); } // its the first operator of the expression or the previous operator was: +/plus if (calcOperatorTypeWFIn == CalcOperatorTypeWF.Nothing || calcOperatorTypeWFIn == CalcOperatorTypeWF.Plus) { // start a new calc expression exprExecCalcMulOut = new ExprExecCalcMul(); exprExecCalcMulOut.AddOperandNumber(listExprExecBase[pos]); exprExecCalcMulOut.ListOperator.Add(exprCalculation.ListOperator[pos]); calcOperatorTypeWFOut = CalcOperatorTypeWF.Mul; // save it in the main addition expression mainCalcAdd.ListExprExecCalc.Add(exprExecCalcMulOut); return(true); } // the previous operator is a mul exprExecCalcMulIn.AddOperandNumber(listExprExecBase[pos]); exprExecCalcMulIn.ListOperator.Add(exprCalculation.ListOperator[pos]); calcOperatorTypeWFOut = CalcOperatorTypeWF.Mul; exprExecCalcMulOut = exprExecCalcMulIn; return(true); }