等额本息还款公式

来源:互联网 发布:linux ant java 打包 编辑:程序博客网 时间:2024/06/09 21:07

贷款计算基类:

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

原创粉丝点击