c++ - c++ 中的 csv 文件管理(具体设置标题和每个标题将分别具有的值)
问题描述
所以我会发布我的代码,然后在下面问我的疑问,这不是作业,我正在练习我表弟的旧作业,所以请冷静一下,因为这是作业,所以不帮助你。
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
class Loan{
public:
Loan();
float principle;
float months;
float interest;
float monthly_payments();
float accrued();
float remaining_bal();
};
float Loan::monthly_payments() {
float payment;
float j = interest/12;
payment = j * principle /(1 - pow((1 + j), -1 * months));
return payment;
}
Loan::Loan() {
}
float Loan::accrued() {
float j = interest/12;
float accrued = j*principle;
return accrued ;
}
float Loan::remaining_bal() {
float x = monthly_payments();
float y = accrued();
float remaining = principle - x + y;
return remaining;
}
int main() {
char filename[16]; //this will allow the file name be 16 in length
cout << "Enter the name of an existing file: ";
cin.getline (filename,16);
cout << "\nThe name of your file is: " << filename << endl;
ofstream myFile; // this will allow for file to be saved
myFile.open (filename); // open file named: myFile
if( myFile.good() )
cout << "\nFile open is OK \n";
else
cout << "\nfile open FAILED \n";
Loan l1;
cout<<"Enter principle, months and interest rate"<<endl;
cin>>l1.principle>>l1.months>>l1.interest;
float x = l1.principle;
float y = l1.months;
float z = l1.interest;
float accrued = l1.accrued();
float monthly = l1.monthly_payments();
float remaining = l1.remaining_bal();
while (remaining > 0){
myFile<<"month"<<"beginning balance"<<"monthly interest"<<"accrued interest"<<"final balance";
}
return 0;
}
这就是我到目前为止所拥有的,现在我想要上面的列,并以 csv 文件的形式分别将值添加到每个列中,棘手的部分是这些值在每次迭代时都会更新,所以我坚持如何从这里开始。任何帮助将不胜感激。
谢谢!
解决方案
你在公式中犯了一些错误。
要获取下个月的值,您需要计算新月份的值(基于当前值),然后继续计算。
不幸的是,在我看来,您的结构或一般方法中也存在设计问题。但我尽可能地保留了你的原始设计。请尝试重构。
我更正了公式。
而且,我添加了循环的值。这些现在包括一些计算,以获得本月的当前值。然后我们把剩余的余额作为当前余额。然后我们继续运行循环。
为了统计目的,我还做了一些积累。
请尝试一下,并输入扩展名为“.csv”的文件名。然后您可以使用 Excel 打开文件后记。你会看到你的桌子。当然你也可以用notepad++打开。
我还更新了一些编程风格问题。
请参见
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
class Loan {
public:
Loan() {}
long double principal;
long double months;
long double interest;
long double monthlyPayment();
long double accrued(long double currentBalance);
long double remainingBalance(long double currentBalance);
};
inline long double Loan::monthlyPayment() {
const long double interestPerMonth = interest / 100.0 / 12.0;
const long double temp = std::pow((1.0 + interestPerMonth), months);
return principal / ((temp - 1) / (interestPerMonth * temp));
}
long double Loan::accrued(long double currentBalance) {
long double j = interest / 12 / 100;
long double accrued = j * currentBalance;
return accrued;
}
long double Loan::remainingBalance(long double currentBalance) {
long double x = monthlyPayment();
long double y = accrued(currentBalance);
long double remaining = currentBalance - x + y;
return remaining;
}
int main() {
// Get filename
std::cout << "Enter the name of an existing file: ";
if (std::string fileName; std::cin >> fileName) {
// Show fielname again
std::cout << "\nThe name of your file is: " << fileName << "\n";
// Try to pen file and check, if it could be opened
if (std::ofstream myFile(fileName); myFile) {
// Give positive feedback
std::cout << "\nFile open is OK\n\n";
// Define structure
Loan l1;
// Get data from user
std::cout << "Enter principal, months and interest rate" << std::endl;
std::cin >> l1.principal >> l1.months >> l1.interest;
// Calculate the monthly rate
long double monthly = l1.monthlyPayment();
// Show some statistics
std::cout << "\n\nMonthly payment will be: " << monthly << "\n\n Resulting table:\n\n";
// We want to sum up some values for further statistics
long double overallInterest = 0.0;
long double overallRate = 0.0;
long double overallPaidBack = 0.0;
// Display header
myFile << std::right << std::setw(11) << "Month;" << std::setw(16) << "Balance;" << std::setw(11) << "Rate;" <<
std::setw(11) << "Interest;" << std::setw(11) << "Paid;" << std::setw(16) << "Final\n";
// Get initial values for calculations
long double currentBalance = l1.principal;
unsigned int month = 1;
// float has always rounding errors. We ignore everything below 0.1
while (currentBalance > 0.1) {
// The interes rate for this month
long double accrued = l1.accrued(currentBalance);
// Overall, for statistics
overallInterest += accrued;
// What is the balance after this month
long double remainingBalance = l1.remainingBalance(currentBalance);
// Output values in CSV format
myFile << std::right << std::setw(10) << month << ";" << std::setw(15) << currentBalance <<
";" << std::setw(10) << monthly << ";" << std::setw(10) << accrued << ";" << std::setw(10) << monthly - accrued
<< ";" << std::setw(15) << remainingBalance << "\n";
// Set start point for next month
currentBalance = remainingBalance;
// Now we will look at next months
++month;
// Calculate statistical values
overallRate += monthly;
overallPaidBack += (monthly - accrued);
}
// Show some statistics on sceen
std::cout << "\nOverall paid interest: " << overallInterest << "\nOverall paid rates: " << overallRate
<< "\nOverall paid: " << overallInterest + overallRate
<< "\n\nFor loan of: " << overallPaidBack << "\n";
}
else {
std::cerr << "**** Error. File '" << fileName << "' could not be opened.\n";
}
}
return 0;
}
推荐阅读
- mysql - SQL:我的查询中出现错误 1064,我找不到原因
- php - 重新验证响应未提交
- apache-spark - Spark 中的 CachedBatch 是什么?
- ios - 如何在我的控制器中快速添加过渡动画?
- android - 进度条 setProgress 在 UI 线程上不起作用
- javascript - 在材料表单元格角度切换此元素
- java - 无法使用带有 Apache 2.4 FOP 的 Spring boot 2.1.6.RELEASE 编译应用程序
- android - 如何删除约束布局中视图之间的额外空间?
- android - Android 应用程序仅在某些设备中崩溃,其中大部分是华为
- r - 如何获取未选择的行号?