############################################################################# # # Script: infix-postfix.pl # Author: Premshree Pillai # Description: Using this script you can : # - Convert an Infix expression to Postfix # - Convert a Postfix expression to Infix # - Evaluate a Postfix expression # - Evaluate an Infix expression # Web: http://www.qiksearch.com # E-mail: qiksearch@rediffmail.com # Created: 23/09/02 (dd/mm/yy) # # © 2002 Premshree Pillai. All rights reserved. # ############################################################################ sub init { @string=split(/&/,$ENV{QUERY_STRING}); @exp=split(/=/,$string[0]); $expression=$exp[1]; # Convert the HTML encoding $expression =~ tr/+/ /; $expression =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $expression =~ s///g; # Convert HTML stuff as necessary. $expression =~ s/<([^>]|\n)*>//g; @act=split(/=/,$string[1]); $action=$act[1]; } init(); print 'Infix-Postfix'; print ''; print ''; print '
'; print 'Infix-Postfix
'; print '
'; print ''; print ' '; print ' '; print '

'; print ''; print ''; if(($exp[0] ne 'expression') || ($act[0] ne 'action')) { print "Please use the above form!"; } else { if(!(($action eq '0') || ($action eq '1') || ($action eq '2') || ($action eq '3'))) { print "Invalid Argument for Action"; } else { print ''; if($action eq '0') { print 'Postfix Expression : ',InfixToPostfix($expression),''; } if($action eq '1') { print 'Infix Expression : ',PostfixToInfix($expression),''; } if($action eq '2') { print 'Postfix Eval : ',PostfixEval($expression),''; } if($action eq '3') { print 'Infix Eval : ',eval($expression),''; } print ''; } } print '

Algorithms used »'; print '© 2002 Premshree Pillai.'; print '
Sign my Guestbook | Qiksearch.com'; print '
'; print '
'; print ''; sub isOperand { ($who)=@_; if((!isOperator($who)) && ($who ne "(") && ($who ne ")")) { return 1; } else { return; } } sub isOperator { ($who)=@_; if(($who eq "+") || ($who eq "-") || ($who eq "*") || ($who eq "/") || ($who eq "^")) { return 1; } else { return; } } sub topStack { (@arr)=@_; my $arr_len=@arr; return($arr[$arr_len-1]); } sub isEmpty { (@arr)=@_; my $arr_len=@arr; if(($arr_len)==0) { return 1; } else { return; } } sub prcd { ($who)=@_; my $retVal; if($who eq "^") { $retVal="5"; } elsif(($who eq "*") || ($who eq "/")) { $retVal="4"; } elsif(($who eq "+") || ($who eq "-")) { $retVal="3"; } elsif($who eq "(") { $retVal="2"; } else { $retVal="1"; } return($retVal); } sub genArr { ($who)=@_; my @whoArr; for($i=0; $i