C++ 中的 CSV 文件管理(特别是设置标头和每个标头将分别具有的值)

csv file management in c++ (specifically setting headers and the values that each header will have respectively)

本文关键字:文件管理 CSV 中的 特别是 C++ 设置      更新时间:2023-10-16

所以我会发布我的代码,然后在下面提出我的疑问,这不是作业,我正在练习我表弟的旧作业,所以请冷静下来,因为这是家庭作业。

#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打开文件后记。你会看到你的桌子。当然,您也可以使用记事本++打开。

我还更新了一些编程风格问题。

请看

#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 OKnn";
// 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 << "nnMonthly payment will be:  " << monthly << "nn Resulting table:nn";
// 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) << "Finaln";
// 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
<< "nnFor loan of:           " << overallPaidBack << "n";
}
else {
std::cerr << "**** Error. File '" << fileName << "' could not be opened.n";
}
}
return 0;
}