简易房贷月供计算器

下面是用C语言编写的简易房贷月供计算器,供参考。

#include "stdio.h" 
#include "math.h"
int main()
{
	double amountOfLoan,annualInterest,repaymentPeriod; //input
	double monthlyPayments;  //output
	double monthlyInterest,numOfMonth,tmp; // intermediate variables
	//Input
	printf("\n A simple loan calculator\n\n");
	printf(" Please input amount of loan(¥):");
	scanf("%lf",&amountOfLoan);
	printf("\n Please input annual interest(%%):");
	scanf("%lf",&annualInterest);
	printf("\n Please input repayment period(years):");
	scanf("%lf",&repaymentPeriod);
	//calculate 
	monthlyInterest = annualInterest/100/12;
	numOfMonth = repaymentPeriod*12;
	tmp = pow(1+monthlyInterest,numOfMonth);
	monthlyPayments = (amountOfLoan * tmp * monthlyInterest)/(tmp-1); 
	//output
	printf("\n\n You need to repay ¥%.2f each month. Good luck!\n",monthlyPayments);
	return 0;
}

下面是用HTML、CSS和Javascript脚本语言写的一个简易贷款月供计算器,可以在浏览器中运行。HTML定义了网页元素,CSS定义了页面样式,Javascript实现了核心的计算部分。Javascript片段(标签<script type="text/javascript">和</script>之间的代码)的实现思路和上面写的C语言代码是相似的:先获取数据,然后借助数学库函数进行计算,最后输出结果。不同的是,这里从网页上的文本框获取数据,结果也是显示到页面元素上。

<!DOCTYPE html>  
<html>  
<head>  
    <title>JavaScript Loan Calculator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">	
    <style>		
	*{
		margin:0;
		padding:0;
	}
	body{
		background-color:snow;
		font-size:14pt;
	}
	.container{
		width:70%;
		height:1000px;
		background-color:antiquewhite;
		margin:0 auto;
	}		
        .output{font-weight:bold;}  
        #payment{text-decoration:underline;}
	table{
		width:40%;
		height:50%;
	}
	th,td{
		vertical-align:center;			
	}  
    </style>  
</head>  
<body>
    <div class="container">	
    <table align="center">  
        <tr>  
            <th colspan=2>JavaScript Loan Calculator</th>               
        </tr>  
        <tr>  
            <td>Amount of the loan(¥):</td>  
            <td><input id='amount' onchange='calculate();'></td>             
        </tr>  
         <tr>  
            <td>Annual interest(%):</td>  
            <td><input id='apr' onchange='calculate();'></td>             
        </tr>  
         <tr>  
            <td>Repayment period(years):</td>  
            <td><input id='years' onchange='calculate();'></td>             
        </tr>  
         <tr>  
            <td>Approximate Payments:</td>  
            <td><button onclick='calculate();'>Calculate</button></td>             
        </tr>  
         <tr>  
            <td>Monthly Payments:</td>  
            <td>¥<span class='output' id='payment'></span></td>             
        </tr>  
         <tr>  
            <td>Total Payments:</td>  
            <td>¥<span class='output' id='total'></span></td>             
        </tr>  
         <tr>  
            <td>Total Interest:</td>  
            <td>¥<span class='output' id='totalinterest'></span></td>             
        </tr>
    </table>
    </div>		
	
    <script type="text/javascript">  
        "use strict";  
        function $(id){  
            return document.getElementById(id);  
        }  
          
        function calculate ()
        {  
            var amount = $('amount');  
            var apr = $('apr');  
            var years = $('years');              
            var payment = $('payment');  
            var total = $('total');  
            var totalinterest = $('totalinterest');  
             
            var principal = parseFloat(amount.value);  
            var interest = parseFloat(apr.value)/100/12;  
            var payments = parseFloat(years.value)*12;  
            
            var x = Math.pow(1+interest ,payments);  
            var monthly = (principal * x * interest)/(x-1);  
              
            if(isFinite(monthly))
            {  
                payment.innerHTML = monthly.toFixed(2);  
                total.innerHTML = (monthly*payments).toFixed(2);  
                totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2);
            }
        }
    </script>  
</body>  
</html>

把上述代码复制到一个文本文档中,后缀名改为.html,双击即可在浏览器中运行。

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页