Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,27 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
|
||||
my $number = '[+-]?(?:\.\d+|\d+(?:\.\d*)?)';
|
||||
my $operator = '[-+*/^]';
|
||||
|
||||
my @tests = ('3 4 2 * 1 5 - 2 3 ^ ^ / +');
|
||||
|
||||
for (@tests) {
|
||||
while (
|
||||
s/ \s* ((?<left>$number)) # 1st operand
|
||||
\s+ ((?<right>$number)) # 2nd operand
|
||||
\s+ ((?<op>$operator)) # operator
|
||||
(?:\s+|$) # more to parse, or done?
|
||||
/
|
||||
' '.evaluate().' ' # substitute results of evaluation
|
||||
/ex
|
||||
) {}
|
||||
say;
|
||||
}
|
||||
|
||||
sub evaluate {
|
||||
(my $a = "($+{left})$+{op}($+{right})") =~ s/\^/**/;
|
||||
say $a;
|
||||
eval $a;
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict; # https://rosettacode.org/wiki/Parsing/RPN_calculator_algorithm
|
||||
use warnings;
|
||||
use Text::ASCIITable;
|
||||
|
||||
while( <DATA> )
|
||||
{
|
||||
my $table = Text::ASCIITable->new({headingText => $_});
|
||||
$table->setCols(qw(Token Stack));
|
||||
$table->alignCol({Token => 'center', Stack => 'left'});
|
||||
my @stack;
|
||||
for ( split )
|
||||
{
|
||||
/\d/ ? push @stack, $_ : splice @stack, -2, 2,
|
||||
/\+/ ? $stack[-2] + $stack[-1] :
|
||||
/\-/ ? $stack[-2] - $stack[-1] :
|
||||
/\*/ ? $stack[-2] * $stack[-1] :
|
||||
/\// ? $stack[-2] / $stack[-1] :
|
||||
$stack[-2] ** $stack[-1];
|
||||
$table->addRow( $_, "@stack" );
|
||||
}
|
||||
print $table;
|
||||
}
|
||||
|
||||
__DATA__
|
||||
3 4 2 * 1 5 - 2 3 ^ ^ / +
|
||||
Loading…
Add table
Add a link
Reference in a new issue