Some fixes

This commit is contained in:
Chris 2022-10-05 01:46:34 +02:00
parent a6e8d252b1
commit 2c6496f708
1 changed files with 13 additions and 3 deletions

View File

@ -31,7 +31,7 @@ class Calculator
public function run() public function run()
{ {
while (true) { 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; if (trim($in) === '') continue;
readline_add_history(trim($in)); readline_add_history(trim($in));
@ -49,10 +49,11 @@ class Calculator
{ {
if ($ret !== null) { if ($ret !== null) {
$_p = $this->vars['_p']??null; $_p = $this->vars['_p']??null;
$_w = $this->vars['_w']??($color?20:0);
if (ctype_digit($_p)) { if (ctype_digit($_p)) {
$out = sprintf("%.{$_p}f", $ret); $out = sprintf("%{$_w}.{$_p}f", $ret);
} else { } else {
$out = $ret; $out = sprintf("%{$_w}s", $ret);
} }
echo $color ? " \e[92;1m{$out}\e[0m\n" : "{$out}\n"; echo $color ? " \e[92;1m{$out}\e[0m\n" : "{$out}\n";
} else { } else {
@ -181,6 +182,14 @@ class Calculator
$a = array_pop($out); $b = $sta[++$p]; $a = array_pop($out); $b = $sta[++$p];
$t = pow($a, $b); $t = pow($a, $b);
break; break;
case '!':
$a = array_pop($out);
$s = 0;
while ($a > 0) {
$s += $a--;
}
$t = $s;
break;
} }
array_push($out, $t); array_push($out, $t);
} }
@ -260,6 +269,7 @@ class Calculator
'|='. // = '|='. // =
'|[\+\-\*\/\^]'. // + - * / ^ '|[\+\-\*\/\^]'. // + - * / ^
'|\;'. // ; '|\;'. // ;
'|!'. // !
')/', $expr, $m)) { ')/', $expr, $m)) {
$toks[] = $m[0]; $toks[] = $m[0];
$expr = trim(substr($expr, strlen($m[0]))); $expr = trim(substr($expr, strlen($m[0])));