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;
                    }

                }

        

    暂时不支持运算符的优先级和括号。
  • 相关阅读:
    JavaScript继承详解 转
    Chinese Consumer and Websites
    【转载】C#防SQL注入过滤危险字符信息
    记一次在数据库中查询:“包含”或者“仅包含”某些商品的订单的方法
    IE 6 position: relative + li 问题
    【转】c# 位操作
    基于asp.net MVC的无刷新文件上传
    C++ 类继承内存布局
    美杜杉 主动防御最新版
    [转]COM 连接点
  • 原文地址:https://www.cnblogs.com/jetz/p/449299.html
Copyright © 2011-2022 走看看