#!/usr/bin/perl -w use strict; ## DEFINE THE NOTATION HERE! my $notation = "((118-117)x(2+7))"; ## Position hash with anony array my %pos_hash = ("!" => 0, "+" => 1, "-" => 2, "x" => 3, "/" => 4, "(" => 5, ")" => 6); my @postfix; #infix data my %action_hash = ("!" => [4, 1, 1, 1, 1, 1, 5], "+" => [2, 2, 2, 1, 1, 1, 2], "-" => [2, 2, 2, 1, 1, 1, 2], "x" => [2, 2, 2, 2, 2, 1, 2], "/" => [2, 2, 2, 2, 2, 1, 2], "(" => [5, 1, 1, 1, 1, 1, 3] ); #holders my @texas; my @cali; $texas[0] = "!"; my $current_pos = 0; #Define functions sub seek_action { my $cur_texas = shift; $cur_texas = $texas[$cur_texas]; my $symbol = shift; my $move_to = $pos_hash{$symbol}; return $action_hash{"$cur_texas"}->[$move_to]; } sub postfix_cal { my $a; my $b; my @postfix1 = @_; my @ret_stack; my $result; print "Postfix is @postfix1\n"; foreach my $o (@postfix1) { if ($o =~ /\d+/) { push (@ret_stack, $o); } else { $b = pop (@ret_stack); $a = pop (@ret_stack); if ($o eq '+') { $result = $a + $b; } elsif ($o eq '-') { $result = $a - $b; } elsif ($o eq 'x') { $result = $a * $b; } elsif ($o eq '/') { $result = $a / $b; } push (@ret_stack, $result); } } return pop(@ret_stack); } while ($notation =~ /(\d+|\/|x|\-|\+|\(|\))/g) { push (@postfix, $1); } my $i; for ($i = 0; $i < scalar@postfix; $i++) { my $action; #find action if ($postfix[$i] !~ /\d+/) { $action = seek_action($current_pos, $postfix[$i]); } else { push(@cali, "$postfix[$i]"); next; } if (defined($action)) { #perform action if ($action == 1) { push (@texas, $postfix[$i]); $current_pos = scalar@texas; $current_pos--; next; } elsif ($action == 2) { my $move_cali = pop(@texas); push(@cali, $move_cali); $current_pos = scalar@texas; $current_pos--; $i--; next; } elsif ($action == 3) { pop(@texas); $current_pos = scalar@texas; $current_pos--; next; } elsif ($action == 4) { last; } elsif ($action == 5) { print "Error!\n"; last; } } } my $result = postfix_cal(@cali); print "Answer is: $result\n";