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

* Reads a linear representation of a comparator network on standard * input and produces a graphical representation on standard * output. Excess whitespace (such as multiple spaces between * integers, newlines, and tabs) in the input should be ignored * by logically coalescing each contiguous whitespace sequence * into a single space character. The output should match the * graphical representation presented earlier * character-for-character. * @author Ed Ropple */ public class CnetLinToGraMain { /** * Reads in the linear numerical representation of the comparator network and * passes it to cnetlib's shared functions, which return the graphical * representation of the comparator network for display on stdout. * * @param args The command line arguments for the program. Are ignored * by this program. */ 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 the character X on a new line to stop"); System.out.println("reading from standard output. 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(); Boolean breakLoop = false; Scanner s = new Scanner(System.in); // 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(); 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); } ArrayList output = Functions.outputComparatorGraphic(comparatorList); System.out.println(); for (int i = 0; i < output.size(); i++) { System.out.println(output.get(i)); } System.exit(0); } }