等额本息还款公式

贷款计算基类:

#ifndef CLOANBASE_H_INCLUDED

#define CLOANBASE_H_INCLUDED
#include <fstream>
#include <string>
#include <exception>
class CLoanException : public std::exception
{
    std::string m_str;
public:
    CLoanException(const char* msg) throw()
    {
        m_str = msg;
    }
    ~CLoanException() throw() {}
    virtual const char* what() const throw()
    {
        return m_str.c_str();
    }
};
/** /brief class CLoanBase
 * Description; 计算贷款数据的基类
 */
class CLoanBase
{
public:
    CLoanBase(){}
    virtual ~CLoanBase()
    {
    }
    /** /brief GetResult
     * Descriptioin: 通过给定的本金金额,贷款月份及利率,将计算结果信息用输出数据流传出。
     * @param srcAmount[in]: 本金金额
     * @param iMonths[in]: 贷款月数
     * @param fRate[in]: 贷款利率
     * @param ofs[out]: 贷款信息明细
     */
    virtual void GetResult(std::ofstream& ofs, double srcAmount = 0, int iMonths = 0, float fRate = 0) throw(CLoanException) = 0;
protected:
    void ThrowException(const char* str) throw(CLoanException)
    {
        throw CLoanException(str);
    }
};
#endif // CLOANBASE_H_INCLUDED

 

等额本息还款类声明:

#ifndef RETURNSAMEINTEREST_H_INCLUDED
#define RETURNSAMEINTEREST_H_INCLUDED
#include "LoanBase.h"
/** /brief class CReturnSameInterest
 * Description: 等额本息还款类;
 */
class CReturnSameInterest : public CLoanBase
{
public:
    CReturnSameInterest();
    ~CReturnSameInterest();
    virtual void GetResult(std::ofstream& ofs, double srcAmount = 0, int iMonths = 0, float fRate = 0) throw(CLoanException);
};
#endif // RETURNSAMEINTEREST_H_INCLUDED

 

类实现:

#include "ReturnSameInterest.h"
#include <math.h>
CReturnSameInterest::CReturnSameInterest()
{
}
CReturnSameInterest::~CReturnSameInterest()
{
}
void CReturnSameInterest::GetResult(std::ofstream& ofs, double srcAmount, int iMonths, float fRate) throw(CLoanException)
{
    if (0 == srcAmount || 0 == iMonths || 0 == fRate)
    {
        throw "Input parameter is invalid. Please check it!";
    }
    /*!< 每月还款本金额 */
    double returnAmountPerMonth = srcAmount;
    /*!< 每月还款利息 */
    double returnInterestPerMonth = 0;
    /*!< 月利率 */
    float ratePerMonth = fRate / 12;
    /*!< 循环计算当期还款额 */
    /**
     *  每月还款利息:贷款本金 * 月利率 * ((1 + 月利率) ^ 还款期数 - (1 + 月利率) ^ (当前期数 - 1)) / ((1 + 月利率) ^ 当前期数 - 1)
     *  每月还款本金:贷款本金 * 月利率 * (1 + 月利率) ^ (当前期数 - 1) / ((1 + 月利率) ^ 还款期数 - 1)
     */
    for (int i = 1; i < iMonths + 1; ++i)
    {
        double dbResult1 = pow((1 + ratePerMonth), iMonths);
        double dbResult2 = pow((1 + ratePerMonth), (i - 1));
        if (0 == dbResult1 - 1)
        {
            CLoanBase::ThrowException("Invalid parameter div number");
        }
        returnInterestPerMonth = srcAmount * ratePerMonth * (dbResult1 - dbResult2) / (dbResult1 - 1);
        ofs << "每月还款利息为:" << returnInterestPerMonth << "元   每月还款本金为:";
        returnAmountPerMonth = srcAmount * ratePerMonth * dbResult2 / (dbResult1 - 1);
        ofs << returnAmountPerMonth << "元   每月还款额:" << returnInterestPerMonth + returnAmountPerMonth << "元" << std::endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值