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

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

            }

    

暂时不支持运算符的优先级和括号。
posted @ 2006-07-12 20:15  jetz  阅读(1402)  评论(0编辑  收藏  举报