Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercise264;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- public class SortCppCode {
- 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.
- static File src;
- static JLabel listSize;
- public static void main(String[] args) {
- JFrame jframe = new JFrame("Sort C++ Code");//the main frame of the windows that will be opened up
- JButton sortButton = new JButton("Sort");
- sortButton.addActionListener(new ActionListener() {//add the action listener to the sorting button
- public void actionPerformed(ActionEvent e) {
- sort();//when the button is pressed, run the sorting
- }
- });
- sortButton.setEnabled(false);//disable the button by default. Later enabled when the src filename is given
- JLabel srcInputLabel = new JLabel("File Name:");
- JTextField srcInputTextField = new JTextField();
- JButton srcInputButton = new JButton("Confirm");
- srcInputButton.addActionListener(new ActionListener() {//add the action listener to the sorting button
- public void actionPerformed(ActionEvent e) {
- if (srcInputTextField.getText() != null && srcInputTextField.getText() != "") {//as long as there is actually something in the textbox
- src = new File(srcInputTextField.getText()) ;
- sortButton.setEnabled(true);
- jframe.setVisible(true);
- }
- }
- });
- jframe.setLayout(new GridLayout(1,5) );//sets the frame as a grid layout with three columns and two rows
- jframe.add(srcInputLabel);
- jframe.add(srcInputTextField);
- jframe.add(srcInputButton);
- jframe.add(sortButton);
- jframe.pack();
- jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//kill the program when the user closes the window
- jframe.setVisible(true);
- }
- public static void sort() {
- int index = 0;//int used to keep track of where you currently need to be in the "sorted" array
- final String indent = " ";
- final String doubleIndent = " ";
- ArrayList <String> unsorted = new ArrayList <String>();//create an arrayList to store the unsorted lines of code in.
- String[] sorted = new String[0];//create an array to store the sorted lines of code
- if(src != null) {//if the file object does not equal null
- if(src.exists()) {//check if it exists
- try {
- Scanner scan = new Scanner(src);//create a new scanner linked to the file
- while (scan.hasNextLine()) {//import all the lines of code from the file to the arrayList
- unsorted.add(scan.nextLine());
- }
- scan.close();
- sorted = new String[unsorted.size()];
- //START OF SORTING LOGIC
- //search for #include statements
- for (int i = 0; i < unsorted.size();i++) {//for each String in unsorted, check for "#include" lines
- if (unsorted.get(i).contains("include")) {
- sorted[index] = unsorted.get(i);//add to the list
- unsorted.set(i, "");//replaces the current string with an empty one to avoid any duplicates
- index++;
- }
- }
- sorted[index] = "";//add a blank line for readability;
- index++;//increment the index
- //search for the main function
- for (int i = 0; i < unsorted.size();i++) {//find the main function statement
- if (unsorted.get(i).equals("int main()")) {
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //search for the main function bracket
- for (int i = 0; i < unsorted.size();i++) {
- 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
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //search for constants defined within the main function
- for (int i = 0; i < unsorted.size();i++) {
- if (unsorted.get(i).contains(indent) && unsorted.get(i).contains(doubleIndent) == false && unsorted.get(i).contains("for") == false && (unsorted.get(i).contains("int") ||
- /* Placeholder. */ unsorted.get(i).contains("String") ||
- /* Placeholder. */ unsorted.get(i).contains("double") ||
- /* Placeholder. */ unsorted.get(i).contains("boolean"))) {
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //search for a "for" loop
- for (int i = 0; i < unsorted.size(); i++) {
- if (unsorted.get(i).contains(" for") && sorted[index-1].equals(" for") == false) {
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //find a bracket for the for loop
- for (int i = 0; i < unsorted.size();i++) {
- 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
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //search for the contents of the loop
- for (int i = 0; i < unsorted.size();i++) {
- if(unsorted.get(i).contains(doubleIndent)) {
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //search for the closing brackets
- for (int i = 0; i < unsorted.size();i++) {
- 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
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //search for any leftovers of the main function
- for (int i = 0; i < unsorted.size();i++) {
- 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
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- //search for the return statement of the main function
- for (int i = 0; i < unsorted.size();i++) {
- 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
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- for (int i = 0; i < unsorted.size();i++) {
- 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
- sorted[index] = unsorted.get(i);
- unsorted.set(i, "");
- index++;
- }
- }
- } catch (FileNotFoundException e) {
- ErrorFrame ef = new ErrorFrame(e);//create a new frame containing the Java stacktrace of the exception
- }
- }
- }
- try {
- PrintWriter writer = new PrintWriter(outputFileName, "UTF-8");
- for (String s: sorted) {
- writer.println(s);
- }
- writer.close();
- } catch (FileNotFoundException e) {
- ErrorFrame ef = new ErrorFrame(e);
- } catch (UnsupportedEncodingException e) {
- ErrorFrame ef = new ErrorFrame(e);
- }
- }
- }
- Challenge Output:
- #include <iostream>
- int main()
- {
- int sum = 0
- for (int i = 0; i <= 100; ++i)
- {
- sum = i + sum;
- }
- std::cout << sum;
- return 0;
- }
Add Comment
Please, Sign In to add comment