Bueno antes que nada, bienvenido al club...
Y pronto pasará por aquí un moderador llamado Casimiro Noveti y te hará algunas observaciones... asi que me adelanto:
1) Para un mayor entendimiento de tu problema, puedes poner tu código enmarcado en las etiquetas: [Delphi.] y [/Delphi.] (sin los puntos)... de esta forma tu código será mas legible...
2) Normalmente empezamos un nuevo hilo para cada problema, aunque parezca el mismo, haciendo referencia con un link a algún post anterior... Date un paseo por la
guía de estilo para que te informes mejor...
3) ¿Tu problema es "el mismo"? ¿que quieres decir con eso?, si es el mismo, ¿no te sirve la misma solución?, ¿en que falla tu unidad?... lo digo porque no siempre tenemos el tiempo de tomar tu código, compilarlo y analizarlo... debes ayudarnos un poco con algún análisis, ya que es muy común por aquí que la gente pase para que le ayudemos a hacer la tarea con código que ellos mismos no entienden....
Un saludo,
Código Delphi
[-]
unit UCVector;
interface
uses SysUtils;
Const
MaxE = 1024;
Type
ConjuntoElementos = Array[1..MaxE] of Integer;
CEMVector = Class(Exception);
CVector = Class
Private
Dimension : Word;
Elementos : ConjuntoElementos;
Procedure Intercambiar( i,j : Word );
Public
Constructor Crear;
Procedure Dimensionar( N : Word );
Procedure Adicionar( Elemento : Integer); Procedure Insertar( Pos : Word ; Elemento : Integer); Function Elemento( Pos : Word ): Word ;
Procedure Eliminar( Pos : Word );
Function NumeroElementos : Word;
Procedure Invertir;
Procedure Rotar( N : Word );
Function BusquedaSecuencial( Elemento : Integer ) : Word;
Function BusquedaBinaria( Elemento : Integer ) : Word;
Procedure OrdenarIntercambio;
Procedure OrdenarDesc;
Procedure OrdenarBurbuja;
Procedure OrdenarShell;
Procedure EliminarRepetidos;
Function NumeroRepetidos : Word;
Procedure OrdenarFrecuencia;
Function ComoString : String;
End;
implementation
Procedure CVector.Intercambiar( i,j : Word );
Var
Aux : Word;
Begin
Aux := Elementos[ i ];
Elementos[ i ] := Elementos[ j ];
Elementos[ j ] := Aux;
End;
Constructor CVector.Crear;
Begin
Dimension := 0;
End;
Procedure CVector.Dimensionar( N : Word );
Begin
Dimension :=N;
End;
Procedure CVector.Adicionar( Elemento : Integer);
Begin
Try
Inc( Dimension );
Elementos[ Dimension ] := Elemento;
Except
Raise CEMVector.Create('CVector.Adicionar : Fuera de RANGO...');
End;
End;
Procedure CVector.Insertar( Pos : Word ; Elemento : Integer);
Var
i : Word;
Begin
Elementos[ Pos ] := Elemento;
End;
Function CVector.Elemento( Pos : Word ): Word ;
Begin
Result := Elementos[ Pos ];
End;
Procedure CVector.Eliminar( Pos : Word ); Var
i : Word;
Begin
Try
For i := Pos to Dimension-1 do
Elementos[ i ] := Elementos[ i+1 ];
Dec(Dimension);
Except
Raise CEMVector.Create('CVector.Eliminar : Fuera de RANGO...');
End;
End;
Function CVector.NumeroElementos : Word;
Begin
Result := Dimension;
End;
Procedure CVector.Invertir;
Var
Media , i : Integer;
Begin
Media := Dimension div 2;
For i :=1 To Media do
InterCambiar( i , Dimension- i+1 );
End;
Procedure CVector.Rotar( N : Word );
Var
i , j , NMod ,Aux : Word;
Begin
NMod := N Mod Dimension;
For i := 1 to NMod do
Begin
Aux := Elementos[ 1 ];
For j := 2 To Dimension do
Elementos[ j-1 ] := Elementos[ j ];
Elementos[ Dimension ]:= Aux;
End;
End;
Function CVector.BusquedaSecuencial( Elemento : Integer ) : Word;
Var
Pos : Word;
Begin
Pos := 1;
While( Pos<= Dimension )and( Elementos[ Pos ]<> Elemento )do
Inc( Pos );
If( Pos > Dimension )Then
Pos := 0;
Result := Pos;
End;
Function CVector.BusquedaBinaria( Elemento : Integer ) : Word;
Var
Ini , Fin , Media :word;
Sw:boolean;
Begin
Ini := 1;
Fin := Dimension;
Sw := False;
Media := 0;
While (Ini <= Fin ) and ( not Sw ) Do
Begin
Media :=( Fin + Ini )div 2;
If( Elemento = Elementos[ Media ] )Then
Sw:=true
Else
If( Elemento < elementos[ Media ] )Then
Fin := Media - 1
Else
Ini := Media + 1;
End;
Result := Media;
End;
Procedure CVector.OrdenarIntercambio; Var
i , j: Word;
Begin
For i := 1 to Dimension-1 do
For j := i+1 to Dimension do
if( Elementos[ j ] < Elementos[ i ] )then Intercambiar( j , i );
End;
Procedure CVector.OrdenarBurbuja;
Var
i , j , Aux, Aux2 : Word;
Begin
Aux2:= (Dimension DIV 2)+1;
For i := 1 To Aux2-1 Do
For j := Aux2 To Dimension-i Do
If( Elementos [ j ] >Elementos [ j+1 ] )Then
Begin
Aux := Elementos [ j ];
Elementos [ j ] := Elementos [ j+1 ];
Elementos [ j+1 ] := Aux;
End;
Aux2:= (Dimension DIV 2)+1;
For i := 1 To Aux2-1 Do
For j := 1 To Aux2-1 Do
If( Elementos [ j ] Then
Begin
Aux := Elementos [ j ];
Elementos [ j ] := Elementos [ j+1 ];
Elementos [ j+1 ] := Aux;
End;
End;
procedure CVector.OrdenarDesc; Var
i , j: Word;
Begin
For i := 1 to Dimension-1 do
For j := i+1 to Dimension do
if( Elementos[ j ] > Elementos[ i ] )then
Intercambiar( j , i );
End;
Procedure CVector.OrdenarShell;
Var
Inter , i , j , k : Integer;
Begin
Inter := Dimension div 2;
While( Inter > 0 )Do
Begin
For i := (Inter + 1) to Dimension do
Begin
j := i - Inter;
While( j > 0)do
Begin
k := j + Inter;
If( Elementos [ j ]<=Elementos [ k ])then
j := 0
Else
Intercambiar( j, k);
j := j - Inter;
End;
End;
Inter := Inter div 2;
End;
End;
Procedure CVector.EliminarRepetidos;
Var
i , Aux :Word;
Begin
Aux := Elementos[ 1 ];
i := 2;
While( i <= Dimension )do
Begin
if( Aux = Elementos[ i ] )then
Eliminar(i)
Else
Begin
Aux := Elementos[ i ];
inc(i);
End;
End;
End;
Function CVector.NumeroRepetidos : Word;
Var
i , Aux , Cont :Word;
sw : Boolean;
Begin
Aux := Elementos[ 1 ];
i := 2;
Cont := 0;
sw := true;
While( i <= Dimension )do
Begin
if( Aux = Elementos[ i ] )then
Begin
If( sw )then
Begin
Inc(Cont);
sw :=false;
End;
End
Else
Begin
Aux := Elementos[ i ];
sw :=true;
End;
inc(i);
End;
Result := Cont;
End;
Procedure CVector.OrdenarFrecuencia;
Var
Dato , Repe :CVector;
i , j , Aux , Cant :Word;
Begin
Dato := CVector.Crear;
Repe := CVector.Crear;
i := 1;
Aux := Elementos[ i ];
Cant := 1;
While( i < Dimension )do
Begin
i := i + 1;
if( Aux = Elementos[ i ] )then
Cant := Cant + 1
Else
Begin
Dato.Adicionar(Aux);
Repe.Adicionar(Cant);
Aux := Elementos[ i ];
Cant := 1;
End;
End;
Dato.Adicionar(Aux);
Repe.Adicionar(Cant);
For i := 1 to Repe.Dimension-1 do
For j := i+1 to Repe.Dimension do
If( Repe.Elemento( j ) < Repe.Elemento( i ) )then
Begin
Repe.Intercambiar( j , i );
Dato.Intercambiar( j , i );
End;
Dimension := 0;
for i:= 1 to Dato.Dimension do
Begin
Cant := Repe.Elemento( i );
Aux := Dato.Elemento( i );
for j:= 1 to Cant do
Adicionar(Aux);
End;
End;
Function CVector.ComoString : String;
Var
Cad : String;
Pos : Integer;
Begin
If( Dimension = 0 )then
Cad:='[]'
Else
Begin
Cad :='['+ IntToStr( Elementos[ 1 ] );
For Pos := 2 to Dimension do
Cad := Cad +','+ IntToStr( Elementos[ Pos ] );
Cad := Cad +']';
End;
Result := Cad;
End;
end.