-
Lenguaje
Pascal
-
Descripción
Programa de repartición de despensas, que lleva:
- Fecha.
- Nombre del beneficiado.
- Edad.
- Dirección.
- Número de integrantes de su familia.
- Tipo de sangre.
Se deberán guardar los datos y presentar pantalla limpia en cada captura.
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
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
program program_beneficiados;
uses cmem, crt, sysutils;
type Comparable = function (const datum1 : pointer; const datum2 : pointer) : integer;
type ForEachFunction = procedure (datum : pointer; params : pointer);
type Beneficiado = object
nombre : string;
dia : integer;
mes : integer;
ano : integer;
edad : integer;
direccion : string;
integrantes_de_familia : integer;
tipo_de_sangre : string;
end;
type pBeneficiado = ^Beneficiado;
function compare_beneficiado (const beneficiado1 : pBeneficiado; const beneficiado2 : pBeneficiado) : integer;
begin
compare_beneficiado := strcomp (@beneficiado1^.nombre[1], @beneficiado2^.nombre[1]);
end;
function read_str (message : string) : string;
var string_var : string;
begin
write (stdout, message, ': ');
readln (string_var);
read_str := string_var;
end;
function read_integer (message : string) : integer;
var integer_var : integer;
begin
write (stdout, message, ': ');
readln (integer_var);
read_integer := integer_var;
end;
function read_field (var file_stream : text; var field : string) : boolean;
var c : char;
begin
field := '';
while not eof (file_stream) do
begin
read (file_stream, c);
if (c=#13) then
continue;
if (c=#9) or (c=#10) then
begin
read_field := true;
exit;
end
else
field := field + c;
end;
read_field := false;
end;
type pinteger = ^integer;
procedure print_beneficiado (datum : pBeneficiado; counter : pinteger);
begin
writeln (stdout, 'nombre : ', datum^.nombre);
writeln (stdout, 'dia : ', datum^.dia);
writeln (stdout, 'mes : ', datum^.mes);
writeln (stdout, 'ano : ', datum^.ano);
writeln (stdout, 'edad : ', datum^.edad);
writeln (stdout, 'direccion : ', datum^.direccion);
writeln (stdout, 'integrantes de familia: ', datum^.integrantes_de_familia);
writeln (stdout, 'tipo de sangre : ', datum^.tipo_de_sangre);
writeln (stdout);
inc (counter^);
end;
type pText = ^text;
procedure print_on_file (datum : pBeneficiado; file_stream : pText);
begin
write (file_stream^, datum^.nombre, #9);
write (file_stream^, datum^.dia, #9);
write (file_stream^, datum^.mes, #9);
write (file_stream^, datum^.ano, #9);
write (file_stream^, datum^.edad, #9);
write (file_stream^, datum^.direccion, #9);
write (file_stream^, datum^.integrantes_de_familia, #9);
write (file_stream^, datum^.tipo_de_sangre, #10);
end;
type ppointer = ^pointer;
type ppBeneficiado = ^pBeneficiado;
function array_insert (array_var : ppointer; var length : integer; datum : pointer) : ppointer;
begin
array_var := realloc (array_var, sizeof (pointer) * (length+1));
array_var[length] := datum;
inc (length);
array_insert := array_var;
end;
function array_search (array_var : ppointer; length : integer; compare: Comparable; const datum : pointer) : pointer;
begin
if length > 0 then
if compare (array_var[0], datum) = 0 then
array_search := array_var[0]
else
array_search := array_search (array_var + 1, length - 1, compare, datum)
else
array_search := nil;
end;
function array_remove (array_var : ppointer; var length : integer; const datum : pointer) : ppointer;
var i : integer = 0;
begin
while (i<length) and (datum<>array_var[i]) do
inc (i);
if i<length then
begin
for i:=i+1 to length-1 do
array_var[i-1] := array_var[i];
dec (length);
array_var := realloc (array_var, sizeof (pointer) * length);
end;
array_remove := array_var;
end;
procedure quick_sort (array_var : ppointer; first_index : integer; last_index : integer; compare: Comparable);
var smaller, bigger : integer;
var pivot : pointer;
begin
smaller := first_index;
bigger := last_index;
if last_index>first_index then
begin
pivot := array_var[(first_index+last_index) div 2];
while smaller <= bigger do
begin
while (smaller < last_index) and (compare (array_var[smaller], pivot)<0) do
inc (smaller);
while (bigger > first_index) and (compare (array_var[bigger], pivot)>0) do
dec (bigger);
if smaller <= bigger then
begin
pivot := array_var[smaller];
array_var[smaller] := array_var[bigger];
array_var[bigger] := pivot;
inc (smaller);
dec (bigger);
end;
end;
if first_index < bigger then
quick_sort (array_var, first_index, bigger, compare);
if smaller < last_index then
quick_sort (array_var, smaller, last_index, compare);
end;
end;
procedure array_foreach (array_var : ppointer; length : integer; params : pointer; func : ForEachFunction);
begin
if length>0 then
begin
func (array_var[0], params);
array_foreach (array_var + 1, length - 1, params, func);
end;
end;
const path = 'beneficiados.tsv';
var datum : ^Beneficiado = nil;
var beneficiado1 : Beneficiado;
var main_option : char;
var counter, suboption : integer;
var field : string = '';
var file_stream : text;
var beneficiados : ppBeneficiado = nil;
var length : integer = 0;
begin
counter := 0;
if fileexists (path) then
begin
assign (file_stream, path);
reset (file_stream);
while read_field (file_stream, field) do
begin
new (datum);
datum^.nombre := field;
read_field (file_stream, field);
val (field, datum^.dia);
read_field (file_stream, field);
val (field, datum^.mes);
read_field (file_stream, field);
val (field, datum^.ano);
read_field (file_stream, field);
val (field, datum^.edad);
read_field (file_stream, field);
datum^.direccion := field;
read_field (file_stream, field);
val (field, datum^.integrantes_de_familia);
read_field (file_stream, field);
datum^.tipo_de_sangre := field;
beneficiados := ppBeneficiado (array_insert (ppointer (beneficiados), length, datum));
end;
close (file_stream);
end;
repeat
clrscr;
writeln (stdout, 'MEN'#233#10#13);
writeln (stdout, '1.- Altas');
writeln (stdout, '2.- Consultas');
writeln (stdout, '3.- Actualizaciones');
writeln (stdout, '4.- Bajas');
writeln (stdout, '5.- Ordenar registros');
writeln (stdout, '6.- Listar registros');
writeln (stdout, '7.- Salir');
write (stdout, 'Selecciona una opci'#162'n: ');
repeat
main_option := readkey;
until (main_option>='1') and (main_option<='7');
writeln (stdout, main_option, #10#13);
if (beneficiados = nil) and (main_option <> '1') and (main_option <> '7') then
begin
write (stdout, 'No hay registros.'#10#13#10#13'Presiona una tecla para continuar . . . ');
readkey;
continue;
end;
if main_option<'5' then
begin
beneficiado1.nombre := read_str ('Ingresa el nombre del beneficiado');
datum := array_search (ppointer (beneficiados), length, Comparable(@compare_beneficiado), @beneficiado1);
if datum <> nil then
begin
writeln (stdout);
print_beneficiado (datum, @counter);
end;
end;
if (main_option = '1') and (datum <> nil) then
writeln (stdout, 'El registro ya existe.')
else if (main_option>='2') and (main_option<='4') and (datum = nil) then
writeln (stdout, #10#13'Registro no encontrado.')
else case main_option of
'1':
begin
new (datum);
datum^.nombre := beneficiado1.nombre;
datum^.dia := read_integer ('Ingresa el dia');
datum^.mes := read_integer ('Ingresa el mes');
datum^.ano := read_integer ('Ingresa el ano');
datum^.edad := read_integer ('Ingresa el edad');
datum^.direccion := read_str ('Ingresa el direccion');
datum^.integrantes_de_familia := read_integer ('Ingresa el integrantes de familia');
datum^.tipo_de_sangre := read_str ('Ingresa el tipo de sangre');
beneficiados := ppBeneficiado (array_insert (ppointer (beneficiados), length, datum));
writeln (stdout, #10#13'Registro agregado correctamente.');
end;
'3':
begin
writeln (stdout, 'Men'#163' de actualizaci'#162'n de campos');
writeln (stdout, '1.- dia');
writeln (stdout, '2.- mes');
writeln (stdout, '3.- ano');
writeln (stdout, '4.- edad');
writeln (stdout, '5.- direccion');
writeln (stdout, '6.- integrantes de familia');
writeln (stdout, '7.- tipo de sangre');
repeat
suboption := read_integer ('Selecciona el n'#163'mero de campo a modificar');
if (suboption<1) or (suboption>7) then
writeln (stdout, 'Opci'#162'n incorrecta');
until (suboption>=1) and (suboption<=7);
case suboption of
1: datum^.dia := read_integer ('Ingresa el nuevo dia');
2: datum^.mes := read_integer ('Ingresa el nuevo mes');
3: datum^.ano := read_integer ('Ingresa el nuevo ano');
4: datum^.edad := read_integer ('Ingresa el nuevo edad');
5: datum^.direccion := read_str ('Ingresa el nuevo direccion');
6: datum^.integrantes_de_familia := read_integer ('Ingresa el nuevo integrantes de familia');
7: datum^.tipo_de_sangre := read_str ('Ingresa el nuevo tipo de sangre');
end;
writeln (stdout, #10#13'Registro actualizado correctamente.');
end;
'4':
begin
beneficiados := ppBeneficiado (array_remove (ppointer (beneficiados), length, datum));
dispose (datum);
writeln (stdout, 'Registro eliminado correctamente.');
end;
'5':
begin
quick_sort (ppointer (beneficiados), 0, length - 1, Comparable(@compare_beneficiado));
writeln (stdout, 'Registros ordenados correctamente.');
end;
'6':
begin
counter := 0;
array_foreach (ppointer (beneficiados), length, @counter, ForEachFunction (@print_beneficiado));
writeln (stdout, 'Total de registros: ', counter, '.');
end;
end;
if (main_option<'7') then
begin
write (stdout, #10#13'Presiona una tecla para continuar . . . ');
readkey;
end;
until main_option = '7';
assign (file_stream, path);
rewrite (file_stream);
array_foreach (ppointer (beneficiados), length, @file_stream, ForEachFunction (@print_on_file));
close (file_stream);
end.
uses cmem, crt, sysutils;
type Comparable = function (const datum1 : pointer; const datum2 : pointer) : integer;
type ForEachFunction = procedure (datum : pointer; params : pointer);
type Beneficiado = object
nombre : string;
dia : integer;
mes : integer;
ano : integer;
edad : integer;
direccion : string;
integrantes_de_familia : integer;
tipo_de_sangre : string;
end;
type pBeneficiado = ^Beneficiado;
function compare_beneficiado (const beneficiado1 : pBeneficiado; const beneficiado2 : pBeneficiado) : integer;
begin
compare_beneficiado := strcomp (@beneficiado1^.nombre[1], @beneficiado2^.nombre[1]);
end;
function read_str (message : string) : string;
var string_var : string;
begin
write (stdout, message, ': ');
readln (string_var);
read_str := string_var;
end;
function read_integer (message : string) : integer;
var integer_var : integer;
begin
write (stdout, message, ': ');
readln (integer_var);
read_integer := integer_var;
end;
function read_field (var file_stream : text; var field : string) : boolean;
var c : char;
begin
field := '';
while not eof (file_stream) do
begin
read (file_stream, c);
if (c=#13) then
continue;
if (c=#9) or (c=#10) then
begin
read_field := true;
exit;
end
else
field := field + c;
end;
read_field := false;
end;
type pinteger = ^integer;
procedure print_beneficiado (datum : pBeneficiado; counter : pinteger);
begin
writeln (stdout, 'nombre : ', datum^.nombre);
writeln (stdout, 'dia : ', datum^.dia);
writeln (stdout, 'mes : ', datum^.mes);
writeln (stdout, 'ano : ', datum^.ano);
writeln (stdout, 'edad : ', datum^.edad);
writeln (stdout, 'direccion : ', datum^.direccion);
writeln (stdout, 'integrantes de familia: ', datum^.integrantes_de_familia);
writeln (stdout, 'tipo de sangre : ', datum^.tipo_de_sangre);
writeln (stdout);
inc (counter^);
end;
type pText = ^text;
procedure print_on_file (datum : pBeneficiado; file_stream : pText);
begin
write (file_stream^, datum^.nombre, #9);
write (file_stream^, datum^.dia, #9);
write (file_stream^, datum^.mes, #9);
write (file_stream^, datum^.ano, #9);
write (file_stream^, datum^.edad, #9);
write (file_stream^, datum^.direccion, #9);
write (file_stream^, datum^.integrantes_de_familia, #9);
write (file_stream^, datum^.tipo_de_sangre, #10);
end;
type ppointer = ^pointer;
type ppBeneficiado = ^pBeneficiado;
function array_insert (array_var : ppointer; var length : integer; datum : pointer) : ppointer;
begin
array_var := realloc (array_var, sizeof (pointer) * (length+1));
array_var[length] := datum;
inc (length);
array_insert := array_var;
end;
function array_search (array_var : ppointer; length : integer; compare: Comparable; const datum : pointer) : pointer;
begin
if length > 0 then
if compare (array_var[0], datum) = 0 then
array_search := array_var[0]
else
array_search := array_search (array_var + 1, length - 1, compare, datum)
else
array_search := nil;
end;
function array_remove (array_var : ppointer; var length : integer; const datum : pointer) : ppointer;
var i : integer = 0;
begin
while (i<length) and (datum<>array_var[i]) do
inc (i);
if i<length then
begin
for i:=i+1 to length-1 do
array_var[i-1] := array_var[i];
dec (length);
array_var := realloc (array_var, sizeof (pointer) * length);
end;
array_remove := array_var;
end;
procedure quick_sort (array_var : ppointer; first_index : integer; last_index : integer; compare: Comparable);
var smaller, bigger : integer;
var pivot : pointer;
begin
smaller := first_index;
bigger := last_index;
if last_index>first_index then
begin
pivot := array_var[(first_index+last_index) div 2];
while smaller <= bigger do
begin
while (smaller < last_index) and (compare (array_var[smaller], pivot)<0) do
inc (smaller);
while (bigger > first_index) and (compare (array_var[bigger], pivot)>0) do
dec (bigger);
if smaller <= bigger then
begin
pivot := array_var[smaller];
array_var[smaller] := array_var[bigger];
array_var[bigger] := pivot;
inc (smaller);
dec (bigger);
end;
end;
if first_index < bigger then
quick_sort (array_var, first_index, bigger, compare);
if smaller < last_index then
quick_sort (array_var, smaller, last_index, compare);
end;
end;
procedure array_foreach (array_var : ppointer; length : integer; params : pointer; func : ForEachFunction);
begin
if length>0 then
begin
func (array_var[0], params);
array_foreach (array_var + 1, length - 1, params, func);
end;
end;
const path = 'beneficiados.tsv';
var datum : ^Beneficiado = nil;
var beneficiado1 : Beneficiado;
var main_option : char;
var counter, suboption : integer;
var field : string = '';
var file_stream : text;
var beneficiados : ppBeneficiado = nil;
var length : integer = 0;
begin
counter := 0;
if fileexists (path) then
begin
assign (file_stream, path);
reset (file_stream);
while read_field (file_stream, field) do
begin
new (datum);
datum^.nombre := field;
read_field (file_stream, field);
val (field, datum^.dia);
read_field (file_stream, field);
val (field, datum^.mes);
read_field (file_stream, field);
val (field, datum^.ano);
read_field (file_stream, field);
val (field, datum^.edad);
read_field (file_stream, field);
datum^.direccion := field;
read_field (file_stream, field);
val (field, datum^.integrantes_de_familia);
read_field (file_stream, field);
datum^.tipo_de_sangre := field;
beneficiados := ppBeneficiado (array_insert (ppointer (beneficiados), length, datum));
end;
close (file_stream);
end;
repeat
clrscr;
writeln (stdout, 'MEN'#233#10#13);
writeln (stdout, '1.- Altas');
writeln (stdout, '2.- Consultas');
writeln (stdout, '3.- Actualizaciones');
writeln (stdout, '4.- Bajas');
writeln (stdout, '5.- Ordenar registros');
writeln (stdout, '6.- Listar registros');
writeln (stdout, '7.- Salir');
write (stdout, 'Selecciona una opci'#162'n: ');
repeat
main_option := readkey;
until (main_option>='1') and (main_option<='7');
writeln (stdout, main_option, #10#13);
if (beneficiados = nil) and (main_option <> '1') and (main_option <> '7') then
begin
write (stdout, 'No hay registros.'#10#13#10#13'Presiona una tecla para continuar . . . ');
readkey;
continue;
end;
if main_option<'5' then
begin
beneficiado1.nombre := read_str ('Ingresa el nombre del beneficiado');
datum := array_search (ppointer (beneficiados), length, Comparable(@compare_beneficiado), @beneficiado1);
if datum <> nil then
begin
writeln (stdout);
print_beneficiado (datum, @counter);
end;
end;
if (main_option = '1') and (datum <> nil) then
writeln (stdout, 'El registro ya existe.')
else if (main_option>='2') and (main_option<='4') and (datum = nil) then
writeln (stdout, #10#13'Registro no encontrado.')
else case main_option of
'1':
begin
new (datum);
datum^.nombre := beneficiado1.nombre;
datum^.dia := read_integer ('Ingresa el dia');
datum^.mes := read_integer ('Ingresa el mes');
datum^.ano := read_integer ('Ingresa el ano');
datum^.edad := read_integer ('Ingresa el edad');
datum^.direccion := read_str ('Ingresa el direccion');
datum^.integrantes_de_familia := read_integer ('Ingresa el integrantes de familia');
datum^.tipo_de_sangre := read_str ('Ingresa el tipo de sangre');
beneficiados := ppBeneficiado (array_insert (ppointer (beneficiados), length, datum));
writeln (stdout, #10#13'Registro agregado correctamente.');
end;
'3':
begin
writeln (stdout, 'Men'#163' de actualizaci'#162'n de campos');
writeln (stdout, '1.- dia');
writeln (stdout, '2.- mes');
writeln (stdout, '3.- ano');
writeln (stdout, '4.- edad');
writeln (stdout, '5.- direccion');
writeln (stdout, '6.- integrantes de familia');
writeln (stdout, '7.- tipo de sangre');
repeat
suboption := read_integer ('Selecciona el n'#163'mero de campo a modificar');
if (suboption<1) or (suboption>7) then
writeln (stdout, 'Opci'#162'n incorrecta');
until (suboption>=1) and (suboption<=7);
case suboption of
1: datum^.dia := read_integer ('Ingresa el nuevo dia');
2: datum^.mes := read_integer ('Ingresa el nuevo mes');
3: datum^.ano := read_integer ('Ingresa el nuevo ano');
4: datum^.edad := read_integer ('Ingresa el nuevo edad');
5: datum^.direccion := read_str ('Ingresa el nuevo direccion');
6: datum^.integrantes_de_familia := read_integer ('Ingresa el nuevo integrantes de familia');
7: datum^.tipo_de_sangre := read_str ('Ingresa el nuevo tipo de sangre');
end;
writeln (stdout, #10#13'Registro actualizado correctamente.');
end;
'4':
begin
beneficiados := ppBeneficiado (array_remove (ppointer (beneficiados), length, datum));
dispose (datum);
writeln (stdout, 'Registro eliminado correctamente.');
end;
'5':
begin
quick_sort (ppointer (beneficiados), 0, length - 1, Comparable(@compare_beneficiado));
writeln (stdout, 'Registros ordenados correctamente.');
end;
'6':
begin
counter := 0;
array_foreach (ppointer (beneficiados), length, @counter, ForEachFunction (@print_beneficiado));
writeln (stdout, 'Total de registros: ', counter, '.');
end;
end;
if (main_option<'7') then
begin
write (stdout, #10#13'Presiona una tecla para continuar . . . ');
readkey;
end;
until main_option = '7';
assign (file_stream, path);
rewrite (file_stream);
array_foreach (ppointer (beneficiados), length, @file_stream, ForEachFunction (@print_on_file));
close (file_stream);
end.