1+ <?php
2+
3+ // Implementación de la Pila (Stack - LIFO)
4+ class Stack {
5+ private $ items = [];
6+
7+ // Método para añadir un elemento a la pila
8+ public function push ($ element ) {
9+ array_push ($ this ->items , $ element );
10+ }
11+
12+ // Método para quitar un elemento de la pila
13+ public function pop () {
14+ if ($ this ->isEmpty ()) {
15+ echo "Error: Pila vacía \n" ;
16+ return null ;
17+ }
18+ return array_pop ($ this ->items );
19+ }
20+
21+ // Método para verificar si la pila está vacía
22+ public function isEmpty () {
23+ return empty ($ this ->items );
24+ }
25+
26+ // Método para obtener el elemento en la cima de la pila sin quitarlo
27+ public function peek () {
28+ if ($ this ->isEmpty ()) {
29+ echo "Error: Pila vacía \n" ;
30+ return null ;
31+ }
32+ return end ($ this ->items );
33+ }
34+ }
35+
36+ // Implementación de la Cola (Queue - FIFO)
37+ class Queue {
38+ private $ items = [];
39+
40+ // Método para añadir un elemento a la cola
41+ public function enqueue ($ element ) {
42+ array_push ($ this ->items , $ element );
43+ }
44+
45+ // Método para quitar un elemento de la cola
46+ public function dequeue () {
47+ if ($ this ->isEmpty ()) {
48+ echo "Error: Cola vacía \n" ;
49+ return null ;
50+ }
51+ return array_shift ($ this ->items );
52+ }
53+
54+ // Método para verificar si la cola está vacía
55+ public function isEmpty () {
56+ return empty ($ this ->items );
57+ }
58+
59+ // Método para obtener el primer elemento de la cola sin quitarlo
60+ public function peek () {
61+ if ($ this ->isEmpty ()) {
62+ echo "Error: Cola vacía \n" ;
63+ return null ;
64+ }
65+ return $ this ->items [0 ];
66+ }
67+ }
68+
69+ // Función para el simulador de navegador web
70+ function webBrowserSimulator () {
71+ $ backStack = new Stack ();
72+ $ forwardStack = new Stack ();
73+ $ currentPage = "" ;
74+
75+ echo "Simulador de navegador web (escribe 'salir' para terminar): \n" ;
76+
77+ while (true ) {
78+ $ input = strtolower (trim (readline ("Ingresa una acción o nombre de página web: " )));
79+
80+ if ($ input === 'salir ' ) {
81+ break ;
82+ } elseif ($ input === 'atrás ' ) {
83+ if (!$ backStack ->isEmpty ()) {
84+ $ forwardStack ->push ($ currentPage );
85+ $ currentPage = $ backStack ->pop ();
86+ echo "Página actual: $ currentPage \n" ;
87+ } else {
88+ echo "No hay páginas anteriores \n" ;
89+ }
90+ } elseif ($ input === 'adelante ' ) {
91+ if (!$ forwardStack ->isEmpty ()) {
92+ $ backStack ->push ($ currentPage );
93+ $ currentPage = $ forwardStack ->pop ();
94+ echo "Página actual: $ currentPage \n" ;
95+ } else {
96+ echo "No hay páginas siguientes \n" ;
97+ }
98+ } else {
99+ if ($ currentPage !== "" ) {
100+ $ backStack ->push ($ currentPage );
101+ }
102+ $ forwardStack = new Stack (); // Limpia la pila de adelante
103+ $ currentPage = $ input ;
104+ echo "Página actual: $ currentPage \n" ;
105+ }
106+ }
107+ }
108+
109+ // Función para el simulador de impresora compartida
110+ function printerSimulator () {
111+ $ printQueue = new Queue ();
112+
113+ echo "\nSimulador de impresora compartida (escribe 'salir' para terminar): \n" ;
114+
115+ while (true ) {
116+ $ input = strtolower (trim (readline ("Ingresa un nombre de documento o 'imprimir': " )));
117+
118+ if ($ input === 'salir ' ) {
119+ break ;
120+ } elseif ($ input === 'imprimir ' ) {
121+ $ document = $ printQueue ->dequeue ();
122+ if ($ document !== null ) {
123+ echo "Imprimiendo: $ document \n" ;
124+ }
125+ } else {
126+ $ printQueue ->enqueue ($ input );
127+ echo "Documento añadido a la cola: $ input \n" ;
128+ }
129+ }
130+ }
131+
132+ // Ejecutar los simuladores
133+ webBrowserSimulator ();
134+ printerSimulator ();
135+
136+ ?>
0 commit comments