Skip to content

Commit 7460e0a

Browse files
committed
#7 typescript
1 parent 44a62ee commit 7460e0a

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import * as readline from 'readline';
2+
3+
// Implementación de la Pila (Stack - LIFO)
4+
class Stack<T> {
5+
private items: T[] = [];
6+
7+
// Método para añadir un elemento a la pila
8+
push(element: T): void {
9+
this.items.push(element);
10+
}
11+
12+
// Método para quitar un elemento de la pila
13+
pop(): T | undefined {
14+
if (this.isEmpty()) {
15+
console.log("Error: Pila vacía");
16+
return undefined;
17+
}
18+
return this.items.pop();
19+
}
20+
21+
// Método para verificar si la pila está vacía
22+
isEmpty(): boolean {
23+
return this.items.length === 0;
24+
}
25+
26+
// Método para obtener el elemento en la cima de la pila sin quitarlo
27+
peek(): T | undefined {
28+
if (this.isEmpty()) {
29+
console.log("Error: Pila vacía");
30+
return undefined;
31+
}
32+
return this.items[this.items.length - 1];
33+
}
34+
}
35+
36+
// Implementación de la Cola (Queue - FIFO)
37+
class Queue<T> {
38+
private items: T[] = [];
39+
40+
// Método para añadir un elemento a la cola
41+
enqueue(element: T): void {
42+
this.items.push(element);
43+
}
44+
45+
// Método para quitar un elemento de la cola
46+
dequeue(): T | undefined {
47+
if (this.isEmpty()) {
48+
console.log("Error: Cola vacía");
49+
return undefined;
50+
}
51+
return this.items.shift();
52+
}
53+
54+
// Método para verificar si la cola está vacía
55+
isEmpty(): boolean {
56+
return this.items.length === 0;
57+
}
58+
59+
// Método para obtener el primer elemento de la cola sin quitarlo
60+
peek(): T | undefined {
61+
if (this.isEmpty()) {
62+
console.log("Error: Cola vacía");
63+
return undefined;
64+
}
65+
return this.items[0];
66+
}
67+
}
68+
69+
// Función para el simulador de navegador web
70+
function webBrowserSimulator(): Promise<void> {
71+
const backStack = new Stack<string>();
72+
const forwardStack = new Stack<string>();
73+
let currentPage = "";
74+
75+
const rl = readline.createInterface({
76+
input: process.stdin,
77+
output: process.stdout
78+
});
79+
80+
console.log("Simulador de navegador web (escribe 'salir' para terminar):");
81+
82+
function askForAction(): Promise<void> {
83+
return new Promise((resolve) => {
84+
rl.question("Ingresa una acción o nombre de página web: ", (input: string) => {
85+
input = input.trim().toLowerCase();
86+
87+
if (input === 'salir') {
88+
rl.close();
89+
resolve();
90+
} else if (input === 'atrás') {
91+
if (!backStack.isEmpty()) {
92+
forwardStack.push(currentPage);
93+
currentPage = backStack.pop() || "";
94+
console.log("Página actual:", currentPage);
95+
} else {
96+
console.log("No hay páginas anteriores");
97+
}
98+
} else if (input === 'adelante') {
99+
if (!forwardStack.isEmpty()) {
100+
backStack.push(currentPage);
101+
currentPage = forwardStack.pop() || "";
102+
console.log("Página actual:", currentPage);
103+
} else {
104+
console.log("No hay páginas siguientes");
105+
}
106+
} else {
107+
if (currentPage) {
108+
backStack.push(currentPage);
109+
}
110+
forwardStack = new Stack<string>(); // Limpia la pila de adelante
111+
currentPage = input;
112+
console.log("Página actual:", currentPage);
113+
}
114+
115+
askForAction().then(resolve);
116+
});
117+
});
118+
}
119+
120+
return askForAction();
121+
}
122+
123+
// Función para el simulador de impresora compartida
124+
function printerSimulator(): Promise<void> {
125+
const printQueue = new Queue<string>();
126+
127+
const rl = readline.createInterface({
128+
input: process.stdin,
129+
output: process.stdout
130+
});
131+
132+
console.log("\nSimulador de impresora compartida (escribe 'salir' para terminar):");
133+
134+
function askForDocument(): Promise<void> {
135+
return new Promise((resolve) => {
136+
rl.question("Ingresa un nombre de documento o 'imprimir': ", (input: string) => {
137+
input = input.trim().toLowerCase();
138+
139+
if (input === 'salir') {
140+
rl.close();
141+
resolve();
142+
} else if (input === 'imprimir') {
143+
const document = printQueue.dequeue();
144+
if (document) {
145+
console.log("Imprimiendo:", document);
146+
}
147+
} else {
148+
printQueue.enqueue(input);
149+
console.log("Documento añadido a la cola:", input);
150+
}
151+
152+
askForDocument().then(resolve);
153+
});
154+
});
155+
}
156+
157+
return askForDocument();
158+
}
159+
160+
// Función principal asíncrona para ejecutar los simuladores
161+
async function main() {
162+
await webBrowserSimulator();
163+
await printerSimulator();
164+
}
165+
166+
// Ejecutar la función principal
167+
main();

0 commit comments

Comments
 (0)