# \$cleanEquation()

### By Degausser on Mar 03, 2017

\$cleanEquation(2+4/3^3) - Output: (2 + (4 / (3 ^ 3)))

Predefined bracket rules are kept, calculated outputs remain intact
\$cleanEquation(2+16/4%3+2^4) - Output: ((2 + (16 / (4 % 3))) + (2 ^ 4))
\$cleanEquation(2+16/4%(3+2)^4) - Output: (2 + (16 / (4 % ((3 + 2) ^ 4))))

Excessive brackets are cleared. Algebra can be used.
\$cleanEquation((n+(((2-y)))^2+3)) - Output: ((n + ((2 - y) ^ 2)) + 3)

Supports negative values, simplifies clusters
\$cleanequation(4+--16/4-+--+++-++-3) - Output: ((4 - (-16 / 4)) - -3)

Update June 2017: Fixed some ordering to match mIRCs presedence

``````alias cleanEquation {
var %f = \$1-
if (\$count(%f,\$chr(40)) != \$count(%f,\$chr(41))) %f = \$iif(\$v1 > \$v2,%f) \$str(\$iif(\$v1 < \$v2,\$chr(40),\$chr(41)) \$+ \$chr(32),\$abs(\$calc(\$v2 - \$v1))) \$iif(\$v1 < \$v2,%f)
var %e = \$chr(40) \$cleanseEquation(%f) \$chr(41), %x = 1, %p, %count = 0, %abc = 161, %signs = \$+(^,*/,\$chr(37),+-), %q, %abcd, %y, %m, %t
while (\$numtok(%e,32) > 1) {
%p = \$gettok(%e,%x,32)
if (%p == \$chr(40)) {
inc %count
var %count [ \$+ [ \$+(_,%count) ] ] %x
}
elseif (%p == \$chr(41)) {
var %abc [ \$+ [ \$+(_,%abc) ] ] \$gettok(%e,\$+(\$(\$+(\$chr(37),count,_,%count),2),-,%x),32)
%e = \$iif(\$+(\$(\$+(\$chr(37),count,_,%count),2)) > 1, \$gettok(%e,\$+(1,-,\$calc(\$(\$+(\$chr(37),count,_,%count),2) - 1)),32),\$null) \$chr(%abc) \$gettok(%e,\$+(\$calc(%x + 1),-),32)
%m = \$remove(%e,\$chr(32))
if (\$calc(\$count(%m,\$chr(40))) > 1) && (\$calc((\$len(%m) - \$count(%m,\$chr(40))) * 2) <= 1) {
%t = \$calc(\$pos(%e,\$chr(40),\$pos(%e,\$chr(40),0)) + 1)
%e = \$chr(40) \$mid(%e,%t,\$calc((\$pos(%e,\$chr(41),1) - 1) - %t)) \$chr(41)
}
inc %abc
%x = 0
%count = 0
}
inc %x
}
while (%abc > 161) {
dec %abc
%p = \$replace(\$(\$+(\$chr(37),abc,_,%abc),2),\$str(\$chr(40),2),\$chr(40),\$str(\$chr(41),2),\$chr(41))
if (\$numtok(%p,32) == 3) && (\$gettok(%p,2,32) !isnum) %p = \$gettok(%p,2,32)
%abcd = 261
if (\$numtok(%p,32) > 5) {
%y = 1
while (%y <= 5) {
%x = 1
while (%x <= \$numtok(%p,32)) {
%q = \$gettok(%p,%x,32)
if (%q isin \$mid(%signs,%y,\$iif(%y == 1,1,\$iif(%y == 2,3,2)))) {
var %abcd [ \$+ [ \$+(_,%abcd) ] ] \$+(\$chr(40),\$chr(32),\$gettok(%p,\$+(\$calc(%x - 1),-,\$calc(%x + 1)),32),\$chr(32),\$chr(41))
%p = \$gettok(%p,\$+(1,-,\$calc(%x - 2)),32) \$chr(%abcd) \$gettok(%p,\$+(\$calc(%x + 2),-),32)
inc %abcd
}
else inc %x
}
inc %y \$iif(%y == 1,1,3)
}
while (%abcd > 261) {
dec %abcd
if (\$numtok(%p,32) == 3) && (\$gettok(%p,2,32) !isnum) %p = \$(\$+(\$chr(37),abcd,_,%abcd),2)
else %p = \$replacecs(%p,\$chr(%abcd),\$(\$+(\$chr(37),abcd,_,%abcd),2))
}
}
%e = \$replacecs(%e,\$chr(%abc),%p)
}
%e = \$iif(\$regsubex(regextest,%e,\(([^()]|(?R))*\),\$null),\$+(\$chr(40),%e,\$chr(41)),%e)
return \$replace(%e,\$+(\$chr(40),\$chr(32)),\$chr(40),\$+(\$chr(32),\$chr(41)),\$chr(41))
}
alias cleanseEquation {
var %t, %s = \$replace(\$remove(\$chr(40) \$1- \$chr(41),\$chr(32)),\$chr(41),\$scc(\$chr(41)),\$chr(40),\$scc(\$chr(40)))
while (%s != \$replace(%s,**,*,^^,^,\$str(\$chr(37),2),\$chr(37),//,/,++++,++,----,--,-+-+,-+,+-+-,+-,++-+,-+,--+-,+-,-++-,+-,+--+,-+,+---,+-,-+++,-+,++--,--,--++,++,+++-,+-,---+,-+)) %s = \$v2
%s = \$replace(%s,+-+,+,+--,+-,+++,+,++-,+-,-++,+,-+-,+-,--+,+,---,+-,-+,-,^--,^,*--,*,\$+(\$chr(37),--),\$chr(37),/--,/,^+-,^-,*+-,*-,\$+(\$chr(37),+-),\$+(\$chr(37),-),/+-,/-)
%s = \$replace(%s,\$scb(--),\$scb(\$null),\$scb(+-),\$scb(-),\$scb(++),\$scb(\$null),\$scb(-+),\$scb(-),++,+,-+,-,*-,* \$chr(512),+-,+ \$chr(512),--,- \$chr(512),/-,/ \$chr(512),^-,^ \$chr(512),\$+(\$chr(37),-),\$chr(37) \$chr(512),\$+(\$chr(40),\$chr(160),-),\$chr(512),\$+(\$chr(40),\$chr(160),+),\$null,-,\$scc(-),*+,*,^+,^,\$+(\$chr(37),+),\$chr(37),/+,/)
return \$replace(%s,+,\$scc(+),^,\$scc(^),%,\$scc(%),*,\$scc(*),/,\$scc(/),\$chr(512),-,\$chr(513),+,\$chr(160),\$chr(32))
}
alias -l scc return \$+(\$chr(160),\$1,\$chr(160))
alias -l scb return \$+(\$chr(40),\$chr(160),\$1)
``````

Arigateaux   -  Jun 05, 2017

So, a problem I see is this:
\$cleanEquation(2+16/4%3+2^4) - Output: ((2 + (16 / (4 % 3))) + (2 ^ 4))
This gives two completely different answers. This should probably be fixed.

Note: order of operations dictates that % is on the same level as * and /

raccoon   -  Mar 03, 2017

How does it handle and distinguish unary negative versus binary subtraction? Eg: `-3^2` and `-1/2x*y` and `3--2`

Degausser  -  Mar 03, 2017

Negative values are something I completely overlooked, thanks! Will try updating to handle those possibilities. At the moment as you've probably noticed, it doesn't handle them at all =x

• fixed
raccoon  -  Mar 03, 2017

Does the solution to -3^2 result in -9 or +9? The world wants to know ^_^

Degausser  -  Mar 03, 2017

It's pretty bad when someone else puts more effort into bug crunching than the author. A bit make shift, but working as expected now.. I think. Thank you. :)

raccoon  -  Mar 04, 2017

Well, to be honest, I haven't tested it. I just know the issues. :)

Even the world's most popular spreadsheet software doesn't agree on these behaviors -- if you save and load your stock market profile between Open Office, Microsoft Excel, Quattro Pro and Lotus 123, any formulas you put in that use unary negative and powers are likely to disagree from program to program. It's any wonder the stock market hasn't crashed in 3 decades!