• Lenguaje

    Pascal

  • Descripción

    Agenda telefónica

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
program program_contactos;
uses cmem, crt, sysutils;

type Comparable = function (const datum1 : pointer; const datum2 : pointer) : integer;
type ForEachFunction = procedure (datum : pointer; params : pointer);

type Contacto = object
    alias : string;
    telefono_fijo : string;
    telefono_movil : string;
    correo_electronico : string;
end;
type pContacto = ^Contacto;

function compare_contacto (const contacto1 : pContacto; const contacto2 : pContacto) : integer;
begin
    compare_contacto := strcomp (@contacto1^.alias[1], @contacto2^.alias[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_contacto (datum : pContacto; counter : pinteger);
begin
    writeln (stdout, 'alias             : ', datum^.alias);
    writeln (stdout, 'telefono fijo     : ', datum^.telefono_fijo);
    writeln (stdout, 'telefono movil    : ', datum^.telefono_movil);
    writeln (stdout, 'correo electronico: ', datum^.correo_electronico);
    writeln (stdout);
    inc (counter^);
end;

type pText = ^text;
procedure print_on_file (datum : pContacto; file_stream : pText);
begin
    write (file_stream^, datum^.alias, #9);
    write (file_stream^, datum^.telefono_fijo, #9);
    write (file_stream^, datum^.telefono_movil, #9);
    write (file_stream^, datum^.correo_electronico, #10);
end;

type ppointer = ^pointer;
type ppContacto = ^pContacto;

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 = 'contactos.tsv';
var datum : ^Contacto = nil;
var contacto1 : Contacto;
var main_option : char;
var counter, suboption : integer;
var field : string = '';
var file_stream : text;
var contactos : ppContacto = 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^.alias := field;
            read_field (file_stream, field);
            datum^.telefono_fijo := field;
            read_field (file_stream, field);
            datum^.telefono_movil := field;
            read_field (file_stream, field);
            datum^.correo_electronico := field;
            contactos := ppContacto (array_insert (ppointer (contactos), 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 (contactos = 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
            contacto1.alias := read_str ('Ingresa el alias del contacto');
            datum := array_search (ppointer (contactos), length, Comparable(@compare_contacto), @contacto1);
            if datum <> nil then
            begin
                writeln (stdout);
                print_contacto (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^.alias := contacto1.alias;
                    datum^.telefono_fijo := read_str ('Ingresa el telefono fijo');
                    datum^.telefono_movil := read_str ('Ingresa el telefono movil');
                    datum^.correo_electronico := read_str ('Ingresa el correo electronico');
                    contactos := ppContacto (array_insert (ppointer (contactos), 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.- telefono fijo');
                    writeln (stdout, '2.- telefono movil');
                    writeln (stdout, '3.- correo electronico');
                    repeat
                        suboption := read_integer ('Selecciona el n'#163'mero de campo a modificar');
                            if (suboption<1) or (suboption>3) then
                        writeln (stdout, 'Opci'#162'n incorrecta');
                    until (suboption>=1) and (suboption<=3);
                    case suboption of
                        1: datum^.telefono_fijo := read_str ('Ingresa el nuevo telefono fijo');
                        2: datum^.telefono_movil := read_str ('Ingresa el nuevo telefono movil');
                        3: datum^.correo_electronico := read_str ('Ingresa el nuevo correo electronico');
                    end;
                    writeln (stdout, #10#13'Registro actualizado correctamente.');
                end;
            '4':
                begin
                    contactos := ppContacto (array_remove (ppointer (contactos), length, datum));
                    dispose (datum);
                    writeln (stdout, 'Registro eliminado correctamente.');
                end;
            '5':
                begin
                    quick_sort (ppointer (contactos), 0, length - 1, Comparable(@compare_contacto));
                    writeln (stdout, 'Registros ordenados correctamente.');
                end;
            '6':
                begin
                    counter := 0;
                    array_foreach (ppointer (contactos), length, @counter, ForEachFunction (@print_contacto));
                    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 (contactos), length, @file_stream, ForEachFunction (@print_on_file));
    close (file_stream);
end.