/// <summary> /// Do calculation of all multiplication sub expression. /// replace it by the result value. /// </summary> /// <param name="execResult"></param> /// <param name="exprCalculation"></param> /// <param name="mainCalcAdd"></param> /// <returns></returns> private bool DoCalculationSubExprMul(ExecResult execResult, ExprCalculation exprCalculation, ExprExecCalcAdd mainCalcAdd) { int pos = -1; List <ExprExecCalcBase> listListExprExecCalcResult = new List <ExprExecCalcBase>(); foreach (ExprExecCalcBase execCalcBase in mainCalcAdd.ListExprExecCalc) { pos++; // the item should be a number (int or double) or a mul expr ExprExecCalcMul execCalcMul = execCalcBase as ExprExecCalcMul; // not a calc multiplication, next if (execCalcMul == null) { listListExprExecCalcResult.Add(execCalcBase); continue; } // do the calculation of the mul expr, and replace it by the result value ExprExecCalcValue execCalcValueResult; DoCalculationExprMul(execResult, exprCalculation, execCalcMul, out execCalcValueResult); // save the new value (in place of the mul expr) listListExprExecCalcResult.Add(execCalcValueResult); } // replace the result list mainCalcAdd.ListExprExecCalc = listListExprExecCalcResult; return(true); }
/// <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); }
/// <summary> /// Do the calculation of the multiplication/division expr comme l'add mais le rs va remplacer l'objet Mul dans le main add) /// /// </summary> /// <param name="execResult"></param> /// <param name="exprCalculation"></param> /// <param name="mainCalcAdd"></param> /// <param name="exprExecCalcResult"></param> /// <returns></returns> private bool DoCalculationExprMul(ExecResult execResult, ExprCalculation exprCalculation, ExprExecCalcMul execCalcMul, out ExprExecCalcValue execCalcValueResult) { // the temporary result, to start the addition calculation ExprExecValueInt resInt = new ExprExecValueInt(); // the integer neutral value, the first calculation is 1*operand[0] resInt.Value = 1; ExpressionExecBase exprExecCalcResult = resInt; ExpressionExecBase exprExecTmpResult = null; int pos = 0; // scan addition expressions, two by two operands foreach (ExprExecCalcValue execCalcValue in execCalcMul.ListExprExecCalcValue) { // the item should be a number: an int or a double // get the operator ExprOperatorCalculation operatorCalc; // the first calc is special: res := 0 + firstOperand if (pos == 0) { // put the default neutral operator: + operatorCalc = new ExprOperatorCalculation(); operatorCalc.Operator = OperatorCalculationCode.Multiplication; //operatorCalc.Token = mainCalcAdd.ListOperator[0].Token; } else { // there one operator less than operand operatorCalc = execCalcMul.ListOperator[pos - 1]; } // Do the calculation: tmpRes := res +/- currentValue DoCalculationTwoOperands(execResult, exprCalculation, exprExecCalcResult, execCalcValue.ExprExecValue, operatorCalc, out exprExecTmpResult); // the current temporary result exprExecCalcResult = exprExecTmpResult; pos++; } // then replace the calc mul expr by the result execCalcValueResult = new ExprExecCalcValue(); execCalcValueResult.ExprExecValue = exprExecTmpResult; //mainCalcAdd.ListExprExecCalc[posMulInMainAdd] = execCalcValueResult; return(true); }