import java.util.Scanner; import java.util.StringTokenizer; import java.util.ArrayList; /** * Satisfies Problem 19.d of Homework 1. Quoting from the assignment: *

* Provides a detailed view of the effect of a given comparator * network on a given input to that network. The program reads, on * standard input, the canonical linear representation of a * comparator network followed by the input data to that * network. These two components of the input are separated by one or * more blank lines. The input data consists of n integers, separated * by whitespace, where n is the number of inputs to the comparator * network. The program pro duces, on standard output, a graphical * trace of the action of the given network on the input data. * @author Ed Ropple */ public class CnetShowRunMain { /** * Accepts input of both comparator network (in linear format) and following * data set, then uses the comparator network to work through the reordering * of the data set in a stepwise manner while displaying all steps to * stdout. * * @param args */ public static void main(String[] args) { String input = ""; System.out.println("Enter below a whitespace-delineated list of"); System.out.println("integers that will serve as the basis for the"); System.out.println("comparator network this program will use."); System.out.println("Enter a single newline in order to stop"); System.out.println("reading from standard output; will seamlessly"); System.out.println("transition to data set reading, so you can paste"); System.out.println("in a comparator network plus set without any"); System.out.println("worry of not being understood. No error handling"); System.out.println("is done in the case of garbage characters, so"); System.out.println("proof your own values."); System.out.println(); Scanner s = new Scanner(System.in); // read from stdin, emerge on newline while (true) { System.out.print("> "); String foo = s.nextLine().trim(); if (foo.length() < 1) break; input += foo + " "; } input = input.trim(); StringTokenizer st = new StringTokenizer(input); if (st.countTokens() % 2 == 1) { System.err.println("ERROR: Invalid input - odd number of tokens."); System.exit(1); } int numberOfComparators = -1; ArrayList comparatorList = new ArrayList(); while (st.hasMoreTokens()) { int x = Integer.parseInt(st.nextToken()); if (x > numberOfComparators) numberOfComparators = x; int y = Integer.parseInt(st.nextToken()); if (y > numberOfComparators) numberOfComparators = y; comparatorList.add(new ComparatorPair(x, y)); } numberOfComparators += 1; // fencepost prevention if (numberOfComparators > 10) { System.err.println("Too many comparators. Can only have from 0-9."); System.exit(1); } System.out.println(); System.out.println("Comparator network input ended. Please input"); System.out.println("data set. Terminate entry with X on new line."); System.out.println("Note that the comparator network has a total of " + new Integer(numberOfComparators).toString()); System.out.println("comparators; inputting more or less sortable numbers"); System.out.println("than are in the set will generate an exception."); System.out.println("Integers of any size are acceptable, but the output"); System.out.println("may look misaligned/mashed together if you use"); System.out.println("integers larger than two digits."); System.out.println(); // begin data input song-and-dance ArrayList dataList = new ArrayList(); Boolean breakLoop = false; input = ""; // read from stdin, emerge on X or x while (breakLoop == false) { System.out.print("> "); String foo = s.nextLine().trim(); if (foo.length() < 1) continue; if (foo.charAt(0) == 'X' || foo.charAt(0) == 'x') { breakLoop = true; continue; } else { input += foo + " "; } } input = input.trim(); st = new StringTokenizer(input); while (st.hasMoreTokens()) { dataList.add(Integer.parseInt(st.nextToken())); } if (dataList.size() != numberOfComparators) { System.err.println("ERROR: dataList.size() != numberOfComparators."); System.err.println("Terminating..."); System.err.println(); System.exit(1); } Integer[] dataArray = new Integer[dataList.size()]; dataArray = dataList.toArray(dataArray); // grab a list of comparator graphics for use in the display... ArrayList outputGraphic = Functions.outputComparatorGraphic(comparatorList); System.out.println(); System.out.println(); System.out.println(outputGraphic.get(0)); System.out.println(outputGraphic.get(1)); outputPaddedDataSetList(dataArray); // this really should be extracted into a function but it's easier not // to special-case it for(int i = 0; i < comparatorList.size(); i++) { System.out.println(outputGraphic.get(i + 2)); ComparatorPair c = comparatorList.get(i); // not the most elegant swap ever, but it works int x = dataArray[c.left()]; int y = dataArray[c.right()]; int w; if (x > y) { w = y; y = x; x = w; } dataArray[c.left()] = x; dataArray[c.right()] = y; outputPaddedDataSetList(dataArray); } } /** * Outputs to stdout a padded data set to comply with the rules in * Problem 19.d. * @param dataSetArray the data set array to output */ public static void outputPaddedDataSetList(Integer[] dataSetArray) { for (int i = 0; i < dataSetArray.length; i++) { int j = 3 - dataSetArray[i].toString().length(); for (int k = 0; k < j; k++) { System.out.print(" "); } System.out.print(dataSetArray[i].toString()); } System.out.println(); } }