C++ 房贷计算 : 等额本金和等额本息



#include <iostream>
#include <cmath>
using namespace std;

class Loan{
protected:
	double interest_rate; // 月利率
	unsigned int months; // 还款时间
	double total_loans; // 贷款额度
	
	double gross_interest;// 总利息
public:
	virtual void calcRepayInfo()=0; // 计算还款信息
	Loan(double _t,unsigned int _m,double _i):total_loans(_t),interest_rate(_i),months(_m){}
	void dispBasicInfo(){
		cout<<"贷款总额:"<<total_loans<<endl
			<<"还款月数:"<<months<<endl;
	}
};

//等额本息
class EquCorpusInterest:public Loan{
	double monthly_repayment;
public:
	EquCorpusInterest(double _i,unsigned int _m,double _t):Loan(_i,_m,_t){}
	void calcRepayInfo(){
		dispBasicInfo();

		double per_mon_repay = 0.0;
		per_mon_repay = total_loans*interest_rate*pow(1+interest_rate,(int)months)/(pow(1+interest_rate,(int)months)-1);

		cout<<"等额本息还款法:"<<endl
			<<"每月还款额:"<<per_mon_repay<<endl
			<<"总利息:"<<per_mon_repay*months-total_loans<<endl
			<<"总还款额:"<<per_mon_repay*months<<endl<<endl;
	}
};

//等额本金
class EquCorpus:public Loan{
	double first_month_repayment;
public:
	EquCorpus(double _i,unsigned int _m,double _t):Loan(_i,_m,_t){}
	void calcRepayInfo(){
		dispBasicInfo();
		double total_repayment = 0;
		first_month_repayment = total_loans/months+(total_loans-total_repayment)*interest_rate; // 首月还款
		double decay_repay = total_loans/months*interest_rate; // 每月减少的还款

		gross_interest = 0.0;
		double per_mon_repay = 0;
		for(int i=1;i<=months;i++){
			per_mon_repay = total_loans/months+(total_loans-total_repayment)*interest_rate; // 每月还款
			total_repayment += total_loans/months;  // 总还款
			gross_interest += (total_loans-total_repayment)*interest_rate; // 总利息
		}

		cout<<"等额本金还款法:"<<endl
			<<"首月还款额:"<<first_month_repayment<<"(次月减少还款额:"<<decay_repay<<")"<<endl
			<<"总利息:"<<gross_interest<<endl
			<<"总还款:"<<gross_interest+total_repayment<<endl<<endl;
	}
};

void show(Loan* x){
	x->calcRepayInfo();
}

int main(){
	EquCorpusInterest eci(1000000,360,0.0405/12);
	EquCorpus ec(1000000,360,0.0405/12);
	Loan* pLoan = &eci;
	show(pLoan);
	pLoan = &ec;
	show(pLoan);
	return 0;
}

用这个可以验证正确性:http://fangd.sinaapp.com/
本题假设利率为0.0405

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真·skysys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值