Repository navigation
Expand file tree
/
Copy pathShareCalculator.cpp
More file actions
397 lines (389 loc) · 12.4 KB
/
Copy pathShareCalculator.cpp
File metadata and controls
397 lines (389 loc) · 12.4 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;
const char SECTION_SEPERATOR=',';
const char USER_SEPERATOR=';';
const char DATA_SEPERATOR=':';
const string SPLIT_EQUALLY="equally";
const string SPLIT_UNEQUALLY="unequally";
const string SPLIT_ADJUSTMENT="adjustment";
typedef pair<string, double> User;
struct Transaction
{
string creditor;
string debtor;
double amount;
};
void GetUsers(string filePath, vector<User> &users)
{
ifstream usersFile;
usersFile.open(filePath);
User user;
user.second = 0;
getline(usersFile, user.first);
while (getline(usersFile, user.first))
{
users.push_back(user);
}
usersFile.close();
}
void GetCosts(string filePath, vector<string> &costTypes, vector<string> &payers, vector<string> &borrowers)
{
ifstream costsFile;
costsFile.open(filePath);
string line;
string token;
while (getline(costsFile, line))
{
stringstream sline(line);
getline(sline, token, SECTION_SEPERATOR);
costTypes.push_back(token);
getline(sline, token,SECTION_SEPERATOR);
payers.push_back(token);
getline(sline, token, SECTION_SEPERATOR);
borrowers.push_back(token);
}
costsFile.close();
}
int findUser(vector<User> &users, string userName)
{
for (int i = 0; i < users.size(); i++)
{
if (users[i].first == userName)
{
return i;
}
}
return -1;
}
double splitPayers(vector<User> &users, string payers)
{
string payerName;
string pricePayed;
double totalCost = 0;
stringstream payersStream(payers);
while (getline(payersStream, payerName, DATA_SEPERATOR))
{
getline(payersStream, pricePayed, USER_SEPERATOR);
users[findUser(users, payerName)].second += stod(pricePayed);
totalCost += stod(pricePayed);
}
return totalCost;
}
bool hasBorrower(vector<string> borrowersName)
{
if (borrowersName.size() != 0)
return true;
return false;
}
void splitBetweenBorrowers(vector<User> &users, vector<string> borrowersName, double &totalCost)
{
double eachShare;
eachShare = floor((totalCost / borrowersName.size()) * 100) / 100;
for (int i = 0; i < borrowersName.size() - 1; i++)
users[findUser(users, borrowersName[i])].second -= eachShare;
users[findUser(users, borrowersName[borrowersName.size() - 1])].second -= totalCost - eachShare * (borrowersName.size() - 1);
}
void splitBetweeenAll(vector<User> &users, double &totalCost)
{
double eachShare;
eachShare = floor((totalCost / users.size()) * 100) / 100;
for (int i = 0; i < users.size() - 1; i++)
users[i].second -= eachShare;
users[users.size() - 1].second -= totalCost - eachShare * (users.size() - 1);
}
void distubuteShares(double &totalCost, vector<User> &users, vector<string> borrowersName)
{
if (hasBorrower(borrowersName))
{
splitBetweenBorrowers(users, borrowersName, totalCost);
}
else
{
splitBetweeenAll(users, totalCost);
}
}
void splitEqually(vector<User> &users, string payers, string borrowers)
{
double eachShare, totalCost = splitPayers(users, payers);
vector<string> borrowersName;
stringstream borrowersStream(borrowers);
string borrower;
while (getline(borrowersStream, borrower, USER_SEPERATOR))
{
borrowersName.push_back(borrower);
}
distubuteShares(totalCost, users, borrowersName);
}
void splitUnequally(vector<User> &users, string payers, string borrowers)
{
splitPayers(users, payers);
stringstream borrowersStream(borrowers);
string borrowerName, borrowerShare;
while (getline(borrowersStream, borrowerName, DATA_SEPERATOR))
{
getline(borrowersStream, borrowerShare, USER_SEPERATOR);
users[findUser(users, borrowerName)].second -= stod(borrowerShare);
}
}
string calculateOverShares(vector<User> &users, double &totalCost, string borrowers)
{
stringstream borrowersStream(borrowers);
string borrowerName, borrowerOvershare;
while (getline(borrowersStream, borrowerName,DATA_SEPERATOR))
{
getline(borrowersStream, borrowerOvershare, USER_SEPERATOR);
users[findUser(users, borrowerName)].second -= stod(borrowerOvershare);
totalCost -= stod(borrowerOvershare);
}
return borrowerName;
}
void adjust(vector<User> &users, string payers, string borrowers)
{
double totalCost = splitPayers(users, payers);
string lastBorrower = calculateOverShares(users, totalCost, borrowers);
double eachShare = floor((totalCost * 100) / users.size()) / 100;
for (int i = 0; i < users.size(); i++)
{
if (users[i].first != lastBorrower)
{
users[i].second -= eachShare;
}
}
users[findUser(users, lastBorrower)].second -= totalCost - (users.size() - 1) * eachShare;
}
void DetermineCostType(vector<User> &users, vector<string> &costTypes, vector<string> &payers, vector<string> &borrowers)
{
for (int i = 0; i < costTypes.size(); i++)
{
if (costTypes[i] == SPLIT_EQUALLY)
splitEqually(users, payers[i], borrowers[i]);
else if (costTypes[i] == SPLIT_UNEQUALLY)
splitUnequally(users, payers[i], borrowers[i]);
else if (costTypes[i] == SPLIT_ADJUSTMENT)
adjust(users, payers[i], borrowers[i]);
}
}
void divideUsers(vector<User> users, vector<User> &creditors, vector<User> &debtors)
{
for (User u : users)
{
if (u.second >= 0)
{
creditors.push_back(u);
}
else
{
debtors.push_back(u);
}
}
}
void sortByMoney(vector<User> &unsortedUsers, int firstUser, int secondUser)
{
if (abs(unsortedUsers[firstUser].second) < abs(unsortedUsers[secondUser].second))
{
User tempUser;
tempUser = unsortedUsers[firstUser];
unsortedUsers[firstUser] = unsortedUsers[secondUser];
unsortedUsers[secondUser] = tempUser;
}
}
void sortbyName(vector<User> &unsortedUsers, int firstUser, int secondUser)
{
for (int letterNum = 0; letterNum < unsortedUsers[firstUser].first.size() && letterNum < unsortedUsers[secondUser].first.size(); letterNum++)
{
if (unsortedUsers[firstUser].first[letterNum] > unsortedUsers[secondUser].first[letterNum])
{
User tempUser;
tempUser = unsortedUsers[firstUser];
unsortedUsers[firstUser] = unsortedUsers[secondUser];
unsortedUsers[secondUser] = tempUser;
break;
}
else if (unsortedUsers[firstUser].first[letterNum] < unsortedUsers[secondUser].first[letterNum])
{
break;
}
}
}
void bubbleSortUsers(vector<User> &unsortedUsers)
{
for (int i = 0; i < unsortedUsers.size(); i++)
{
for (int j = i + 1; j < unsortedUsers.size(); j++)
{
sortByMoney(unsortedUsers, i, j);
if (abs(unsortedUsers[i].second) == abs(unsortedUsers[j].second))
{
sortbyName(unsortedUsers, i, j);
}
}
}
}
void addTransaction(string debtorName, string creditorName, double amount, vector<Transaction> &transactions)
{
Transaction newTransaction;
newTransaction.creditor = creditorName;
newTransaction.debtor = debtorName;
newTransaction.amount = amount;
transactions.push_back(newTransaction);
}
bool containCreditor(vector<User> creditors)
{
for (User u : creditors)
{
if (u.second != 0)
{
return true;
}
}
return false;
}
int findRemainingCreditor(vector<User> creditors)
{
int creditorNum = 0;
while (creditors[creditorNum].second == 0)
{
creditorNum++;
}
return creditorNum;
}
bool hasDebt(vector<User> debtors, int debtorNum)
{
if (floor(abs(debtors[debtorNum].second * 100)) > 0)
{
return true;
}
return false;
}
void clearDebt(vector<User> &debtors, vector<User> &creditors, int creditorNum, vector<Transaction> &transactions, int debtorNum)
{
while (hasDebt(debtors, debtorNum))
{
if (abs(debtors[debtorNum].second) > creditors[creditorNum].second)
{
addTransaction(debtors[debtorNum].first, creditors[creditorNum].first, creditors[creditorNum].second, transactions);
debtors[debtorNum].second += creditors[creditorNum].second;
creditors[creditorNum].second = 0;
creditorNum++;
continue;
}
if (abs(debtors[debtorNum].second) < creditors[creditorNum].second)
{
addTransaction(debtors[debtorNum].first, creditors[creditorNum].first, abs(debtors[debtorNum].second), transactions);
creditors[creditorNum].second -= abs(debtors[debtorNum].second);
debtors[debtorNum].second = 0;
continue;
}
if (abs(debtors[debtorNum].second) == creditors[creditorNum].second)
{
addTransaction(debtors[debtorNum].first, creditors[creditorNum].first, creditors[creditorNum].second, transactions);
debtors[debtorNum].second = 0;
creditors[creditorNum].second = 0;
continue;
}
}
}
vector<Transaction> calculateTransactions(vector<User> &creditors, vector<User> &debtors)
{
vector<Transaction> transactions;
for (int i = 0; i < debtors.size(); i++)
{
int creditorNum = findRemainingCreditor(creditors);
clearDebt(debtors, creditors, creditorNum, transactions, i);
}
return transactions;
}
void sortByAmount(vector<Transaction> &transactions, int firstTran, int secondTran)
{
if (abs(transactions[firstTran].amount) < abs(transactions[secondTran].amount))
{
Transaction tempTransaction;
tempTransaction = transactions[firstTran];
transactions[firstTran] = transactions[secondTran];
transactions[secondTran] = tempTransaction;
}
}
void sortByName(string firstUser, string secondUser, int firstTran, int secondTran, vector<Transaction> &transactions)
{
for (int k = 0; k < firstUser.size() || k < secondUser.size(); k++)
{
if (firstUser[k] > secondUser[k])
{
Transaction tempTransaction;
tempTransaction = transactions[firstTran];
transactions[firstTran] = transactions[secondTran];
transactions[secondTran] = tempTransaction;
break;
}
else if (firstUser[k] < secondUser[k])
{
break;
}
}
}
void sortTransacitons(vector<Transaction> &transactions)
{
for (int i = 0; i < transactions.size(); i++)
{
for (int j = i + 1; j < transactions.size(); j++)
{
if (abs(transactions[i].amount) < abs(transactions[j].amount))
sortByAmount(transactions, i, j);
if (abs(transactions[i].amount) == abs(transactions[j].amount) && transactions[i].debtor == transactions[j].debtor)
sortByName(transactions[i].creditor,transactions[j].creditor,i,j,transactions);
if (abs(transactions[i].amount) == abs(transactions[j].amount) && transactions[i].debtor != transactions[j].debtor)
sortByName(transactions[i].debtor,transactions[j].debtor,i,j,transactions);
}
}
}
void testGetUser(vector<User> users)
{
for (User u : users)
{
cout << u.first << " " << u.second << endl;
}
}
void testGetCosts(vector<string> &costTypes, vector<string> &payers, vector<string> &borrowers)
{
for (int i = 0; i < costTypes.size(); i++)
{
cout << costTypes[i] << endl
<< payers[i] << endl
<< borrowers[i] << endl;
}
}
void print(vector<User> users)
{
for (User u : users)
{
cout << u.first << " " << u.second << endl;
}
}
void printTransactions(vector<Transaction> transactions)
{
for (Transaction t : transactions)
{
cout << t.debtor << " -> " << t.creditor << ": " << t.amount << endl;
}
}
int main(int argc, char *argv[])
{
const string USERF_PATH = argv[1];
const string COSTSF_PATH = argv[2];
vector<User> users, creditors, debtors;
vector<string> costTypes, payers, borrowers;
vector<Transaction> transactions;
GetUsers(USERF_PATH, users);
GetCosts(COSTSF_PATH, costTypes, payers, borrowers);
DetermineCostType(users, costTypes, payers, borrowers);
divideUsers(users, creditors, debtors);
bubbleSortUsers(creditors);
bubbleSortUsers(debtors);
transactions = calculateTransactions(creditors, debtors);
sortTransacitons(transactions);
printTransactions(transactions);
}