|
| 1 | +(* Para mi papá, la razón de ser quien soy. |
| 2 | + * |
| 3 | + * EJERCICIO: |
| 4 | + * Explora el concepto de clase y crea un ejemplo que implemente un inicializador, |
| 5 | + * atributos y una función que los imprima (teniendo en cuenta las posibilidades |
| 6 | + * de tu lenguaje). |
| 7 | + * Una vez implementada, créala, establece sus parámetros, modifícalos e imprímelos |
| 8 | + * utilizando su función. |
| 9 | + * |
| 10 | + * DIFICULTAD EXTRA (opcional): |
| 11 | + * Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas |
| 12 | + * en el ejercicio número 7 de la ruta de estudio) |
| 13 | + * - Deben poder inicializarse y disponer de operaciones para añadir, eliminar, |
| 14 | + * retornar el número de elementos e imprimir todo su contenido. |
| 15 | + *) |
| 16 | +program fduron; |
| 17 | + |
| 18 | +{$APPTYPE CONSOLE} |
| 19 | + |
| 20 | +{$R *.res} |
| 21 | + |
| 22 | +uses |
| 23 | + System.SysUtils; |
| 24 | + |
| 25 | +type |
| 26 | + TPersona = Class |
| 27 | + private |
| 28 | + FNombre: String; |
| 29 | + FEdad: Integer; |
| 30 | + FColorFavorito: String; |
| 31 | + FHobbie: string; |
| 32 | + function GetNombre: String; |
| 33 | + procedure SetNombre(const Value: String); |
| 34 | + function GetEdad: Integer; |
| 35 | + procedure SetEdad(const Value: Integer); |
| 36 | + public |
| 37 | + constructor Create(ANombre: String; Edad: Integer); |
| 38 | + property Nombre: String read GetNombre write SetNombre; |
| 39 | + property Edad: Integer read GetEdad write SetEdad; |
| 40 | + property ColorFavorito: String read FColorFavorito write FColorFavorito; |
| 41 | + property Hobbie: string read FHobbie write FHobbie; |
| 42 | + procedure ImprimirDatos; |
| 43 | + end; |
| 44 | + |
| 45 | + TPila = Class //LIFO |
| 46 | + private |
| 47 | + FListaPila: Array of String; |
| 48 | + function GetCantidadDeElementos: Integer; |
| 49 | + public |
| 50 | + procedure Push(Valor: String); |
| 51 | + function Pop: String; |
| 52 | + property CantidadDeElementos: Integer read GetCantidadDeElementos; |
| 53 | + procedure ImprimirContenido; |
| 54 | + End; |
| 55 | + |
| 56 | + TCola = Class //FIFO |
| 57 | + private |
| 58 | + FListaCola: Array of String; |
| 59 | + function GetCantidadDeElementos: Integer; |
| 60 | + public |
| 61 | + procedure ALaCola(Valor: String); |
| 62 | + function DeLaCola: String; |
| 63 | + property CantidadDeElementos: Integer read GetCantidadDeElementos; |
| 64 | + procedure ImprimirContenido; |
| 65 | + End; |
| 66 | + |
| 67 | +{ TPersona } |
| 68 | + |
| 69 | +constructor TPersona.Create(ANombre: String; Edad: Integer); |
| 70 | +begin |
| 71 | + FNombre := ANombre; |
| 72 | + FEdad := Edad; |
| 73 | +end; |
| 74 | + |
| 75 | +function TPersona.GetEdad: Integer; |
| 76 | +begin |
| 77 | + Result := FEdad; |
| 78 | +end; |
| 79 | + |
| 80 | +function TPersona.GetNombre: String; |
| 81 | +begin |
| 82 | + Result := FNombre; |
| 83 | +end; |
| 84 | + |
| 85 | +procedure TPersona.SetEdad(const Value: Integer); |
| 86 | +begin |
| 87 | + FEdad := Value; |
| 88 | +end; |
| 89 | + |
| 90 | +procedure TPersona.SetNombre(const Value: String); |
| 91 | +begin |
| 92 | + FNombre := Value; |
| 93 | +end; |
| 94 | + |
| 95 | +procedure TPersona.ImprimirDatos; |
| 96 | +begin |
| 97 | + WriteLn('INFORMACIÓN DE LA PERSONA:'); |
| 98 | + WriteLn('Nombre: ', Nombre); |
| 99 | + WriteLn('Edad: ', Edad); |
| 100 | + WriteLn('Color favorito: ', ColorFavorito); |
| 101 | + WriteLn('Hobbie: ', Hobbie); |
| 102 | + WriteLn; |
| 103 | +end; |
| 104 | + |
| 105 | +{ TPila } |
| 106 | + |
| 107 | +function TPila.GetCantidadDeElementos: Integer; |
| 108 | +begin |
| 109 | + Result := Length(FListaPila); |
| 110 | +end; |
| 111 | + |
| 112 | +procedure TPila.ImprimirContenido; |
| 113 | +begin |
| 114 | + for var i := Length(FListaPila) - 1 downto 0 do |
| 115 | + WriteLn(FListaPila[i]); |
| 116 | +end; |
| 117 | + |
| 118 | +function TPila.Pop: String; |
| 119 | +var |
| 120 | + Posicion: Integer; |
| 121 | +begin |
| 122 | + Posicion := Length(FListaPila) - 1; |
| 123 | + Result := FListaPila[Posicion]; |
| 124 | + SetLength(FListaPila, Posicion); |
| 125 | +end; |
| 126 | + |
| 127 | +procedure TPila.Push(Valor: String); |
| 128 | +var |
| 129 | + Posicion: Integer; |
| 130 | +begin |
| 131 | + Posicion := Length(FListaPila) + 1; |
| 132 | + SetLength(FListaPila, Posicion); |
| 133 | + FListaPila[Posicion - 1] := Valor; |
| 134 | +end; |
| 135 | + |
| 136 | +{ TCola } |
| 137 | + |
| 138 | +procedure TCola.ALaCola(Valor: String); |
| 139 | +var |
| 140 | + Posicion: Integer; |
| 141 | +begin |
| 142 | + Posicion := Length(FListaCola) + 1; |
| 143 | + SetLength(FListaCola, Posicion); |
| 144 | + FListaCola[Posicion - 1] := Valor; |
| 145 | +end; |
| 146 | + |
| 147 | +function TCola.DeLaCola: String; |
| 148 | +var |
| 149 | + Posicion: Integer; |
| 150 | +begin |
| 151 | + Result := FListaCola[0]; |
| 152 | + //Recorrer los elementos hacia atrás |
| 153 | + for var i := 0 to Length(FListaCola) - 2 do |
| 154 | + FListaCola[i] := FListaCola[i + 1]; |
| 155 | + SetLength(FListaCola, Length(FListaCola) - 1); |
| 156 | +end; |
| 157 | + |
| 158 | +function TCola.GetCantidadDeElementos: Integer; |
| 159 | +begin |
| 160 | + Result := Length(FListaCola); |
| 161 | +end; |
| 162 | + |
| 163 | +procedure TCola.ImprimirContenido; |
| 164 | +begin |
| 165 | + for var Elemento in FListaCola do |
| 166 | + WriteLn(Elemento); |
| 167 | +end; |
| 168 | + |
| 169 | +var |
| 170 | + UnaPersona: TPersona; |
| 171 | + UnaPila: TPila; |
| 172 | + UnaCola: TCola; |
| 173 | +begin |
| 174 | + UnaPersona := TPersona.Create('Juanita', 25); |
| 175 | + UnaPersona.Edad := 26; |
| 176 | + UnaPersona.ColorFavorito := 'Rojo'; |
| 177 | + UnaPersona.Hobbie := 'Bailar'; |
| 178 | + UnaPersona.ImprimirDatos; |
| 179 | + UnaPersona.free; |
| 180 | + WriteLn; |
| 181 | + |
| 182 | + WriteLn('**********************************'); |
| 183 | + WriteLn('* DIFICULTAD EXTRA *'); |
| 184 | + WriteLn('**********************************'); |
| 185 | + UnaPila := TPila.Create; |
| 186 | + WriteLn('-= PILAS =-'); |
| 187 | + UnaPila.Push('Valor en la pila uno'); |
| 188 | + UnaPila.Push('Valor en la pila dos'); |
| 189 | + UnaPila.Push('Valor en la pila tres'); |
| 190 | + UnaPila.Push('Valor en la pila CUATRO'); |
| 191 | + WriteLn('Elementos en la pila: ', UnaPila.CantidadDeElementos); |
| 192 | + WriteLn('Contenido de la pila:'); |
| 193 | + UnaPila.ImprimirContenido; |
| 194 | + WriteLn; |
| 195 | + WriteLn('Elemento eliminado: ', UnaPila.Pop); |
| 196 | + WriteLn('Nuevo contenido de la pila:'); |
| 197 | + UnaPila.ImprimirContenido; |
| 198 | + WriteLn; |
| 199 | + UnaPila.Free; |
| 200 | + |
| 201 | + UnaCola := TCola.Create; |
| 202 | + WriteLn('-= COLAS =-'); |
| 203 | + UnaCola.ALaCola('Valor en la cola uno'); |
| 204 | + UnaCola.ALaCola('Valor en la cola dos'); |
| 205 | + UnaCola.ALaCola('Valor en la cola tres'); |
| 206 | + UnaCola.ALaCola('Valor en la cola cuatro'); |
| 207 | + UnaCola.ALaCola('Valor en la cola CINCO'); |
| 208 | + WriteLn('Elementos en la cola: ', UnaCola.CantidadDeElementos); |
| 209 | + WriteLn('Contenido de la cola:'); |
| 210 | + UnaCola.ImprimirContenido; |
| 211 | + WriteLn; |
| 212 | + WriteLn('Elemento eliminado: ', UnaCola.DeLaCola); |
| 213 | + WriteLn('Nuevo contenido de la pila:'); |
| 214 | + UnaCola.ImprimirContenido; |
| 215 | + UnaCola.Free; |
| 216 | + |
| 217 | + ReadLn; |
| 218 | + |
| 219 | +end. |
0 commit comments