1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// How do I read in the data from a file and put it into a two dimensional
// array and them sum all the values in the principle paid and interest paid
// columns and compare them to the running totals I calculated.
#include <cmath>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
struct TableDtls {
double monthly_rate {};
int num_months {};
double monthly_payment {};
double monthly_interest_amount {};
double monthly_principle_amount {};
double running_interest_total {};
double running_principle_total {};
double new_balance {};
double unpaid_balance {};
void setToCalc();
};
void TableDtls::setToCalc()
{
monthly_interest_amount = 0.0;
monthly_principle_amount = 0.0;
running_interest_total = 0.0;
running_principle_total = 0.0;
new_balance = 0.0;
}
struct Loan {
using table = std::tuple<double, double, double, double, double, double, double>;
double lprinc {}, // loan amount
yint {}, // year-based interest rate
term {}; // duration left
TableDtls det;
std::vector<table> amm;
double calcMonthPay();
void calcTable();
bool operator==(const Loan& other) const;
bool operator!=(const Loan& other) const;
friend std::ofstream& operator<<(std::ofstream& os, const Loan& rhs)
{
os << rhs.lprinc << ' ' << rhs.yint << ' ' << rhs.term << '\n';
for(size_t i{}; i<rhs.amm.size(); ++i) {
os << std::right << std::setw(6) << i;
const auto& t = rhs.amm.at(i);
os << std::right << std::setw(22) << std::get<0>(t)
<< std::right << std::setw(20) << std::get<1>(t)
<< std::right << std::setw(17) << std::get<2>(t)
<< std::setw(19) << std::get<3>(t)
<< std::right << std::setw(18) << std::get<4>(t)
<< std::right << std::setw(15) << std::get<5>(t)
<< std::right << std::setw(23) << std::get<6>(t) << '\n';
}
return os;
}
friend std::ostream& operator<<(std::ostream& os, const Loan& rhs)
{
std::cout << "Payment # Mortgage Loan Balance Mortgage Payment "
" Principle Amount Principle Paid Interest Amount "
" Interest Paid New Mortgage Balance\n\n";
os << rhs.lprinc << ' ' << rhs.yint << ' ' << rhs.term << '\n';
for(size_t i{}; i<rhs.amm.size(); ++i) {
os << std::right << std::setw(6) << i;
const auto& t = rhs.amm.at(i);
os << std::right << std::setw(22) << std::get<0>(t)
<< std::right << std::setw(20) << std::get<1>(t)
<< std::right << std::setw(17) << std::get<2>(t)
<< std::setw(19) << std::get<3>(t)
<< std::right << std::setw(18) << std::get<4>(t)
<< std::right << std::setw(15) << std::get<5>(t)
<< std::right << std::setw(23) << std::get<6>(t) << '\n';
}
return os;
}
friend std::ifstream& operator>>(std::ifstream& is, Loan& rhs)
{
int lines {};
for(std::string line; std::getline(is, line); ++lines) {
if(line.empty()) { continue; }
std::istringstream iss(line);
if(!lines) { iss >> rhs.lprinc >> rhs.yint >> rhs.term; continue; }
double a, b, c, d, e, f, g, h; // on the file there are 8 columns
iss >> a >> b >> c >> d >> e >> f >> g >> h;
rhs.amm.push_back(std::make_tuple(b, c, d, e, f, g, h));
}
return is;
}
};
// throws std::out_of_range if denominator result 0
double Loan::calcMonthPay()
{
det.monthly_rate = yint / 12.0;
// calculate year fraction in months:
double ipart {};
double frac = 10.0 * std::modf(term, &ipart);
int coeff = static_cast<int>(std::round(10 / frac));
det.num_months = 12 * ipart + 12 / coeff;
double numerator = det.monthly_rate * pow((1.0 + det.monthly_rate), det.num_months);
double denominator = pow((1.0 + det.monthly_rate), det.num_months) - 1;
if (denominator == 0) { throw std::out_of_range("Denominator is 0!"); }
det.monthly_payment = lprinc * (numerator / denominator);
return det.monthly_payment;
}
void Loan::calcTable()
{
det.unpaid_balance = lprinc;
det.setToCalc();
for (int payment = 1; payment <= det.num_months; payment++) {
det.monthly_interest_amount = det.unpaid_balance * det.monthly_rate;
det.monthly_principle_amount = det.monthly_payment
- det.monthly_interest_amount;
det.new_balance = det.unpaid_balance - det.monthly_principle_amount;
det.running_interest_total += det.monthly_interest_amount;
det.running_principle_total += det.monthly_principle_amount;
amm.push_back(std::make_tuple(det.unpaid_balance,
det.monthly_payment,
det.monthly_principle_amount,
det.running_principle_total,
det.monthly_interest_amount,
det.running_interest_total,
det.new_balance) );
det.unpaid_balance = det.new_balance;
}
}
bool Loan::operator==(const Loan& other) const
{
if(this == &other) { return true; } // check self comparison
if(lprinc != other.lprinc) { return false; }
if(yint != other.yint) { return false; }
if(term != other.term) { return false; }
// data inside det are just needed for computation and are irrelevant
if(amm.size() != other.amm.size()) { return false; }
for(size_t i {}; i < amm.size(); ++i) {
const auto& t1 = amm.at(i);
const auto& t2 = other.amm.at(i);
// choose a reasonable discrepancy here -----------\/
if(std::fabs(std::get<0>(t1) - std::get<0>(t2)) > 0.01) { return false; }
if(std::fabs(std::get<1>(t1) - std::get<1>(t2)) > 0.01) { return false; }
if(std::fabs(std::get<2>(t1) - std::get<2>(t2)) > 0.01) { return false; }
if(std::fabs(std::get<3>(t1) - std::get<3>(t2)) > 0.01) { return false; }
if(std::fabs(std::get<4>(t1) - std::get<4>(t2)) > 0.01) { return false; }
if(std::fabs(std::get<5>(t1) - std::get<5>(t2)) > 0.01) { return false; }
if(std::fabs(std::get<6>(t1) - std::get<6>(t2)) > 0.01) { return false; }
}
return true;
}
bool Loan::operator!=(const Loan& other) const { return !(*this == other); }
|