Skip to content

Commit d8ccef3

Browse files
authored
Merge pull request mouredev#6213 from eulogioep/main
#7 java, javascript, php y typescript
2 parents 593ab62 + 7460e0a commit d8ccef3

File tree

4 files changed

+644
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)