-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassignedseating.c
227 lines (197 loc) · 7.55 KB
/
assignedseating.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
//Program Assignment 1 - Assigned Seating
//Alexander Gershfeld
//Arup Guha
//COP3502
//9-12-2023
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INITSIZE 10
#define MAXLEN 50
#define MAXROWS 100000 //one hundred thousand
//where the order details are stored
typedef struct order {
int s_seat;
int e_seat;
char* name;
} order;
//this manages a single row ; in each theater row contains a set of orders
typedef struct theaterrow {
//pointer pointing to other orders within a single row
order** list_orders;
int max_size;
int cur_size;
} theaterrow;
//takes in the input to purchase an order; array for name DMA; then strcpyd onto struct
order* make_order(int start, int end, char* this_name);
//allocate memory for the row while setting a limit as well as the size to 0
theaterrow* make_empty_row();
//returns a value based on whether the orders inputted, from the same row, are conflicting
int conflict(order* order1, order* order2);
//returns a 0 or 1 on whether the seats within the order are taken or not
int can_add_order(theaterrow* this_row, order* this_order);
//will adds this order to this row, if success then 1, if no then 0. adjusts memory if needed.
int add_order(theaterrow* this_row, order* this_order);
//if a seat in a row is owned, return the name, otherwise NULL
char* get_owner(theaterrow** theater, int row, int seat_num);
//does the same thing as the previous function but for the selected row and seat number
char* get_row_owner(theaterrow* this_row, int seat_num);
//returns 1 if the seat number compared is contained in the range of seats under myorder
int contains(order* myorder, int seat_no);
void free_order(order* this_order);
void free_row(theaterrow* this_row);
int main() {
theaterrow** regal = (theaterrow**)calloc(MAXROWS + 1, sizeof(theaterrow*)); //where all rows then orders are located
char input[MAXLEN * 2]; //allocates enough space for name and seating
char* option, * tmpName;
int tmpRow, tmpSeatStart, tmpSeatEnd;
int onSwitch = 1;
//sentinel driven loop
while (onSwitch) {
int result; //variable that tracks success or failure with adding orders
if(fgets(input, MAXLEN + 10, stdin) == NULL) {
printf("Input not receieved. Bye!\n");
return 0;
}
input[strcspn(input, "\n")] = 0; //gets rid of return value in input
option = strtok(input, " "); //breaks down first word; used to direct the loop
//buy command menu
if (strcmp("BUY", option) == 0) {
//variables of input further broken down
tmpRow = (atoi(strtok(NULL, " "))) - 1; //
tmpSeatStart = atoi(strtok(NULL, " ")); // Converts string token to an integer
tmpSeatEnd = atoi(strtok(NULL, " ")); //
tmpName = strtok(NULL, " ");
//creation of the order as well as any necessary rows
order* tmpOrder = make_order(tmpSeatStart, tmpSeatEnd, tmpName);
if (regal[tmpRow] == NULL) {
regal[tmpRow] = make_empty_row();
}
//adding the order to the main list only if it acceptable, then printed whether it is or not
result = add_order(regal[tmpRow], tmpOrder); //all changes in this function will be applied to the row of user choice
if (result == 0) {
printf("FAILURE\n");
free_order(tmpOrder); //frees order if theres conflict, no need for the order
}
else if (result == 1) {
printf("SUCCESS\n");
}
}
else if (strcmp("LOOKUP", option) == 0) {
tmpRow = (atoi(strtok(NULL, " "))) - 1;
tmpSeatStart = (atoi(strtok(NULL, " ")));
char* owner = get_owner(regal, tmpRow, tmpSeatStart);
if (owner == NULL) {
printf("No one\n");
}
else {
printf("%s\n", owner);
}
}
//exit condition
else if (strcmp("QUIT", option) == 0) {
onSwitch = 0; //off switch :)
//passes the rows of the main double pointer as well as its constituents to be freed
for (int i = 0; i < MAXROWS + 1; i++) {
free_row(regal[i]);
}
}
}
free(regal);
return 0;
}
order* make_order(int start, int end, char* this_name) {
order* tmp = (order*)malloc(sizeof(order));
tmp->name = (char*)calloc(MAXLEN, sizeof(char));
tmp->s_seat = start;
tmp->e_seat = end;
strcpy(tmp->name, this_name);
return tmp;
}
theaterrow* make_empty_row() {
theaterrow* tmp = (theaterrow*)malloc(sizeof(theaterrow));
tmp->cur_size = 0;
tmp->max_size = INITSIZE;
tmp->list_orders = (order**)calloc((tmp->max_size), sizeof(order*));
return tmp;
}
int conflict(order* order1, order* order2) {
//compares j for all values of i
for (int i = order1->s_seat; i <= order1->e_seat; i++) {
for (int j = order2->s_seat; j <= order2->e_seat; j++) {
if (i == j) {
return 1; //failure
}
}
}
return 0; //success
}
int can_add_order(theaterrow* this_row, order* this_order) {
//Check for any conflicts among all orders in the row
int res;
for (int i = 0; i < this_row->cur_size; i++) {
res = conflict(this_row->list_orders[i], this_order);
if (res == 1) {
return 0; //failure
}
}
return 1; //success
}
int add_order(theaterrow* this_row, order* this_order) {
//first checks to see if the attempted order does not conflict with others
if (can_add_order(this_row, this_order) == 0) {
return 0; //failure
}
//Row needs more memory
if (this_row->cur_size == this_row->max_size) {
int newSize = this_row->max_size * 2; //calculate double the previous size
this_row->max_size = newSize;
this_row->list_orders = (order**)realloc(this_row->list_orders, newSize * sizeof(order*)); //realloc
}
//Order can be added to row
this_row->list_orders[this_row->cur_size] = this_order;
this_row->cur_size++;
return 1; //success
}
char* get_owner(theaterrow** theater, int row, int seat_num) {
if (theater[row] == NULL) {
return NULL; //row not made yet
}
char* ret = get_row_owner(theater[row], seat_num);
if (ret == NULL) {
return NULL; //seat not taken
}
return ret; //seat taken
}
char* get_row_owner(theaterrow* this_row, int seat_num) {
for (int i = 0; i < this_row->cur_size; i++) {
if (contains(this_row->list_orders[i], seat_num) == 1) {
return this_row->list_orders[i]->name; //seat taken by name
}
}
return NULL; //seat not taken
}
int contains(order* myorder, int seat_no) {
for (int i = myorder->s_seat; i <= myorder->e_seat; i++) {
if (seat_no == i) {
return 1; //seat taken
}
}
return 0; //seat not taken
}
void free_order(order* this_order) {
free(this_order->name);
free(this_order);
}
void free_row(theaterrow* this_row) {
//filters out rows that were not used
if (this_row == NULL) {
return;
}
//uses the free order function iterated up to current size of row used
for (int i = 0; i < this_row->cur_size; i++) {
free_order(this_row->list_orders[i]);
}
free(this_row->list_orders);
free(this_row);
}