forked from YuriyStr/TP_Lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task6.c
211 lines (188 loc) · 4.47 KB
/
Task6.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PLACE_COUNT 12
int places_free = PLACE_COUNT;
typedef struct _Seat
{
int id;
int taken;
char first_name[80];
char last_name[80];
} Seat;
Seat arr[PLACE_COUNT]; // Array of all seats in the plane
int ids[PLACE_COUNT]; // Array used for sorting booked seats alphabetically
void add(int);
void rem(int);
void swap (int*, int*);
int main()
{
FILE *flightInfo = fopen("flight.bin", "rb");
if (flightInfo == NULL)
{
for (int i = 0; i < PLACE_COUNT; i++)
{
arr[i].id = i + 1;
arr[i].taken = 0;
arr[i].first_name[0] = '\0';
arr[i].last_name[0] = '\0';
}
}
else
{
fread(arr, sizeof(Seat), PLACE_COUNT, flightInfo);
fclose(flightInfo);
for (int i = 0; i < PLACE_COUNT; i++)
if (arr[i].taken)
{
add(i);
}
}
while (1)
{
char code;
printf("a - Show the amount of unbooked places\n");
printf("b - Show the list of unbooked places\n");
printf("c - Show the list of booked places\n");
printf("d - Book place\n");
printf("e - Unbook place\n");
printf("f - Quit\n\n");
scanf(" %c", &code);
getchar();
switch (code)
{
case 'a':
{
printf("%d\n", places_free);
break;
}
case 'b':
{
for (int i = 0; i < PLACE_COUNT; i++)
if (!arr[i].taken)
printf("%d\n", arr[i].id);
break;
}
case 'c':
{
for (int i = 0; i < PLACE_COUNT - places_free; i++)
printf("%d: %s, %s\n", arr[ids[i] - 1].id, arr[ids[i] - 1].last_name, arr[ids[i] - 1].first_name);
break;
}
case 'd':
{
int seatCode;
char firstName[80];
char lastName[80];
char confirm;
printf("Enter the seat code: ");
scanf("%d", &seatCode);
getchar();
if (seatCode < 1 || seatCode > PLACE_COUNT)
{
printf("Error: invalid seat code\n");
break;
}
if (arr[seatCode - 1].taken)
{
printf("Error: seat already booked\n");
break;
}
printf("Enter your first name: ");
scanf("%s", firstName);
getchar();
printf("Enter your last name: ");
scanf("%s", lastName);
getchar();
printf("You want to book seat #%d for passenger %s %s. Press \'y\' to confirm booking.\n", seatCode, firstName, lastName);
scanf("%c", &confirm);
getchar();
if (confirm == 'y')
{
arr[seatCode - 1].id = seatCode;
arr[seatCode - 1].taken = 1;
strcpy(arr[seatCode - 1].first_name, firstName);
strcpy(arr[seatCode - 1].last_name, lastName);
add(seatCode - 1);
}
break;
}
case 'e':
{
int seatCode;
char confirm;
printf("Enter the seat code: ");
scanf("%d", &seatCode);
getchar();
if (seatCode < 1 || seatCode > PLACE_COUNT)
{
printf("Error: invalid seat code\n");
break;
}
if (!arr[seatCode - 1].taken)
{
printf("Error: seat not booked\n");
break;
}
printf("You want to unbook seat #%d for passenger %s %s. Press \'y\' to confirm unbooking.\n", seatCode, arr[seatCode - 1].first_name, arr[seatCode - 1].last_name);
scanf("%c", &confirm);
getchar();
if (confirm == 'y')
{
arr[seatCode - 1].taken = 0;
rem(arr[seatCode - 1].id);
}
break;
}
case 'f':
{
FILE *outputFile = fopen("flight.bin", "wb");
fwrite(arr, sizeof(Seat), PLACE_COUNT, outputFile);
fclose(outputFile);
return 0;
}
default:
break;
}
printf("\n");
}
return 0;
}
// Method adds new element to the array of taken seats so that it remains sorted alphabetically
void add (int idx)
{
ids[PLACE_COUNT - places_free] = arr[idx].id;
for (int i = PLACE_COUNT - 1 - places_free; i >= 0; i--)
{
int res = strcmp(arr[ids[i] - 1].last_name, arr[idx].last_name);
if (res > 0 || (res == 0 && strcmp(arr[ids[i] - 1].first_name, arr[idx].first_name) > 0))
{
swap(&(ids[i]), &(ids[i + 1]));
}
else
break;
}
places_free--;
}
// Placing an element to the tail of the array of taken seats so that it is not seen anymore
void rem(int idx)
{
int indexof;
for (int i = 0; i < PLACE_COUNT - places_free; i++)
if (ids[i] == idx)
{
indexof = i;
break;
}
for (int i = indexof + 1; i < PLACE_COUNT - places_free; i++)
{
swap(&(ids[i]), &(ids[i - 1]));
}
places_free++;
}
void swap(int *a, int *b)
{
int tmp = *b;
*b = *a;
*a = tmp;
}