zoukankan      html  css  js  c++  java
  • 用正则表达式完成简单公式的替换计算

                //用正则表达式完成替换计算
                
    //检验
                if(Common.GetMatchStr(this.sumitem,@"\w+([+\-*/]\w+)*").Length!=sumitem.Length)
                    
    return -1

                
    string op=Common.GetMatchStr(sumitem,@"^\w+");
                
    double ret=Cvt(op);

                
    while(op.Length !=sumitem.Length )    //
                {
                    
    string oper=sumitem.Substring(op.Length,1);
                    sumitem
    =sumitem.Substring(op.Length+1);
                    op
    =Common.GetMatchStr(sumitem,@"^\w+");
                    
    double opval=Cvt(op);

                    
    switch(oper)
                    
    {
                        
    case "+":
                            ret
    +=opval;
                            
    break;
                        
    case "-":
                            ret
    -=opval;
                            
    break;
                        
    case "*":
                            ret
    *=opval;
                            
    break;
                        
    case "/":
                            ret
    /=opval;
                            
    break;
                    }

                }

        

    暂时不支持运算符的优先级和括号。
  • 相关阅读:
    百度网盘下载速度慢的问题解决
    问题汇总
    centos 遇到Name or service not known
    centos7 下 python3 和python2 同时存在但是无法使用pip3 的解决方案
    pycharm2020(最简单的方法)配置远程连接服务器
    pycharm2020.1.2激活
    centos安装成功bart标注工具
    keras遇到bert实战一(bert实现分类)
    使用Array.slice(0) 实现数组浅拷贝
    try/catch/finally 语句
  • 原文地址:https://www.cnblogs.com/jetz/p/449299.html
Copyright © 2011-2022 走看看