-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject(with csv file).cpp
More file actions
311 lines (262 loc) · 7.03 KB
/
Copy pathproject(with csv file).cpp
File metadata and controls
311 lines (262 loc) · 7.03 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
template<typename T>
class Node {
public:
T data1;
T data2;
T data3;
Node* next;
Node()
{
data1 = T();
data2 = T();
data3 = T();
next = nullptr;
}
Node(const T& d1, const T& d2, const T& d3, Node* n)
{
data1 = d1;
data2 = d2;
data3 = d3;
next = n;
}
};
template<typename T>
class MyLinkedList {
private:
Node<T>* head = nullptr;
int size = 0;
public:
bool isEmpty() const
{
return head == nullptr;
}
void pushBack(Node<T>* newNode)
{
if (isEmpty())
head = newNode;
else
{
Node<T>* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
}
size++;
}
void clear()
{
while (!isEmpty())
{
Node<T>* temp = head;
head = head->next;
delete temp;
size--;
}
if (size != 0)
throw out_of_range("There is an error in clear function");
}
// Destructor
~MyLinkedList()
{
clear();
}
void print() const
{
Node<T>* temp = head;
while (temp)
{
cout << temp->data1 << " " << temp->data2 << " " << temp->data3 << endl;
temp = temp->next;
}
cout << endl;
}
void print(ofstream& fout) const
{
fout << head->data1 << " " << head->data2 << " " << head->data3 << endl;
}
void popFront()
{
if (isEmpty())
{
cout << "The list is already empty." << endl;
return;
}
Node<T>* temp = head;
head = head->next;
if (temp)
{
delete temp;
size--;
}
else
{
cout << "Error: Attempted to pop from an empty list." << endl;
}
}
int findHeartBeat() const {
if (isEmpty()) {
throw out_of_range("Not enough elements in the list to calculate BPM.");
}
Node<T>* current = head;
if (current->next == nullptr)
return -1;
Node<T>* nextNode = current->next;
float bpm = 60.0 / (nextNode->data3 - current->data3);
if (bpm <= 60)
return 1;
else if (bpm > 60 && bpm < 100)
return 2;
else
return 3;
}
void sepWrite(ofstream& brady, ofstream& normal, ofstream& tachy)
{
if (!brady.is_open() || !tachy.is_open() || !normal.is_open())
{
cout << "An Error occured in sepWrite() function while opening files" << endl;
}
else
{
cout << "Files opened successfully" << endl;
int last = 0;
while (size>1)
{
switch (findHeartBeat())
{
case 1:
print(brady);
last = 1;
break;
case 2:
print(normal);
last = 2;
break;
case 3:
print(tachy);
last = 3;
break;
}
popFront();
}
switch (last)
{
case 1:
print(brady);
break;
case 2:
print(normal);
break;
case 3:
print(tachy);
break;
}
popFront();
clear();
}
}
void writeToList(ifstream& file)
{
if (!file.is_open())
{
cout << "An Error occured in writeToList function while opening files" << endl;
}
else
{
cout << "File opened successfully" << endl;
string line;
while (getline(file, line))
{
stringstream inputString(line);
float data1, data2, data3;
string tempString;
getline(inputString, tempString, ';');
data1 = atof(tempString.c_str());
getline(inputString, tempString, ';');
data2 = atof(tempString.c_str());
getline(inputString, tempString, ';');
data3 = atof(tempString.c_str());
Node<float>* temp = new Node<float>(data1, data2, data3, nullptr);
pushBack(temp);
}
}
}
void writeToFile(ofstream& file)
{
if(!file.is_open())
throw out_of_range("An Error occured in writeToFile function while opening files");
while (!isEmpty())
{
print(file);
popFront();
}
clear();
}
void combine(ofstream& combinedfile, ifstream& file1, ifstream& file2)
{
if (!combinedfile.is_open() || !file1.is_open() || !file2.is_open())
{
cout << "An Error occured in combine function while opening files" << endl;
}
writeToList(file1);
writeToFile(combinedfile);
combinedfile << "***************************" << endl;
writeToList(file2);
writeToFile(combinedfile);
clear();
}
};
int main()
{
MyLinkedList<float> list;
ifstream file1("person1-TEST.csv");
list.writeToList(file1);
list.print();
file1.close();
ofstream brady1("person1.bradycardia.txt");
ofstream normal1("person1.normal.txt");
ofstream tachy1("person1.tachycardia.txt");
list.sepWrite(brady1, normal1, tachy1);
brady1.close();
normal1.close();
tachy1.close();
list.clear();
cout << endl << endl;
ifstream file2("person2-TEST.csv");
list.writeToList(file2);
list.print();
file2.close();
ofstream brady2("person2.bradycardia.txt");
ofstream normal2("person2.normal.txt");
ofstream tachy2("person2.tachycardia.txt");
list.sepWrite(brady2, normal2, tachy2);
brady2.close();
normal2.close();
tachy2.close();
ofstream combinedBrady("Bradycardia-Person-1-2.txt");
ofstream combinedNormal("Normal-Person-1-2.txt");
ofstream combinedTachy("Tachycardia-Person-1-2.txt");
ifstream person1Brady("person1.bradycardia.txt");
ifstream person2Brady("person2.bradycardia.txt");
list.combine(combinedBrady, person1Brady, person2Brady);
combinedBrady.close();
person1Brady.close();
person2Brady.close();
ifstream person1Normal("person1.normal.txt");
ifstream person2Normal("person2.normal.txt");
list.combine(combinedNormal, person1Normal, person2Normal);
combinedNormal.close();
person1Normal.close();
person2Normal.close();
ifstream person1Tachy("person1.tachycardia.txt");
ifstream person2Tachy("person2.tachycardia.txt");
list.combine(combinedTachy, person1Tachy, person2Tachy);
combinedTachy.close();
person1Tachy.close();
person2Tachy.close();
return 0;
}