⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 training.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.util.*;
import java.io.*;
import javagently.*;

public class Training {

  /* Training schedules program    by J M Bishop   Jan 1997
   * ==========================    Java 1.1
   *                               updated August 2000
   * Creates class lists and schedules for employees,
   * including a list of those who are taking special courses.
   *
   * Uses bit sets and its own StringTokenizers rather than
   * javagently's Text class.
  */

  BitSet courses [];
  BitSet schedules [];
  int employeeMax, courseMax;
  Stream in = new Stream (System.in);

  public static void main (String arg []) throws IOException {
    new Training ();
  }

  Training () throws IOException {

    // The main program declares two arrays of sets and then
    // calls the three static methods to read and print out
    // the data.
    System.out.println ("*** Training Schedules ****");
    System.out.print("What is the highest course number? ");
    courseMax = in.readInt();
    System.out.print("What is the highest employee number?");
    employeeMax = in.readInt();

    // Create an array of sets, one for each course
    // Each set will be EmployeeMax big
    // but is created later as the data is read in
    courses = new BitSet [courseMax];

    // Create an array of sets, one for each employee
    // Each set will be CourseMax big
    // but is created later as the data is read in
    schedules = new BitSet [employeeMax];

    System.out.println("Enter each employee's schedule as follows:");
    System.out.println("Employee number  course numbers");
    System.out.println("Example 100  12  7  4  15");
    System.out.println("End with a blank line");

    readIn();
    display();
    userCodes();
  }

   void readIn () throws IOException {
    // Uses its own StringTokenizer since data is read a line
    // at a time. A blank line signifies zero tokens and
    // ends the reading of data.

    String s;
    StringTokenizer T;
    int n, c, ntokens;

    while(true) {
      s = in.readLine();
      T = new StringTokenizer (s);
      ntokens = T.countTokens ();
      if (ntokens == 0) break; // no more employees
      n = getInt(T);

      //Create a schedule for employee n
      schedules [n] = new BitSet (courseMax);
      for (int i=0; i<ntokens-1; i++) {
        c = getInt(T);

        // Create course c if not yet started
        if (courses[c] == null)
          courses[c] = new BitSet (employeeMax);

        // Put employee n on course c
        courses[c].set(n);

        // Put course c in employee n's schedule
        schedules [n].set(c);
      }
    }
    System.out.println("Data read in successfully.");
  }

  void display () {
    // prints each of the set arrays slightly differently.
    // calls printSet for printing a single set

    System.out.println("The course lists");
    System.out.println("================");
    for (int c =0; c<courseMax; c++) {
      System.out.print(c+": ");
      if (courses[c]==null) System.out.println("No students");
      else
        printSet(courses[c]);
    }
    System.out.println("The schedules");
    System.out.println("=============");
    for (int n = 0; n < employeeMax; n++) {
      if (schedules[n] != null) {
        System.out.print(n+": ");
        printSet(schedules[n]);
      }
    }
  }

  void userCodes () throws IOException {
    // A method to illustrate a bit set operation.
    // given a subset of the course numbers,
    // create the union (or) of all the employees
    // signed up for them and print that set.

    Text.prompt("Which are the computer-related courses?");
    BitSet codesNeeded = new BitSet(employeeMax);

    String s;
    StringTokenizer T;
    int c, ntokens;

    s = in.readLine();
    T = new StringTokenizer (s);
    ntokens = T.countTokens ();
    for (int i=0; i<ntokens; i++) {
      c = getInt(T);
      codesNeeded.or(courses[c]);
    }
    System.out.println("Employees needing usercodes");
    System.out.println("===========================");
    printSet(codesNeeded);
  }

  void printSet (BitSet b) {
    // Prints the members of a single bit set

    for (int i = 0; i < b.size(); i++)
      if (b.get(i)) System.out.print(i+"  ");
    System.out.println();
  }

  int getInt (StringTokenizer T) {
    String item = T.nextToken();
    return Integer.valueOf (item.trim()).intValue();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -