Skip to content

Instantly share code, notes, and snippets.

@blackakula
Last active January 15, 2019 21:59
Show Gist options
  • Save blackakula/6c0b9bb4100f4226e0dec2fcc510455c to your computer and use it in GitHub Desktop.
Save blackakula/6c0b9bb4100f4226e0dec2fcc510455c to your computer and use it in GitHub Desktop.
<?php
$GLOBALS['plusMinus'] = [
'-' => function ($a, $b) { return $a - $b; },
'+' => function ($a, $b) { return $a + $b; },
];
$GLOBALS['multiplyDivide'] = [
'*' => function ($a, $b) { return $a * $b; },
'/' => function ($a, $b) { return $a / $b; },
];
function perform(&$operations, &$expr, $step) {
$parts = preg_split('@[' . implode('', array_keys($operations)) . ']@', $expr, -1, PREG_SPLIT_OFFSET_CAPTURE);
return count($parts) > 1
? array_reduce(
array_slice($parts, 1),
function ($result, $part) use (&$operations, &$expr, &$step) {
return $operations[$expr[$part[1] - 1]]($result, _calc($step, $part[0]));
},
$step <=2 && trim($parts[0][0]) === '' && $expr[$parts[1][1] - 1] === '-' ? 0 : _calc($step, $parts[0][0])
) : $expr;
}
function _calc($step, $expr) {
if ($step <= 1) {
do {
$parts = preg_split('/\(([^()]+)\)/', $expr, -1, PREG_SPLIT_DELIM_CAPTURE);
$expr = count($parts) > 1
? array_reduce(
array_keys($parts),
function ($result, $index) use (&$parts) {
return $result . ($index % 2 === 0 ? $parts[$index] : _calc(2, $parts[$index]));
},
''
) : $expr;
} while (count($parts) > 1);
}
if ($step <= 2) {
$expr = perform($GLOBALS['plusMinus'], $expr, 3);
}
if ($step <= 3) {
$expr = perform($GLOBALS['multiplyDivide'], $expr, 4);
}
return floatval(trim((string)$expr));
}
function calc($expr) {
return _calc(1, $expr);
}
echo calc('-100-(4+4) * (2*(3 + 2))+2*10 / 5') . "\n";
echo calc('((((69-66-53*96-69)*(74+7+78-5)+(61-86*79*64-50)*(73+87-27+11-37+97-61*4))*((92*37*78-48)-(51+3*96)-(62*2-49-40+43)-(58*51-81))+((78*38)+(26*92)-(93*36*42*43)*(61*97-3+23)-(41-91-1*62)*(40+8*62-92-30)+(44*74))-(78+84-97*66+36+33+23)+((54+60-52*96*25-68-61)*(55+85*5+17-15)*(92*96*78+48+86)+(59-30-54+24*16-10))-(99-49+80+41*84+66+33+46))+(54+92*13-6*90-20)-(28*33*40)+((96-60+80+88-81)*((1-100*96+25-91)*(89-95)-(1*59)+(14+98*32*43*70-36-68)+(10-89))+((57-56-91+29)*(47+41)*(91-3*59*60-36+41)-(39*13-51)-(5-4*61-29))-((33-39-37+95)+(8+76-38+83+39-69)+(5-5-34*67)*(5-68+24+82)+(80-52))-((31-88*23+48)-(63*97)-(2*65*100*32+52)*(33*93-91*70+30)+(2*8))*((42+35*3+9-82)*(91*27+20*54*64*29+12)))*(54-17)-((78*75*13-3*68)*(83-8*69*100+46*35)-(65+16-2+95*47-47-23)+(55*91-69+72)-(10+39-53-83)-(91*9-36+95)))-(((23-21*54+42*49)-((40+8-71*83*72)-(97-10)+(30+52-1)-(52-20*67-49+72)-(2-94*58))+((13+42-67-7*41)*(25+3+5-58+63)*(20*27-80+79)-(93+43+64-78)-(4*14-10+6-90-8))*(75+37-96)*((100+56)-(15+70-45-2*71+37)-(19+34*63)+(87*68+79)))*((82-53*55-35-1)-((24-1*66)+(10*22+96)*(70*91+10*44+93)*(44+38-33*88)+(83-32-69)-(34+71+36)*(71*35-85))*(((88+74-84+81*47)-(78*81+90*11-87-5)*(2+63-6-36-32)*(57+76+72+24)+(93-20+45))+(21*37-73*79)-((7-94*53+19*63+18+94)-(4+93-21)*(45+9+47-36)*(16+80*19)+(37+90+69-32+61)+(53*100*43-90+39))-((19+9+56*4-66)-(8-55*56+97)*(88*9+51-66+80*23)-(79-1))*((86*39*6+15*56)-(14-67-91-17*46*35*91*70+7))))-((83-24*28)-((51*67-38+46)+(78-13+22*11-97)+(59+33*44)-(52+68+98)*(3*93-67-62+18-7+6))+((73-22*79-9-72)-(29*25-55+76-1)-(38*95+71+74+94)*(74-75-93)*(75*88-100-35)+(88-91)))+((93*95*85)-((57-12-91+98+85)-((30+80*23)-(55*91*21-65-64+52*97))*(42*63+5+82+46*2))-((66-52)+(62-88*81*32)*(62+58+97-27-76*2)*(70+30*26+73*70*18)-(96-38-45))+((61+100-48)-(48+70)-(42+53)-(49*62-86*10-97)+(60*83*96-13))+((30+59-75+34+61)-(86-32-83*81*46)*(33*1+100*77*78)+(20+16+94*81)*(31-7-66+30+32)+(92-60*84+95)-(43+77*6*66))*((58*59*76*15)*((59-19+30+40)+(1-69-36-15*72-4))+(3*40+85-14)-((69*67+73+97-29-43)-(98*91)-(11*100-38*74-43+42-32)-(56-8-63-54-61)*(29*46*32-16)*(72*82*7-16*99)*(74+25+5)-(89*49*31-33))-((49-94+97+85*51)*(14-20-32+98))+((38*69*20*39)+(11+41*25-42)-(43+23-19-76)*(42+68-9-77-44)-(12+12-53-25*24-84)))))') . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment