$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)

alias cleanEquation {
  var %e = $chr(40) $cleanseEquation($1-) $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 < 3,1,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 < 3,1,2)
      }
      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(baddxd,%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 -l 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)

Comments

Sign in to comment.
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!

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.