From 15cf3dcafba78dda35d8ae22066cbc75141737d8 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 5 May 2013 19:35:11 +0200 Subject: [PATCH 1/2] added operator % --- rpn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpn b/rpn index 70606be..268cc8f 100755 --- a/rpn +++ b/rpn @@ -10,7 +10,7 @@ my $stack_color = 'blue'; print color $input_color; while (<>) { for (split) { - if (m{^[-+*/]|\*\*$}) { + if (m{^[-+*/%]|\*\*$}) { my $y = pop @stack; my $x = pop @stack; my $z = eval "$x $_ $y"; From 4e77d495e73e05af92f5459d83f4fc3ae8adc263 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 29 Sep 2013 14:18:04 +0200 Subject: [PATCH 2/2] Implemented format and fixed parse error. --- rpn | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rpn b/rpn index 268cc8f..d366bfd 100755 --- a/rpn +++ b/rpn @@ -3,14 +3,16 @@ use warnings; use strict; use Term::ANSIColor; use Math::Trig ':pi'; +use Scalar::Util qw(looks_like_number); my @stack; my $input_color = 'red'; my $stack_color = 'blue'; +my $format; print color $input_color; while (<>) { for (split) { - if (m{^[-+*/%]|\*\*$}) { + if (m{^([-+*/%]|\*\*)$}) { my $y = pop @stack; my $x = pop @stack; my $z = eval "$x $_ $y"; @@ -41,13 +43,20 @@ while (<>) { } elsif (m{^ld$}) { my $x = pop @stack; push @stack, log($x) / log(2); + } elsif (m{^format$}) { + $format = pop @stack; } else { push @stack, $_; } } for (@stack) { print color $stack_color; - print "$_\n"; + if ($format && looks_like_number($_)) { + printf($format, $_); + print "\n"; + } else { + print "$_\n"; + } } print color $input_color; }