in reply to Re: Re: Re: Inputing an Operator is it possible?
in thread Inputing an Operator is it possible?
Fix: I had the rhs and lhs backwards in the eval originally#!/usr/bin/perl -w use strict; my @stack = (); my %ops = ( '+' => \&calc, '-' => \&calc, '*' => \&calc, '/' => \&calc, 'p' => sub { print $_, "\n" foreach @stack; }, 'q' => sub { exit 0; } ); while(<>) { chomp; if (exists $ops{$_}) { $ops{$_}->($_); # The badass regex is out of the Perl Cookbook, page 44 } elsif ($_ =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { push @stack, $_; } else { warn "Bad input: $_\n"; } } sub calc { my $op = shift; my $rhs = pop @stack; my $lhs = pop @stack; if ((! defined $lhs) || (! defined $rhs)) { warn "Not enough values on stack!\n"; push @stack, $rhs if defined $rhs; return undef; } my $res = eval $lhs.$op.$rhs; print "$res\n"; push @stack, $res; }
|
|---|