Directorio de trabajadores (Altas, bajas, modificaciones, busqueda)

pascal:
        uses
                crt;
        const
                F_nombres='nombres.txt';
                F_dependencias='dependencias.txt';
        F_control='control.txt';
        type
                item_nombres = record
                                id:string;
                nombre:string;
                                apellido:string;
                                telefono:string;
                                calle:string;
                                borrado:boolean;
                        end;
                item_dependencias = record
                                id:string;
                                nombre:string;
                                telefono: string;
                                borrado:boolean;
                        end;
                item_control = record
                                id:string;
                                id_nombre:string;
                                id_dependencia:string;
                                borrado:boolean;
                        end;
                archivo_nombres = file of item_nombres;
                archivo_dependencias = file of item_dependencias;
                archivo_control = file of item_control;
        array_n=array [1..100] of item_nombres;
        array_d=array [1..100] of item_dependencias;
        var
                elemento_nombres,aux_n:item_nombres;
                elemento_dependencias,aux_d:item_dependencias;
                elemento_control,aux_c:item_control;
                tecla:char;
                File_nombres,fnaux:archivo_nombres;
                File_dependencias,fdaux:archivo_dependencias;
                File_control,fcaux:archivo_control;
                opc:char;
                palabra:string;
                c:integer;
                d:char;
                posicion:integer;
                insertar:boolean;
        id:string;
        a_n:array_n;
        a_d:array_d;
        cantidad:integer;
        borrado:boolean;
        encontrado:boolean;
       
        procedure quitar_eliminados;
        begin
           assign(Fnaux,'fnaux');
            rewrite(Fnaux);
            close(Fnaux);
                assign(File_nombres,F_nombres);
                        {$i-}
            reset(File_nombres);
            {$i+}
            if IOResult<>0 then
                rewrite(File_nombres);
            while not(eof(File_nombres)) do
                begin
                        read(File_nombres,elemento_nombres);
                    if elemento_nombres.borrado = false then
                        begin
                                assign(Fnaux,'fnaux');
                            {$i-}
                            reset(Fnaux);
                            {$i+}
                            if IOResult<>0 then
                                        rewrite(Fnaux);
                            seek(Fnaux,FileSize(Fnaux));
                            write(Fnaux,elemento_nombres);
                                                        close(Fnaux);
                        end;
                end;
            rewrite(File_nombres);
                        assign(fnaux,'fnaux');
            {$i-}
            reset(fnaux);
            {$i+}
            if IOResult<>0 then
                rewrite(fnaux);
            seek(Fnaux,0);
            while not(eof(fnaux)) do
                begin
                        read(fnaux,elemento_nombres);
                    write(File_nombres,elemento_nombres);
                end;
            close(fnaux);
            close(File_nombres);

            assign(Fdaux,'fdaux');
            rewrite(Fdaux);
            close(Fdaux);
                assign(File_dependencias,F_dependencias);
                        {$i-}
            reset(File_dependencias);
            {$i+}
            if IOResult<>0 then
                rewrite(File_dependencias);
            while not(eof(File_dependencias)) do
                begin
                        read(File_dependencias,elemento_dependencias);
                    if elemento_dependencias.borrado = false then
                        begin
                                assign(Fdaux,'fdaux');
                            {$i-}
                            reset(Fdaux);
                            {$i+}
                            if IOResult<>0 then
                                        rewrite(Fdaux);
                            seek(Fdaux,FileSize(Fdaux));
                            write(Fdaux,elemento_dependencias);
                                                        close(Fdaux);
                        end;
                end;
            rewrite(File_dependencias);
                        assign(fdaux,'fdaux');
            {$i-}
            reset(fdaux);
            {$i+}
            if IOResult<>0 then
                rewrite(fdaux);
            seek(Fdaux,0);
            while not(eof(fdaux)) do
                begin
                        read(fdaux,elemento_dependencias);
                    write(File_dependencias,elemento_dependencias);
                end;
            close(fdaux);
            close(File_dependencias);

            assign(Fcaux,'fcaux');
            rewrite(Fcaux);
            close(Fcaux);
                assign(File_control,F_control);
                        {$i-}
            reset(File_control);
            {$i+}
            if IOResult<>0 then
                rewrite(File_control);
            while not(eof(File_control)) do
                begin
                    read(File_control,elemento_control);
                    if elemento_control.borrado = false then
                        begin
                                assign(Fcaux,'fcaux');
                            {$i-}
                            reset(Fcaux);
                            {$i+}
                            if IOResult<>0 then
                                        rewrite(Fcaux);
                            seek(Fcaux,FileSize(Fcaux));
                            write(Fcaux,elemento_control);
                                                        close(Fcaux);
                        end;
                end;
            rewrite(File_control);
                        assign(fcaux,'fcaux');
            {$i-}
            reset(fcaux);
            {$i+}
            if IOResult<>0 then
                rewrite(fcaux);
            seek(Fcaux,0);
            while not(eof(fcaux)) do
                begin
                    read(fcaux,elemento_control);
                    write(File_control,elemento_control);
                end;
            close(fcaux);
            close(File_control);
        end;


    begin
                textcolor(white);
                palabra:='';
                repeat
                        clrscr;
            quitar_eliminados;
                        writeln('1.- Insertar nombre ');
                        writeln('2.- Insertar dependencia ');
            writeln;
            writeln('3.- Busqueda por nombre');textcolor(white);
                 writeln('4.- Busqueda por dependencia');
            writeln;
            writeln('5.- Modificar nombre');
            writeln('6.- Modificar dependencia');
            writeln;
            writeln('7.- Eliminar nombre');
            writeln('8.- Eliminar dependencia');
                        writeln;

            opc:=readkey;
                        case opc of
                                '1':
                                begin
                                        assign(File_dependencias,F_dependencias);
                                        {$i-}
                                        reset(File_dependencias);
                                        {$i+}
                                        if IOResult<>0 then
                                                rewrite(File_dependencias);
                                        if FileSize(File_dependencias) > 0 then
                                                begin
                                                        assign(File_nombres,F_nombres);
                                                        {$i-}
                                                        reset(File_nombres);
                                                        {$i+}
                                                        if IOResult<>0 then
                                                                rewrite(File_nombres);
                                                        with elemento_nombres do
                                                                repeat
                                                                        clrscr;
                                                                        write('nombre: ');readln(nombre);
                                                                        write('apellido: ');readln(apellido);
                                                                        write('telefono: ');readln(telefono);
                                                                        write('direccion: ');readln(calle);
                                                                        str(FileSize(File_nombres)+1,id);
                                                                        seek(File_nombres,FileSize(File_nombres));
                                                                        elemento_nombres.borrado:=false;
                                                                        write(File_nombres,elemento_nombres);
                                                                        posicion:=0;
                                                                        Repeat
                                                                                Repeat
                                                                                        clrscr;
                                                                                        read(File_dependencias,elemento_dependencias);
                                                                                        write(elemento_dependencias.id,' - ',elemento_dependencias.nombre);
                                                                                        seek(File_dependencias,posicion);
                                                                                        d:=readkey;
                                                                                        if(d='-') AND (FilePos(File_dependencias) > 0)then
                                                                                                begin
                                                                                                        posicion:=posicion-1;
                                                                                                        seek(File_dependencias,posicion)
                                                                                                end;
                                                                                        if(d='+') AND (FilePos(File_dependencias) < FileSize(File_dependencias)-1)then
                                                                                                begin
                                                                                                        posicion:=posicion+1;
                                                                                                        seek(File_dependencias,posicion)
                                                                                                end;
                                                                                until (ord(d)=27)OR(ord(d)=13);
                                                                                assign(File_control,F_control);
                                                                                {$i-}
                                                                                reset(File_control);
                                                                                {$i+}
                                                                                if IOResult<>0 then
                                                                                        rewrite(File_control);
                                                                                insertar:=true;
                                                                                while not(eof(File_control)) do
                                                                                        begin
                                                                                                read(File_control,elemento_control);
                                                                                                if (elemento_nombres.id = elemento_control.id_nombre) AND
                                                                                                   (elemento_dependencias.id = elemento_control.id_dependencia)
                                                                                                then
                                                                                                        begin
                                                                                                                insertar:=false;
                                                                                                                break;
                                                                                                        end
                                                                                                                else
                                                                                                        begin
                                                                                                                insertar:=true;
                                                                                                        end;
                                                                                        end;
                                                                                if insertar=true then
                                                                                        begin
                                                                                                str(FileSize(File_control),elemento_control.id);
                                                                                                elemento_control.id_nombre:=elemento_nombres.id;
                                                                                                elemento_control.id_dependencia:=elemento_dependencias.id;
                                                                                                seek(File_control,FileSize(File_control));
                                                elemento_control.borrado:=false;
                                                                                                write(File_control,elemento_control);
                                                                                        end;
                                                                                writeln;
                                                                                if insertar=false then
                                                                                        writeln('Esta persona ya pertenecia a esta dependencia');
                                                                                writeln('¨Deseas asignarle otra dependencia? s/n');
                                                                                tecla:=upcase(readkey);
                                                                                close(File_control);
                                                                        until tecla='N';
                                                                writeln('Desea introducir otra persona? s/n');
                                                                tecla:=upcase(readkey);
                                                                until tecla='N';
                                                        close(File_nombres);
                                                end
                                                        else
                                                begin
                                                        write('ERROR: Debe haber al menos una dependencia antes!');
                                                        readkey;
                                                end;
                                        close(File_dependencias);
                                end;
                                '2':
                                begin
                                        assign(File_dependencias,F_dependencias);
                                        {$i-}
                                        reset(File_dependencias);
                                        {$i+}
                                        if IOResult<>0 then
                                                rewrite(File_dependencias);
                                        with elemento_dependencias do
                                                repeat
                                                clrscr;
                                                        write('dependencia: ');readln(nombre);
                                                        write('telefono: ');readln(telefono);
                                                        str(FileSize(File_dependencias)+1,id);
                                                        seek(File_dependencias,FileSize(File_dependencias));
                                                        elemento_dependencias.borrado:=false;
                                                        write(File_dependencias,elemento_dependencias);
                                                        writeln;
                                                writeln('Desea introducir otra dependencia? s/n');
                                                tecla:=upcase(readkey);
                                                until tecla='N';
                                        close(File_dependencias);
                                end;
       
                                '3':
                                begin
                                        clrscr;
                                                repeat
                                                        tecla:=readkey;
                                                        clrscr;
                                                        case ord(tecla) of
                                                                                8:
                                                                                begin
                                                                                        delete(palabra,length(palabra),1);
                                                                                end;
                                                                        else
                                                                                begin
                                                                                        palabra:=concat(palabra,tecla);
                                                                                end;
                                                                end;
                                                        gotoxy(1,1);writeln(palabra);
                                                        assign(File_nombres,F_nombres);
                                                        {$i-}
                                                        reset(File_nombres);
                                                        {$i+}
                                                        if IOResult<>0 then
                                                                rewrite(File_nombres);
                                                        c:=0;
                                                        while not(eof(File_nombres)) do
                                                                begin
                                                                        read(File_nombres,elemento_nombres);
                                                                        if
                                                                                (pos(upcase(palabra),upcase(elemento_nombres.id)) > 0) OR
                                                                                (pos(upcase(palabra),upcase(elemento_nombres.nombre)) > 0) OR
                                                                                (pos(upcase(palabra),upcase(elemento_nombres.apellido)) > 0)
                                                                                then
                                                                                begin
                                                                                        c:=c+1;
                                                                                        write(elemento_nombres.id,'.- ');
                                                                                        write(elemento_nombres.nombre,' ');
                                                                                        write(elemento_nombres.apellido);
                                                                                        writeln;
                                            writeln('    -Calle: ',elemento_nombres.calle);
                                            writeln('    -Telefono: ',elemento_nombres.telefono);
                                            writeln;
                                                                                        assign(File_control,F_control);
                                                                                        {$i-}
                                                                                        reset(File_control);
                                                                                        {$i+}
                                                                                        if IOResult<>0 then
                                                                                                rewrite(File_control);
                                                                                        while not(eof(File_control)) do
                                                                                                begin
                                                                                                        read(File_control,aux_c);
                                                                                                        if elemento_nombres.id = aux_c.id_nombre then
                                                                                                                begin
                                                                                                                        {}
                                                                                                                        assign(File_dependencias,F_dependencias);
                                                                                                                        {$i-}
                                                                                                                        reset(File_dependencias);
                                                                                                                        {$i+}
                                                                                                                        if IOResult<>0 then
                                                                                                                                rewrite(File_dependencias);
                                                                                                                        while not(eof(file_dependencias)) do
                                                                                                                                begin
                                                                                                                                        read(file_dependencias,aux_d);
                                                                                                                                        if aux_d.id = aux_c.id_dependencia then
                                                                                                                                                begin
                                                                                                                                                        textcolor(Green);
                                                                                                                                                        writeln('     -',aux_d.nombre);
                                                                                                                                                        writeln('       -Telefono: ',aux_d.telefono);
                                                                                                                                                        textcolor(White);
                                                                                                                                                        break;
                                                                                                                                                end;
                                                                                                                                end;
                                                                                                                        close(File_dependencias);
                                                                                                                        {}
                                                                                                                end;
                                                                                                end;
                                                                                        close(File_control);
                                                                                end;
                                                                end;
                                                        close(File_nombres);
                                                until ord(tecla)=27;
                                                palabra:='';
                                end;
       
                                '4':
                                begin
                                        clrscr;
                                                repeat
                                                        tecla:=readkey;
                                                        clrscr;
                                                        case ord(tecla) of
                                                                                8:
                                                                                begin
                                                                                        delete(palabra,length(palabra),1);
                                                                                end;
                                                                        else
                                                                                begin
                                                                                        palabra:=concat(palabra,tecla);
                                                                                end;
                                                                end;
                                                        gotoxy(1,1);writeln(palabra);
                                                        assign(File_dependencias,F_dependencias);
                                                        {$i-}
                                                        reset(File_dependencias);
                                                        {$i+}
                                                        if IOResult<>0 then
                                                                rewrite(File_dependencias);
                                                        c:=0;
                                                        while not(eof(File_dependencias)) do
                                                                begin
                                                                        read(File_dependencias,elemento_dependencias);
                                                                        if
                                                                                (pos(upcase(palabra),upcase(elemento_dependencias.id)) > 0) OR
                                                                                (pos(upcase(palabra),upcase(elemento_dependencias.nombre)) > 0)
                                                                                then
                                                                                begin
                                                                                        c:=c+1;
                                                                                        write(elemento_dependencias.id,'.- ');
                                                                                        write(elemento_dependencias.nombre);
                                                                                        writeln;
                                            writeln('    -Telefono: ',elemento_dependencias.telefono);
                                            writeln;
                                            assign(File_control,F_control);
                                                                                        {$i-}
                                                                                        reset(File_control);
                                                                                        {$i+}