Perl递归帮助

时间:2010-08-23 20:02:02

标签: perl

我正在尝试编写一个计算股票股息的计划。我没有子程序就这样做了。现在,我正在尝试修改它,以便它可以使用递归例程运行。对此有何帮助?因为我不太擅长这个。

这是原始剧本+可怜的尝试。

print "A stock xyz's price is now $100. It has 3.78% dividend. You have 1000 of it and reinvest the dividend into the stock.\n"; 

my %hash; 
@stocknum = 1000; 
@dividend = 6780; 

while ($#dividend != 20) { 
    $a = $dividend[-1]; 
    $stock = $stocknum[-1]; 
    $div_total= $stock*100*0.0678; 

    $stock_total = $stock + int($a/100); 
    push (@stocknum, $stock_total);  
    push (@dividend, $div_total); 

    if ($#dividend == 20) { 
        last; 
    } 
} 

shift (@dividend); 
$stock_num = $stocknum[-1]; 
$div = $stock_num*100*0.0678; 
push (@dividend, $div); 
@hash{@stocknum} = @dividend; 

foreach $key(sort keys %hash) { 
    print "Stock number: $key\t"."Dividend: $hash{$key}\n";  
}  

$dividend=0.0378; 

1 个答案:

答案 0 :(得分:5)

我认为你不想要递归。我想你只想循环你所追求的支付周期数。看起来你出于某种原因让所有人都混淆了数组。

print <<'HERE';
A stock xyz's price is now $100. It has 6.78% dividend. 
You have 1000 of it and reinvest the dividend into the stock.

HERE

my $shares   = 1000;
my $price    =  100;
my $dividend =    6.78 / 100;
my $cycles   = $ARGV[0] || 20;

foreach ( 1 .. $cycles ) {
    local $cycle      = $_;
    local $payout     = $shares * $dividend * $price;
    local $new_shares = $payout / $price;

    write();

    $shares += $new_shares;
    }

format STDOUT =
@###    @####.######    @#####.#######    @##.######  @####.######
$cycle, $shares,          $payout,          $new_shares, $shares+$new_shares, 
.

format STDOUT_TOP =
                        @##.####%
                        $dividend
Cycle   Shares          Payout          New Shares  Total Shares
----------------------------------------------------------------
.

这给了我输出:

A stock xyz's price is now $100. It has 6.78% dividend. 
You have 1000 of it and reinvest the dividend into the stock.

                          0.0678%
Cycle   Shares          Payout          New Shares  Total Shares
----------------------------------------------------------------
   1     1000.000000      6780.0000000     67.800000   1067.800000
   2     1067.800000      7239.6840000     72.396840   1140.196840
   3     1140.196840      7730.5345752     77.305346   1217.502186
   4     1217.502186      8254.6648194     82.546648   1300.048834
   5     1300.048834      8814.3310942     88.143311   1388.192145
   6     1388.192145      9411.9427423     94.119427   1482.311572
   7     1482.311572     10050.0724603    100.500725   1582.812297
   8     1582.812297     10731.4673731    107.314674   1690.126971
   9     1690.126971     11459.0608610    114.590609   1804.717579
  10     1804.717579     12235.9851873    122.359852   1927.077431
  11     1927.077431     13065.5849830    130.655850   2057.733281
  12     2057.733281     13951.4316449    139.514316   2197.247597
  13     2197.247597     14897.3387104    148.973387   2346.220985
  14     2346.220985     15907.3782750    159.073783   2505.294767
  15     2505.294767     16985.8985220    169.858985   2675.153752
  16     2675.153752     18137.5424418    181.375424   2856.529177
  17     2856.529177     19367.2678194    193.672678   3050.201855
  18     3050.201855     20680.3685775    206.803686   3257.005541
  19     3257.005541     22082.4975671    220.824976   3477.830517
  20     3477.830517     23579.6909021    235.796909   3713.627426

不要担心我使用format;自从我重写了一些关于它的perlfaq之后,我已经将它变成了Use formats to create paginated, plaintext reports。您可以使用printf轻松创建输出:

print <<'HERE';
A stock xyz's price is now $100. It has 6.78% dividend. 
You have 1000 of it and reinvest the dividend into the stock.

Cycle   Shares          Payout          New Shares  Total Shares
----------------------------------------------------------------
HERE

my $shares   = 1000;
my $price    =  100;
my $dividend =    6.78 / 100;
my $cycles   = $ARGV[0] || 20;

foreach ( 1 .. $cycles ) {
    my $payout     = $shares * $dividend * $price;
    my $new_shares = $payout / $price;

    printf "%4d   %12.6f    %12.6f    %10.6f  %12.6f\n",
        $_, $shares, $payout, $new_shares, $shares + $new_shares;

    $shares += $new_shares;
    }

作为旁注,你真的不想要递归,特别是如果你能帮助它,尤其不要在Perl中。其他语言可以使用它,因为它们知道如何展开递归以将其转换为迭代过程。作为动态语言的Perl不能真正做到这一点,因为它不知道子程序在下一次运行时是否具有相同的定义。它作为一个计算机科学主题很好,因为它使编程变得更容易,并且他们知道它最终都能解决。我想我会在掌握Perl 的某个地方讨论这个问题,但Mark Jason Dominus在 Higher-Order Perl 中广泛介绍了它。基本上,您使用队列代替递归,这是一种更好的练习技巧。