Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. package Lab10;
  2.  
  3. import java.awt.EventQueue;
  4. import javax.swing.JFrame;
  5. import javax.swing.JButton;
  6. import javax.swing.JTextPane;
  7. import javax.swing.JLabel;
  8. import java.awt.event.ActionListener;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.Writer;
  14. import java.util.Scanner;
  15. import java.util.StringTokenizer;
  16. import java.awt.event.ActionEvent;
  17.  
  18. public class Lab10GUI_2 {
  19.  
  20. private JFrame frame;
  21.  
  22. static File inputFile = new File("C:\\Temp\\input.txt");
  23. static File outputFile = new File("C:\\Temp\\output.txt");
  24.  
  25. static StringTokenizer st;
  26. static String[] arr;
  27. static int noClick = 0;
  28.  
  29. public static void main(String[] args) {
  30. try {
  31. textInput();
  32. } catch (FileNotFoundException e1) {
  33. e1.printStackTrace();
  34. }
  35. EventQueue.invokeLater(new Runnable() {
  36. public void run() {
  37. try {
  38. Lab10GUI_2 window = new Lab10GUI_2();
  39. window.frame.setVisible(true);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. });
  45. }
  46.  
  47. static void textInput() throws FileNotFoundException {
  48. Scanner sc = new Scanner(inputFile);
  49. String str = new String();
  50. while ( sc.hasNextLine() ) {
  51. str += sc.nextLine() + "\n";
  52. }
  53. st = new StringTokenizer(str, " \t\n\r,.");
  54. sc.close();
  55. arr = new String[st.countTokens()];
  56. int i = 0;
  57. while (st.hasMoreTokens()) {
  58. arr[i] = st.nextToken();
  59. i++;
  60. }
  61. }
  62. private static int leastLength(String str1, String str2) {
  63. int lenFirst = str1.length();
  64. int lenSecond = str2.length();
  65. if (lenFirst >= lenSecond) {
  66. return lenFirst;
  67. } else {
  68. return lenSecond;
  69. }
  70. }
  71. private static int leastLength2(String str1, String str2) {
  72. int lenFirst = str1.length();
  73. int lenSecond = str2.length();
  74. if (lenFirst <= lenSecond) {
  75. return lenFirst;
  76. } else {
  77. return lenSecond;
  78. }
  79. }
  80.  
  81. private static boolean isMore(String str1, String str2) {
  82.  
  83. int bol = leastLength(str1, str2);
  84.  
  85. int length1 = str1.length();
  86. int length2 = str2.length();
  87.  
  88. int[] com1 = new int[bol];
  89. int[] com2 = new int[bol];
  90.  
  91. for (int i = 0; i < bol; i++) {
  92.  
  93. if((length1) > i) {
  94. com1[i] = str1.charAt(i);
  95. } else {
  96. com1[i] = 0;
  97. }
  98.  
  99. if((length2) > i) {
  100. com2[i] = str2.charAt(i);
  101. } else {
  102. com2[i] = 0;
  103. }
  104.  
  105. }
  106.  
  107. /*
  108. for (int i = 0; i < bol; i++) {
  109. System.out.print(com1[i] + " ");
  110.  
  111.  
  112. }
  113.  
  114. System.out.println();
  115.  
  116. for (int i = 0; i < bol; i++) {
  117. System.out.print(com2[i] + " ");
  118. }
  119. */
  120.  
  121. for (int i = 0; i < bol; i++) {
  122.  
  123. if( com1[i] > com2[i] && com2[i] == 0 ) {
  124.  
  125. return true;
  126.  
  127. } else if (( com1[i] == com2[i] ) ) {
  128.  
  129. continue;
  130. } else if ( com1[i] > com2[i] ) {
  131. return true;
  132. } else if ( com1[i] == 0 && com2[i] != 0 ) {
  133.  
  134. return false;
  135. } else {
  136.  
  137. return false;
  138. }
  139.  
  140. }
  141.  
  142.  
  143. return true;
  144.  
  145. }
  146.  
  147. public static void sortByAlpha(String arr[]) {
  148. int n = arr.length;
  149. int timer = 1;
  150. while(timer != 0) {
  151. timer = 0;
  152.  
  153.  
  154.  
  155. for (int i = 0; i < n - 1; i++) {
  156.  
  157.  
  158.  
  159. if (isMore(arr[i], arr[i + 1])) {
  160. String temp = arr[i];
  161. arr[i] = arr[i + 1];
  162. arr[i + 1] = temp;
  163. timer++;
  164.  
  165. }
  166.  
  167. }
  168.  
  169. }
  170.  
  171. }
  172.  
  173. public Lab10GUI_2() {
  174. initialize();
  175. }
  176.  
  177. private void initialize() {
  178.  
  179.  
  180. frame = new JFrame();
  181. frame.setBounds(100, 100, 473, 211);
  182. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  183. frame.getContentPane().setLayout(null);
  184.  
  185. JTextPane textOutput = new JTextPane();
  186. textOutput.setEditable(false);
  187. textOutput.setBounds(59, 11, 273, 164);
  188. frame.getContentPane().add(textOutput);
  189.  
  190. JLabel label_1 = new JLabel("Вывод:");
  191. label_1.setBounds(10, 11, 46, 14);
  192. frame.getContentPane().add(label_1);
  193.  
  194.  
  195. JButton button_1 = new JButton("Отсортировать");
  196. button_1.addActionListener(new ActionListener() {
  197. public void actionPerformed(ActionEvent e) {
  198. if(noClick == 0) {
  199. sortByAlpha(arr);
  200. String inpTextArea = new String();
  201. try {
  202. Writer wr = new FileWriter(outputFile);
  203. for (int i = 0; i < arr.length; i++) {
  204. wr.write(arr[i] + "\r\n");
  205. inpTextArea += arr[i] + "\r\n";
  206. }
  207. textOutput.setText(inpTextArea);
  208. wr.close();
  209. noClick = 0;
  210. } catch (IOException e1) {
  211. e1.printStackTrace();
  212. }
  213. }
  214.  
  215. }
  216. });
  217. button_1.setBounds(342, 11, 113, 23);
  218. frame.getContentPane().add(button_1);
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement