2010年11月21日 星期日

Expression Parser - 五則運算式解析

import java.util.HashMap;

public class OperatorPrioritySaver {
 HashMap priorityMap = new HashMap();
 public OperatorPrioritySaver() {
  priorityMap.put("+", 1);
  priorityMap.put("-", 1);
  priorityMap.put("*", 2);
  priorityMap.put("/", 2);
  priorityMap.put("%", 2);
 }
 int comp(String op1, String op2) throws RuntimeException{
  if(priorityMap.get(op1)==null ){
   throw new RuntimeException("not an accept op :" +op1);
  }
  if(priorityMap.get(op2)==null ){
   throw new RuntimeException("not an accept op :" +op2);
  }
  return priorityMap.get(op1).compareTo(priorityMap.get(op2));
 }
 boolean acceptOperator(String test){
  return priorityMap.keySet().contains(test);
 }
}
import java.util.Stack;

public class ExpressionParser {
 private String exprString ;
 private int value ;

 OperatorPrioritySaver opsvr = new OperatorPrioritySaver();

 Stack nums = new Stack();
 Stack ops = new Stack();
 
 public ExpressionParser(String exprString) {
  this.exprString = exprString ;
 }
 public String getExprString(){
  return exprString ;
 }
 public int getValue() {
  return value;
 }
 
 public String toString() {
  return exprString +" = " + value ;
 }
 public void parse() {
  String terns[] = exprStr2Terns();
  
  int num = 0 ;
  String op = null ;
  for(String tern : terns){
   try{
    num = Integer.parseInt(tern) ;
    op = null ;
   }catch(NumberFormatException e){
    op = tern ;
   }
   
   if(op == null){ // number
    nums.push(num);
   }else{ // operator
    String top = null ;
    if("(".equals(op) || ")".equals(op)){
     if("(".equals(op)){
      ops.push(op);
     }else if(")".equals(op)){
      while( (top = ops.pop()) != null && 
        (!top.equals("("))){
       doOperator(top);
      }
     }
    }else if(opsvr.acceptOperator(op)){
     while( ops.size() > 0 &&
       (top = ops.peek()) != null && 
       (!top.equals("(")) && 
       (opsvr.comp(top, op)>=0)){
      top = ops.pop();
      doOperator(top);
     }
     ops.push(op);
    }else {
     System.err.println("Unsupported operator : " + op);
    }
   }
  }
  while(ops.size() != 0){
   doOperator(ops.pop());
  }
  
  value = nums.get(0);
 }
 private String[] exprStr2Terns() {
  // 目前使用空格隔開所有的符號
  // 想不用空格隔開 可以自行修改
  return exprString.split(" ");
 }

 private void doOperator(String op) {
  char opCh = op.toCharArray()[0];
  int num2 = 0, num1 = 0;
  switch(opCh){
  case '+':
   num2 = nums.pop();
   num1 = nums.pop();
   nums.push(num1+num2);
   break;
  case '-': 
   num2 = nums.pop();
   num1 = nums.pop();
   nums.push(num1-num2);
   break;
  case '*': 
   num2 = nums.pop();
   num1 = nums.pop();
   nums.push(num1*num2);
   break;
  case '/': 
   num2 = nums.pop();
   num1 = nums.pop();
   nums.push(num1/num2);
   break;
  case '%': 
   num2 = nums.pop();
   num1 = nums.pop();
   nums.push(num1%num2);
   break;
  }
 }
 public static void main(String[] args) {
  ExpressionParser expr = new ExpressionParser("( 1 + 2 ) * 3");
  expr.parse();
  System.out.println(expr);
 }
}

2010年11月19日 星期五

安裝了Ubuntu

昨晚失眠~~ (我到底再說什麼到底XD)
其實是因為最近睡覺時間都是四五點,昨晚又真的時間到了還很不想睡
於是起床開了我那見鬼的XP,竟然開不了機
在有著十分充分理由的情況下,我把他給format掉了!!!

