等额还款本金法类声明:
#ifndef RETURNSAMECAPITAL_H_INCLUDED
#define RETURNSAMECAPITAL_H_INCLUDED
#include "LoanBase.h"
/** /brief class CReturnSameCapital * Description: 等额本金还款类; */
class CReturnSameCapital : public CLoanBase
{
public:
CReturnSameCapital();
~CReturnSameCapital();
virtual void GetResult(std::ofstream& ofs, double srcAmount = 0, int iMonths = 0, float fRate = 0) throw(CLoanException);
};
#endif // RETURNSAMECAPITAL_H_INCLUDED
实现:
#include "ReturnSameCapital.h"
CReturnSameCapital::CReturnSameCapital()
{
}
CReturnSameCapital::~CReturnSameCapital()
{
}
void CReturnSameCapital::GetResult(std::ofstream& ofs, double srcAmount, int iMonths, float fRate) throw(CLoanException)
{
if (0 == srcAmount || 0 == iMonths || 0 == fRate)
{
CLoanBase::ThrowException("Input parameter is invalid. Please check it!");
}
/*!< 每月还款本金额 */
double returnAmountPerMonth = srcAmount / iMonths;
/*!< 每月还款总额 */
double returnTotalAmountPerMonth = 0;
/*!< 月利率 */
float ratePerMonth = fRate / 12;
/*!< 循环计算当期还款额 */
for (int i = 0; i < iMonths; ++i)
{
returnTotalAmountPerMonth = returnAmountPerMonth * (1 - i * ratePerMonth) + srcAmount * ratePerMonth;
if (ofs.good())
{
ofs << "每月还款额:" << returnTotalAmountPerMonth << "元 每月还款本金:" << returnAmountPerMonth
<< "元 每月还款利息:" << returnTotalAmountPerMonth - returnAmountPerMonth << "元" <<std::endl;
}
}
}
测试代码:
#include <iostream>
#include <fstream>
#include "ReturnSameCapital.h"
#include "ReturnSameInterest.h"
using namespace std;
int main()
{
cout << "Hello world" << endl;
ofstream ofs;
locale loc = locale::global(locale(""));
ofs.open("d://testlog.log");
locale::global(loc);
CLoanBase* pLoanCalculate = new CReturnSameInterest();
try
{
// 贷款金额为100000,贷款月数为60,利率为0.0576
pLoanCalculate->GetResult(ofs, 100000, 60, 0.0576);
}
catch(CLoanException& e)
{
cout << e.what() << endl;
}
ofs.close();
return 0;
}