当前位置: 首页 > article >正文

基于C++实现一个房贷计算小程序(含代码)

房贷计算程序,主要实现以下功能:

  1. 用户友好的界面:使用文本菜单来引导用户选择功能。
  2. 支持不同还款频率:例如每季度还款、每半年还款等。
  3. 支持贷款提前还款:计算提前还款对总支付利息的影响。
  4. 详细的还款计划表:输出每月的本金、利息和总还款额。

完整示例代码免费下载链接

在这里插入图片描述

下面是一个复杂的C++程序示例,涵盖了以上功能:

C++ 程序代码

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>

using namespace std;

// Function to validate user input
bool validateInput(double value) {
    return value > 0;
}

// Function to calculate monthly payment for equal principal repayment
double calculateEqualPrincipalPayment(double principal, double annualInterestRate, int years) {
    double monthlyInterestRate = annualInterestRate / 12 / 100;
    int totalMonths = years * 12;
    double totalPayment = 0;

    for (int month = 1; month <= totalMonths; ++month) {
        double remainingPrincipal = principal * (totalMonths - month + 1) / totalMonths;
        double monthlyPayment = (remainingPrincipal * monthlyInterestRate) + (principal / totalMonths);
        totalPayment += monthlyPayment;
    }

    return totalPayment / totalMonths; // Average monthly payment
}

// Function to calculate monthly payment for equal monthly installments
double calculateEqualInstallmentsPayment(double principal, double annualInterestRate, int years) {
    double monthlyInterestRate = annualInterestRate / 12 / 100;
    int totalMonths = years * 12;

    double numerator = monthlyInterestRate * std::pow(1 + monthlyInterestRate, totalMonths);
    double denominator = std::pow(1 + monthlyInterestRate, totalMonths) - 1;
    return principal * (numerator / denominator);
}

// Function to generate a repayment schedule
void generateRepaymentSchedule(double principal, double annualInterestRate, int years, int frequency) {
    double rate = annualInterestRate / frequency / 100;
    int totalPeriods = years * frequency;
    double principalPayment = principal / totalPeriods;

    cout << fixed << setprecision(2);
    cout << "Repayment Schedule:\n";
    cout << setw(10) << "Period" << setw(20) << "Principal Payment" << setw(20) << "Interest Payment" << setw(20) << "Total Payment\n";

    double remainingPrincipal = principal;
    for (int period = 1; period <= totalPeriods; ++period) {
        double interestPayment = remainingPrincipal * rate;
        double totalPayment = principalPayment + interestPayment;
        remainingPrincipal -= principalPayment;

        cout << setw(10) << period
             << setw(20) << principalPayment
             << setw(20) << interestPayment
             << setw(20) << totalPayment
             << endl;
    }
}

// Main function
int main() {
    double principal; // Loan amount
    double annualInterestRate; // Annual interest rate in percentage
    int years; // Loan term in years
    int repaymentOption; // Repayment option (1 for Equal Principal, 2 for Equal Installments)
    int frequency; // Repayment frequency (e.g., 12 for monthly, 4 for quarterly, 2 for semi-annually)

    cout << "Enter the loan amount: ";
    cin >> principal;
    if (!validateInput(principal)) {
        cerr << "Invalid loan amount. Must be greater than zero." << endl;
        return 1;
    }

    cout << "Enter the annual interest rate (in percentage): ";
    cin >> annualInterestRate;
    if (!validateInput(annualInterestRate)) {
        cerr << "Invalid annual interest rate. Must be greater than zero." << endl;
        return 1;
    }

    cout << "Enter the loan term (in years): ";
    cin >> years;
    if (!validateInput(years)) {
        cerr << "Invalid loan term. Must be greater than zero." << endl;
        return 1;
    }

    cout << "Choose repayment option (1 for Equal Principal, 2 for Equal Installments): ";
    cin >> repaymentOption;
    if (repaymentOption != 1 && repaymentOption != 2) {
        cerr << "Invalid option. Choose 1 or 2." << endl;
        return 1;
    }

    cout << "Choose repayment frequency (12 for monthly, 4 for quarterly, 2 for semi-annually): ";
    cin >> frequency;
    if (frequency != 12 && frequency != 4 && frequency != 2) {
        cerr << "Invalid frequency. Choose 12, 4, or 2." << endl;
        return 1;
    }

    double payment;
    if (repaymentOption == 1) {
        payment = calculateEqualPrincipalPayment(principal, annualInterestRate, years);
        cout << "Your average payment (Equal Principal) is: $" << payment << endl;
    } else if (repaymentOption == 2) {
        payment = calculateEqualInstallmentsPayment(principal, annualInterestRate, years);
        cout << "Your monthly payment (Equal Installments) is: $" << payment << endl;
    }

    // Calculate total payment and total interest
    int totalPeriods = years * frequency;
    double totalPayment = payment * totalPeriods;
    double totalInterest = totalPayment - principal;

    cout << "Total payment over the life of the loan: $" << totalPayment << endl;
    cout << "Total interest paid: $" << totalInterest << endl;

    // Generate repayment schedule
    generateRepaymentSchedule(principal, annualInterestRate, years, frequency);

    return 0;
}

