利用字符串进行数值计算的问题
想写一个简单的计算器程序,将整个表达式输入之后再计算结果,比如1+2*5/8,然后按=时出现结果,现在有一问题,我不知道如何将这个字符串转换为表达式进行数值计算。谢谢,结题给分。
------解决方案--------------------//先在项目中添加COM引用Microsoft Script Control 1.0 
 using MSScriptControl;   
 ScriptControl vScriptControl = new ScriptControl(); 
 vScriptControl.Language =  "JavaScript "; 
 Text = vScriptControl.Eval( "1+2*5/8 ").ToString(); 
------解决方案--------------------private string  calculateparenthesesexpression(string expression) 
   { 
    arraylist operatorlist = new arraylist(); 
    string operator1; 
    string expressionstring =  " "; 
    string operand3; 
    expression = expression.replace( "  ", " "); 
    while(expression.length >  0) 
    { 
     operand3 =  " "; 
     //取数字处理 
     if(char.isnumber(expression[0])) 
     { 
      while(char.isnumber(expression[0])) 
      { 
       operand3 += expression[0].tostring() ; 
       expression = expression.substring(1); 
       if(expression ==  " ")break;     
      } 
      expressionstring += operand3 +  "| "; 
     }   
     //取“c”处理 
     if(expression.length > 0 && expression[0].tostring() ==  "( ") 
     { 
      operatorlist.add( "( ");   
      expression = expression.substring(1);  
     }   
     //取“)”处理 
     operand3 =  " "; 
     if(expression.length > 0 && expression[0].tostring() ==  ") ") 
     { 
      do 
      {         
       if(operatorlist[operatorlist.count -1].tostring() !=  "( ") 
       { 
        operand3 += operatorlist[operatorlist.count -1].tostring() +  "| " ; 
        operatorlist.removeat(operatorlist.count - 1) ; 
       } 
       else 
       { 
        operatorlist.removeat(operatorlist.count - 1) ; 
        break; 
       }         
      }while(true); 
      expressionstring += operand3; 
      expression = expression.substring(1);  
     }   
     //取运算符号处理 
     operand3 =  " "; 
     if(expression.length > 0 &&  (expression[0].tostring() ==  "* " || expression[0].tostring() ==  "/ " || expression[0].tostring() ==  "+ " || expression[0].tostring() ==  "- ")) 
     { 
      operator1 = expression[0].tostring(); 
      if(operatorlist.count> 0) 
      {          
       if(operatorlist[operatorlist.count -1].tostring() ==  "( " || verifyoperatorpriority(operator1,operatorlist[operatorlist.count - 1].tostring()))          
        { 
         operatorlist.add(operator1);  
        } 
        else   
        { 
         operand3 += operatorlist[operatorlist.count - 1].tostring() +  "| ";  
         operatorlist.removeat(operatorlist.count - 1); 
         operatorlist.add(operator1);  
         expressionstring += operand3 ;           
        }         
      } 
      else 
      { 
       operatorlist.add(operator1);  
      } 
      expression = expression.substring(1);  
     } 
    }   
    operand3 =  " "; 
    while(operatorlist.count != 0) 
    { 
     operand3 += operatorlist[operatorlist.count -1].tostring () +  "| "; 
     operatorlist.removeat(operatorlist.count -1);  
    }    
    expressionstring += operand3.substring(0, operand3.length -1);  ;        
    return calculateparenthesesexpressionex(expressionstring);   
   } <