-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHOTEL.c
354 lines (274 loc) · 8.18 KB
/
HOTEL.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
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<stdbool.h>
struct contato {
char nome[30];
char telefone[15];
int numero_hospedes;
bool disponivel;
};
struct contato temp;
int andar, apart;
struct contato reservas[30][8]; //fazendo a matriz de um hotel com 30 andares e 8 apartamentos em cada andar.
void guarda_string(char string[], int cont){
int i, j;
// printf("No guarda string ...");
switch(cont){
case 1:
strcpy(temp.nome, " ");
strcpy(temp.telefone, " ");
temp.numero_hospedes=0;
//andar= atoi(string);
sscanf(string, "%d", &andar);
i=andar;
// printf("\nandar: %d", i);
break;
case 2:
//apart= atoi(string);
sscanf(string, "%d", &apart);
j=apart;
// printf("\nquarto: %d", j);
break;
case 3:
strcpy(temp.nome, string);
break;
case 4:
strcpy(temp.telefone, string);
break;
case 5:
//temp.numero_hospedes= atoi(string);
sscanf(string, "%d", &temp.numero_hospedes);
// printf("Salvando ... Apartamento: [%d-%d], %s, %s, %d hospedes\n", i, j, temp.nome, temp.telefone, temp.numero_hospedes);
i=andar;
j=apart;
strcpy(reservas[andar][apart].nome, temp.nome);
strcpy(reservas[andar][apart].telefone, temp.telefone);
reservas[andar][apart].numero_hospedes=temp.numero_hospedes;
reservas[i][j].disponivel=false;
// printf("Salvando ... Apartamento: [%d-%d], %s, %s, %d hospedes\n", i, j, reservas[i][j].nome, reservas[i][j].telefone, reservas[i][j].numero_hospedes);
break;
}
system("PAUSE");
}
void salvar_arquivo(){
int i, j;
char string[50];
char buffer[50];
FILE *output;
output = fopen("arquivo.txt", "w");
if(output){
for(i=0;i<=29;i++){
for(j=0;j<=7;j++){
if(!reservas[i][j].disponivel){
// printf("Salvando ... Apartamento: [%d-%d], %s, %s, %d hospedes\n", i, j, reservas[i][j].nome, reservas[i][j].telefone, reservas[i][j].numero_hospedes);
strcpy(string, "&");
sprintf(buffer, "%d", i);
strcat(string, buffer);
strcat(string, "#");
sprintf(buffer, "%d", j);
strcat(string, buffer);
strcat(string, "#");
strcat(string, reservas[i][j].nome);
strcat(string, "#");
strcat(string, reservas[i][j].telefone);
strcat(string, "#");
sprintf(buffer, "%d", reservas[i][j].numero_hospedes);
strcat(string, buffer);
strcat(string, "#");
fputs(string, output);
}
}
}
}else{
printf("deu erro na abertura do arquivo");
}
fclose(output);
}
void ler_arquivo(){
int cont=0, size_string;
char ch;
char string[30];
FILE *input;
input = fopen("arquivo.txt", "r");
if(input){
ch = fgetc(input);
while(ch!=EOF){
// printf("%c\n", ch);
switch(ch){
case '&':
strcpy(string, " ");
cont=1;
break;
case '#':
// printf("%s\n", string);
guarda_string(string, cont);
cont++;
strcpy(string, " ");
break;
default:
size_string = strlen(string);
if(size_string < 254) {
string[size_string] = ch;
string[size_string+1] = '\0';
}
}
ch = fgetc(input);
}
fclose(input);
}else{
printf("deu erro na abertura do arquivo");
}
}
void inicializar(){
int i, j;
for(i=0;i<=29;i++){
for(j=0;j<=7;j++){
strcpy(reservas[i][j].nome, " ");
strcpy(reservas[i][j].telefone, " ");
reservas[i][j].numero_hospedes=0;
reservas[i][j].disponivel=true;
}
}
}
void incluir(){
int i, j;
printf("Qual o andar desejado?");
scanf("%d", &i);
printf("Qual apartamento?");
scanf("%d", &j);
if(reservas[i][j].disponivel){
printf("Esta disponivel. digite suas informacoes\n");
printf("Digite o nome do hospede: ");
fflush(stdin);
gets(reservas[i][j].nome);
printf("Digite o telefone do hospede: ");
fflush(stdin);
gets(reservas[i][j].telefone);
printf("Digite a quantidade de hospedes no quarto: ");
scanf("%d", &reservas[i][j].numero_hospedes);
reservas[i][j].disponivel=false;
}else{
printf("este apartamento %d-%d esta ocupado", i, j);
system("PAUSE");
}
}
void alterar(){
int i, j;
printf("Digite qual de qual apartamento deseja alterar o cadastro\n\n");
printf("Andar: \n");
scanf("%d", &i);
printf("Apartamento: \n");
scanf("%d", &j);
if(!reservas[i][j].disponivel){
printf("Digite o nome: \n");
fflush(stdin);
gets(reservas[i][j].nome);
printf("Digite o telefone: \n");
fflush(stdin);
gets(reservas[i][j].telefone);
printf("Digite o numero de hospedes: \n");
scanf("%d", &reservas[i][j].numero_hospedes);
printf("Reserva alterada\n");
system("PAUSE");
}else{
printf("o Apartamento %d-%d nao esta ocupado", i, j);
system("PAUSE");
}
}
void excluir(){
int i, j;
printf("Digite qual apartamento deseja excluir do cadastro... \n");
printf("Andar: \n");
scanf("%d", &i);
printf("Apartamento: \n");
scanf("%d", &j);
if(!reservas[i][j].disponivel){
strcpy(reservas[i][j].nome, " ");
strcpy(reservas[i][j].telefone, " ");
reservas[i][j].numero_hospedes=0;
reservas[i][j].disponivel=true;
printf("Cadastro Excluido...\n");
system("PAUSE");
}else{
printf("Este apartamento esta vago...");
system("PAUSE");
}
}
void consultar(){
int i, j, opcao;
printf("1 - visualizar todos os apartamentos\n");
printf("2 - visualizar um apartamento especifico\n\n");
printf("Digite qual sua opcao: ");
scanf("%d", &opcao);
switch(opcao){
case 1:
for(i=0;i<=29;i++){
for(j=0;j<=7;j++){
if(!reservas[i][j].disponivel){
printf("Apartamento: [%d-%d], %s, %s, %d hospedes\n", i, j, reservas[i][j].nome, reservas[i][j].telefone, reservas[i][j].numero_hospedes);
}else{
printf("Apartamento: [%d-%d] esta disponivel\n", i, j);
}
}
}
system("PAUSE");
break;
case 2:
printf("Digite qual o apartamento voce quer visualizar\n");
printf("andar\n");
scanf("%d", &i);
printf("Apartamento\n");
scanf("%d", &j);
if(!reservas[i][j].disponivel){
printf("Apartamento: [%d-%d], %s, %s, %d hospedes\n", i, j, reservas[i][j].nome, reservas[i][j].telefone, reservas[i][j].numero_hospedes);
}else{
printf("Apartamento disponivel\n");
break;
}
default:
printf("opcao invalida");
break;
}
system("PAUSE");
}
int main(){
inicializar();
ler_arquivo();
int opc;
while(opc!=6){
printf("\n\n---------Menu--------\n\n");
printf("1 - Incluir Reserva\n\n");
printf("2 - Alterar Reserva\n\n");
printf("3 - Excluir Reserva\n\n");
printf("4 - Consultar Reserva\n\n");
printf("5 - Salvar Arquivo\n\n");
printf("6 - Sair\n\n");
printf("Digite a sua opcao: ");
scanf("%d", &opc);
switch(opc){
case 1:
incluir();
break;
case 2:
alterar();
break;
case 3:
excluir();
break;
case 4:
consultar();
break;
case 5:
printf("antes de salvar 1 ...");
system("PAUSE");
salvar_arquivo();
break;
default:
printf("opcao invalida");
break;
}
system("cls");
}
system("PAUSE");
}