示例#1
0
  /**
   * A function that receives parses a 2D string representation of a spreadsheet into a spreadsheet
   * object, displays, executes a list of commands on the spreadsheet and displays it after that
   *
   * @param stringSpreadsheet String representation of the provided spreadsheet where the element at
   *     the ith row and jth column is the expression for the cell at the ith row and jth column
   * @param commands List of (CellLocation, String) tuples that associate what the cell at the
   *     specified location should have its value set to
   */
  public static void genSpreadSheet(
      String[][] stringSpreadsheet, List<Tuple<CellLocation, String>> commands) {
    SpreadsheetState spreadsheet = new SpreadsheetState();
    for (int i = 0; i < stringSpreadsheet.length; i++) {
      for (int z = 0; z < stringSpreadsheet[i].length; i++) {
        spreadsheet.setExpression(new CellLocation(Util.toAddress(i, z)), stringSpreadsheet[i][z]);
      }
    }
    // print out the results of initializing the spreadsheet
    System.out.println(
        "----------------------\n" + spreadsheet.toString() + "\n----------------------\n\n");

    for (Tuple<CellLocation, String> c : commands) {
      spreadsheet.setExpression(c.fst(), c.snd());
    }

    // print out the results of setting the value according to the specified commands
    System.out.println(
        "----------------------\n" + spreadsheet.toString() + "\n----------------------");
  }
示例#2
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter dimensions");
    NUM_ROWS = sc.nextInt();
    NUM_COLS = sc.nextInt();
    SpreadsheetState spreadsheet = new SpreadsheetState();
    System.out.println("No of entries");

    int entries = sc.nextInt();
    System.out.println("Enter the element row and column followed by the expression");

    for (int i = 0; i < entries; ) {
      // The row and column must be space seperated
      String row = sc.next();
      if (Util.toInt(row.toCharArray()) >= NUM_ROWS) {
        System.out.println("Exceeded row size");
        continue;
      }
      int column = sc.nextInt();
      if (column >= NUM_COLS) {
        System.out.println("Exceeded column size");
        continue;
      }
      spreadsheet.setExpression(new CellLocation(row + column), sc.next());
      spreadsheet.recalculate();
      i++;
    }
    for (int i = 0; i < 5; i++) {
      System.out.println("Look at value..?");
      String element = sc.next();
      System.out.println(spreadsheet.getValue(new CellLocation(element)));
    }

    System.out.println(spreadsheet);
    sc.close();
  }