fixed register retrieval, added %+ and %-

This commit is contained in:
Thomas von Dein
2019-03-04 15:41:24 +01:00
parent 49d0254287
commit d77455140a

36
rpnc
View File

@@ -12,7 +12,7 @@ my $term = Term::ReadLine->new('rpn calc');
my $debug = 0; my $debug = 0;
my $showstack = 1; my $showstack = 1;
my $tty = 1; my $tty = 1;
my $VERSION = '1.05'; my $VERSION = '1.06';
my $sub = 0; my $sub = 0;
my $maxstack = 10; my $maxstack = 10;
my $maxreg = 5; my $maxreg = 5;
@@ -88,6 +88,7 @@ my %alias = qw(^ ** x ^ < << > >> + + - - / / * * & & | |);
# hand coded functions # hand coded functions
my %func = ( my %func = (
'%' => sub { '%' => sub {
# X % of Y
if (scalar @_ == 2) { if (scalar @_ == 2) {
my ($a, $b) = @_; my ($a, $b) = @_;
return "($a / 100) * $b"; return "($a / 100) * $b";
@@ -110,6 +111,30 @@ my %func = (
return 0; return 0;
} }
}, },
'%+' => sub {
# Y + (X $ of Y)
if (scalar @_ == 2) {
my ($a, $b) = @_;
return "$a + (($a / 100) * $b)";
}
else {
print "percent only possible with 2 values\n";
undo();
return 0;
}
},
'%-' => sub {
# Y - (X $ of Y)
if (scalar @_ == 2) {
my ($a, $b) = @_;
return "$a - (($a / 100) * $b)";
}
else {
print "percent only possible with 2 values\n";
undo();
return 0;
}
},
'v' => sub { 'v' => sub {
if (scalar @_ == 2) { if (scalar @_ == 2) {
my ($a, $b) = @_; my ($a, $b) = @_;
@@ -289,9 +314,9 @@ sub dumpstack {
} }
if (@register) { if (@register) {
my $p = scalar @register; my $p = 1;
foreach my $n (@register) { foreach my $n (@register) {
printf "register R%d: %s\n", $p--, $n; printf "register R%d: %s\n", $p++, $n;
} }
} }
print "\n"; print "\n";
@@ -348,8 +373,7 @@ sub getlast {
sub getreg { sub getreg {
my $n = shift; my $n = shift;
print scalar @register; if ($n <= scalar @register) {
if (scalar @register <= $n) {
return $register[$n-1]; return $register[$n-1];
} }
else { else {
@@ -479,7 +503,7 @@ Stack Management: - substract
s show the stack / divide s show the stack / divide
sa show the whole stack * multiply sa show the whole stack * multiply
scx clear X (last stack element) ^ expotentiate scx clear X (last stack element) ^ expotentiate
sc clear stack % percent sc clear stack % percent (%+ add %- substract)
sr reverse the stack %d percentual difference sr reverse the stack %d percentual difference
srt rotate the stack & bitwise AND srt rotate the stack & bitwise AND
| bitwise OR | bitwise OR