Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lab10;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import javax.swing.JTextPane;
- import javax.swing.JLabel;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- import java.awt.event.ActionEvent;
- public class Lab10GUI_2 {
- private JFrame frame;
- static File inputFile = new File("C:\\Temp\\input.txt");
- static File outputFile = new File("C:\\Temp\\output.txt");
- static StringTokenizer st;
- static String[] arr;
- static int noClick = 0;
- public static void main(String[] args) {
- try {
- textInput();
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Lab10GUI_2 window = new Lab10GUI_2();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- static void textInput() throws FileNotFoundException {
- Scanner sc = new Scanner(inputFile);
- String str = new String();
- while ( sc.hasNextLine() ) {
- str += sc.nextLine() + "\n";
- }
- st = new StringTokenizer(str, " \t\n\r,.");
- sc.close();
- arr = new String[st.countTokens()];
- int i = 0;
- while (st.hasMoreTokens()) {
- arr[i] = st.nextToken();
- i++;
- }
- }
- private static int leastLength(String str1, String str2) {
- int lenFirst = str1.length();
- int lenSecond = str2.length();
- if (lenFirst >= lenSecond) {
- return lenFirst;
- } else {
- return lenSecond;
- }
- }
- private static int leastLength2(String str1, String str2) {
- int lenFirst = str1.length();
- int lenSecond = str2.length();
- if (lenFirst <= lenSecond) {
- return lenFirst;
- } else {
- return lenSecond;
- }
- }
- private static boolean isMore(String str1, String str2) {
- int bol = leastLength(str1, str2);
- int length1 = str1.length();
- int length2 = str2.length();
- int[] com1 = new int[bol];
- int[] com2 = new int[bol];
- for (int i = 0; i < bol; i++) {
- if((length1) > i) {
- com1[i] = str1.charAt(i);
- } else {
- com1[i] = 0;
- }
- if((length2) > i) {
- com2[i] = str2.charAt(i);
- } else {
- com2[i] = 0;
- }
- }
- /*
- for (int i = 0; i < bol; i++) {
- System.out.print(com1[i] + " ");
- }
- System.out.println();
- for (int i = 0; i < bol; i++) {
- System.out.print(com2[i] + " ");
- }
- */
- for (int i = 0; i < bol; i++) {
- if( com1[i] > com2[i] && com2[i] == 0 ) {
- return true;
- } else if (( com1[i] == com2[i] ) ) {
- continue;
- } else if ( com1[i] > com2[i] ) {
- return true;
- } else if ( com1[i] == 0 && com2[i] != 0 ) {
- return false;
- } else {
- return false;
- }
- }
- return true;
- }
- public static void sortByAlpha(String arr[]) {
- int n = arr.length;
- int timer = 1;
- while(timer != 0) {
- timer = 0;
- for (int i = 0; i < n - 1; i++) {
- if (isMore(arr[i], arr[i + 1])) {
- String temp = arr[i];
- arr[i] = arr[i + 1];
- arr[i + 1] = temp;
- timer++;
- }
- }
- }
- }
- public Lab10GUI_2() {
- initialize();
- }
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 473, 211);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- JTextPane textOutput = new JTextPane();
- textOutput.setEditable(false);
- textOutput.setBounds(59, 11, 273, 164);
- frame.getContentPane().add(textOutput);
- JLabel label_1 = new JLabel("Вывод:");
- label_1.setBounds(10, 11, 46, 14);
- frame.getContentPane().add(label_1);
- JButton button_1 = new JButton("Отсортировать");
- button_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(noClick == 0) {
- sortByAlpha(arr);
- String inpTextArea = new String();
- try {
- Writer wr = new FileWriter(outputFile);
- for (int i = 0; i < arr.length; i++) {
- wr.write(arr[i] + "\r\n");
- inpTextArea += arr[i] + "\r\n";
- }
- textOutput.setText(inpTextArea);
- wr.close();
- noClick = 0;
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- button_1.setBounds(342, 11, 113, 23);
- frame.getContentPane().add(button_1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement