Professional Documents
Culture Documents
UNIDAD IV
Ing. Mario M. Lpez Winnipeg
CONTENIDO
Cortes de control de 2 o mas niveles.
Dispersin Hash
Segmentacin.
Intercalacin
Purga.
Segmentacin ordenada.
Cortes de control ordenados.
PROV
MESA
VOTOS
SCZ
COTOCA
15
SCZ
WARNES
123
CBBA
CERCAD
O
223
CBBA
PUNATA
23
CBBA
PUNATA
345
LPZ
MURILLO
23
PROV
MES
A
VOTO
S
SCZ
COTOCA
15
SCZ
WARNES
123
CBBA
CERCAD
O
223
CBBA
PUNATA
23
CBBA
PUNATA
345
LPZ
MURILLO
23
LABORATORIO
Crear un archivo texto con la informacin de la votacin
de los departamentos de Bolivia
LABORATORIO
Crear la Clase CorteControl con los siguientes atributos
votos array[1..100] of Tvoto, donde tvoto es un tipo de
dato definido de la siguiente forma:
Tvoto=record
dpto : string;
prov : string;
mesa: integer;
nro: integer;
End;
LABORATORIO
CEMCorteControl = Class(Exception);
CCorteControl = Class
Private
Votos: array [1..Max ] of TVoto;
N : Integer;
function llenarespacio(S:string;Tam:byte;tipo:char):String;
Public
Constructor Crear;
Procedure CargarVotos(NombreArchivo:String);
Procedure Mostrar(Pant:Tmemo);
End;
LABORATORIO
implementation
function CCorteControl.llenarespacio(S:string;Tam:byte;tipo:char):String;
var i:integer;
begin
if tipo='D' then
for i:= length(s) to Tam do
// Llenando espacios a la
s:=s+' ';
// Derecha de S
if tipo='A' then
for i:= length(s) to Tam do
// Llenando espacios adelante
s:=' '+s;
// de S
if tipo='C' then
for i:= length(s) to Tam do
if odd(i) then s:=' '+s
// Llenando espacios a ambos
else s:=s+' ';
// lados de S
llenarEspacio:=S;
end;
LABORATORIO
Constructor CCorteControl.Crear;
begin
n:=0;
end;
Procedure CCorteControl.CargarVotos(NombreArchivo:String);
var F:TextFile;
R:Tvoto;
Linea:string;
begin
Assign(F,nombreArchivo);
reset(f);
R.prov:=copy(linea,1,pos(' ',linea)-1);
while not eof(f) do
delete(linea,1, pos(' ',linea));
begin
readln(f,Linea);
R.mesa:=StrToInt( copy(linea,1,pos(' ',linea)linea:=linea+' ';
1) );
R.dpto:=copy(linea,1,pos(' ',linea)-1);
delete(linea,1, pos(' ',linea));
delete(linea,1, pos(' ',linea));
R.nro:=StrToInt( copy(linea,1,pos(' ',linea)-1) );
delete(linea,1, pos(' ',linea));
n:=n+1;
votos [n]:=R;
end;
end;
LABORATORIO
Procedure CCorteControl.Mostrar(Pant:Tmemo);
var linea:string;
i:integer;
begin
pant.Clear;
i:=1;
while i<=N do
Begin
linea:=LlenarEspacio(
votos[i].Dpto ,10 ,'D') +
LlenarEspacio(
votos[i].prov ,10 ,'D')+
LlenarEspacio(IntToStr(votos[i].mesa),5 ,'D') +
LlenarEspacio(IntToStr(votos[i].nro) ,6 ,'A');
pant.Lines.Append(linea);
i:=i +1;
end;
end;
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR
LABORATORIO
Public
AplCorte: CCorteContro;
begin
aplCorte:=ccortecontrol.crear;
end;
begin
AplCorte.cargarvotos('C:\Users\m
ario lopez\Desktop\votos.txt');
end;
begin
aplCorte.mostrar(memo1);
end;
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR
LABORATORIO
begin
aplCorte.CorteUnNivel(memo1);
end;
LABORATORIO
while i<=N do
Begin
linea:=LlenarEspacio(votos[i].Dpto,10,'D') +
Procedure
LlenarEspacio(votos[i].prov ,10,'D')+
CCorteControl.CorteUnNivel(Pant:Tmemo);
LlenarEspacio(IntToStr(votos[i].mesa),5,'D') +
LlenarEspacio(IntToStr(votos[i].nro),6,'A');
var linea:string;
if CampoDpto=votos[i].Dpto then
i:integer;
Begin
CampoDpto:String;
pant.Lines.Append(linea);
totalDpto:=TotalDpto + votos[i].nro;
TotalDpto: integer;
end
begin
else
pant.Clear;
Begin
pant.Lines.Append(LlenarEspacio('-------------------',35,'A'));
i:=1;
pant.Lines.Append(LlenarEspacio('Total Dpto '+
TotalDpto:=0;
intToStr(TotalDpto),34,'A'));
CampoDpto:= votos[1].Dpto;
pant.Lines.Append(linea);
CampoDpto:=votos[i].Dpto;
totalDpto:=votos[i].nro;
End;
i:=i +1;
end;
pant.Lines.Append(LlenarEspacio('-------------------',35,'A'));
pant.Lines.Append(LlenarEspacio('Total Dpto '+intToStr(TotalDpto),34,'A'));
end;
LABORATORIO
begin
aplcorte.CorteDosNivel(memo1);
end;
LABORATORIO
DISPERSION - HASH
Una tabla HASH es una estructura de datos que soporta
la eliminacin, recuperacin e insercin de elementos de
forma muy rpida.
Una funcin hash es una funcin que asigna
una clave a un ndice dentro del arreglo.
Este ndice indica donde esta almacenado el
elemento.
INSERCION
Ejemplo: insertar el valor Hanson en el vector A
Hanson
Hash ( Valor)
Hanson
INSERCION
Ultimo digito +1
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR
INSERCION
LABORATORIO
CVectorHash = Class
Private
MaxIndex : Integer;
Elementos : Attay [ 1.. Max ] of String;
private
function hash( valor:String):Integer;
Public
Constructor Crear;
Procedure Poner( Elemento : Integer);
Function MaximoIndice : Word;
Function ComoString : String;
End;
LABORATORIO
Function CvectorHash.Crear;
Begin
MaxIndex:=0;
End;
Procedure CVectorHash.ComoString:String;
Var i:integer;
s:string;
Begin
s:=;
For i:= 1 to maxindex do
if length(elementos[ i ] )>0 then
s:= s + elementos[ i ]+ , ;
comostring:=s;
End;
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR
LABORATORIO
LABORATORIO
Begin
Apl: CvectorHash.crear;
End;
Begin
Apl.poner( edit1.text );
End;
Begin
label1.caption:= apl.comostring;
End;
SEGMENTACION
r
a
g
ti
s
e
v
in
r
o
p
a
e
r
Ta
PURGA
La purga consiste en eliminar los elementos repetidos del Vector
1
Procedure Cvector.Purga;
Var
Valor,i,j: integer;
Begin
i:=1;
While i<= N do
Begin
Valor:= Elementos[ i ];
j:= i+1;
while j<= N do
Begin
if elementos[j ] = valor then
Begin
for k:= j+1 to N do
elementos[ k -1 ]:= elementos[k-1];
n:=n-1;
PURGA
End;
j:=j+1;
// Verificar si esta lnea esta correcta.
End;
i:=i+1;
End;
End;
TECNICAS ALGORITMICAS
EN UNO O MAS OBJETOS DE TIPO VECTOR
INTERCALAR
1
10
10
20
20
30
30
40
40
INTERCALAR
SEGMENTACION ORDENADA
r
a
g
ti
s
e
v
in
r
o
p
a
e
r
Ta
CORTE DE COTROL
ORDENADO
Los cortes de control ordenados se refieren
a la condicin que debe cumplir un vector
para poder aplicar el algoritmo de corte de
control. Caso contrario es necesario
ordenar el vector de acuerdo a los niveles
que se tengan que realizar los cortes de
control.
LABORATORIO
If votos[i].dpto. > votos[i+1].dpto. then
// Intercambiar registro
else
if votos[i].dpto.= votos[i+1].dpto. then
if votos[i].prov > votos[i +1].prov then
// Intercambiar Registro.