安裝的過程也不是十分順利
因為我最先找的10.10並不是很順利的可以安裝
我本來以為是我自己硬碟怪怪的
還找了指令把|MBR給清乾淨
dd if=/dev/zero of=/mnt/sda bs=446 count=1
這個指令其實是在做複製位元的動作 把if複製到of去長度為bs 這個就是在將硬碟磁區的mbr清除
bs=446是因為 最前面的512 byte其實就是整個mbr
而之前的446個byte是在存開機程式的 後面的(512 - 446)byte則是 儲存磁區分割的資料
count參數呢~~ 待查!!

搞了半天 等我從燒了一片10.04以後就順利的安裝完成了!!! GOOD!!

Ubuntu + lazyscripts + apt-get 讓我省了不少時間
音樂影片都可以順利的撥放
看到deb結尾的副檔名就是很順利的ko了

目前要在我的ubuntu裡面安裝LAMP希望可以順利成功
PS.安裝MySQL時抓下來的是rpm檔 在安裝了rpm要來rpm -ivh時
沒想到竟然可以用alien將rpm轉換成deb 然後在點兩下就安裝了 真的是太方便了!!!!
還差把LAMP都整合在一起 這個感覺起來是大工程~~ 加油!!!

2010年11月16日 星期二

國中時的題目 用天秤找出一堆砝碼中唯一過重的那一個。

import java.util.Arrays;
import java.util.Random;

public class DrawingPanel {
 static int N = 8;
 public static void main(String[] args) {
  float balls[] = new float[N];
  Arrays.fill(balls, 1f);

  Random rand = new Random();
  balls[rand.nextInt(N) % balls.length] = 1.2f;
  
  int rs = compare(balls, 0, balls.length);
  
  for(int i = 0 ; i < balls.length ;i++){
   System.out.println(String.format("%c : %.1f", 'A'+i, balls[i]));
  }
  System.out.println("ans : " + (char)('A'+rs));
  
 }
 static int compare(float[] balls, int first, int last) {
  int size = last-first;
  if(size == 1){
   return first ;
  }
  int group = size / 3 ;
  if( size % 3 != 0)
   group = size / 3 + 1 ;
  
  float gp1 = sum(balls, first, first+group);
  float gp2 = sum(balls, first+group, first+2*group);
  if(gp1 > gp2){
   return compare(balls, first, first+group);
  }else if(gp1 < gp2){
   return compare(balls, first+group, first+2*group);
  }else{
   return compare(balls, first+2*group, last);
  }
  
 }
 static float sum(float arr[], int first, int last){
  float sum = 0f ;
  for(int i = first ; i < last ; i++)
   sum += arr[i];
  return sum ;
 }
}
這是在某個論壇回答人家問題的時候寫的!!
想到可以把他貼過來~~

2010年11月13日 星期六

Java - 字型處理

取得所有Font
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for(Font f: fonts){
System.out.println(f.getName());
}
自行設定字型
Font font = new Font(fontName, fontStyle, fontSize);
Font newFont = font.deriveFont(fontType); // int
Font newFont = font.deriveFont(fontSize); // float
Font newFont = font.deriveFont(fontType, fontSize); // int, float
設定Font(在JPanel中)
setFont(font);
取得FontMetrics,並用其取得文字在某種字型時的資訊
FontMetrics metrics = getFontMetrics(font);
messageWidth = metrics.stringWidth(message);

正規表示式中的 ? + * {m,n}用法


整理成圖片應該有好看一點。

2010年11月12日 星期五

java - Regular Expression

Java Regular Expression 用法!!


String target = "a" ;
Pattern ptn = Pattern.compile("[a-zA-Z]");
if(ptn.matcher(target).find()){
    System.out.print("find!!");
}

要找時間把正規表示式的寫法整理一下,每次用都要查真的是煩死人了!!

2010年11月6日 星期六

if和goto代替for

這是在知識家幫人家回答的題目,用if和goto代替for寫出九九乘法表(的格式)
想說打都打了,不要浪費!
for(int i = 0 ; i < 9 ; i++){
  // work here
}
============變成============
  int i = 0 ;
