Blockland Forums > Modification Help
strMath() not working propperly
Port:
--- Quote from: Ipquarx on January 16, 2012, 12:44:30 PM ---It's fairly short compared to port's and as it looks, quite a bit more memory/processing-power efficiant.
--- End quote ---
Well yeah, it wasn't really that well written and was done in a rush (~6 minutes).
Ipquarx:
If you're wondering the recursive part of mine is, it's the parenthesis's.
The first IF statement has that.
Destiny/Zack0Wack0:
Port's causes an infinite loop due to sub string errors, and Ipquarx's has a syntax error on line 3.
Ipquarx:
--- Quote from: Destiny/Zack0Wack0 on January 17, 2012, 12:28:10 AM ---Port's causes an infinite loop due to sub string errors, and Ipquarx's has a syntax error on line 3.
--- End quote ---
I just noticed that thank you (but it was on line 2, forgot a +), and also a parenthesis error.
This is the fixed version:
--- Code: ---function doEquation(%e)
{
%e = stripChars(%e, " ");
%old = %e;
%e = strReplace(%e, "+", " + ");
%e = strReplace(%e, "x", " x ");
%e = strReplace(%e, "/", " / ");
%e = strReplace(%e, "-", " - ");
%e = strReplace(%e, "^", " ^ ");
if(%e $= %old)
return %e;
while(strPos(%e, "(") != false && strPos(%e, ")") > 0)
{
%start = strPos(%e, "(");
%end = %start;
%level = 1;
while(%level != 0 && %end != strLen(%e))
{
%end++;
if(getsubStr(%e, %end, 1) $= "(")
%level++;
if(getsubStr(%e, %end, 1) $= ")")
%level--;
}
%e = getsubStr(%e, 0, %start) @ doEquation(getsubStr(%e, %start+1, %end - strLen(getsubStr(%e, 0, %start + 1)))) . getsubStr(%e, %end + 1, strLen(%e) - %end);
}
for(%a=0;%a<getWordCount(%e);%a++)
{
if(getWord(%e, %a) $= "^" && %a != 0)
{
%f = getWord(%e, %a-1);
%l = getWord(%e, %a+1);
%e = setWord(%e, %a, mPow(%f, %l));
%e = removeWord(removeWord(%e, %a+1), %a-1);
%a = %a - 2;
}
}
for(%a=0;%a<getWordCount(%e);%a++)
{
if(getWord(%e, %a) $= "x" || getWord(%e, %a) $= "/" && %a != 0)
{
%f = getWord(%e, %a - 1);
%l = getWord(%e, %a + 1);
%o = getWord(%e, %a);
switch%(%o)
{
case "x":
%e = setWord(%e, %a, %f * %l);
%e = removeWord(%e, %a + 1);
%e = removeWord(%e, %a - 1);
%a = %a - 2;
case "/":
%e = setWord(%e, %a, %f / %l);
%e = removeWord(%e, %a + 1);
%e = removeWord(%e, %a - 1);
%a = %a - 2;
}
}
}
for(%a=0;%a<getWordCount(%e);%a++)
{
if(getWord(%e, %a) $= "+" || getWord(%e, %a) $= "-" && %a != 0)
{
%f = getWord(%e, %a - 1);
%l = getWord(%e, %a + 1);
%o = getWord(%e, %a);
switch%(%o)
{
case "+":
%e = setWord(%e, %a, %f + %l);
%e = removeWord(%e, %a + 1);
%e = removeWord(%e, %a - 1);
%a = %a - 2;
case "-":
%e = setWord(%e, %a, %f - %l);
%e = removeWord(%e, %a + 1);
%e = removeWord(%e, %a - 1);
%a = %a - 2;
}
}
}
return %e;
}
--- End code ---
I also put a PHP version of this online at http://ipmath.hostzi.com/equation.php?in= (enter a equation without any spaces here)