Last active
August 29, 2015 14:02
-
-
Save HabaCo/f4c75ec8752a33f0be17 to your computer and use it in GitHub Desktop.
讀檔&計算車流量
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
// 車流量 | |
public class Q940307 { | |
public static void main(String[] args) throws IOException { | |
String fileName = "940307.SM"; | |
FileReader fr = new FileReader(fileName); | |
BufferedReader br = new BufferedReader(fr); | |
int[] sumCar = new int[4]; // 4 種車型 | |
int[] sumDay = new int[7]; // 7 天 | |
int[][] data = new int[7][4]; // 建一個二維的資料表,但依照題意其實用不到 | |
String s; | |
while ((s=br.readLine())!=null && s.length()>0){ | |
String[] tmp = s.split(","); // [0]星期 [1]大型車 [2]中型車 [3]小型車 [4]公務車 | |
// sumCar[i] 將各種車型的流量加總 | |
// sumDay[0, 1 .. 6] 將各個天數的流量加總 | |
switch(tmp[0]){ | |
case "SUNDAY": | |
for (int i=0; i<4; i++){ | |
data[0][i] += Integer.parseInt(tmp[i+1].trim()); // trim() = 去掉頭尾的空白字元 | |
sumCar[i] += Integer.parseInt(tmp[i+1].trim()); | |
sumDay[0] += Integer.parseInt(tmp[i+1].trim()); | |
} | |
break; | |
case "MONDAY": | |
for (int i=0; i<4; i++){ | |
data[1][i] += Integer.parseInt(tmp[i+1].trim()); | |
sumCar[i] += Integer.parseInt(tmp[i+1].trim()); | |
sumDay[1] += Integer.parseInt(tmp[i+1].trim()); | |
} | |
break; | |
case "TUESDAY": | |
for (int i=0; i<4; i++){ | |
data[2][i] += Integer.parseInt(tmp[i+1].trim()); | |
sumCar[i] += Integer.parseInt(tmp[i+1].trim()); | |
sumDay[2] += Integer.parseInt(tmp[i+1].trim()); | |
} | |
break; | |
case "WEDNESDAY": | |
for (int i=0; i<4; i++){ | |
data[3][i] += Integer.parseInt(tmp[i+1].trim()); | |
sumCar[i] += Integer.parseInt(tmp[i+1].trim()); | |
sumDay[3] += Integer.parseInt(tmp[i+1].trim()); | |
} | |
break; | |
case "THURSDAY": | |
for (int i=0; i<4; i++){ | |
data[4][i] += Integer.parseInt(tmp[i+1].trim()); | |
sumCar[i] += Integer.parseInt(tmp[i+1].trim()); | |
sumDay[4] += Integer.parseInt(tmp[i+1].trim()); | |
} | |
break; | |
case "FRIDAY": | |
for (int i=0; i<4; i++){ | |
data[5][i] += Integer.parseInt(tmp[i+1].trim()); | |
sumCar[i] += Integer.parseInt(tmp[i+1].trim()); | |
sumDay[5] += Integer.parseInt(tmp[i+1].trim()); | |
} | |
break; | |
case "SATURDAY": | |
for (int i=0; i<4; i++){ | |
data[6][i] += Integer.parseInt(tmp[i+1].trim()); | |
sumCar[i] += Integer.parseInt(tmp[i+1].trim()); | |
sumDay[6] += Integer.parseInt(tmp[i+1].trim()); | |
} | |
break; | |
} | |
} | |
fr.close(); | |
// // 列出每一天的大、中、小、公務車流量 | |
// for(int[] arr : data){ | |
// for(int a: arr) | |
// System.out.print(a+" "); | |
// System.out.println(); | |
// } | |
// System.out.println(); | |
// int select=1; // User 選項 | |
// if (select==1){ | |
int sum=0; | |
for(int i:sumDay) | |
sum+=i; | |
orderByDay(sum,sumDay); | |
System.out.println(); | |
// } | |
// else if (select ==2){ | |
int sum2=0; | |
for(int i:sumCar) | |
sum2+=i; | |
orderByCar(sum2,sumCar); | |
// } | |
} | |
// 以日期顯示 | |
public static void orderByDay(int sum, int[] sumDay){ | |
for(int i=1; i<=sumDay.length; i++){ | |
switch(i%7){ | |
case 0: | |
System.out.print("星期日\t"); | |
break; | |
case 1: | |
System.out.print("星期一\t"); | |
break; | |
case 2: | |
System.out.print("星期二\t"); | |
break; | |
case 3: | |
System.out.print("星期三\t"); | |
break; | |
case 4: | |
System.out.print("星期四\t"); | |
break; | |
case 5: | |
System.out.print("星期五\t"); | |
break; | |
case 6: | |
System.out.print("星期六\t"); | |
break; | |
} | |
System.out.println(linePercent(sum,sumDay[i%7])+" "+adjustInt(sumDay[i%7])); | |
} | |
} | |
// 以車型顯示 | |
public static void orderByCar(int sum, int[] sumCar){ | |
for(int i=0; i<sumCar.length; i++){ | |
switch(i){ | |
case 0: | |
System.out.print("大型車\t"); | |
break; | |
case 1: | |
System.out.print("中型車\t"); | |
break; | |
case 2: | |
System.out.print("小型車\t"); | |
break; | |
case 3: | |
System.out.print("公務車\t"); | |
break; | |
} | |
System.out.println(linePercent(sum,sumCar[i])+" "+adjustInt(sumCar[i])); | |
} | |
} | |
// 條狀圖 | |
public static String linePercent(int max, int range){ | |
String s=""; | |
// max = 總流量, range = 該單位流量 | |
// 100*range/max = 百分比 | |
for (int i=0; i<100*range/max; i+=2) // 單純為了不要太長而調整百分比/單位 o_O | |
s+="■"; | |
return s; | |
} | |
// 遞迴調整數字格式 (如果除1000的餘數超過四位數則加上逗點) | |
public static String adjustInt(int n){ | |
String s =""; | |
if (n>1000) | |
s+=String.valueOf(adjustInt(n/1000))+","+adjustInt(n%1000); | |
else | |
s+=String.valueOf(n); | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment