提供了一个类似的字符串"y = 1, x = 2, w = x + y +1"
如何使 .net 或任何语言将其转换为数学表达式并进行计算?
现在我更关心它背后的逻辑而不是框架。
我的逻辑如下,但它仍然不完整是否有更快或更好的东西,如果可以的话也有助于完成逻辑???
伪代码:
symbols[] = { "=","+" ... "-"}
string a = "y = 1, x = 2, w = x + y +1";
a = a.replace(" ","");
highlevelsplit = a.split("=");
for expr e in highlevelsplit
{
if(e.containts(=))
{
highlevelsplit[highlevelsplit.indexof(e)] = calc(highlevelsplit[highlevelsplit.indexof(e)+1]
}
}
function calc(string expr)
{
array[] = expr.toCharArray();
variables[][];
var c;
var currentVariable;
for int i=0; i<array.length-1; i++
{
c = array[i];
if( symbols.containts(c) )
{
if( c.equals("+"))
{
}
....
}
else if ( c.notNumber && variables.containtsKey(c))
{
}
else if ( Float.TryParse(c))
{
}
else //probably a letter?
{
variables.push(c,0);
currentVariable = c;
}
}
}