Está en la página 1de 6

Cpila.

java miércoles, 17 de febrero de 2021 18:43

1 import java.util.ArrayList;
2
3 public class Cpila {
4
5 private int pos;
6 private ArrayList datos;
7
8 public Cpila()
9{
10 datos=new ArrayList();
11 pos=-1;
12 }
13
14 public boolean empty()
15 {
16 if (pos==-1)
17 return true;
18 else
19 return false;
20 }
21 public void push(Object info)
22 {
23 datos.add(info);
24 pos++;
25 }
26
27 public Object pop()
28 {
29 Object regreso;
30 if( !empty())
31 {
32 regreso=datos.get(pos);
33 datos.remove(pos);
34 pos--;
35 }
36 else
37 regreso= null;
38 return regreso;
39 }
40
41 public Object peek()
42 {
43 Object regreso;
44 if( !empty())
45 {
46 regreso=datos.get(pos);
47
48 }
49 else
50 regreso= null;
51 return regreso;
52 }
53
54 public String toString()
55 {
56 String cadena="";
57 for (int ciclo=0;ciclo<datos.size()-1;ciclo++)
58 { String data =datos.get(ciclo).toString();
59 cadena=cadena+data+"\n";
60 System.out.println(cadena);
61 }
62 return cadena;

Page 1
Cpila.java miércoles, 17 de febrero de 2021 18:43

63 }
64 }
65

Page 2
ejercicio01_c.java miércoles, 17 de febrero de 2021 18:43

1 import java.awt.BorderLayout;
2 import java.awt.EventQueue;
3
4 import javax.swing.JFrame;
5 import javax.swing.JPanel;
6 import javax.swing.border.EmptyBorder;
7
8
9
10 import javax.swing.JTextField;
11 import javax.swing.JButton;
12 import javax.swing.JLabel;
13 import java.awt.List;
14 import javax.swing.JTextArea;
15 import java.awt.event.ActionListener;
16 import java.awt.event.ActionEvent;
17
18 public class ejercicio01_c extends JFrame {
19
20 private JPanel contentPane;
21 private JTextField Tentrada;
22 private JTextField Tsalida;
23 private JTextArea Tdatos;
24 Cpila datos;
25 /**
26 * Launch the application.
27 */
28 public static void main(String[] args) {
29 EventQueue.invokeLater(new Runnable() {
30 public void run() {
31 try {
32 ejercicio01_c frame = new ejercicio01_c();
33 frame.setVisible(true);
34 } catch (Exception e) {
35 e.printStackTrace();
36 }
37 }
38 });
39 }
40
41 /**
42 * Create the frame.
43 */
44 public ejercicio01_c() {
45 datos = new Cpila();
46
47
48 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
49 setBounds(100, 100, 450, 221);
50 contentPane = new JPanel();
51 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
52 setContentPane(contentPane);
53 contentPane.setLayout(null);
54
55 Tentrada = new JTextField();
56 Tentrada.setBounds(190, 10, 96, 19);
57 contentPane.add(Tentrada);
58 Tentrada.setColumns(10);
59
60 JButton Binvertir = new JButton("Invertir Palabra");
61 Binvertir.addActionListener(new ActionListener() {
62 public void actionPerformed(ActionEvent e) {

Page 1
ejercicio01_c.java miércoles, 17 de febrero de 2021 18:43

63
64 char ch;
65 String entrada = Tentrada.getText();
66 String cadena=entrada.trim();
67 for(int pos=0;pos<cadena.length();pos++)
68 { ch = cadena.charAt(pos);
69 datos.push(ch);
70 Tdatos.setText(ch+"\n"+Tdatos.getText());
71 }
72 while (!datos.empty())
73 {
74 ch=(char) datos.pop();
75 Tsalida.setText(ch+Tsalida.getText());
76 }
77 }
78 });
79 Binvertir.setBounds(155, 50, 120, 21);
80 contentPane.add(Binvertir);
81
82 Tsalida = new JTextField();
83 Tsalida.setEditable(false);
84 Tsalida.setBounds(200, 81, 207, 19);
85 contentPane.add(Tsalida);
86 Tsalida.setColumns(10);
87
88 JButton Bsalir = new JButton("Salir");
89 Bsalir.addActionListener(new ActionListener() {
90 public void actionPerformed(ActionEvent e) {
91 dispose();
92 }
93 });
94 Bsalir.setBounds(304, 50, 85, 21);
95 contentPane.add(Bsalir);
96
97 JLabel lblNewLabel = new JLabel("Introduce una Palabra");
98 lblNewLabel.setBounds(45, 13, 101, 13);
99 contentPane.add(lblNewLabel);
100
101 Tdatos = new JTextArea();
102 Tdatos.setEnabled(false);
103 Tdatos.setBounds(45, 48, 85, 117);
104 contentPane.add(Tdatos);
105 }
106 }
107

