In: Computer Science
modify code to write the output as an HTML table to a file in the output directory.
The file that is saying to work at :
SOURCE CODE IN PERL:
print "Enter principal amount: ";
$P=;
while($P<=0)
{
print "Principal must be positive. Try again: ";
$P=;
}
print "Enter number of times interest is applied in a year:
";
$n=;
while($n!=12 && $n!=4 && $n!=2 &&
$n!=1)
{
print "It must be 12, 4, 2 or 1. Try again: ";
$n=;
}
print "Enter the annual rate of interest: ";
$r=;
while($r<=0 || $r>=25)
{
print "Rate of interest should be between 1 and 24. Try again:
";
$r=;
}
print "Enter the number of years: ";
$t=;
while($t<=0 || $t>=100)
{
print "Rate of interest should be between 1 and 99. Try again:
";
$t=;
}
print "\nYear Period Starting Balance Interest Ending
Balance\n";
for($i=1; $i<=$t; $i=$i+1)
{
for($j=1; $j<=$n; $j=$j+1)
{
$interest=$P*($r/($n*100));
print sprintf("%-4d %-6d \$%-15.2f \$%-7.2f \$%-13.2f\n",$i, $j,
$P, $interest, $P+$interest);
$P=$P+$interest;
}
}
print sprintf("Final Balance is \$%.2f\n", $P);
print "Enter principal amount: ";
$P= <STDIN>;
while($P<=0)
{
print "Principal must be positive. Try again: ";
$P= <STDIN>;
}
print "Enter number of times interest is applied in a year: ";
$n= <STDIN>;
while($n!=12 && $n!=4 && $n!=2 && $n!=1)
{
print "It must be 12, 4, 2 or 1. Try again: ";
$n= <STDIN>;
}
print "Enter the annual rate of interest: ";
$r= <STDIN>;
while($r<=0 || $r>=25)
{
print "Rate of interest should be between 1 and 24. Try again: ";
$r= <STDIN>;
}
print "Enter the number of years: ";
$t= <STDIN>;
while($t<=0 || $t>=100)
{
print "Rate of interest should be between 1 and 99. Try again: ";
$t= <STDIN>;
}
print "\nYear Period Starting Balance Interest Ending Balance\n";
#open the file for output named output table.html
open(outFile, '>', "output table.html") or die $!;
#this is the table headers
$toWrite="<table border=\"1\">
<tr>
<th>Year</th>
<th>Period</th>
<th>Starting Balance</th>
<th>Interest</th>
<th>Ending Balance</th>
</tr>\n";
#this command writes the content from $toWrite to the output file
print outFile $toWrite;
for($i=1; $i<=$t; $i=$i+1)
{
for($j=1; $j<=$n; $j=$j+1)
{
$interest=$P*($r/($n*100));
print sprintf("%-4d %-6d \$%-15.2f \$%-7.2f \$%-13.2f\n",$i, $j, $P, $interest, $P+$interest);
#generate the table row data and save it to variable toWrite
$toWrite = sprintf("<tr><td>%-4d</td><td> %-6d </td> <td>\$%-15.2f</td> <td>\$%-7.2f</td> <td>\$%-13.2f</td></tr>\n",$i, $j, $P, $interest, $P+$interest);
$P=$P+$interest;
print outFile $toWrite; #write table row to the output file
}
}
print outFile "</table>"; #ending html table tag
close(outFile);
print sprintf("Final Balance is \$%.2f\n", $P);
I have modified the code so it will write a html table as output table.html to the same directory.
I have given the explanation in the code as comment.
Heres a result/output of a test run
After running the code the written html table file (output table.html) contains
<table border="1">
<tr>
<th>Year</th>
<th>Period</th>
<th>Starting Balance</th>
<th>Interest</th>
<th>Ending Balance</th>
</tr>
<tr><td>1 </td><td> 1 </td> <td>$5000.00 </td> <td>$125.00 </td> <td>$5125.00 </td></tr>
<tr><td>1 </td><td> 2 </td> <td>$5125.00 </td> <td>$128.12 </td> <td>$5253.12 </td></tr>
<tr><td>2 </td><td> 1 </td> <td>$5253.12 </td> <td>$131.33 </td> <td>$5384.45 </td></tr>
<tr><td>2 </td><td> 2 </td> <td>$5384.45 </td> <td>$134.61 </td> <td>$5519.06 </td></tr>
<tr><td>3 </td><td> 1 </td> <td>$5519.06 </td> <td>$137.98 </td> <td>$5657.04 </td></tr>
<tr><td>3 </td><td> 2 </td> <td>$5657.04 </td> <td>$141.43 </td> <td>$5798.47 </td></tr>
<tr><td>4 </td><td> 1 </td> <td>$5798.47 </td> <td>$144.96 </td> <td>$5943.43 </td></tr>
<tr><td>4 </td><td> 2 </td> <td>$5943.43 </td> <td>$148.59 </td> <td>$6092.01 </td></tr>
<tr><td>5 </td><td> 1 </td> <td>$6092.01 </td> <td>$152.30 </td> <td>$6244.31 </td></tr>
<tr><td>5 </td><td> 2 </td> <td>$6244.31 </td> <td>$156.11 </td> <td>$6400.42 </td></tr>
</table>
which if you open it in browser looks like