yumi1996

Reddit Daily Programmer Challenge #264 (Easy)

May 9th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.77 KB | None | 0 0
  1. package exercise264;
  2.  
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.PrintWriter;
  9. import java.io.UnsupportedEncodingException;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JTextField;
  17.  
  18. public class SortCppCode {
  19.  
  20.     static String outputFileName = "sortedCode.txt";//the string that stores the name of the file that will contain the output of the program: the sorted code.
  21.  
  22.     static File src;
  23.     static JLabel listSize;
  24.  
  25.     public static void main(String[] args) {
  26.         JFrame jframe = new JFrame("Sort C++ Code");//the main frame of the windows that will be opened up
  27.         JButton sortButton = new JButton("Sort");
  28.  
  29.         sortButton.addActionListener(new ActionListener() {//add the action listener to the sorting button
  30.             public void actionPerformed(ActionEvent e) {
  31.                 sort();//when the button is pressed, run the sorting
  32.             }
  33.         });
  34.         sortButton.setEnabled(false);//disable the button by default. Later enabled when the src filename is given
  35.  
  36.         JLabel srcInputLabel = new JLabel("File Name:");
  37.         JTextField srcInputTextField = new JTextField();
  38.         JButton srcInputButton = new JButton("Confirm");
  39.  
  40.         srcInputButton.addActionListener(new ActionListener() {//add the action listener to the sorting button
  41.             public void actionPerformed(ActionEvent e) {
  42.  
  43.                 if (srcInputTextField.getText() != null && srcInputTextField.getText() != "") {//as long as there is actually something in the textbox
  44.                     src = new File(srcInputTextField.getText()) ;
  45.  
  46.                     sortButton.setEnabled(true);
  47.                     jframe.setVisible(true);
  48.                 }
  49.             }
  50.         });
  51.         jframe.setLayout(new GridLayout(1,5) );//sets the frame as a grid layout with three columns and two rows
  52.         jframe.add(srcInputLabel);
  53.         jframe.add(srcInputTextField);
  54.         jframe.add(srcInputButton);
  55.         jframe.add(sortButton);
  56.  
  57.         jframe.pack();
  58.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//kill the program when the user closes the window
  59.         jframe.setVisible(true);
  60.     }
  61.  
  62.     public static void sort() {
  63.         int index = 0;//int used to keep track of where you currently need to be in the "sorted" array
  64.         final String indent = "  ";
  65.         final String doubleIndent = "    ";
  66.  
  67.         ArrayList <String> unsorted = new ArrayList <String>();//create an arrayList to store the unsorted lines of code in.
  68.         String[] sorted = new String[0];//create an array to store the sorted lines of code
  69.  
  70.         if(src != null) {//if the file object does not equal null
  71.             if(src.exists()) {//check if it exists
  72.                 try {
  73.                     Scanner scan = new Scanner(src);//create a new scanner linked to the file
  74.  
  75.                     while (scan.hasNextLine()) {//import all the lines of code from the file to the arrayList
  76.                         unsorted.add(scan.nextLine());
  77.                     }
  78.                     scan.close();
  79.                     sorted = new String[unsorted.size()];
  80.  
  81.                     //START OF SORTING LOGIC
  82.  
  83.                     //search for #include statements
  84.                     for (int i = 0; i < unsorted.size();i++) {//for each String in unsorted, check for "#include" lines
  85.                         if (unsorted.get(i).contains("include")) {
  86.                             sorted[index] = unsorted.get(i);//add to the list
  87.                             unsorted.set(i, "");//replaces the current string with an empty one to avoid any duplicates
  88.                             index++;
  89.                         }
  90.                     }
  91.  
  92.                     sorted[index] = "";//add a blank line for readability;
  93.                     index++;//increment the index
  94.  
  95.                     //search for the main function
  96.                     for (int i = 0; i < unsorted.size();i++) {//find the main function statement
  97.                         if (unsorted.get(i).equals("int main()")) {
  98.                             sorted[index] = unsorted.get(i);
  99.                             unsorted.set(i, "");
  100.                             index++;
  101.                         }
  102.                     }
  103.  
  104.                     //search for the main function bracket
  105.                     for (int i = 0; i < unsorted.size();i++) {
  106.                         if(unsorted.get(i).equals("{") && sorted[index-1].equals("{") == false) {//find a bracket, insert it into array, as long as the previous entry is not also a bracket
  107.                             sorted[index] = unsorted.get(i);
  108.                             unsorted.set(i, "");
  109.                             index++;
  110.                         }
  111.                     }
  112.  
  113.                     //search for constants defined within the main function
  114.                     for (int i = 0; i < unsorted.size();i++) {
  115.  
  116.                         if (unsorted.get(i).contains(indent) && unsorted.get(i).contains(doubleIndent) == false && unsorted.get(i).contains("for") == false && (unsorted.get(i).contains("int") ||
  117.                         /* Placeholder. */                                                                                                              unsorted.get(i).contains("String") ||
  118.                         /* Placeholder. */                                                                                                              unsorted.get(i).contains("double") ||
  119.                         /* Placeholder. */                                                                                                              unsorted.get(i).contains("boolean"))) {
  120.                             sorted[index] = unsorted.get(i);
  121.                             unsorted.set(i, "");
  122.                             index++;
  123.                         }
  124.                     }
  125.  
  126.                     //search for a "for" loop
  127.                     for (int i = 0; i < unsorted.size(); i++) {
  128.  
  129.                         if (unsorted.get(i).contains("  for") && sorted[index-1].equals("  for") == false) {
  130.  
  131.                             sorted[index] = unsorted.get(i);
  132.                             unsorted.set(i, "");
  133.                             index++;
  134.                         }
  135.                     }
  136.  
  137.                     //find a bracket for the for loop
  138.                     for (int i = 0; i < unsorted.size();i++) {
  139.                         if(unsorted.get(i).equals("  {") && sorted[index-1].equals("  {") == false) {//find a bracket, insert it into array, as long as the previous entry is not also a bracket
  140.                             sorted[index] = unsorted.get(i);
  141.                             unsorted.set(i, "");
  142.                             index++;
  143.                         }
  144.                     }
  145.  
  146.                     //search for the contents of the loop
  147.                     for (int i = 0; i < unsorted.size();i++) {
  148.                         if(unsorted.get(i).contains(doubleIndent)) {
  149.                             sorted[index] = unsorted.get(i);
  150.                             unsorted.set(i, "");
  151.                             index++;
  152.                         }
  153.                     }
  154.  
  155.                     //search for the closing brackets
  156.                     for (int i = 0; i < unsorted.size();i++) {
  157.                         if(unsorted.get(i).equals("  }") && sorted[index-1].equals("  }") == false) {//find a bracket, insert it into array, as long as the previous entry is not also a bracket
  158.                             sorted[index] = unsorted.get(i);
  159.                             unsorted.set(i, "");
  160.                             index++;
  161.                         }
  162.                     }
  163.  
  164.                     //search for any leftovers of the main function
  165.                     for (int i = 0; i < unsorted.size();i++) {
  166.                         if(unsorted.get(i).contains(indent)  && unsorted.get(i).contains("return") == false) {//find a bracket, insert it into array, as long as the previous entry is not also a bracket
  167.                             sorted[index] = unsorted.get(i);
  168.                             unsorted.set(i, "");
  169.                             index++;
  170.                         }
  171.                     }
  172.  
  173.                     //search for the return statement of the main function
  174.                     for (int i = 0; i < unsorted.size();i++) {
  175.                         if(unsorted.get(i).contains(indent)  && unsorted.get(i).contains("return")) {//find a bracket, insert it into array, as long as the previous entry is not also a bracket
  176.                             sorted[index] = unsorted.get(i);
  177.                             unsorted.set(i, "");
  178.                             index++;
  179.                         }
  180.                     }
  181.  
  182.                     for (int i = 0; i < unsorted.size();i++) {
  183.                         if(unsorted.get(i).equals("}") && sorted[index-1].equals("}") == false) {//find a bracket, insert it into array, as long as the previous entry is not also a bracket
  184.                             sorted[index] = unsorted.get(i);
  185.                             unsorted.set(i, "");
  186.                             index++;
  187.                         }
  188.                     }
  189.  
  190.  
  191.                 } catch (FileNotFoundException e) {
  192.                     ErrorFrame ef = new ErrorFrame(e);//create a new frame containing the Java stacktrace of the exception
  193.                 }
  194.             }
  195.         }
  196.         try {
  197.             PrintWriter writer = new PrintWriter(outputFileName, "UTF-8");
  198.             for (String s: sorted) {
  199.                 writer.println(s);
  200.             }
  201.             writer.close();
  202.         } catch (FileNotFoundException e) {
  203.             ErrorFrame ef = new ErrorFrame(e);
  204.         } catch (UnsupportedEncodingException e) {
  205.             ErrorFrame ef = new ErrorFrame(e);
  206.         }
  207.     }
  208. }
  209.  
  210.  
  211. Challenge Output:
  212.  
  213. #include <iostream>
  214.  
  215. int main()
  216. {
  217.   int sum = 0
  218.   for (int i = 0; i <= 100; ++i)
  219.   {
  220.     sum = i + sum;
  221.   }
  222.   std::cout << sum;
  223.   return 0;
  224. }
Add Comment
Please, Sign In to add comment