Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-16 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found