房贷还款计算

来源:互联网 发布:外置光驱 知乎 编辑:程序博客网 时间:2024/04/27 21:28

  买房子了,按揭还款,为了了解两种贷款方式(等额本金方式和等额本息方式)的区别,以及每个月还款的详细信息,自己写了一个程序用来计算。

  两种方式还款结果如下(都是贷40W,20年,年利率假定为0.0655):


  等额本金方式和等额本息方式的概念如下:

  等额本金:本金保持相同,利息逐月递减,月还款数递减。

  等额本息:本金逐月递增,利息逐月递减,月还款数不变。

  理解了概念,要计算就很简单了。可参见:http://cs.jiwu.com/zhuanti/101905/

  解释一下结果中每一列:

还款的第几年还款的第几年的第几个月当月本金当前月应还的本金当月利息当前月应还有利息当月还款当前月应还的本金和利息总和共还本金到当前月一共还的本金共还利息到当前月一共还的利息共还款到当前月一共还的本金和利息总和剩余未还本金剩余未还的本金提前还总金额如果提前还剩余的本金,此列表示需要还的本金和日利息总和(按最多30天来算的日利息)提前还利息如果提前还剩余的本金,此列表示需要还的剩余本金的日利息(按最多30天来算的日利息)提前总共还如果提前还剩余的本金,此列表示从开始还到还完一共所还的总和  最后,代码如下:

