zoukankan      html  css  js  c++  java
  • 递归在PHP中的应用举例

    递归在PHP中的应用举例

    <?php
    //amortizationTable表示还贷计算函数
    function amortizationTable($pNum, $periodicPayment, $balance, $monthlyInterest)
    {
    //计算支付利息
    $paymentInterest = round ($balance*$monthlyInterest, 2);
    //计算还款额
    $paymentPrincipal = round($periodicPayment - $paymentInterest,2);
    //用余额减去还款额
    $newBalance = round($balance-$paymentPrincipal,2);
    //如果余额<每月还款,设置为0
    if ($newBalance < $paymentPrincipal){
       $newBalance = 0;
    }
    
    printf ("<tr><td>%d</td>",$pNum);
    printf ("<td>$%s</td>", number_format ($newBalance,2));
    printf ("<td>$%s</td>", number_format ($periodicPayment,2));
    printf ("<td>$%s</td>", number_format ($paymentPrincipal,2));
    printf ("<td>$%s</td></tr>", number_format ($paymentInterest,2));
    
    #if balance not yet zero, recursively call amortizationTable()
    if ($newBalance>0){
       $pNum++;
       amortizationTable($pNum, $periodicPayment,
                          $newBalance, $monthlyInterest);
    	}else{
    	return 0;
    	}
    }
    
    $balance = 10000.00;                                 // 贷款余额
    $interestRate = 0.0575;                              //贷款利率
    $monthlyInterest = $interestRate/12;                 //每月利率
    $termLength = 5;                                     //贷款期限,单位为年
    $paymentsPerYear = 12;                               //每年支付次数
    $paymentNumber = 1;                                  //付款迭代
    $totalPayments = $termLength*$paymentsPerYear;       //确定付款次数
    $intCalc = 1 + $interestRate / $paymentsPerYear;     //确定分期付款的利息部分
    $periodicPayment = $balance * pow($intCalc, $totalPayments)*($intCalc -1)/(pow($intCalc,$totalPayments) -1);
    
    //每月还款额限制到小数点后两位
    $periodcPayment = round($periodicPayment,2);
    
    //创建表
    echo "<table width='50%' align = 'center' border = '1'>";
    echo "<tr>
          <th>Payment Number</th><th>Balance</th>
    	  <th>payment</th><th>Principal</th><th>Interest</th>
    	  </tr>";
    	  
    //创建递归函数
    amortizationTable ($paymentNumber, $periodcPayment, $balance,$monthlyInterest);
    //关闭表
    echo "</table>";
    ?>
    

      

  • 相关阅读:
    C#简单读取MongoDB数据
    百度地图自定义图标
    递归实现DropDownList层级
    [MYSQL]-EXPLAIN用法
    java对 zip文件的压缩和解压(ant解决中文乱码)
    将Excel表结构导入到Powerdesigner
    weblogic解决jar包冲突
    深入理解javascript原型和闭包(3)——prototype原型
    深入理解javascript原型和闭包(2)——函数和对象的关系
    深入理解JavaScript的原型和闭包(一)
  • 原文地址:https://www.cnblogs.com/hww836967373/p/3210456.html
Copyright © 2011-2022 走看看