LOOP_BEGIN :
  if(i < 9){
    // work here
    i++ ;
    goto LOOP_BEGIN ;
  }else{
    goto LOOP_END ;
  }
LOOP_END :
以下為完整的程式
#include <iostream>
#include <cstdlib>
using namespace std ;
int main(){
    int i = 9;
LOOP_1_BEGIN:
    if(i >= 1){
        int j = 9 ;
LOOP_2_BEGIN:
        if(j >= 1){
            cout << i << "*" << j << " " ;
            j-- ;
            goto LOOP_2_BEGIN ;
        }else{
            goto END_LOOP_2 ;    
        }
END_LOOP_2 :
        cout << endl ;
        i-- ;
        goto LOOP_1_BEGIN ;
    }else{
        goto END_LOOP_1 ;    
    }
END_LOOP_1 :
    system("pause");
    return 0 ;    
}

2010年11月3日 星期三

mod_perl 安裝

只要選對了Perl的版本 就可以了!!!!

昨天弄了一個晚上,到ActiveState的網站上用新版的Perl 5.12.xxx要來安裝mod_perl,結果都是失敗的!
On不起來就是On不起來。雖然裡面有mod_cgi可以用,但是我的老毛病又犯了!! 硬是要弄到好 >"<


後來改用ActivePerl-5.8.xxx,在執行下面指令,並且依照安裝的過程的提示,一切就順利的OK了!! 感動!!

ppm install http://theoryx5.uwinnipeg.ca/ppms/mod_perl.ppd
執行完後,會在中間所指定的資料夾產生mod_perl,只要把它放在Apache2.2中的modules資料夾裡面,並且修改httpd.conf加入以下指令就行了。
LoadFile "C:/Path/to/Perl/bin/perl58.dll"
   LoadModule perl_module modules/mod_perl.so

以上mod_perl的安裝算是完成了,phpinfo()已經可以看到mod_perl了,接著需要將路徑指派給perl。

<ifmodule perl_module>
 LoadFile "C:/Perl/bin/perl58.dll"
 
 <directory "C:/Apache2.2/cgi-bin">
  SetHandler perl-script
  PerlResponseHandler ModPerl::Registry
  PerlOptions +ParseHeaders
  AllowOverride all 
  Options all 
  Order allow,deny
  Allow from all
 </Directory>
</IfModule>

2010年11月2日 星期二

安裝 Apache 2.2 和 設定PHP5

複製自php安裝目錄install.txt L652~L673
內容看起來是安裝php5到apache1.3
Installing as an Apache module

    You should add the following lines to your Apache httpd.conf file:

    Example 2-3. PHP as an Apache 1.3.x module

    This assumes PHP is installed to c:\php. Adjust the path if this is not the case.

    For PHP 5:
# Add to the end of the LoadModule section
LoadModule php5_module "C:/php/php5apache.dll"  我的路徑不一樣,檔案則是php5apache2_2.dll

# Add to the end of the AddModule section
AddModule mod_php5.c 在apache2.2中沒有AddModule所以我沒有用


    For both:
# Add this line inside the <ifmodule> conditional brace</ifmodule>
AddType application/x-httpd-php .php

# For syntax highlighted .phps files, also add
AddType application/x-httpd-php-source .phps 這一行也沒有設定

參考上面的文件,

  1. 安裝完apache2.2,
  2. 安裝PHP5
  3. 設定httpd.conf
    • LoadModule php5_module "C:\Program Files\PHP\php5apache2_2.dll"
    • <IfModule php5_module>
          AddType application/x-httpd-php .php
      </IfModule>
    • <IfModule dir_module>
          DirectoryIndex index.php index.htm index.html
      </IfModule>

    接著,就是測試了,重點只有不要吧PHP的括胡用錯。 Orz

<?
    phpinfo();
?>

PS.使用PHP5.2.14和PHP5.3.3的msi包裝安裝,在選擇了某些extension時,會讓Apache開不起來。