Back to Blog

Saving an Interest Calculator

Just an Archive for My Planned Returns in Alipay Xiaohebao (小荷包)

For now, the idea is to start by saving one yuan a day and increase it by 0.03 yuan each day. Let's begin there. Run the code and see how much I can save in a year, with one fen of returns per 200 yuan, a fixed 200-yuan deposit each month, and the daily deposits.

Oh, if I win any prize money, I'll put that in too. Let's see whether I can save enough for a Switch 2 or some other gadget...
Or the money for GTA6 next year? Or the next GIDLE album?
Start with a year. Having a little money put aside is more useful than anything.


I wrapped it up in functions and threw it into a little repository.
It's a small CMake project. These are roughly the latest changes.

main.cpp

#include <iostream>
#include "simulate.h"
using namespace std;

int main()
{
    //system("chcp 65001");
    double initial_input = 1.33;                    // 初始存入金额
    double total_sum = 3600;                        // 总存款
    double per_rate = 0.03;                         // 每月递增比例
    double monthly_expense = 2000 - 150 - 200;      // 每月固定支出(生活费、订阅费等)
    double daily_food_expense = (5 + 20 + 20) * 30; // 每月餐费支出
    printf("初始存入金额为 %.2f, 每月递增比率为 %.2f\n\n", initial_input, per_rate);
    simulateYear(initial_input, total_sum, per_rate, monthly_expense, daily_food_expense);
    return 0;
}

Function library: simulate.h

#include "simulate.h"
#include <iostream>
#include<cmath>
using namespace std;

void simulateYear(double initial_input, double total_sum, double per_rate, double monthly_expense, double daily_food_expense)
{
    int month = 2;            // 当前月份
    double monthly_input = 0; // 本月存款
    double remain = 0;        // 剩余余额
    double package_interest = 0;
    double package_monthly_interest = 0;

    // 模拟365天
    for (int i = 0; i < 365; i++)
    {
        simulateDay(total_sum, monthly_input, initial_input, per_rate, package_interest, package_monthly_interest);

        // 每个月结束后,计算剩余余额
        if (i % 30 == 0 && i != 0)
        {
            simulateMonth(total_sum, monthly_input, monthly_expense, daily_food_expense, remain, package_monthly_interest, month);
            month++;
            monthly_input = 0; // 重置本月存款
        }
    }

    // 打印最终结果
    printf("小荷包带来的收益为: %.2f\n", package_interest);
    printf("本年度未存款的可支配的支出为: %.2f\n", 2000 * 12 - total_sum);
    printf("每日存入金额最终为: %.2f\n", initial_input);
    printf("最终剩余余额为: %.2f\n", remain);
}

void simulateDay(double &total_sum, double &monthly_input, double &initial_input, double per_rate, double &package_interest, double &package_monthly_interest)
{
    total_sum += initial_input;     // 总存款累计
    monthly_input += initial_input; // 本月存款累计
    initial_input += per_rate;      // 增加递增金额
    // 计算收益:每200元存款产生0.01元收益
    double current_interest = floor(total_sum / 200) * 0.01; // 满200元的部分计算收益
    total_sum += current_interest;                           // 收益加到存款里
    monthly_input += current_interest;
    package_interest += current_interest;
    package_monthly_interest += current_interest;
}

void simulateMonth(double &total_sum, double &monthly_input, double monthly_expense, double daily_food_expense, double &remain, double &package_monthly_interest, int month)
{
    total_sum += 200;                                                             // 每月存入两百
    double current_remain = monthly_expense - monthly_input - daily_food_expense; // 当前月剩余金额
    remain += current_remain;                                                     // 累计剩余余额
    cout << "第 " << month << " 个月的存入金额为: " << monthly_input << " 本月所有存入总额为: " << monthly_input << endl;
    printf("本月餐费为 %.2f\n", monthly_expense - monthly_input);
    printf("剩余余额为 %.2f\n", remain);
    printf("本月收益为 %.2f\n", package_monthly_interest);
    printf("目前存入总金额为 %.2f\n", total_sum + 0.04);
    cout << endl;
    package_monthly_interest = 0;
}


0 / 2000
Loading comments...