#!/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";

In reply to Infix/Postfix Notation by xgunnerx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.