关于一道Perl语言编程问题(英文附简翻译)[求助

关于一道Perl语言编程问题(英文附简翻译)[求助

汗 还真是一样。。 呵呵 我是读澳洲curtin科技大学的,这个是我们B TECH的一个科目computer systems201里的一个perl 部分的问题。主要我们是做CISCO 这个是assignment1的题目 ...assignment2更变态 小弟真是才疏学浅 做不来。。。 哎
请问这个是否受用在这道题.
请问这个是否受用在这道题中?
#!/usr/bin/perl

print "Please enter 'batch' (without quotes) to process file\n";
print "Please enter 'interactive' (without quotes) to manually input
data\n";
print "Enter 'q' or 'quit' (without quotes) to quit\n\n";
print "Input :";
chomp($mode = <STDIN>);

if ($mode =~ "batch")
{
print "Enter file name and path if different directory : ";
chomp($processbatch = <STDIN>);

if (-e $processbatch)
{
open(FILE,$processbatch);
while(chomp($line = <FILE>))
{
if ($line =~ /\d\w/)
{
next;
}
else {
print "Postfix notation for your expression : ";
print chgPostfix($line);
print "\n";
print "The answer of your expression would be : ";
print Postans(joinArr(chgPostfix($line)));
print "\n\n";
}
}
print "STATUS: This is the end of the file!\n";
close FILE;
}
else
{
print "ERROR: The filename you entered doesn't exist or not in that
directory ($processbatch)\n";
}
}
elsif ($mode =~ "interactive")
{
print "Please enter your expression here : ";
chomp($infixarg = <STDIN> );
if ($infixarg =~ /\w\d/)
{
die "ERROR : That's not number, can't convert and calculate\n";
}
print "Postfix notation for your expression : ";
print chgPostfix($infixarg);
print "\n\n";
print "The answer of your expression would be : ";
print Postans(joinArr(chgPostfix($infixarg)));
print "\n";
}
elsif ($mode =~ /quit|q/)
{
print "Quit!\nThank you for using the calculator... :D\n";
}
else
{
print "ERROR : Unknown command. Please re-run program\n";
}

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<length($who); $i++)
{
$whoArr[$i]=substr($who,$i,1);
}
return(@whoArr);
}

sub chgPostfix
{
($infixStr)=@_;
my @infixArr=genArr($infixStr);
my @postfixArr;
my @stackArr;
my $postfixPtr=0;
for($i=0; $i<length($infixStr); $i++)
{
if(isOperand($infixArr[$i]))
{
$postfixArr[$postfixPtr]=$infixArr[$i];
$postfixPtr++;
}
if(isOperator($infixArr[$i]))
{
if($infixArr[$i] ne "^")
{
while((!isEmpty(@stackArr)) &&
(prcd($infixArr[$i])<=prcd(topStack(@stackArr))))
{
$postfixArr[$postfixPtr]=topStack(@stackArr);
pop(@stackArr);
$postfixPtr++;
}
}
else
{
while((!isEmpty(@stackArr)) &&
(prcd($infixArr[$i])<prcd(topStack(@stackArr))))
{
$postfixArr[$postfixPtr]=topStack(@stackArr);
pop(@stackArr);
$postfixPtr++;
}
}
push(@stackArr,$infixArr[$i]);
}
if($infixArr[$i] eq "(")
{
push(@stackArr,$infixArr[$i])
}
if($infixArr[$i] eq ")")
{
while(topStack(@stackArr) ne "(")
{
$postfixArr[$postfixPtr]=pop(@stackArr);
$postfixPtr++;
}
pop(@stackArr);
}
}
while(!isEmpty(@stackArr))
{
if(topStack(@stackArr) eq "(")
{
pop(@stackArr)
}
else
{
$temp=@postfixArr;
$postfixArr[$temp]=pop(@stackArr);
}
}
return(@postfixArr);
}

sub PostfixToInfix
{
($postfixStr)=@_;
my @stackArr;
my @postfixArr=genArr($postfixStr);
for($i=0; $i<length($postfixStr); $i++)
{
if(isOperand($postfixArr[$i]))
{
push(@stackArr,$postfixArr[$i]);
}
else
{
$temp=topStack(@stackArr);
pop(@stackArr);
$pushVal=topStack(@stackArr).$postfixArr[$i].$temp;
pop(@stackArr);
push(@stackArr,$pushVal);
}
}
return((@stackArr));
}

