-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvestmentAnalysisProgramme.cpp
More file actions
142 lines (125 loc) · 4.09 KB
/
InvestmentAnalysisProgramme.cpp
File metadata and controls
142 lines (125 loc) · 4.09 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
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
// InvestmentAnalysisProgramme.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <chrono>
using namespace std;
bool isExist(string add) {
ifstream file(add);
if (file.is_open()) {
return true;
}
return false;
}
int main()
{
string path; //To save the name of the input file.
string line;
bool readFile = true; //To judge if the input file has been entirely read.
bool process = false; //To judge if users enter the correct file name.
vector<string> words; //To save all original data from input file.
vector<string> investment; //To save all investment data.
vector<double> contribution;
vector<double> profit;
int indexCount = 1; //To save the index of all cells in the input file.
bool formatCheck = false;
double allInvestment = 0;
auto start1 = chrono::steady_clock::now();
auto end1 = chrono::steady_clock::now();
while (!formatCheck) {
process = false;
indexCount = 1;
while (!process) {
cout << "Please enter the path of the input file:" << endl;
cin >> path;
process = isExist(path);
if (!process) {
cout << "Invalid path. Check and enter again!\n" << endl;
}
}
start1 = chrono::steady_clock::now();//Start counter
ifstream file(path);//Open input file
readFile = true;
while (readFile) {
if (indexCount % 4) {
if (getline(file, line, ',')) {
readFile = true;
words.push_back(line);
indexCount++;
}
else {
readFile = false;
formatCheck = true;
}
}
else {
if (getline(file, line)) {
readFile = true;
words.push_back(line);
investment.push_back(line);
indexCount++;
if (indexCount == 5) {
if (!(words[0] == "Investor's name" && words[1] == "Bank account" && words[2] == "Sort code" && words[3] == "Current year's investment(£)")) {
cout << "Your format of file is incorrect. Please check and enter the name of correct input file.\n" << endl;
readFile = false;
words.clear();
investment.clear();
}
}
}
else {
readFile = false;
formatCheck = true;
}
}
}//All original data is read into words. All investment data is read into investment.
file.close();//Close input file
end1 = chrono::steady_clock::now();
}
auto start2 = chrono::steady_clock::now();
vector<string> rows;
for (int i = 0; i < words.size(); i += 4) {
rows.push_back(words[i] + "," + words[i + 1] + "," + words[i + 2] + "," + words[i + 3]);
}//combine every row of original file
for (int i = 1; i < investment.size(); i++) {
allInvestment = allInvestment + stod(investment[i]);
}//calculate the total investment
if (allInvestment > 0) {
for (int i = 1; i < investment.size(); i++) {
contribution.push_back(stod(investment[i]) / allInvestment);
}
for (int i = 0; i < contribution.size(); i++) {
if (stod(investment[i + 1]) > 0) {
profit.push_back(contribution[i] * 0.2 * allInvestment);
}
else {
profit.push_back(0);
}
}
}
else if (allInvestment == 0){
cout << "Sorry, the total investment in your input file is 0. Please check and correct it then run this programme again." << endl;
exit(0);
}
else if (allInvestment < 0) {
for (int i = 1; i < investment.size(); i++) {
contribution.push_back(stod(investment[i]) / allInvestment);
}
for (int i = 0; i < contribution.size(); i++) {
profit.push_back(0);
}
}
ofstream ofile("Outcome.csv");
ofile << rows[0] << ",Contribution of the investor,expected profit" << endl;
for (int i = 1; i < rows.size(); i++) {
ofile << rows[i] << "," << fixed << setprecision(2) << contribution[i - 1] * 100 << "%," << profit[i - 1] << endl;
}
ofile.close();
auto end2 = chrono::steady_clock::now();
std::chrono::duration<double> elapsed_time = (end1 - start1) + (end2 - start2);
cout << fixed << setprecision(8) << "The processing time is " << elapsed_time.count() << " seconds." << endl;
system("pause");
}