Example #1
0
File: Mc.cs Project: burstas/rmps
 private bool lbtInt(String tmpOpr, Op o, int i)
 {
     var n = LBT_EXP.Length;
     for (int m = 0; m < n; m += 1)
     {
         if (LBT_EXP[m] == o.S)
         {
             // 操作符级别判断
             if (LBT_INT[m] + 7 == RBT_INT[i])
             {
                 oprStack.Pop();
                 if (stepList != null)
                 {
                     var t = (String)numStack.Peek();
                     String p = LBT_EXP[m] + t + RBT_EXP[m];
                     stepList.Add(new Items { K = mathExps, V = p, D = t });
                     mathExps = mathExps.Replace(p, t);
                 }
                 return true;
             }
             throw new Exception(String.Format("您输入的表达式不正确:{0}与{1}不匹配!", LBT_EXP[m], tmpOpr));
         }
     }
     return false;
 }
Example #2
0
File: Mc.cs Project: burstas/rmps
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public String calculate()
        {
            if (string.IsNullOrEmpty(mathExps))
            {
                throw new Exception("表达式为空!");
            }

            mathExps = mathExps.Replace(" ", "").Trim();
            var regex = new Regex("=[=]*=");
            mathExps = regex.Replace(mathExps, "=");
            if (mathExps == "" || mathExps == "=")
            {
                throw new Exception("请输入一个完整的运算表达式!");
            }

            if (mathExps.EndsWith("="))
            {
                mathExps = mathExps.Substring(0, mathExps.Length - 1);
            }

            int edx = mathExps.LastIndexOf('=') + 1;
            mathExps = mathExps.Substring(edx, mathExps.Length - edx);

            // 操作数堆栈
            numStack = new Stack();
            // numStack.Push("0");
            // 操作符堆栈
            oprStack = new Stack();
            // oprStack.Push(new Op());

            // 操作数缓冲区
            var numBuf = new StringBuilder();
            // 操作符缓冲区
            var oprBuf = new StringBuilder();
            // 表达式字符串
            char[] expBuf = ('(' + mathExps + ')').ToCharArray();
            // 临时字符串
            String tmpOpr;
            // 上一个字符(串)是否为操作符,true表示是;false表示否
            lastIsOpr = false;

            // 循环处理每一个表达式字符
            foreach (char c in expBuf)
            {
                // 负号
                if (lastIsOpr && (c == '-' || c == '+'))
                {
                    if (oprBuf.Length != 0)
                    {
                        throw new Exception(String.Format("计算错误:未知运算符 “{0}”,请确认您输入计算式的正确性!", oprBuf));
                    }
                    lastIsOpr = false;
                    numBuf.Append(c);
                    continue;
                }

                // 操作数
                if ((c >= '0' && c <= '9') || c == '.')
                {
                    if (oprBuf.Length != 0)
                    {
                        throw new Exception(String.Format("计算错误:未知运算符 “{0}”,请确认您输入计算式的正确性!", oprBuf));
                    }
                    lastIsOpr = false;
                    numBuf.Append(c);
                    continue;
                }

                // e
                if (c == 'ê')
                {
                    if (oprBuf.Length != 0)
                    {
                        throw new Exception(String.Format("计算错误:未知运算符 “{0}”,请确认您输入计算式的正确性!", oprBuf));
                    }
                    lastIsOpr = false;
                    String ep = Decimal.Round(new Decimal(Math.E), decimals).ToString();
                    numBuf.Append(ep);
                    mathExps = mathExps.Replace("ê", ep);
                    continue;
                }

                // π
                if (c == 'π')
                {
                    if (oprBuf.Length != 0)
                    {
                        throw new Exception(String.Format("计算错误:未知运算符 “{0}”,请确认您输入计算式的正确性!", oprBuf));
                    }
                    lastIsOpr = false;
                    String ep = Decimal.Round(new Decimal(Math.PI), decimals).ToString();
                    numBuf.Append(ep);
                    mathExps = mathExps.Replace("π", ep);
                    continue;
                }

                // 操作符
                tmpOpr = oprBuf.Append(c).ToString();

                // 左括号
                if (lbtExp(tmpOpr, oprBuf, numBuf))
                {
                    continue;
                }

                // 右括号
                if (rbtExp(tmpOpr, oprBuf, numBuf))
                {
                    continue;
                }

                // 运算符运算
                var j = OPR_EXP.Length;
                for (int i = 0; i < j; i += 1)
                {
                    if (OPR_EXP[i] == tmpOpr.ToLower())
                    {
                        lastIsOpr = ConstUI.OPR_FAC_EXP != tmpOpr;
                        // 清除当前操作符
                        oprBuf.Remove(0, oprBuf.Length);

                        // 左操作数不为空的情况下,左操作数入栈
                        if (numBuf.Length > 0)
                        {
                            numStack.Push(numBuf.ToString());
                            numBuf.Remove(0, numBuf.Length);
                        }

                        Items kvItem;
                        // 当前运算符
                        var newOpr = new Op(tmpOpr, OPR_INT[i]);

                        // 循环处理每一个操作符
                        while (true)
                        {
                            // 操作符级别判断
                            if (newOpr.L > ((Op)oprStack.Peek()).L)
                            {
                                oprStack.Push(newOpr);
                                break;
                            }

                            // 运算结果
                            kvItem = calculate(mathExps, decimals);
                            // 统计运算步骤
                            if (stepList != null)
                            {
                                stepList.Add(kvItem);
                                mathExps = mathExps.Replace(kvItem.V, kvItem.D);
                            }
                        }
                        break;
                    }
                }
            }

            if (oprStack.Count > 0)
            {
                throw new Exception("表达式存在错误,系统无法计算!");
            }

            return numStack.Count != 0 ? Decimal.Round(Decimal.Parse((String)numStack.Pop()), decimals).ToString() : "";
        }