Page 2
ejercicio01_d.java miércoles, 17 de febrero de 2021 18:42

1 import java.awt.BorderLayout;
13
14 public class ejercicio01_d extends JFrame {
15
16 private JPanel contentPane;
17 private JTextField Tentrada;
18 private JTextField Tsalida;
19 Cpila datos;
20
21 /**
22 * Launch the application.
23 */
24 public static void main(String[] args) {
25 EventQueue.invokeLater(new Runnable() {
26 public void run() {
27 try {
28 ejercicio01_d frame = new ejercicio01_d();
29 frame.setVisible(true);
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33 }
34 });
35 }
36
37 /**
38 * Create the frame.
39 */
40 public ejercicio01_d() {
41
42 datos= new Cpila();
43 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
44 setBounds(100, 100, 450, 173);
45 contentPane = new JPanel();
46 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
47 setContentPane(contentPane);
48 contentPane.setLayout(null);
49
50 Tentrada = new JTextField();
51 Tentrada.setBounds(305, 10, 96, 19);
52 contentPane.add(Tentrada);
53 Tentrada.setColumns(10);
54
55 JLabel lblNewLabel = new JLabel("introduce los numeros con separador de comas");
56 lblNewLabel.setBounds(37, 13, 243, 13);
57 contentPane.add(lblNewLabel);
58
59 JButton Bordenar = new JButton("Ordenar");
60 Bordenar.addActionListener(new ActionListener() {
61 public void actionPerformed(ActionEvent e) {
62 String cadena = Tentrada.getText().trim();
63 StringTokenizer tokens=new StringTokenizer(cadena,",");
64 while(tokens.hasMoreTokens())
65 { String num =tokens.nextToken();
66 datos.push(Integer.parseInt(num));
67 System.out.println(num);
68 }
69 Cpila temporal=datos;
70 Cpila datosordenados=new Cpila();
71 while(!datos.empty())
72 {
73 int numero = (int) datos.pop();

Page 1
ejercicio01_d.java miércoles, 17 de febrero de 2021 18:42

74 System.out.println(numero);
75
76 while( ( !datosordenados.empty()) && ((int)datosordenados.peek()>
numero))
77 {
78 datos.push(datosordenados.pop());
79 }
80 datosordenados.push(numero);
81 }
82 Tsalida.setText(datosordenados.toString());
83
84
85 }});
86
87 Bordenar.setBounds(316, 53, 85, 21);
88 contentPane.add(Bordenar);
89
90 JButton Bsalida = new JButton("Salida");
91 Bsalida.addActionListener(new ActionListener() {
92 public void actionPerformed(ActionEvent e) {
93 dispose();
94 }
95 });
96 Bsalida.setBounds(316, 99, 85, 21);
97 contentPane.add(Bsalida);
98
99 Tsalida = new JTextField();
100 Tsalida.setBounds(37, 100, 243, 19);
101 contentPane.add(Tsalida);
102 Tsalida.setColumns(10);
103 }
104
105 }
106

Page 2

También podría gustarte