等额还款本金法

等额还款本金法类声明:

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值