-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactStar.c
341 lines (299 loc) · 8.97 KB
/
TransactStar.c
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// Calculate min-hop route and display the same
void minimum_Hops(int E[][2], int C[], int n, int m) {
int i, j, hopcount = 0, arr[n], A[10], F[10];
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
arr[i] = E[j][i];
if (n == 1)
hopcount++;
}
}
for (i = 0; i < 7; i++) {
A[i] = i;
}
F[0] = 0;
for (i = 0; i < 5; i++) {
F[i + 1] = F[i] + i;
}
printf("The min-hop transaction route >>>\n\n");
for (i = 1; i < 5; i++) {
if (F[i] == 0) {
printf("S");
} else if (F[i] == 1) {
printf("A");
} else if (F[i] == 2) {
printf("B");
} else if (F[i] == 3) {
printf("C");
} else if (F[i] == 4) {
printf("D");
} else if (F[i] == 5) {
printf("E");
} else if (F[i] == 6) {
printf("R");
break;
}
printf(" -> ");
}
n = n + 1;
m = m - 1;
}
// Take detailed info for creating adjacency matrix
void information() {
int n, m, N, M, i, j;
printf("\n\n");
printf("Enter number of nodes: ");
scanf("%d", & n);
printf("Enter number of edges: ");
scanf("%d", & m);
N = n - 1;
M = m - 1;
int E[M][2];
int C[M];
printf("Enter details of amount flow for each transaction (lender,borrower,amount):\n");
for (i = 0; i < M; i++) {
scanf("%d%d%d", & E[i][0], & E[i][1], & C[i]);
}
int R;
printf("Enter Receiver index number: ");
scanf("%d", & R);
int Adj[N + 1][N + 1];
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
Adj[i][j] = 0;
}
}
for (i = 0; i < M; i++) {
int x = E[i][0];
int y = E[i][1];
Adj[x][y] = C[i];
}
printf("\n");
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
if (Adj[i][j] == 0)
printf("%d ", Adj[i][j]);
else
printf("%d ", Adj[i][j]);
}
printf("\n");
}
printf("\n\n");
minimum_Hops(E, C, N, M);
}
// MAIN FUNCTION -----
int main() {
char Choice;
printf("Enter Advanced Mode? (Y/N) ");
scanf("%c", & Choice);
if (Choice == 'Y')
information();
else {
int i, j, k;
int n;
printf("\n");
printf("Enter number of people involved in transaction: ");
scanf("%d", & n);
struct node {
float lend;
float borrow;
char name[50];
char status[50];
struct node * next[n];
};
printf("\n");
struct node participant[n];
int adjMatrix[n][n];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
adjMatrix[i][j] = 0;
int ctr = 0;
while (ctr != n) {
printf("Enter name of participant: ");
scanf("%s", & participant[ctr].name);
ctr++;
}
printf("\n");
ctr = 0;
while (ctr != n) {
char Res;
int num;
char borrower[50];
char lender[50];
float borrow;
float lend;
participant[ctr].borrow = 0.0 f;
participant[ctr].lend = 0.0 f;
printf("Has %s borrowed anything? (Y/N) ", participant[ctr].name);
scanf("\n%c", & Res);
if (Res == 'Y') {
printf("How many people have you borrowed from? ");
scanf("%d", & num);
for (k = 0; k < num; k++) {
printf("Whom have you borrowed? ");
scanf("%s", & borrower);
for (i = 0; i < n; i++) {
if (strcmp(participant[i].name, borrower) == 0) {
adjMatrix[ctr][i] = 1;
}
}
printf("How much have you borrowed? ");
scanf("%f", & borrow);
participant[ctr].borrow += borrow;
}
printf("\n");
} else {
participant[ctr].borrow = 0.0 f;
printf("\n");
}
printf("Has %s lend anything? (Y/N) ", participant[ctr].name);
scanf("\n%c", & Res);
if (Res == 'Y') {
printf("How many people have you lend money to? ");
scanf("%d", & num);
for (k = 0; k < num; k++) {
printf("Whom have you lend? ");
scanf("%s", & lender);
for (i = 0; i < n; i++) {
if (strcmp(participant[i].name, lender) == 0) {
adjMatrix[ctr][i] = 2;
}
}
printf("How much have you lend? ");
scanf("%f", & lend);
participant[ctr].lend += lend;
}
printf("\n");
} else {
participant[ctr].lend = 0.0 f;
printf("\n");
}
ctr++;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%d ", adjMatrix[i][j]);
printf("\n");
}
printf("\n\n");
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < n; j++) {
if (adjMatrix[i][j] == 2) {
participant[i].next[c++] = (participant + j);
}
}
while (c < n) {
participant[i].next[c++] = NULL;
}
}
while (1) {
printf(">>>>> ~ ~ ~ ~ ~ >>>>> ~ ~ ~ ~ ~ >>>>> ~ ~ ~ ~ ~ >>>>>\n");
printf("-------------------------------- MENU -----------------------------------\n");
printf("| 1. Choose sender and receiver |\n");
printf("| 2. Confirm if exact amount sent by sender is transferred to receiver |\n");
printf("| 3. View details of passbook of any particular person |\n");
printf("| 4. View details of people who have amount to pay back |\n");
printf("| 5. View details of people who have amount due from others |\n");
printf("| 6. Display the least-hop transaction route for faster fund transfer |\n");
printf("| 7. Exit |\n");
printf("-------------------------------------------------------------------------\n");
printf(">>>>> ~ ~ ~ ~ ~ >>>>> ~ ~ ~ ~ ~ >>>>> ~ ~ ~ ~ ~ >>>>>\n");
int ch;
int totSend;
int totRecv;
int sendrecv = 0;
char sender[50];
char receiver[50];
char name[50];
printf("\n\n");
printf("Enter choice >>> ");
scanf("%d", & ch);
switch (ch) {
case 1:
if (sendrecv != 1) {
printf("Enter sender's name: ");
scanf("%s", & sender);
printf("Enter receiver's name: ");
scanf("%s", & receiver);
for (i = 0; i < n; i++) {
if (strcmp(participant[i].name, sender) == 0) {
strcpy(participant[i].status, "sender");
}
if (strcmp(participant[i].name, receiver) == 0) {
strcpy(participant[i].status, "receiver");
}
}
printf("Designations successfully assigned!\n");
sendrecv = 1;
} else {
printf("Sender and receiver already chosen!\n");
}
printf("\n\n");
break;
case 2:
for (i = 0; i < n; i++) {
if (strcmp(participant[i].status, "receiver") == 0) {
totRecv = participant[i].borrow;
}
if (strcmp(participant[i].status, "sender") == 0) {
totSend = participant[i].lend;
}
}
if (totRecv != totSend) {
printf("Exact amount sent by sender not transferred to receiver!\n");
} else {
printf("Exact amount sent by sender successfully transferred to receiver!!!\n");
}
printf("\n\n");
break;
case 3:
printf("Enter name of person: ");
scanf("%s", & name);
printf("Loading... details...\n");
printf("Name : %s\n", name);
for (i = 0; i < n; i++) {
if (strcmp(participant[i].name, name) == 0) {
printf("Total amount borrowed : %0.2f\n", participant[i].borrow);
printf("Total amount lend : %0.2f\n", participant[i].lend);
}
}
printf("\n\n");
break;
case 4:
printf("Details of people who have amount to pay back :\n");
for (i = 0; i < n; i++) {
if (participant[i].borrow != 0 && strcmp(participant[i].status, "receiver") != 0) {
printf("Name : %s\n", participant[i].name);
printf("Total amount to be paid back : %0.2f\n", participant[i].borrow);
}
}
printf("\n\n");
break;
case 5:
printf("Details of people who have amount due from others :\n");
for (i = 0; i < n; i++) {
if (participant[i].lend != 0 && strcmp(participant[i].status, "sender") != 0) {
printf("Name : %s\n", participant[i].name);
printf("Total amount to retrieve from others : %0.2f\n", participant[i].lend);
}
}
printf("\n\n");
break;
case 6:
information();
break;
case 7:
exit(0);
break;
default:
exit(0);
break;
}
}
}
return 0;
}