#!/usr/bin/perl -w

unless (-e $ARGV[0]){
    print <<EOT
      
    Usage: $0 <file>
    
    This programs accepts as input file format:
    lowrate highrate step
    -cashfowA0 cashflowA1 cashflowA2  ...  cashflowAN
    -casflowB0 cashflowB1 cashflowB2  ...  cashflowBM

EOT
      ;      
    exit -1;
}

open IN, "<", $ARGV[0] or die "Cannot open $ARGV[0]: $!";
chomp ($in = <IN>);
($lo,$hi,$step) = split /\s+/,$in;
while (<IN>){
    chomp;
    $counter++;
    @cashflow = split /\s+/;
    for ($i = $lo; $i<=$hi; $i+=$step){
	print "Project $counter, rate $i: NPV = ";
	$npv=0;
	$index=0;
	for my $cf (@cashflow){
	    $npv += $cf/((1+$i)**$index);
	    $index++;
	}
	print "$npv\n";
    }
    $n = $d = 0;
    for my $cf (@cashflow){
	if ($cf <= 0){
	    $d += abs($cf);	    
	}else{
	    $n += $cf;
	}
    }
    $ROI = $n/$d;
    print "Project $counter, ROI = $ROI\n";
    $n = $d = 0;
    for ($i=$lo; $i <= $hi; $i += $step){
	print "Project $counter, rate $i: Residual Profit = ";
	$rp = 0;
	for my $cf (@cashflow){
	    if ($cf <= 0){
		$d += abs($cf);
	    }else{
		$n += $cf;
	    }
	}
	$rp = $n - $d * $i;
	print "$rp\n";
    }
}
close IN;

exit 1;

__END__
