-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInflation.h
More file actions
108 lines (107 loc) · 2.77 KB
/
Copy pathInflation.h
File metadata and controls
108 lines (107 loc) · 2.77 KB
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
#pragma once
#include <map>
#include <array>
#include <fstream>
#include <string>
#include <vector>
namespace Hugh
{
struct InflationTable
{
std::map<int, std::array<double, 13>> Mapping;
std::vector<double> Percents;
std::vector<double> YearMappings;
public:
using iterator = std::vector<double>::iterator;
using riterator = std::vector<double>::reverse_iterator;
InflationTable(std::string table)
{
std::ifstream file(table);
if (!file.is_open())
{
throw std::exception("Stuff");
}
while (!file.eof())
{
int year;
file >> year;
std::array<double, 13> months;
file.get();
for (int i = 0; i < months.size(); i++)
{
double val;
file >> val;
file.get();
months[i] = val;
}
Mapping[year] = months;
}
YearMappings.resize(Mapping.size());
auto it = Mapping.begin();
for (int i = 0; i < Mapping.size() && it != Mapping.end(); i++, it++)
{
YearMappings[i] = it->second[12];
}
Percents.resize(Mapping.size());
Percents[0] = 0;
auto begin = Mapping.begin();
auto begin2 = Mapping.begin();
std::advance(begin2, 1);
auto end = Mapping.end();
for (int i = 1; i < Mapping.size() && begin2 != end; i++, begin++, begin2++)
{
Percents[i] = ((begin2->second[12] - begin->second[12]) / begin->second[12]) * 100;
}
}
iterator begin()
{
return YearMappings.begin();
}
iterator end()
{
return YearMappings.end();
}
auto crbegin() -> decltype(YearMappings.crbegin())
{
return YearMappings.crbegin();
}
auto crend() -> decltype(YearMappings.crbegin())
{
return YearMappings.crend();
}
size_t size()
{
return Mapping.size();
}
inline double at(int year)
{
return Mapping[year][12];
}
double operator[](int year)
{
return YearMappings[year];
}
friend std::ostream &operator<<(std::ostream &stream, const InflationTable &table)
{
for (auto v : table.Mapping)
{
stream << v.first;
for (auto va : v.second)
{
stream << "," << va;
}
stream << std::endl;
}
return stream;
}
double getYearReturnPercent(int year)
{
return YearMappings[year];
}
double CalculateInflation(int startYear, int endYear, double amount)
{
double inflate = (Mapping[startYear][12] - Mapping[endYear][12]) / Mapping[startYear][12];
return amount * inflate;
}
};
}; // namespace Hugh