Guest User

Untitled

a guest
Feb 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. public class ConjuntoTabla implements Conjunto {
  2. //Descripción general: Implementación en forma de tabla de enteros
  3.  
  4. private int _tablaInt[];
  5.  
  6. /*Tabla de enteros ordenada de forma creciente
  7. La longitud de la tabla siempre coincide con el número de
  8. elementos del conjunto que representa */
  9.  
  10. public ConjuntoTabla() {
  11. this._tablaInt = new int [0];
  12. }
  13.  
  14. public void setElem(int i, int n) {
  15. this. _tablaInt[i] = n;
  16. }
  17.  
  18. public int getElem(int i) {
  19. return this._tablaInt[i];
  20. }
  21.  
  22. //Se ha añadido n al conjunto ordenadamente
  23. public void añadir(int n) {
  24. if (!this.pertenece(n)) {
  25. int i = this.nelem();
  26. int aux[] = new int[i+1];
  27. for (int j=0; j < i; j++) {
  28. aux[j] = getElem(j);
  29. }
  30. while (i > 0 && n < getElem(i-1)) {
  31. aux[i] = getElem(i-1);
  32. i--;
  33. }
  34. aux[i] = n;
  35. _tablaInt = aux;
  36. }
  37.  
  38.  
  39. public boolean pertenece(int n) {
  40. int medio, izq = 0, der = this.nelem()-1;
  41. boolean encontrado = false;
  42. while (!encontrado && izq <= der) {
  43. medio = (izq + der)/2;
  44. if (n < getElem(medio)) der = medio -1;
  45. else if (n > getElem(medio)) izq = medio + 1;
  46. else encontrado = true;
  47. }
  48. return encontrado;
  49. }
  50.  
  51. public boolean vacio() {
  52. return (this.nelem() == 0);
  53. }
  54.  
  55. public Conjunto union(Conjunto c) {
  56. ConjuntoTabla aux;
  57. aux = (ConjuntoTabla)c;
  58. if (aux.vacio()) return this;
  59. else if (this.vacio()) return c;
  60. else {
  61. ConjuntoTabla union = new ConjuntoTabla();
  62. for (int i=0; i < this.nelem(); i++) {
  63. union.añadir(getElem(i));
  64. }
  65. for (int i=0; i < c.nelem(); i++) {
  66. union.añadir(aux.getElem(i));
  67. }
  68. return union;
  69. }
  70. }
  71.  
  72. public Conjunto interseccion(Conjunto c) {
  73. ConjuntoTabla interseccion = new ConjuntoTabla();
  74. ConjuntoTabla aux;
  75. aux = (ConjuntoTabla)c;
  76. if (!this.vacio() && !aux.vacio()) {
  77. for (int i=0; i < this.nelem(); i++) {
  78. for (int j=0; j < aux.nelem(); j++) {
  79. int n = this.getElem(i);
  80. if (n==aux.getElem(j)) {
  81. interseccion.añadir(n);
  82. }
  83. }
  84. }
  85. }
  86. return interseccion;
  87. }
  88.  
  89. public Conjunto diferencia(Conjunto c) {
  90. ConjuntoTabla diferencia = new ConjunoTabla();
  91. ConjuntoTabla aux;
  92. aux = (ConjuntoTabla)c;
  93. if (!aux.vacio()) {
  94. for (int i=0; i<this.nelem(); i++) {
  95. int n= this.getElem(i);
  96. if (!aux.pertenece(n)) {
  97. diferencia.añadir(n);
  98. }
  99. }
  100. }
  101. return diferencia;
  102. }
  103.  
  104. public int nelem() {
  105. return (_tablaInt.length);
  106. }
  107.  
  108. public int elem(int i) {
  109. int n = getElem(i-nelem());
  110. return n;
  111. }
  112.  
  113. @Override // Heredado de Object
  114. public String toString() {
  115. String s = "";
  116. for (int i=0; i < nelem(); i++) {
  117. int n = getElem(i);
  118. if (i==0) s = s + n;
  119. else s = s + "," + n;
  120. }
  121. return "[" + s + "]";
  122. }
  123. }
  124.  
  125. //Interfaz, no puedo modificarla
  126.  
  127. public interface Conjunto {
  128. // Descripción general: conjunto (concepto matemático) de enteros
  129.  
  130. public void añadir(int n);
  131. // Pre : ---
  132. // Post: Se ha añadido n al conjunto
  133.  
  134. public boolean pertenece(int n);
  135. // Pre : ---
  136. // Post: Dice si n pertenece al conjunto
  137.  
  138. public boolean vacio();
  139. // Pre : ---
  140. // Post: Dice si el conjunto es vacío
  141.  
  142. public Conjunto union(Conjunto c);
  143. // Pre : ---
  144. // Post: Devuelve la unión de este conjunto con c
  145.  
  146. public Conjunto interseccion(Conjunto c);
  147. // Pre : ---
  148. // Post: Devuelve la intersección de este conjunto con c
  149.  
  150. public Conjunto diferencia(Conjunto c);
  151. // Pre : ---
  152. // Post: Devuelve la diferencia de este conjunto con c
  153.  
  154. public int nelem();
  155. // Pre : ---
  156. // Post: Dice cuantos elementos tiene el conjunto
  157.  
  158. public int elem(int i);
  159. // Pre : 1 <= i <= nelem()
  160. // Post: Devuelve el i-éssimo elemento de este conjunto según orden numérico
  161. }
  162.  
  163. //Programa principal, no puedo modificarlo:
  164.  
  165. import java.util.Scanner;
  166.  
  167. public class TestConjuntoTabla {
  168. // Descripción general: Pequeño test de la clase ConjuntoTabla.
  169. //
  170. // Lee dos secuencias consecutivas de enteros (posiblemente con
  171. // elementos repetidos) separadas por '#', las pone en dos conjuntos,
  172. // y muestra su unión, intersección y diferencia.
  173.  
  174. public static void main(String argv[]) throws Exception {
  175. Scanner s = new Scanner(System.in);
  176. Conjunto c1 = new ConjuntoTabla();
  177. Conjunto c2 = new ConjuntoTabla();
  178.  
  179. System.out.println("Introducir dos secuencias de enteros, separadas por el símbolo #");
  180.  
  181. while (s.hasNextInt())
  182. c1.añadir(s.nextInt());
  183.  
  184. s.next(); // Salta '#'
  185.  
  186. while (s.hasNextInt())
  187. c2.añadir(s.nextInt());
  188.  
  189. System.out.println("nc1: " + c1);
  190. System.out.println("c2: " + c2);
  191. System.out.println("c1 unión c2: " + c1.union(c2));
  192. System.out.println("c1 intersección c2: " + c1.interseccion(c2));
  193. System.out.println("c1 menos c2: " + c1.diferencia(c2));
  194. }
  195.  
  196. }
Add Comment
Please, Sign In to add comment