📄 程嶏清单13-4.txt
字号:
程序清单13-4:finance.pl
package Finance;
require Exporter;
@ISA = (Exporter);
=head1 Finance.pm
Financial Calculator - Financial calculations made easy with Perl
=head 2
use Finance;
$pv = 10000.0;
$rate = 12.5 / 12; # APR per month.
$time = 360 ; # months for loan to mature
$fv = FutureValue();
print $fv;
=cut
@EXPORT = qw(FutureValue,
PresentValue,
FVofAnnuity,
AnnuityOfFV,
getLastAverage,
getMovingAverage,
SetInterest);
#
# Globals, if any
#
local $defaultInterest = 5.0;
sub Finance::SetInterest($) {
my $rate = shift(@_);
$defaultInterest = $rate;
printf "\n \$defaultInterest = $rate";
}
# --------------------------------------------------------------------
# Notes:
# 1. The interest rate $r is given in a value of [0-100].
# 2. The $n given in the terms is the rate at which the interest
# is applied.
#
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# Present value of an investment given
# fv - a future value
# r - rate per period
# n - number of period
# --------------------------------------------------------------------
sub Finance::FutureValue($$$) {
my ($pv,$r,$n) = @_;
my $fv = $pv * ((1 + ($r/100)) ** $n);
return $fv;
}
# --------------------------------------------------------------------
# Present value of an investment given
# fv - a future value
# r - rate per period
# n - number of period
# --------------------------------------------------------------------
sub Finance::PresentValue($$$) {
my $pv;
my ($fv,$r,$n) = @_;
$pv = $fv / ((1 + ($r/100)) ** $n);
return $pv;
}
# --------------------------------------------------------------------
# Get the future value of an annuity given
# mp - Monthly Payment of Annuity
# r - rate per period
# n - number of period
# --------------------------------------------------------------------
sub FVofAnnuity($$$) {
my $fv;
my $oneR;
my ($mp,$r,$n) = @_;
$oneR = ( 1 + $r) ** $n;
$fv = $mp * ( ($oneR - 1)/ $r);
return $fv;
}
# --------------------------------------------------------------------
# Get the annuity from the following bits of information
# r - rate per period
# n - number of period
# fv - Future Value
# --------------------------------------------------------------------
sub AnnuityOfFV($$$) {
my $mp; # mp - Monthly Payment of Annuity
my $oneR;
my ($fv,$r,$n) = @_;
$oneR = ( 1 + $r) ** $n;
$mp = $fv * ( $r/ ($oneR - 1));
return $mp;
}
# --------------------------------------------------------------------
# Get the average of the last "n" values in an array.
# --------------------------------------------------------------------
# The last $count number of elements from the array in @values
# The total number of elements in @values is in $number
#
sub getLastAverage($$@) {
my ($count, $number, @values) = @_;
my $i;
my $a = 0;
return 0 if ($count == 0);
for ($i = 0; $i< $count; $i++) {
$a += $values[$number - $i - 1];
}
return $a / $count;
}
# --------------------------------------------------------------------
# Get a moving average of the values.
# --------------------------------------------------------------------
# The window size is the first parameter, the number of items in the
# passed array is next. (This can easily be calculated within the
# function using the scalar() function, but the subroutine shown here
# is also being used to illustrate how to pass pointers.) The reference to the
# array of values is passed next, followed by a reference to the place
# the return values are to be stored.
#
sub getMovingAve($$\@\@) {
my ($count, $number, $values, $movingAve) = @_;
my $i;
my $a = 0;
my $v = 0;
return 0 if ($count == 0);
return -1 if ($count > $number);
return -2 if ($count < 2);
$$movingAve[0] = 0;
$$movingAve[$number - 1] = 0;
for ($i=0; $i<$count;$i++) {
$v = $$values[$i];
$a += $v / $count;
$$movingAve[$i] = 0;
}
for ($i=$count; $i<$number;$i++) {
$v = $$values[$i];
$a += $v / $count;
$v = $$values[$i - $count - 1];
$a -= $v / $count;
$$movingAve[$i] = $a;
}
return 0;
}
1;
# end of Finance module
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -