提供了类似"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;
}
}
}