#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