sub Postans
{
($postfixStr)=@_;
my @stackArr;
my @postfixArr=genArr($postfixStr);
for($i=0; $i<length($postfixStr); $i++)
{
if(isOperand($postfixArr[$i]))
{
push(@stackArr,$postfixArr[$i]);
}
else
{
$temp=topStack(@stackArr);
pop(@stackArr);
$pushVal=PostfixSubEval(topStack(@stackArr),$temp,$postfixArr[$i]);
pop(@stackArr);
push(@stackArr,$pushVal);
}
}
return(topStack(@stackArr));
}

sub PostfixSubEval
{
($num1,$num2,$sym)=@_;
my $returnVal;
if($sym eq "+")
{
$returnVal=$num1+$num2;
}
if($sym eq "-")
{
$returnVal=$num1-$num2;
}
if($sym eq "*")
{
$returnVal=$num1*$num2;
}
if($sym eq "/")
{
$returnVal=$num1/$num2;
}
if($sym eq "^")
{
$returnVal=$num1**$num2;
}
return($returnVal);
}

sub joinArr
{
(@who)=@_;
my $who_len=@who;
my $retVal;
for($i=0; $i<$who_len; $i++)
{
$retVal.=$who[$i];
}
return $retVal;
}

sub evalInfix
{
($exp)=@_;
return PostfixEval(joinArr(chgPostfix($exp)));
}
A simpler and more perlish one
[quote]#!/usr/bin/perl

use strict;

my %actions = ("+" => sub { my ($op1, $op2) = @_; return $op1 + $op2 },
"-" => sub { my ($op1, $op2) = @_; return $op1 - $op2 },
"*" => sub { my ($op1, $op2) = @_; return $op1 * $op2 },
"/" => sub { my ($op1, $op2) = @_; return $op1 / $op2 },
);

undef @ARGV unless ("--batch-mode" eq shift @ARGV);

my @stack;
while (<>) {
s{^\s*(.*[\S])?\s*$}{$1}; # trim $_
if ($_ + 0 eq $_) { # operand
push @stack, $_;
}
elsif ($actions{$_}) { # operator
my ($op2, $op1) = (pop @stack, pop @stack);
push @stack, $actions{$_}($op1, $op2);
}
else {
die "Illegal token!";
}
}

die "incomplete expression" if (@stack > 1);
print @stack, "\n";
[/quote]




   

请问这道题可以帮忙解答一下吗?
HTML Image Gallery
You are to create a Perl script that will generate a HTML image gallery from a given set of image. Note that this is NOT to be a CGI script that generates the html “on the fly”, but a Perl program that generates a set of HTML pages to be uploaded to a web server.

You Perl script will collect the following pieces of information from the user in a way that seems appropriate to you (e.g. command line, interactively):
- The name of the image gallery
- The location of a directory containing the images
- The location of a directory containing information files

You Perl script should check that the image and information directories exist and are readable, printing an error message and exiting if they are not.

Once you have the input, your script should produce the following HTML files:

The index.html file
- Displays in the title bar the name of the image gallery
- Displays each image in the directory as a thumbnail (note that you do not have to physically resize the images, just restrict their size using the width and/or height attributes in the img tag)
- Each thumbnail is a link to a separate image page
Image Pages
- Displays time image in full isze
- Displays the image name in the title bar
- Displays the contents of an “information” file. The information file will be in a directory provided by the user, and each file will have a name like imagename.txt. The information file will hold a Dimensions attribute a, a Title attribute and 0 or more Captions. These attributes may appear in any order in the file, and will look something like:
Dimensions => WidthxHeight
Title => Picture Title
<timestamp> => Caption
<timestamp> => Caption

The name of the image is the filename without the extension, i.e.
If the filename is augusta.jpg then the name of the image is augusta, and the information file will be augusta.txt

You can find a set of example images and information files, as well as an example of what your finished image gallery should look like at:
http://www.ece.curtin.edu.au/~cs201/assigment2.zip
Bonus (5 marks) - use CGI to allow users to add new comments. It is not possible to get more than 100% in this assignment. The bonus marks allow you to make up marks you may have lost in the first section of the assignment.