扩展说明

  1. 输入验证

    • validateInput 函数检查输入值是否大于零,确保输入的有效性。
  2. 还款方式

    • 等额本金还款:使用 calculateEqualPrincipalPayment 函数计算平均每月还款额。
    • 等额本息还款:使用 calculateEqualInstallmentsPayment 函数计算每月还款额。
  3. 还款频率

    • 通过用户选择的频率(例如每月、每季度、每半年)来调整还款计划。
  4. 详细的还款计划表

    • generateRepaymentSchedule 函数生成详细的还款计划,包括每期的本金支付、利息支付和总支付额。

编译和运行

  1. 保存代码

    • 将代码保存为一个C++源文件,例如 advanced_loan_calculator.cpp
  2. 编译代码

    • 使用C++编译器进行编译,例如使用 g++ 编译器:
      g++ -o advanced_loan_calculator advanced_loan_calculator.cpp
      
  3. 运行程序

    • 执行编译后的程序:
      ./advanced_loan_calculator
      

这个扩展后的程序提供了多种还款方式和详细的还款计划表,可以帮助用户更好地了解其贷款的还款情况。


http://www.kler.cn/a/293439.html

相关文章:

  • 成都睿明智科技有限公司解锁抖音电商新玩法
  • 边缘的检测
  • 深度学习——优化算法、激活函数、归一化、正则化
  • 实现一个BLE HID鼠标
  • Elasticsearch可视化工具Elasticvue插件用法
  • 知识库管理系统:企业数字化转型的加速器
  • C++---由优先级队列认识仿函数
  • 《OpenCV计算机视觉》—— 图像形态学(腐蚀、膨胀等)
  • OpenGL GLFW OIT 实现
  • javaEE-多线程(3)
  • 亿佰特-NT1/NT1-B串口转RJ45以太网模块
  • python 实现newton raphson牛顿-拉夫森算法
  • 在Go语言中,不同类型之间转换的一些主要方法:
  • [数据集][目标检测]灭火器检测数据集VOC+YOLO格式3255张1类别
  • Java设计模式【备忘录模式】-行为型
  • 鸿蒙系统之ArkTs布局组件
  • Ansible在CentOS下批量部署Nginx到Kubernetes集群
  • 认识meson 的使用
  • 卷积神经网络(一)
  • 大端模式和小端模式
  • linux安装composer
  • 关于Vscode的vscode-cpptools中的icph占用c盘内存过大问题
  • 分享一个使用智狐联创AI平台创建的WordPress分类广告插件
  • GDB快捷键问答之04 怎样查看内存块
  • 【学术会议征稿】2024年第十届机械制造技术与工程材料国际学术会议(ICMTEM 2024)
  • Datawhale X 李宏毅苹果书 AI夏令营 入门 Task3-机器学习框架