用复利计算年终余额和年终利息

Calculating year end balance and year end interest with compound interest

我正在尝试创建一个函数,根据当年累积的每月存款和每月利息计算年终余额和年终利息。由于某种原因,它没有输出正确的数字。每当我输入 1 用于投资,50 用于每月存款,%5 用于年利率,5 用于年限。它应该输出:

年终余额年终利息

1 617.55 美元 16.55 美元

2 1265.65 美元 48.10 美元

3 1946.90 美元 81.25 美元

4 2663.01 美元 116.11 美元

5 3415.76 美元 152.75 美元

相反,它正在输出:

年终余额年终利息

1 600.76 美元 0.16 美元

2 1205.65 美元 0.63 美元

3 1817.61 美元 1.39 美元

4 2440.90 美元 2.45 美元

5 3078.92 美元 3.82 美元

有人可以帮忙处理函数 reportWithMonthlyPay() 吗?

这是我目前的代码:

// BankingApp.cpp : This file contains the 'main' function. Program execution begins and 
ends there.
//

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

class Bank {
public: 
    void dataInput();
    void displayInput();
    void reportWithoutMonthlyPay();
    void reportWithMonthlyPay();

private:
    double investment;
    double deposit;
    int years;
    double interest;
    double monthly_interest;
    double monthly_balance;
    double year_end_balance;
    double year_end_interest;
    vector<int> month_numbers;
    vector<int> year_numbers;
};

void Bank::dataInput() {
    cout << "Initial Investment Amount: " << endl;
    cin >> investment;
    cout << "Monthly Deposit: " << endl;
    cin >> deposit;
    cout << "Annual Interest: " << endl;
    cin >> interest;
    cout << "Number of years: " << endl;
    cin >> years;
    system("pause");       // Windows-specific command that tells 
platform to pause 
program
    cout << endl;
    if (cin.get()) {        // If any key is entered, displayInput 
 function is called
        displayInput();     
    }

}

void Bank::displayInput() {
    cout << "Initial Investment Amount: " << "$" << investment << 
 endl;
    cout << "Monthly Deposit: " << "$" << deposit << endl;
    cout << "Annual Interest: " << interest << "%" << endl;
    cout << "Number of years: " << years << endl;
    system("pause");
    cout << endl;
    reportWithoutMonthlyPay();
   }

  void Bank::reportWithoutMonthlyPay() {
    year_numbers.resize(years);
    interest = interest / 100;
    year_end_balance = investment + (investment * interest);
    year_end_interest = investment * interest;
     cout << "   Balance and Interest Without Additional Monthly 
 Deposit   " << endl;
    cout << "=============================================================" << 
  endl;
    cout << "Year    Year End Balance    Year End Earned Interest" 
  << endl;
    cout << "-------------------------------------------------------------" << endl;
    for (int i = 0; i < year_numbers.size(); i++) {
        year_numbers[i] = i + 1; 
        cout << year_numbers[i] << "        $" << fixed << 
  setprecision(2) << 
    year_end_balance << "               $" << year_end_interest << 
  endl;
        year_numbers[i]++;
        year_end_balance += year_end_interest;
        year_end_interest = year_end_balance * interest;
    }
    cout << endl;
    system("pause");
    reportWithMonthlyPay();
}

 void Bank::reportWithMonthlyPay() {
 year_numbers.resize(years);
 month_numbers.resize(12);
 year_end_interest = 0;
 year_end_balance = investment;
  cout << "   Balance and Interest With Additional Monthly Deposit   
  " << endl;
   cout << 
  "==========================================================" << 
 endl;
   cout << "Year     Year End Balance   Year End Earned Interest" 
  << endl;
   cout << "------------------------------------------------------- 
---" << endl;
   for (int i = 0; i < year_numbers.size(); i++) {
       year_numbers[i] = i + 1;
       for (int j = 0; j < month_numbers.size(); j++) {
        year_end_balance += deposit;
        monthly_interest = year_end_balance * (interest / 
(double)12);
        year_end_balance += monthly_interest;
        year_end_interest += monthly_interest;
        month_numbers[j]++;
    }
    cout << year_numbers[i] << "        $" << fixed << 
   setprecision(2) << year_end_balance << "               $" << 
   year_end_interest << endl;
    year_numbers[i]++;
   }

int main()
{
    Bank userInput;

    userInput.dataInput();
}

好的,好吧,我 运行 你的代码和它到处都是。输入问题中输入的 1,50,5,5 不会进行任何每月付款处理,因为它只调用 reportWithoutMonthlyPay

如果我更改代码以调用 reportWithMonthlyPay,则输出与您显示的完全不同。然后我固定了一行,输出是 close

            monthly_interest = year_end_balance * (interest /  (double)1200);

在 reportWithMonthy 中。即按比例减少 100.

输出是

Year     Year End Balance   Year End Earned Interest
-------------------------------------------------------  -- -
1        7.55               .55
2        65.65               .65
3        46.90               5.90
4        63.01               2.01
5        15.76               4.76

仍然不像您给出的预期输出,但更接近。我留给你找出原因

OK 也破解了。对于 reportWithMonthlyPay 中的每年循环,您需要将 year_end_interes 重置为 0。当我这样做时

==========================================================
Year     Year End Balance   Year End Earned Interest
-------------------------------------------------------  -- -
1        7.55               .55
2        65.65               .10
3        46.90               .25
4        63.01               6.11
5        15.76               2.75

所以作业对我来说是A