>>>>> "zs" == z sway <zzway...@gmail.com> writes:

  zs> Hi, I've finished it using recursion.
  zs> #!/usr/bin/perl -w

use warnings is better

  zs> use strict;
  zs> use Data::Dumper;

  zs> while (1) {
  zs>   print "Enter the expression : ";
  zs> chomp($_ = <STDIN>);
  zs> last if not $_;
  zs> my @tree = grow($_);

it is better to use a named variable than $_. $_ can cause issues as it
is a global (recent perl's can make it lexical but still a name is better).

  zs> print Dumper(\@tree);
  zs> my $result = calculate(\@tree);
  zs> print "\nresult\=$result\n\n";

= does not need escaping in strings.

  zs> my $answer = eval "$_";

that is very dangerous, allowing user input to be evaled. do you need to
do that?


  zs> sub grow {
  zs> my $expr = $_[0];

this is better:
        my ( $expr ) = @_ ;

  zs> my ($part, @part, $node);

don't declare vars until they are first used.

  zs> if ($expr =~ /\(/){

you need to indent your code. it is very hard to read without it. 

  zs> @part = ($expr =~ / \( (?:(?>[^()]+) | (?R))* \) /xg); #提取括号中

        my @part = ...


  zs> foreach $part (@part) {
  zs> $part =~ s/^\(|\)$//g;
  zs> my @temp = grow($part); #递归调用
  zs> $part = \@temp;

no need for the @temp array. in fact if you name something temp, you
likely don't need it at all.

        $part = [ grow($part) ] ;

  zs> my $a = $_[0];

don't use $a (or $b) for your own variables. they are reserved for use
by the sort function. also they are bad names in general as they don't
tell the reader what they are for.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to