Guest User

Untitled

a guest
Jul 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. //Dette programmet skal overføre listen med ord til en matrise og boblesortere innholdet
  2.  
  3. import java.io.*;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class Boble
  7. {
  8. public static void main(String[] l) throws IOException
  9. {
  10. //vardeklarasjon
  11. String ord, fil, inn, hmm;
  12. char b;
  13. int t, i;
  14. FileReader fraFil;
  15. String[] list;
  16. boolean sjekk;
  17.  
  18. fil = "liste.txt";
  19. fraFil = new FileReader(fil);
  20. ord = "";
  21. list = new String[100];
  22. hmm="";
  23.  
  24. do
  25. {
  26. t = fraFil.read();
  27.  
  28. sjekk = t==13 || t==10;
  29. if(sjekk)
  30. {
  31. if(ord != "")
  32. {
  33. list = nyArray.inn(list, ord);
  34. ord = "";
  35. }
  36. }
  37. if(!sjekk)
  38. {
  39. b = (char)t;
  40. ord = ord + b;
  41. }
  42. }while (t > 0);
  43.  
  44. for(i = 0; i<list.length; i++)
  45. {
  46. for(t = i +1; t<list.length; t++)
  47. {
  48. if(list[t]!=null)
  49. {
  50. if(list[i].compareTo(list[t])>0)
  51. {
  52. String temp = list [i];
  53. list[i] = list[t];
  54. list[t] = temp;
  55. }
  56. }
  57. }
  58. }
  59.  
  60. inn = JOptionPane.showInputDialog("Skriv inn ord");
  61. do
  62. {
  63. sjekk = Bin.sok(list, inn);
  64. if(sjekk)
  65. {
  66. inn = JOptionPane.showInputDialog("Ordet " + inn + " var i listen. Skriv inn nytt ord \n (Enter avslutter)");
  67. }
  68. if(!sjekk)
  69. {
  70. inn = JOptionPane.showInputDialog("Ordet " + inn + " var ikke i listen. Skriv inn nytt ord \n (Enter avslutter)");
  71. }
  72. }while (inn.compareTo(hmm)!=0);
  73.  
  74. // boblesortering'
  75. fraFil.close();
  76. System.exit(0);
  77.  
  78. }
  79. }
  80.  
  81. class nyArray
  82. {
  83. public static String[] inn(String[] arr, String word)
  84. {
  85. int i;
  86. String word2;
  87.  
  88. do
  89. {
  90. for(i = 0; i < arr.length; i++)
  91. {
  92. word2 = arr[i];
  93. if(word.equals(word2))
  94. {
  95. i = arr.length;
  96. }
  97.  
  98. if(!word.equals(word2))
  99. {
  100.  
  101. if(arr[i]==null || arr[i] =="" && i < arr.length)
  102. {
  103. arr[i] = word;
  104. i = arr.length;
  105. }
  106. }
  107. }
  108. }while (i<arr.length);
  109.  
  110. return arr;
  111. }
  112. }
  113. class Bin
  114. {
  115. public static boolean sok(String [] org, String ord)
  116. {
  117. int b, t, m, i;
  118. b=0;
  119. t=0;
  120.  
  121. for(i=0; i<org.length; i++) //finner siste element i matrisen som har innhold
  122. {
  123. if(org[i] == null || org[i]=="")
  124. {
  125. t = i;
  126. i = org.length;
  127. }
  128. }
  129.  
  130. boolean sjekk2 = false;
  131.  
  132. while(b<=t)
  133. {
  134. m = (t+b)/2;
  135. if(ord.compareTo(org[m])>0)
  136. {
  137. b = m + 1;
  138. }
  139. else if(ord.compareTo(org[m])<0)
  140. {
  141. t = m - 1;
  142. }
  143. if(ord.compareTo(org[m])==0)
  144. {
  145. sjekk2 = true;
  146. b = t + 1;
  147. }
  148. }
  149.  
  150. return sjekk2;
  151. }
  152. }
Add Comment
Please, Sign In to add comment