2009/05/13 Datastructure
上課範例:
class MyComp
{
private static boolean asc = true;
public MyComp() {}
public MyComp(boolean as) { asc = as; }
public int compare(Object o1, Object o2) {
T t1 = (T) o1;
T t2 = (T) o2;
double d1 = t1.doubleValue();
double d2 = t2.doubleValue();
if (asc)
return (int) Math.signum(d1-d2);
else
return -(int) Math.signum(d1-d2);
}
}
/**
*
* @author student
*/
public class ListnArray {
public static void main(String[] args) {
// LinkedList
// lli.add("67");
// lli.add("55");
// lli.add("-7");
// lli.add("12");
// System.out.println("lli="+lli);
// System.out.println("Converting to array ...n The array's content:");
// String val[]= lli.toArray(new String[0]);
// for (int i = 0; i < val.length; i++) {
// System.out.print(val[i]+" ");
// }
Double val[] = {7.1, 8., 9., 1., 2., 3., -7.};
Double val1[] = Arrays.copyOf(val, val.length);
System.out.println("原陣列排序:");
Arrays.sort(val1, new MyComp(true));
for (int i = 0; i < val1.length; i++) {
System.out.print(val1[i]+ " ");
}
System.out.println();
List
// System.out.println("ll="+ll);
LinkedList
System.out.println("原始lli="+lli);
lli.add(78.);
System.out.println("加入後lli="+lli);
Collections.sort(lli, new MyComp(false));
System.out.println("排序後lli="+lli);
}
}
上課範例2:
package ch3;
import java.util.*;
/**
*
* @author student
*/
public class ExpressionTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true)
{
System.out.print("請輸入你的運算式 >");
String expr = scan.nextLine();
if (expr.equals("")) break;
StringTokenizer stk = new StringTokenizer(expr, " +-*/()^", true);
while (stk.hasMoreTokens())
{
String token = stk.nextToken();
if (! token.equals(" ")) System.out.println(token);
}
}
System.out.println("謝謝使用.");
}
}
上課範例3:
package ch3;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author student
*/
public class MyList {
private static Stack ll = new Stack();
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
boolean done = false;
while ( ! done)
{
showMenu();
int n = scan.nextInt();
switch (n)
{
case 0: done = true;
break;
case 1: Insert(); break;
case 2: Delete(); break;
case 3: Print(); break;
case 4: Output2File(); break;
case 5: ReadFromFile(); break;
}
}
System.out.println("謝謝使用.");
}
private static void Delete() {
System.out.print("請輸入您想刪除的數值:");
// ll.remove((Object)(scan.nextInt()));
int n = scan.nextInt();
int index = ll.indexOf(n);
if (index == -1)
{
System.out.println("元素不存在! 直接返回...");
return;
}
while ( true )
{
index = ll.indexOf(n);
if (index == -1) break;
ll.remove(index);
}
System.out.println("元素已刪除.");
}
private static void Insert() {
System.out.print("請輸入您想加入的數值:");
ll.add(scan.nextInt());
}
private static void Output2File() {
System.out.print("請輸入檔名:");
String fn = scan.next();
try {
FileOutputStream fout = new FileOutputStream(fn);
PrintWriter pw = new PrintWriter(fout);
for (int i = 0; i < ll.size(); i++) {
pw.println(ll.get(i));
}
pw.close();
System.out.println("檔案寫入完畢.");
} catch (FileNotFoundException ex) {
Logger.getLogger(MyList.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void Print() {
System.out.println("串列內容: "+ll);
}
private static void ReadFromFile() {
System.out.print("請輸入檔名:");
String fn = scan.next();
try {
FileInputStream fout = new FileInputStream(fn);
scan = new Scanner(fout);
while(scan.hasNextInt())
{
ll.add(scan.nextInt());
}
scan.close();
System.out.println("檔案讀取完畢.");
scan = new Scanner(System.in);
} catch (FileNotFoundException ex) {
Logger.getLogger(MyList.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void showMenu()
{
System.out.println("-------------------------------");
System.out.println(" LinkedList 測試");
System.out.println("-------------------------------");
System.out.println(" 0. 離開");
System.out.println(" 1. 加入元素");
System.out.println(" 2. 刪除元素");
System.out.println(" 3. 印出串列內容");
System.out.println(" 4. 寫入檔案");
System.out.println(" 5. 從檔案讀入");
System.out.println("-------------------------------");
System.out.print(" 請輸入您的選擇? ");
}
}
表示法:
中序表示法 (Infix):運算子位於2個運算元之間,例如:A+B,A*B+C 等。
前序表示法 (prefix):運算子位於2個運算元之前,例如:+AB,+*ABC 等。
後序表示法 (postfix):運算子位於2個運算元之後,例如:AB+,AB*C+ 等。
運算子(operator):四則運算符號、括號、指數…
運算元(operand):數值
e.g. 1. 試將以下中序運算式轉為前序及後序運算式:
(1) a*b+c/d
(2) a+b+c+d
(3) -a+x+y/b*c*a
本文已同步發佈到「生活點滴」