用正则表达式完成简单公式的替换计算

            //用正则表达式完成替换计算
            
//检验
            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;
                }

            }

    

暂时不支持运算符的优先级和括号。
原文地址:https://www.cnblogs.com/jetz/p/449299.html