public bool TryEvaluate(string exp, out int value) { if (m_RPN.Parse(exp)) { object result = m_RPN.Evaluate(); if (result != null && int.TryParse(result.ToString(), out value)) { return(true); } } Debug.LogError("解析表达式 " + exp + " 失败"); value = 0; return(false); }
/// <summary> /// 计算具体的费用 /// </summary> /// <param name="costFormularKey"></param> /// <param name="salarySummary"></param> /// <returns></returns> private static decimal CalculateCostDetails(Guid costFormularKey, SalarySummaryEntity salarySummary) { decimal result = 0M; CostFormularEntity formularEntity = CostFormularBLL.Instance.Get(costFormularKey); if (formularEntity == null) { return(0M); } string formularValue = formularEntity.CostFormularValue; if (string.IsNullOrWhiteSpace(formularValue) == false) { List <string> costElementList = StringHelper.GetPlaceHolderList(formularValue, "{", "}"); foreach (string costElement in costElementList) { string placeHolderContent = string.Empty; switch (costElement) { case "NeedPaySalary": placeHolderContent = salarySummary.SalaryNeedPayBeforeCost.ToString(); break; case "RealPaySalary": placeHolderContent = salarySummary.SalaryNeedPayToLabor.ToString(); break; default: { BasicSettingEntity basicSettingEntity = CostList.Find(w => w.SettingKey == costElement); if (basicSettingEntity != null) { placeHolderContent = basicSettingEntity.SettingValue; } break; } } string placeHolder = "{" + costElement + "}"; formularValue = formularValue.Replace(placeHolder, placeHolderContent); } try { RPN rpn = new RPN(); if (rpn.Parse(formularValue)) { result = Convert.ToDecimal(rpn.Evaluate()); } } catch { result = 0; } } return(result); }
protected void Button1_Click(object sender, EventArgs e) { RPN rpn = new RPN(); string inputData = this.TextBox1.Text; if (rpn.Parse(inputData) == true) { this.Button1.Text = rpn.Evaluate().ToString(); } }
public override void ActionExcute() { String strText = actionCalculateData.strExpression; bool IsVar = false; String item = String.Empty; String tmpExp = String.Empty; if (actionCalculateData.strExpression == String.Empty) { return; } foreach (char ch in strText) { Regex regex = new Regex(@"[~]{1}"); if (IsVar && regex.IsMatch(item)) { Regex rg = new Regex(@"^[0-9]*[+,\-,\*,\/,&,|,=,>,<,(,),.,;]*$"); if (rg.IsMatch(ch.ToString())) { IsVar = false; String strValue = GetValue(item); tmpExp += strValue; item = String.Empty; } } if (ch != '#' && !IsVar) { if (ch != ';') { tmpExp += ch.ToString(); } } else { IsVar = true; if (ch != '#') { item += ch.ToString(); } } } RPN rpn = new RPN(); if (rpn.Parse(tmpExp)) { doubleValue = (double)rpn.Evaluate(); } }
public string SolveFormula(string expression) { if (expression == "") { return(""); } if (expression.Substring(0, 1) == "'") { return(expression.Replace("'", "")); } RPN rpn = new RPN(); if (rpn.Parse(expression)) { Stack <object> operandProcessed = new Stack <object>(); object[] tokens = rpn.Tokens.ToArray(); for (int i = tokens.Length - 1; i >= 0; i--) { object item = (object)tokens[i]; if (item is Operator @operator) { operandProcessed.Push(@operator); } else { Operand operand = (Operand)item; if (operand.Type == OperandType.STRING) { object tag = operand.Value; if (IsSystemVariable((string)tag)) { tag = CalculateSystemVariable((string)tag); } else { tag = GetTagsValue((string)tag); } operand.Value = tag; operand.Type = OperandType.NUMBER; } operandProcessed.Push((object)operand); } } rpn.Tokens = operandProcessed; } return(rpn.Evaluate().ToString()); }
public Boolean GetTagListFromFormula(string Formula, ref List <string> tags) { int i = 0; RPN func = new RPN(); if (func.Parse(Formula)) { foreach (object item in func.Tokens) { if (item is Operand operand) { tags[i] = operand.Value.ToString(); } } } else { return(false); } return(true); }