存个利息代码
This post is not yet available in English. Showing the original version.
January 19, 2025
这只是一个存档,用来存储我的支付宝的小荷包的计划收益
目前想的是一天1元开始0.03元累加的稳定存储,先这样开始吧,运行代码看看一年后200元/分的收益+每个月200的固定存储+每日存储看看一年后能存多少钱。
啊,如果有什么奖金啊我也会存进去的,看看能不能攒出一个switch2或者其他电子产品什么的… 或者明年买GTA6的钱?或者下一张GIDLE专辑的钱? 先一年开始吧,留点钱比什么都有用。
把函数封装了一下,扔到一个小仓库了。 用cmake构建的小项目,以下大概就是新改动吧。
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;
}
函数库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;
}