forked from chitwang/iitk-sem1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_manager.c
243 lines (166 loc) · 5.05 KB
/
key_manager.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
/*Automated Grading Scheme:
Visible: 5 marks for each visible test case.
Hidden: 5 marks for first four Hidden test case and 10 marks for last hidden test case.
Manually Grading Scheme:
Note: Give 0 marks for test-case component if there is any form of hard-coding. Eg: printf("0");
Penalty :
-20% for using any future concepts
-50% marks for using global variable
-50% for static allocation of array, example : int mat[100] or similar declaration
0 marks for not using given template. It is mandatory to utilize each component of template.
------------------------------------------------------------
Mr. T has joined a hotel as an intern. The hotel has N lockers for the housekeeping staff.
T is tasked with managing the lockers. Let us assume that T assigns the locker i to one housekeeping staff. The value i is generated using the locker_index() function provided in the template. The locker_index() function takes the input ID of the housekeeping staff. If a locker is already occupied, T should not assign the locker to the staff.
T can perform the following operations.
Assign a locker to a staff
Remove a staff as the owner of the locker on request
Check whether a staff has already been assigned a locker
Stop the operations (i.e., program exit)
operations-code:
a : assign
r : remove
s : search
e : exit
Constraints
Locker id starts from zero
Use of global variables are not allowed
Use of stdlib.h is allowed
staff_id starts from 1
N is always greater than equal to 2
The remove operation (r) is only invoked for staff having a locker
Input
The first line in each test case will contain the number of available lockers N.
Each line following the first line will be of the form (ignore the angular brackets)
The last line will contain e denoting the end of input
Output
The program should print the correct message corresponding to each operation
Assign a locker to the staff (a):
Success: Print Locker assigned to staff .
Failure: Print Locker < locker_id> to already assigned to staff < staff_id >.
Remove a locker assigned to a staff (r):
Success: Print Removed staff < staff_id > from locker < locker_id >.
Search for a locker assigned to a staff (s):
Success: Print Found, locker < locker_id > assigned to staff < staff_id >.
Failure: Print No locker assigned to staff < staff_id >.
The program should output the status of lockers at the end of input using print_locker_status() function provided in the template
The locker status contains the locker ID and staff ID to whom the locker is assigned in the format :
If a locker is not assigned to any staff, do not print the details
Sample Test Case
Input
3
a 2
a 3
s 5
s 3
a 5
e
Output
Locker 1 assigned to staff 2.
Locker 2 assigned to staff 3.
No locker assigned to staff 5.
Found, locker 2 assigned to staff 3.
Locker 1 is already assigned to staff 2.
1:2 2:3
*/
//solution:
#include <stdio.h>
#include <stdlib.h>
// DO NOT MODIFY THE FUNCTION
// N is the number of lockers in hotel
int locker_index(int staff_id, int N)
{
int index = 0;
index = (staff_id + 5) % N;
return index;
}
// EDIT THIS FUNCTION
void assign(int staff_id, int *locker, int n)
{
// ...
int x = locker_index(staff_id, n);
if (locker[x] != 0)
{
printf("Locker %d is already assigned to staff %d.\n", x, locker[x]);
return;
}
locker[x] = staff_id;
printf("Locker %d assigned to staff %d.\n", x, staff_id);
return;
}
// EDIT THIS FUNCTION
void removal(int *locker, int staff_id, int n)
{
// ...
for (int i = 0; i < n; i++)
{
if (locker[i] == staff_id)
{
locker[i] = 0;
printf("Removed staff %d from locker %d.\n", staff_id, i);
return;
}
}
}
// EDIT THIS FUNCTION
void search_assigned_locker(int *locker, int staff_id, int n)
{
// ...
for (int i = 0; i < n; i++)
{
if (locker[i] == staff_id)
{
printf("Found, locker %d assigned to staff %d.\n", i, staff_id);
return;
}
}
printf("No locker assigned to staff %d.\n", staff_id);
}
// DO NOT MODIFY THE FUNCTION
void print_locker_status(int *ptr, int size)
{
int i = 0;
for (; i < size; i++)
{
if (ptr[i] != 0)
printf("%d:%d ", i, ptr[i]);
}
printf("\n");
}
int main()
{
// ...
int n;
scanf("%d\n", &n);
int *locker = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
*(locker + i) = 0;
}
char type;
int staff_id;
while (1)
{
scanf("%c", &type);
if (type == 'a')
{
scanf("%d\n", &staff_id);
assign(staff_id, locker, n);
}
else if (type == 's')
{
scanf("%d\n", &staff_id);
search_assigned_locker(locker, staff_id, n);
}
else if (type == 'r')
{
scanf("%d\n", &staff_id);
removal(locker, staff_id, n);
}
else if (type == 'e')
{
print_locker_status(locker, n);
break;
}
}
return 0;
}