// Mortgage.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <conio.h>#include <iomanip>#include <math.h>#include <Windows.h>using namespace std;double g_dTotalPrincipal = 0.0; // 总共贷款金额double g_nYears = 0; // 贷款年限double g_dYearRate = 0.0; // 年利率// 显示等额本金还款法void ShowConstantPrincipal(){int nMonths = 12 * g_nYears; // 总还贷月数double dMonthRate = g_dYearRate / 12; // 月利率double dMonthPrincipal = g_dTotalPrincipal / nMonths; // 每个月等额本金 double dRestPrincipal = g_dTotalPrincipal; // 剩余未还本金double dPrincipalTotalPayBack = 0.0; // 总共还本金double dInterestTotal = 0.0; // 总共还利息double dMoneyTotalPayBack = 0.0; // 总共还本息cout << setw(2) <<"年" << "|" << setw(2) << "月" << "|"  << setw(10) << "当月本金" << "|" << setw(10) << "当月利息" << "|" << setw(10) << "当月还款" << "|" << setw(12) << "共还本金" << "|" << setw(12) << "共还利息" << "|" << setw(12) << "共还款" << "|" << setw(12) << "剩余未还本金" << "|"  << setw(12) << "提前还总金额" << "|" << setw(12) << "提前还利息" << "|" << setw(12) << "提前总共还" << "|" << endl;cout << fixed << setprecision(3);for (int i = 1; i <= nMonths; i++){// 第几年if (1 == i % 12){cout << setw(2) << (i / 12) + 1 << "|";}else{cout << setw(2) << " " << "|";}// 第几月cout << setw(2) << (i - 1) % 12 + 1 << "|";// 当月本金cout << setw(10) << dMonthPrincipal << "|";dPrincipalTotalPayBack += dMonthPrincipal;// 当月利息cout << setw(10) << dRestPrincipal * dMonthRate << "|";dInterestTotal += dRestPrincipal * dMonthRate;// 当月还款cout << setw(10) << dMonthPrincipal + dRestPrincipal * dMonthRate << "|";// 共还本金cout << setw(12) << dPrincipalTotalPayBack << "|";// 共还利息cout << setw(12) << dInterestTotal << "|";// 共还款cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal << "|";// 剩余未还本金dRestPrincipal -= dMonthPrincipal;if (dRestPrincipal < 0.0001){// 由于计算误差,最后一次可能剩余未还本金为极小的数dRestPrincipal = 0.0;}cout << setw(12) << dRestPrincipal << "|";// 提前还总金额// 以30天来计算cout << setw(12) << dRestPrincipal + dRestPrincipal * (g_dYearRate / 360) * 30 << "|";// 提前多还cout << setw(12) << dRestPrincipal * (g_dYearRate / 360) * 30 << "|";// 提前总共还if (dRestPrincipal > 0.0){// 如果都还完了,就不存在提前还了cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal + dRestPrincipal  + dRestPrincipal * (g_dYearRate / 360) * 30 << "|";}else{cout << setw(12) << 0.0 << "|";}cout << endl;}}// 显示等额本息还款法void ShowConstantPrincipalInterest(){int nMonths = 12 * g_nYears; // 总还贷月数double dMonthRate = g_dYearRate / 12; // 月利率double dTotalMoney1Month = (g_dTotalPrincipal * dMonthRate * pow((1 + dMonthRate), nMonths)) / (pow((1 + dMonthRate), nMonths) - 1);double dRestPrincipal = g_dTotalPrincipal; // 剩余未还本金double dPrincipalTotalPayBack = 0.0; // 总共还本金double dInterestTotal = 0.0; // 总共还利息double dMoneyTotalPayBack = 0.0; // 总共还本息cout << setw(2) <<"年" << "|" << setw(2) << "月" << "|"  << setw(10) << "当月本金" << "|" << setw(10) << "当月利息" << "|" << setw(10) << "当月还款" << "|" << setw(12) << "共还本金" << "|" << setw(12) << "共还利息" << "|" << setw(12) << "共还款" << "|" << setw(12) << "剩余未还本金" << "|" << setw(12) << "提前还总金额" << "|" << setw(12) << "提前还利息" << "|" << setw(12) << "提前总共还" << "|" << endl;cout << fixed << setprecision(3);for (int i = 1; i <= nMonths; i++){// 第几年if (1 == i % 12){cout << setw(2) << (i / 12) + 1 << "|";}else{cout << setw(2) << " " << "|";}// 第几月cout << setw(2) << (i - 1) % 12 + 1 << "|";// 当月本金cout << setw(10) << dTotalMoney1Month - dRestPrincipal * dMonthRate << "|";dPrincipalTotalPayBack += dTotalMoney1Month - dRestPrincipal * dMonthRate;// 当月利息cout << setw(10) << dRestPrincipal * dMonthRate << "|";dInterestTotal += dRestPrincipal * dMonthRate;// 当月还款cout << setw(10) << dTotalMoney1Month << "|";// 共还本金cout << setw(12) << dPrincipalTotalPayBack << "|";// 共还利息cout << setw(12) << dInterestTotal << "|";// 共还款cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal << "|";// 剩余未还本金dRestPrincipal -= dTotalMoney1Month - dRestPrincipal * dMonthRate;if (dRestPrincipal < 0.0001){// 由于计算误差,最后一次可能剩余未还本金为极小的数dRestPrincipal = 0.0;}cout << setw(12) << dRestPrincipal << "|";// 提前还总金额// 以30天来计算cout << setw(12) << dRestPrincipal + dRestPrincipal * (g_dYearRate / 360) * 30 << "|";// 提前多还cout << setw(12) << dRestPrincipal * (g_dYearRate / 360) * 30 << "|";// 提前总共还if (dRestPrincipal > 0.0){// 如果都还完了,就不存在提前还了cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal + dRestPrincipal  + dRestPrincipal * (g_dYearRate / 360) * 30 << "|";}else{cout << setw(12) << 0.0 << "|";}cout << endl;}}int main(int argc, char* argv[]){const int Console_Screen_Buffer_Min_Width = 140;const int Console_Screen_Buffer_Min_Heigth = 650;CONSOLE_SCREEN_BUFFER_INFO conScrBufInfo;GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &conScrBufInfo);int nConSrcBufWidth = max(Console_Screen_Buffer_Min_Width, conScrBufInfo.dwSize.X);int nConSrcBufHeigth = max(Console_Screen_Buffer_Min_Heigth, conScrBufInfo.dwSize.Y);COORD coordDest = {nConSrcBufWidth, nConSrcBufHeigth}; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordDest);conScrBufInfo.srWindow.Right = nConSrcBufWidth - 1;SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &conScrBufInfo.srWindow);cout << "输入还贷总额:";cin >> g_dTotalPrincipal;cout << "输入还贷总年数:";cin >> g_nYears;cout << "输入年利率:"; cin >> g_dYearRate;int nMortgateType = 0;cout << "输入还贷方式(0:等额本金方式;1:等额本息方式):";cin >> nMortgateType;switch(nMortgateType){case 0: // 等额本金方式ShowConstantPrincipal();break;case 1: // 等额本息方式ShowConstantPrincipalInterest();break;default:break;}cout << "按任意键退出...";getch();return 0;}


原创粉丝点击