From 2c6496f7084231279f40c288a960dc464a6fb331 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Wed, 5 Oct 2022 01:46:34 +0200 Subject: [PATCH] Some fixes --- src/Calculator.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Calculator.php b/src/Calculator.php index 3fc15cf..cc7dd1e 100644 --- a/src/Calculator.php +++ b/src/Calculator.php @@ -31,7 +31,7 @@ class Calculator public function run() { while (true) { - $in = readline("\001\e[92m\002~\001\e[0m\002 "); + $in = readline("\001\e[92m\002=\001\e[0m\002 "); if (trim($in) === '') continue; readline_add_history(trim($in)); @@ -49,10 +49,11 @@ class Calculator { if ($ret !== null) { $_p = $this->vars['_p']??null; + $_w = $this->vars['_w']??($color?20:0); if (ctype_digit($_p)) { - $out = sprintf("%.{$_p}f", $ret); + $out = sprintf("%{$_w}.{$_p}f", $ret); } else { - $out = $ret; + $out = sprintf("%{$_w}s", $ret); } echo $color ? " \e[92;1m{$out}\e[0m\n" : "{$out}\n"; } else { @@ -181,6 +182,14 @@ class Calculator $a = array_pop($out); $b = $sta[++$p]; $t = pow($a, $b); break; + case '!': + $a = array_pop($out); + $s = 0; + while ($a > 0) { + $s += $a--; + } + $t = $s; + break; } array_push($out, $t); } @@ -260,6 +269,7 @@ class Calculator '|='. // = '|[\+\-\*\/\^]'. // + - * / ^ '|\;'. // ; + '|!'. // ! ')/', $expr, $m)) { $toks[] = $m[0]; $expr = trim(substr($expr, strlen($m[0])));