计算任意一个数学运算表达式字符串的值

题:写一个方法计算任意一个数学运算表达式字符串,不区分优先级

例:
"9+8*2"=34
"2-8*9+3"=-51
"2*2*2*2*2"=32
"0"=0

//===========================代码 Begin==============================

    /// <summary>
    /// 传入一个数学运算的表达示字符串不区分优级计算返回其结果
    /// </summary>
    protected string ComputeMathStr(string exStr)
    {
        //去除空字符
        exStr = Regex.Replace(exStr.Trim(), " *", "");

        //匹配字符串是否是以两个数的操作开头
        Match ma = Regex.Match(exStr, @"^(\-?\d+)([\+\-\*\/])(\d+)");

        if (ma.Success)//匹配成功,计算
        {
            string firstStr = ma.Groups[0].Value.ToString();//获取匹配的字符串:如2+3

            int num1 = int.Parse(ma.Groups[1].Value.ToString());//第一个数值
            string opr = ma.Groups[2].Value.ToString();//操作符
            int num2 = int.Parse(ma.Groups[3].Value.ToString());//第二个数值

            string newStr = exStr.Substring(firstStr.Length);//获取后面剩余的字符串
            return ComputeMathStr(GetResultByOperator(opr, num1, num2) + newStr);//递归运算其结果,直到不能匹配为止
        }
        else//匹配失败,直接返回结果
        {
            return exStr;
        }
    }

    /// <summary>
    /// 根据操作符计算两个数的值并返回结果
    /// </summary>
    protected int GetResultByOperator(string opr,int num1,int num2)
    {  
        switch (opr)
        {
            case "+":
                return num1 + num2;
            case "-":
                return num1 - num2;
            case "*":
                return num1 * num2;
            case "/":
                if (num2 == 0)
                {
                    throw new Exception("尝试除以零!");
                }
                return num1 / num2;
            default:
                return 0;
        }
   
    }
//===========================代码 End==================================

转载于:https://www.cnblogs.com/lt-style/p/3455883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
因各个项目中需要使用根据字符串计算,这里写出一个算法,专门计算字符串。配有大量常用公式。只有一个人方法,直接调用即可。 类名:CustomMath 函数名:Calculations(string value) 说明:求解算式表达式字符串 表达式中包含的符号或函数: truncate, ceiling,floor,round,log10, sign,sinh,sqrt, asin,atan,cosh, tanh, sin,cos,tan ,abs,acos, exp,log,max,min,pow,mod,+,-,*,/,',',(,) 函数说明:(不区分大小写) truncate(num) 计算指定数的整数部分 truncate(1.23)=1 ceiling (num) 返回大于或等于指定的双精度浮点数的最小整数 ceiling(1.23)=2 floor(num) 返回小于或等于指定双精度浮点数的最大整数 floor(1.23)=1 round(num) 将双精度浮点舍入为最接近的整数 round(1.23)=1 round(num,num1) 将小数按指定的小数位数舍入 round(1.23,1)=1.2 log10(num) 返回指定数字以 10 为底的对数 log10(10)=1 sign(num) 返回表示数字符号的 sign(1.23)=1 sinh(num) 返回指定角度的双曲正弦 sinh(1.23)=1.5644 sqrt(num) 返回指定数字的平方根 sqrt(9)=3 sqrt(num,num1) 返回指定数字的num1根 sqrt(27,3)=3 asin(num) 返回正弦为指定数字的角度 asin(0.5)=PI/6 atan(num) 返回正切为指定数字的角度 atan(1)=45 cosh(num) 返回指定角度的双曲余弦 cosh(1.23)=1.8567 tanh(num) 返回指定角度的双曲正切 tanh(1.23)=0.8425 sin(num) 返回指定角度的正弦 sin(PI/6)=0.5 cos(num) 返回指定角度的余弦 sin(PI/3)=0.5 tan(num) 返回指定角度的余切 sin(PI/4)=1 abs(num) 返回数字的绝对 abs(-12)=12 acos(num) 返回余弦为指定数字的角度 acos(0.5)=PI/3 exp(num) 返回 e 的指定次幂 exp(1)=2.718 log(num) 返回指定数字的自然对数(底为 e) log(e)=1 log(num,num1) 返回指定数字在使用指定底时的对数 log(e,e)=1 max(num,um1) 返回最大 max(1,2)=2 min(num,num1) 返回最小 min(1,2)=1 pow(num,num1) 返回指定数字的指定次幂 pow(2,2)=4 mod(num,num1) 返回余数 mod(3,2)=1 常量: PI :3.14159265358979323846 E :2.7182818284590452354 YEAR :当前年份 MONTH :当前月份 DAY : 当前日 HOUR :当前时 MINUTE :当前分 SECOND :当前秒 RANDOM 一个随机数(0-1 之间) 实例 系统计算:1+2*3/4-0.5=2 函数计算:1+2*3/4-0.5=2 调用方式:CustomMath.Calculations("1+2*3/4-0.5") 系统计算:(1+2)*3/4-0.5=1.75 函数计算:(1+2)*3/4-0.5=1.75 调用方式:CustomMath.Calculations("(1+2)*3/4-0.5") 系统计算:(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6=1.20185042515466 公式计算:(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6=1.20185042515466 调用方式:CustomMath.Calculations("(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6") 系统计算:sin(pow(3,2)/4)+3.5-9*sqrt(81)=-76.7219268031121 函数计算:sin(pow(3,2)/4)+3.5-9*sqrt(81)=-76.7219268031121 调用方式:CustomMath.Calculations("sin(pow(3,2)/4)+3.5-9*sqrt(81)")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值