-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_functions.c
204 lines (166 loc) · 5.03 KB
/
main_functions.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Input.h"
#include "Queue.h"
#include "File.h"
#include "main_functions.h"
void printMenu(char* option)
{
printf("Options:\n\n");
printf("1) Add a new client.\n");
printf("2) Attend a client.\n");
printf("3) View queues status.\n");
printf("4) View specific client info.\n");
printf("5) Exit.\n\n\n\n");
*option = inputChar(3);
return;
}
void print(CLIENT Client)
{
printf("Client data:\n\n");
printf("Name: %s\n", Client.name);
printf("ID: %i\n", Client.id);
printf("Operation: %i\n", Client.operation); // true -> Guardar | false -> Retirar
printf("Amount of money: %f$\n", Client.amountMoney);
printf("Type: %c\n", Client.type); // 'M' -> Embarazada y Premium 'E' -> Embarazada | 'C' -> Client Comun | 'P' -> Client Premium
printf("Number: %i\n\n\n\n", Client.number);
return;
}
CLIENT getClient(FILE* file, int* clientNumber)
{
CLIENT Client;
Client.number = *clientNumber;
char textLine[51];
fgets(textLine, sizeof(textLine), file);
cleanString(textLine, sizeof(textLine));
/* FORMAT:
Name (George Emmerson)\n
id (44392883)\n
operation (1 o 0)\n
Amount of money (32423.333)\n
type (C, P, E o M)\n
*/
char* nextToken;
strncpy(Client.name, strtok_s(textLine, "", &nextToken), sizeof(Client.name));
fgets(textLine, sizeof(textLine), file);
Client.id = atoi(strtok_s(textLine, "", &nextToken));
fgets(textLine, sizeof(textLine), file);
Client.operation = atoi(strtok_s(textLine, "", &nextToken));
fgets(textLine, sizeof(textLine), file);
Client.amountMoney = atof(strtok_s(textLine, "", &nextToken));
fgets(textLine, sizeof(textLine), file);
Client.type = textLine[0];
return Client;
}
void newClient(QUEUE* que_std, QUEUE* que_pre, int* clientNumber)
{
FILE* fileInput;
fopen_s(&fileInput, "Client Input.txt", "r");
if (fileInput == NULL) {
printf("Couldn't find the data input file.\n\n");
fopen_s(&fileInput, "Client Input.txt", "w");
} else {
if (*clientNumber > 800000) { // (As if the system was permanently on).
*clientNumber = 0;
}
CLIENT Client = getClient(fileInput, clientNumber);
*clientNumber += 1;
if (Client.type == 'C') {
queue(Client, que_std);
que_std->numberOfPendingShifts++;
} else if (Client.type == 'P') {
queue(Client, que_pre);
que_pre->numberOfPendingShifts++;
} else if (Client.type == 'E') {
priorityQueue(Client, que_std);
que_pre->numberOfPendingShifts++;
} else { // Client type is 'M' (Max priority)
Client.type = 'E';
priorityQueue(Client, que_pre);
que_pre->numberOfPendingShifts++;
}
}
close(fileInput);
return;
}
void attendClient(QUEUE* que_std, QUEUE* que_pre, bool* nextQue)
{
Q_NODE* pregnantWoman = isThereAPregnantWoman(que_pre);
if (pregnantWoman != NULL) {
attendSpecially(pregnantWoman, que_pre);
que_pre->numberOfPendingShifts--;
} else {
pregnantWoman = isThereAPregnantWoman(que_std);
if (pregnantWoman != NULL) {
attendSpecially(pregnantWoman, que_std);
que_std->numberOfPendingShifts--;
} else if (que_pre->numberOfPendingShifts > 2) {
dequeue(que_pre);
que_pre->numberOfPendingShifts--;
*nextQue = true;
} else if ((*nextQue || isEmpty(*que_pre)) && !isEmpty(*que_std)) {
dequeue(que_std);
que_std->numberOfPendingShifts--;
*nextQue = false;
} else if (!isEmpty(*que_pre)) {
dequeue(que_pre);
que_pre->numberOfPendingShifts--;
*nextQue = true;
}
}
return;
}
void printQueues(QUEUE que_std, QUEUE que_pre)
{
int i = 1;
printf("Standard\tPremium\n");
while (!isEmpty(que_std) && !isEmpty(que_pre)) {
printf("%i %c\t\t%c\n", i, que_std.head->value.type, que_pre.head->value.type);
que_std.head = que_std.head->next;
que_pre.head = que_pre.head->next;
i++;
}
while (!isEmpty(que_std)) {
printf("%i %c\n", i, que_std.head->value.type);
que_std.head = que_std.head->next;
i++;
}
while (!isEmpty(que_pre)) {
printf("%i\t\t%c\n", i, que_pre.head->value.type);
que_pre.head = que_pre.head->next;
i++;
}
printf("\n\n\n\n\n\n");
return;
}
Q_NODE* searchClient(QUEUE queue, int posQueue)
{
for (int i = 0; i < posQueue && queue.head != NULL; i++) {
queue.head = queue.head->next;
}
return queue.head;
}
void showSpecificClientInfo(QUEUE que_std, QUEUE que_pre)
{
printf("\n\n\nEnter in which one of the queues the client is ('C' Common or 'P' Premium): ");
char que = inputChar(3);
printf("Enter the position where the client is: ");
int posQueue = inputInt(7);
printf("\n\n\n\n");
Q_NODE* ptrClient;
if (que == 'C' || que == 'c') {
ptrClient = searchClient(que_std, posQueue - 1);
} else if (que == 'P' || que == 'p') {
ptrClient = searchClient(que_pre, posQueue - 1);
} else {
printf("The data entered is not valid.\n\n\n\n");
return;
}
if (ptrClient != NULL) {
print(ptrClient->value);
} else {
printf("Sorry, we couldn't find a client in that position.");
}
return;
}