Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <cstdint>
  6. using namespace std;
  7.  
  8. struct Money
  9. {
  10. int64_t cents;
  11.  
  12. inline Money& operator+=(Money m)
  13. {
  14. cents += m.cents;
  15. return *this;
  16. }
  17.  
  18. inline Money& operator-=(Money m)
  19. {
  20. cents -= m.cents;
  21. return *this;
  22. }
  23.  
  24. inline Money& operator*=(int64_t multiplier)
  25. {
  26. cents *= multiplier;
  27. return *this;
  28. }
  29.  
  30. inline Money& operator/=(int64_t divisor)
  31. {
  32. cents /= divisor;
  33. return *this;
  34. }
  35. };
  36.  
  37. constexpr Money operator-(Money money)
  38. {
  39. return {-money.cents};
  40. }
  41.  
  42. constexpr Money operator+(Money a, Money b)
  43. {
  44. return {a.cents + b.cents};
  45. }
  46.  
  47. constexpr Money operator-(Money a, Money b)
  48. {
  49. return {a.cents - b.cents};
  50. }
  51.  
  52. constexpr Money operator*(Money a, int64_t b)
  53. {
  54. return {a.cents * b};
  55. }
  56.  
  57. constexpr Money operator/(Money a, int64_t b)
  58. {
  59. return {a.cents / b};
  60. }
  61.  
  62. constexpr bool operator==(Money a, Money b)
  63. {
  64. return a.cents == b.cents;
  65. }
  66.  
  67. constexpr bool operator!=(Money a, Money b)
  68. {
  69. return a.cents != b.cents;
  70. }
  71.  
  72. constexpr bool operator<(Money a, Money b)
  73. {
  74. return a.cents < b.cents;
  75. }
  76.  
  77. constexpr bool operator<=(Money a, Money b)
  78. {
  79. return a.cents <= b.cents;
  80. }
  81.  
  82. constexpr bool operator>(Money a, Money b)
  83. {
  84. return a.cents > b.cents;
  85. }
  86.  
  87. constexpr bool operator>=(Money a, Money b)
  88. {
  89. return a.cents >= b.cents;
  90. }
  91.  
  92. ostream& operator<<(ostream& stream, Money money)
  93. {
  94. uint64_t cents = money.cents;
  95.  
  96. if (money.cents < 0)
  97. {
  98. stream << '-';
  99. cents = -money.cents;
  100. }
  101.  
  102. uint64_t dollars = cents / 100;
  103. cents -= dollars * 100;
  104.  
  105. stream << dollars << '.';
  106.  
  107. if (cents < 10) stream << '0';
  108.  
  109. return stream << cents;
  110. }
  111.  
  112. struct Loan
  113. {
  114. Money principal;
  115. int64_t interest; // 1 = 0.01%
  116. Money minPayment;
  117. };
  118.  
  119. void RunSimulation(vector<Loan> loans, Money monthlyIncome)
  120. {
  121. static int fileId = 0;
  122. stringstream ss;
  123. ss << "loan_" << ++fileId << ".txt";
  124.  
  125. ofstream fout(ss.str(), ofstream::binary);
  126.  
  127. int monthCount = 0;
  128. Money totalPaid{0};
  129.  
  130. Money minMonthlyIncome{0};
  131. for (auto loan : loans) minMonthlyIncome += loan.minPayment;
  132.  
  133. if (monthlyIncome < minMonthlyIncome)
  134. {
  135. cout << '$' << monthlyIncome << " is not enough. Minimum income is $"
  136. << minMonthlyIncome << ".\n";
  137.  
  138. monthlyIncome = minMonthlyIncome;
  139. }
  140.  
  141. bool keepPaying = true;
  142. int n = 0;
  143.  
  144. while (keepPaying)
  145. {
  146. auto income = monthlyIncome;
  147. ++monthCount;
  148.  
  149. // Pay minimum amounts.
  150.  
  151. fout << "Month " << monthCount << '\n';
  152.  
  153. for (auto& loan : loans)
  154. {
  155. loan.principal -= loan.minPayment;
  156. income -= loan.minPayment;
  157. totalPaid += loan.minPayment;
  158.  
  159. if (loan.principal.cents < 0)
  160. {
  161. income -= loan.principal;
  162. totalPaid += loan.principal;
  163. loan.principal = {0};
  164. }
  165.  
  166. fout << "principal: " << loan.principal << '\n';
  167. }
  168.  
  169. // Apply excess.
  170.  
  171. for (size_t i = loans.size(); i > 0 && income.cents > 0; --i)
  172. {
  173. auto& loan = loans[i - 1];
  174. loan.principal -= income;
  175. totalPaid += income;
  176.  
  177. if (loan.principal.cents < 0)
  178. {
  179. income -= loan.principal;
  180. totalPaid += loan.principal;
  181. loan.principal = {0};
  182. }
  183. }
  184.  
  185. // Apply interest.
  186.  
  187. keepPaying = false;
  188.  
  189. for (auto& loan : loans)
  190. {
  191. auto interest = loan.principal * loan.interest / 120000;
  192. fout << "interest: " << interest << '\n';
  193. keepPaying |= loan.principal.cents > 0;
  194. loan.principal += interest;
  195. }
  196. }
  197.  
  198. cout << "paid $" << totalPaid << " over " << monthCount << " months\n";
  199. fout.close();
  200. }
  201.  
  202. int main(int argc, char** argv)
  203. {
  204. vector<Loan> loans;
  205. loans.push_back({{2000000}, 800, {19113}});
  206. loans.push_back({{200000}, 800, {4055}});
  207. loans.push_back({{500000}, 1800, {12697}});
  208. cout << "HIL first\n";
  209. RunSimulation(loans, {});
  210.  
  211. loans.clear();
  212. loans.push_back({{2000000}, 800, {19113}});
  213. loans.push_back({{500000}, 1800, {12697}});
  214. loans.push_back({{200000}, 800, {4055}});
  215. cout << "LPL first\n";
  216. RunSimulation(loans, {});
  217.  
  218. return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement