读书人

利用字符串进行数值计算的有关问题

发布时间: 2011-12-25 23:21:20 作者: rapoo

利用字符串进行数值计算的问题
想写一个简单的计算器程序,将整个表达式输入之后再计算结果,比如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);

}


// 第二步:把转换成后序表达的式子计算


//23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+ "
private string calculateparenthesesexpressionex(string expression)
{
//定义两个栈
arraylist operandlist =new arraylist();
float operand1;
float operand2;
string[] operand3;

expression = expression.replace( " ", " ");
operand3 = expression.split(convert.tochar( "| "));

for(int i = 0;i < operand3.length;i++)
{
if(char.isnumber(operand3[i],0))
{
operandlist.add( operand3[i].tostring());
}
else
{
//两个操作数退栈和一个操作符退栈计算
operand2 =(float)convert.todouble(operandlist[operandlist.count-1]);
operandlist.removeat(operandlist.count-1);
operand1 =(float)convert.todouble(operandlist[operandlist.count-1]);
operandlist.removeat(operandlist.count-1);
operandlist.add(calculate(operand1,operand2,operand3[i]).tostring()) ;
}

}


return operandlist[0].tostring();
}



//判断两个运算符优先级别
private bool verifyoperatorpriority(string operator1,string operator2)
{

if(operator1== "* " && operator2 == "+ ")
return true;
else if(operator1== "* " && operator2 == "- ")
return true;
else if(operator1== "/ " && operator2 == "+ ")
return true;
else if(operator1== "/ " && operator2 == "- ")
return true;
else
return false;
}

//计算
private float calculate(float operand1, float operand2,string operator2)
{
switch(operator2)
{
case "* ":
operand1 *= operand2;
break;
case "/ ":
operand1 /= operand2;
break;
case "+ ":
operand1 += operand2;
break;
case "- ":
operand1 -= operand2;
break;
default:
break;
}
return operand1;
}

读书人网 >C#

热点推荐