贷款计算器的简单实现(C++)

14 篇文章 2 订阅

贷款计算器

1. 等额本金

当月还款 = (当前未还总额)*(月化利率) + (固定每月还款本金)

例如,借款12万元,分12期还,年化利率为6%(月化利率为0.5%)那么固定每月还款本金为1万元,利息为:
在这里插入图片描述

2. 等额本息

每月还款金额固定,设为A;总借款额设为M,月化利率设为x,期数为p,那么有公式:

在这里插入图片描述

将a_1带入到a_2,a_2带入到a_3,逐项带入,最后a_p为:

在这里插入图片描述

3. 代码

3.1 头文件

//
// Created by HF on 2019/3/3.
//

#ifndef TEST_LOANCALCULATION_H
#define TEST_LOANCALCULATION_H


class LoanCalculation {
public:

    /**
     * 计算贷款每期需还多少
     * @param amount 贷款总额(万元)
     * @param period 期数,以月为单位
     * @param annualizedInterestRate 年化利率(%)
     * @param type 1表示等额本金,2表示等额本息
     */
    void calculate(double amount, int period, double annualizedInterestRate, int type);

private:
    void calEquPrincipal(double amount, int period, double monthlyInterestRate);
    void calEquPrincipalAndInterest(double amount, int period, double monthlyInterestRate);
};


#endif //TEST_LOANCALCULATION_H

3.2 源文件

//
// Created by HF on 2019/3/3.
//
#include <iostream>
#include <string>

#include "LoanCalculation.h"

using namespace std;

void LoanCalculation::calculate(double amount, int period, double annualizedInterestRate, int type) {
    if (period <= 0) {
        cout<<"期数需要大于0!"<<endl;
        return ;
    }

    amount *= 10000;
    double monthlyInterestRate = annualizedInterestRate / 1200;

    if (type == 1) {
        calEquPrincipal(amount, period, monthlyInterestRate);
    }
    else if (type == 2) {
        calEquPrincipalAndInterest(amount, period, monthlyInterestRate);
    }
    return ;
}

void LoanCalculation::calEquPrincipal(double amount, int period, double monthlyInterestRate) {
    double perioidPrincipal = amount / period;
    double interest = amount * monthlyInterestRate;
    double descInterest = perioidPrincipal * monthlyInterestRate;
    double sumInterest = 0.0;
    for (int i = 0; i < period; ++i) {
        cout<< "第" + to_string(i+1) + "个月需还: " + to_string(perioidPrincipal + interest) + " 元"<< endl;
        sumInterest += interest;
        interest -= descInterest;
    }

    cout<<"总利息: " +to_string(sumInterest) + " 元"<<endl;
    return ;
}

void LoanCalculation::calEquPrincipalAndInterest(double amount, int period, double monthlyInterestRate) {
    double x1 = pow((1+monthlyInterestRate), period);
    double x2 = (x1-1)/monthlyInterestRate;
    double res = amount*x1/x2;

    cout<<"每个月需还: " + to_string(res)<<endl;
    double sumInterest = res*period-amount;
    cout<<"总利息: " +to_string(sumInterest) + " 元"<<endl;
    return ;
}

3.3 使用

#include <iostream>
#include "LoanCalculation.h"

using namespace std;

int main() {

    double amount;
    int period;
    double annualizedInterestRate;
    int type;
    cout<<"请输入贷款总额(万元), 期数(月数),年化利率(%)"<<endl;
    cin>>amount>>period>>annualizedInterestRate;
    LoanCalculation loanCalculation;
    cout<<"等额本金:"<<endl;
    loanCalculation.calculate(amount, period, annualizedInterestRate, 1);
    cout<<endl<<"等额本息:"<<endl;
    loanCalculation.calculate(amount, period, annualizedInterestRate, 2);
    return 0;
}

3.4 输出结果

12 12 6
等额本金:
第1个月需还: 10600.000000 元
第2个月需还: 10550.000000 元
第3个月需还: 10500.000000 元
第4个月需还: 10450.000000 元
第5个月需还: 10400.000000 元
第6个月需还: 10350.000000 元
第7个月需还: 10300.000000 元
第8个月需还: 10250.000000 元
第9个月需还: 10200.000000 元
第10个月需还: 10150.000000 元
第11个月需还: 10100.000000 元
第12个月需还: 10050.000000 元
总利息: 3900.000000 元

等额本息:
每个月需还: 10327.971565
总利息: 3935.658778 元
  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值