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

	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+}
											if IOResult<>0 then
												rewrite(File_control);
											while not(eof(File_control)) do
												begin
													read(File_control,aux_c);
													if elemento_dependencias.id = aux_c.id_dependencia then
														begin
															{}
															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,aux_n);
																	if aux_c.id_nombre = aux_n.id then
																		begin
																			textcolor(Green);
																			writeln('     -',aux_n.nombre);
																			writeln('       -direccion: ',aux_n.calle);
																			writeln('       -telefono: ',aux_n.telefono);
																			textcolor(White);
																			break;
																		end;
																end;
															close(File_nombres);
															{}
														end;
												end;
											close(File_control);
										end;
								end;
							close(File_dependencias);
						until ord(tecla)=27;
						palabra:='';
                end;
                '5':
                begin
                	clrscr;
                    write('Id nombre a modificar: ');readln(id);
                    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.id = id then
                            	begin
                                	aux_n.id:=id;
                                    aux_n.borrado:=false;
                                    textcolor(green);writeln('Nombre actual: ',elemento_nombres.nombre);textcolor(white);
                                    write('Nombre nuevo: ');readln(aux_n.nombre);
                                    textcolor(green);writeln('Apellido actual: ',elemento_nombres.apellido);textcolor(white);
                                    write('Apellido nuevo: ');readln(aux_n.apellido);
                                    textcolor(green);writeln('Telefono actual: ',elemento_nombres.telefono);textcolor(white);
                                    write('Telefono nuevo: ');readln(aux_n.telefono);
                                    textcolor(green);writeln('Direccion nueva: ',elemento_nombres.calle);textcolor(white);
                                    write('Direccion nueva: ');readln(aux_n.calle);
                                    seek(File_nombres,FilePos(File_nombres)-1);
                                    write(File_nombres,aux_n);
                                    encontrado:=true;
                                    break;
                                end
                                	else
                                begin
                                    encontrado:=false;
                                end;
                        end;					
                    close(File_nombres);
                    repeat
                    	clrscr;
                        if encontrado=true then
                        	begin
                                writeln('Registro modificado satisfactoriamente');
                            	write('Presione Esc para continuar');
                            end
	                        	else
                            begin
                                textcolor(red);write('ERROR');textcolor(white);write(': No existe el registro con id ');textcolor(green);write(id);textcolor(white);writeln('.');
                                write('Presione Esc para continuar');
                            end;
                        tecla:=readkey;
                    until ord(tecla)=27;
                end;
                '6':
                begin
                	clrscr;
                    write('Id nombre a modificar: ');readln(id);
                    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.id = id then
                            	begin
                                	aux_d.id:=id;
                                    aux_d.borrado:=false;
                                    textcolor(green);writeln('Nombre actual: ',elemento_dependencias.nombre);textcolor(white);
                                    write('Nombre nuevo: ');readln(aux_d.nombre);
                                    textcolor(green);writeln('Telefono actual: ',elemento_dependencias.telefono);textcolor(white);
                                    write('Telefono nuevo: ');readln(aux_d.telefono);
                                    seek(File_dependencias,FilePos(File_dependencias)-1);
                                    write(File_dependencias,aux_d);
                                    encontrado:=true;
                                    break;
                                end
                                	else
                                begin
                                    encontrado:=false;
                                end;
                        end;					
                    close(File_dependencias);
                    repeat
                    	clrscr;
                        if encontrado=true then
                        	begin
                                writeln('Registro modificado satisfactoriamente');
                            	write('Presione Esc para continuar');
                            end
	                        	else
                            begin
                                textcolor(red);write('ERROR');textcolor(white);write(': No existe el registro con id ');textcolor(green);write(id);textcolor(white);writeln('.');
                                write('Presione Esc para continuar');
                            end;
                        tecla:=readkey;
                    until ord(tecla)=27;
                end;
                '7':
                begin
                	clrscr;
                	write('Id nombre a eliminar: ');readln(id);
                    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.id = id then
                            	begin
                                	elemento_nombres.borrado:=true;
                                    seek(file_nombres,FilePos(File_nombres)-1);
                                    write(file_nombres,elemento_nombres);
                                	borrado:=true;
                                    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.id_nombre = id then
                                            	begin
                                                	elemento_control.borrado:=true;
                                                    seek(File_control,FilePos(File_control)-1);
                                                    write(File_control,elemento_control);
                                                end;
                                        end;
                                    close(File_control);
                                    break;
                                end
                                	else
                                begin
                                	borrado:=false;
                                end;
                        end;					
                    close(File_nombres);
                    repeat
                    	clrscr;
                        if borrado=true then
                        	begin
                                writeln('Registro eliminado');
                            	write('Presione Esc para continuar');
                            end
	                        	else
                            begin
                                textcolor(red);write('ERROR');textcolor(white);write(': No existe el registro con id ');textcolor(green);write(id);textcolor(white);writeln('.');
                                write('Presione Esc para continuar');
                            end;
                        tecla:=readkey;
                    until ord(tecla)=27;
                end;
                '8':
                begin
                	clrscr;
                	write('Id dependencia a eliminar: ');readln(id);
                    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.id = id then
                            	begin
                                	elemento_dependencias.borrado:=true;
                                    seek(file_dependencias,FilePos(File_dependencias)-1);
                                    write(file_dependencias,elemento_dependencias);
                                	borrado:=true;
                                    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.id_dependencia = id then
                                            	begin
                                                	elemento_control.borrado:=true;
                                                    seek(File_control,FilePos(File_control)-1);
                                                    write(File_control,elemento_control);
                                                end;
                                        end;
                                    close(File_control);
                                    break;
                                end
                                	else
                                begin
                                	borrado:=false;
                                end;
                        end;					
                    close(File_dependencias);
                    repeat
                    	clrscr;
                        if borrado=true then
                        	begin
                                writeln('Registro eliminado');
                            	write('Presione Esc para continuar');
                            end
	                        	else
                            begin
                                textcolor(red);write('ERROR');textcolor(white);write(': No existe el registro con id ');textcolor(green);write(id);textcolor(white);writeln('.');
                                write('Presione Esc para continuar');
                            end;
                        tecla:=readkey;
                    until ord(tecla)=27;
                end;
            end;
		until ord(opc)=27;
	end.

¿Has encontrado algún error? ¿Tienes la solución? Dejame tu correción ;-)

Antes de comentar: Gran parte de los ejercicios propuestos no tienen librerías debido a que Wordpress las eliminó al verlas como etiquetas HTML. Si sabes/tienes/conoces las librerías que hacen falta, déjalo en los comentarios. Y lo mas importante: Todos los ejemplos fueron realizados por personas con únicamente conocimiento básico del lenguaje, no de programación.

Otro punto importante: Si vas a sugerir un segmento de código en algún lenguaje debes hacerlo así:

  • Si es lenguaje C [c]Código en C[/c]
  • Si es lenguaje Pascal [pascal]Aquí dentro el código de Pascal[/pascal].

De esta manera el código coloreas el código.

Otro punto importante para muchos que sienten que se les ignora: Todos los comentarios los reviso y en su debido momento los apruebo, pero ojo con el con lo siguiente:

Me reservo el derecho de alterar, publicar o no los comentarios as´ como cambiar mis condiciones en el momento que así lo requiera.

¿estas de acuerdo? entonces adelante que ya te he quitado bastante tiempo leyendo esta basura de advertencias :)