Skip to content

Instantly share code, notes, and snippets.

@HabaCo
Created May 27, 2014 13:08
Show Gist options
  • Save HabaCo/3d3161c12b633d37e387 to your computer and use it in GitHub Desktop.
Save HabaCo/3d3161c12b633d37e387 to your computer and use it in GitHub Desktop.
QB B09 with "sample.txt" self-customize
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Ex_9_2 {
public static void main(String[] args) throws FileNotFoundException {
String filepath = "sample.txt";
File file = new File(filepath);
Scanner scan = new Scanner(file);
if (file.exists()) {
/* Print 欄名稱 */
System.out.println("\t\t\t\t\t\t\t\t\t\t\tABOVE");
System.out.println("\t\t\t\t\t\t\t\t\t\t\tGRAND");
System.out.println("WELL_NO\tMON\tTUE\tWED\tTHU\tFRI\tSAT\tSUN\tTOTAL\tAVG\tRANK\tAVG ?");
System.out.println("---------------------------------------------------------------------------------------------");
int DATA_COUNT = 10;
int[][] oil = new int[DATA_COUNT][11]; // 預設10筆油井資料,每筆資料(編號、7天產量、總產量、平均產量、名次)共11欄
float grand_avg_per_week = 0; // 全部油井日平均產量
int oil_sum_all = 0; // 全部油井總產量
int counter = 0; // 紀錄文件裡資料出現筆數
int sort = Integer.parseInt(scan.nextLine()); // 第一筆資料為排序方法
String[] s2; // 儲存字串的陣列
for (int i = 0; scan.hasNext(); i++) {
s2 = scan.nextLine().split(","); // 將讀取的字串以","切割存入 s2 陣列中
counter++;
oil[i][9] = 0; // 初始化單一油井總產量
oil[i][10] = 0; // 初始化單一油井平均產量
for (int j = 0; j < s2.length; j++) { // 將循序資料寫入陣列
oil[i][j] = Integer.parseInt(s2[j]); // .. 將字串轉換整數儲存至油井資訊中
if (j > 0)
oil[i][8] += oil[i][j]; // .. 計算單一油井周總產量
} // ..
oil_sum_all += oil[i][8]; // .. 加總全部油井產量
if (!scan.hasNext())
oil[i + 1][0] = -1; // 設立中斷訊號 (若文件中找不到下一項)
}
grand_avg_per_week = (float) (oil_sum_all / (float) (counter) / 7.0f); // 每日平均產量
/* 資料排序 */
// if (sort==2) // 按產量排序
int maxIndex;
for (int x = 0; x < 10; x++) {
if (oil[x][0] == -1)
break; // loop 中斷條件
maxIndex = x; // maxIndex (預設為第一個油井)
for (int j = x; oil[j][0] != -1; j++)
// 選擇排序法(找出最大值往前放入)
if (oil[j][8] > oil[maxIndex][8]) // 若找到的資料(產量) > 第一項(產量)
maxIndex = j; // 將該資料位置存入 maxIndex 索引
for (int t, k = 0; k < oil[x].length; k++) { // 利用maxIndex索引將最大的資料(產量)交換至第x(最前)項(達成排序
t = oil[x][k];
oil[x][k] = oil[maxIndex][k];
oil[maxIndex][k] = t;
}
oil[x][10] = x + 1; // 產量排序順便記錄名次 (逐次完成依序為大->小)
}
if (sort == 1) { // 按編號排序(第一列)
int minIndex; // minIndex = 最小編號所在位置
for (int i = 0; i < 10; i++) {
if (oil[i][0] == -1) break; // loop 中斷條件
minIndex = i; // minIndex (預設為第一個油井)
for (int j = i; oil[j][0] != -1; j++)
// 選擇排序法(找出最小值往前放入)
if (oil[j][0] < oil[minIndex][0]) // 若找到資料(編號) < 第一項(編號)
minIndex = j; // 將該資料位置存入 minIndex 索引
for (int t, k = 0; k < oil[i].length; k++) { // 透過陣列交換達成排序
t = oil[i][k];
oil[i][k] = oil[minIndex][k];
oil[minIndex][k] = t;
}
}
}
for (int i = 0; i < oil.length; i++) { // Print 陣列內容驗證資料是否正確
if (oil[i][0] == -1) break; // 中斷條件
System.out.print(" ");
for (int j = 0; j < oil[i].length; j++) {
if (j == 9)
System.out.printf("%.1f\t", oil[i][j - 1] / 7.0f); // 周平均產量作小數處理
else
System.out.print(oil[i][j] + "\t");
}
if (oil[i][8] > grand_avg_per_week * 7)
System.out.println("Y"); // 超過日均產量則 Y
else
System.out.println("N"); // 若否則 N
}
System.out.printf(" GRAND AVERGE PER WEEK: %.1f PER WELL",
grand_avg_per_week);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment