-
Notifications
You must be signed in to change notification settings - Fork 0
/
annuity.cpp
84 lines (66 loc) · 2.43 KB
/
annuity.cpp
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <chrono>
#include <stdio.h>
#include <Windows.h>
using namespace std;
#define PERCENTILE 100
enum inputs {
FIXED_AMOUNT = 1,
FIXED_RATE = 2,
FIXED_TIME = 3,
};
class Annuity {
public:
double fixed_amount;
double fixed_interest_rate;
int fixed_time;
};
void printAnnuityDetails(Annuity a) {
cout << setfill(' ') << fixed << setprecision(2);
cout << " ==============================" << endl;
cout << " || ANNUITY (IN CONTRACTS) ||" << endl;
cout << " ==============================" << endl;
cout << " [INTEREST RATE IN PERCENTILE]" << endl;
cout << "\n";
cout << setw(20) << "FIXED AMOUNT" << setw(25) << "FIXED INTEREST RATE" << setw(25) << "FIXED TIME IN YEARS" << endl;
cout << setw(20) << a.fixed_amount << setw(25) << a.fixed_interest_rate << setw(25) << a.fixed_time << endl;
}
void closedFormAnnuity(Annuity a) {
auto begin = chrono::high_resolution_clock::now();
cout << "\nCLOSED FORM ANNUITY: ";
double cfa = 0.0;
a.fixed_interest_rate /= PERCENTILE;
cfa = (a.fixed_amount / a.fixed_interest_rate) * (1 - (1/pow(1 + a.fixed_interest_rate, a.fixed_time)));
cout << cfa << endl;
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cout << "EXECUTED IN: " << elapsed.count() * (1e-6) << " ms" << endl;
cout << "\n";
}
void loopFormAnnuity(Annuity a) {
auto begin = chrono::high_resolution_clock::now();
cout << "LOOP FORM ANNUITY: ";
double lfa = 0.0;
a.fixed_interest_rate /= PERCENTILE;
for(int i = 1; i <= a.fixed_time; i++) {
lfa += (a.fixed_amount / pow((1 + a.fixed_interest_rate), i));
}
cout << lfa << endl;
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cout << "EXECUTED IN: " << elapsed.count() * (1e-6) << " ms" << endl;
cout << "\n";
}
int main(int argc, char* argv[]) {
Annuity a;
a.fixed_amount = atof(argv[FIXED_AMOUNT]);
a.fixed_interest_rate = atof(argv[FIXED_RATE]);
a.fixed_interest_rate /= PERCENTILE;
a.fixed_time = atoi(argv[FIXED_TIME]);
printAnnuityDetails(a);
closedFormAnnuity(a);
loopFormAnnuity(a);
cout << "We have reached the end..." << endl;
}