2013年1月22日 星期二

宣告三個三維陣列,用foreach迴圈印出

宣告三個三維陣列,用foreach迴圈印出
-------------------------------------------------------------------------------------------------------------------
public class Array {
 public Array() {
 }
 public void showArrays(int[][][]... a) {//這行事實上是用一個四維陣列去接外面的三維陣列
  for (int[][][] b : a)
   for (int[][] c : b)
    for (int[] d : c)
     for (int e : d)
      System.out.println(e);
 }
 /** 宣告三個三維陣列,用foreach迴圈印出
  * @CHEN-YU-YU(2013/01/23)
  */
 public static void main(String[] args) {
  int[][][] a = { { { 1, 2, 3 }, { 4, 5, 6 } },
    { { 7, 8, 9 }, { 10, 11, 12 } },
    { { 13, 14, 15 }, { 16, 17, 18 } } };
  int[][][] b = { { { 19, 20, 21 }, { 22, 23, 24 } },
    { { 25, 26, 27 }, { 28, 29, 30 } },
    { { 31, 32, 33 }, { 34, 35, 36 } } };
  int[][][] c = { { { 37, 38, 39 }, { 40, 41, 42 } },
    { { 43, 44, 45 }, { 46, 47, 48 } },
    { { 49, 50, 51 }, { 52, 53, 54 } } };
  Array array = new Array();
  array.showArrays(a, b, c);
 
 }
}
------------------------------------------------------------------------------------------------------------------

2013年1月5日 星期六

Java-驗證身分證的Method


--------------------------------------------------------------------------------------------------------
public class IDCheck {
 private int convert(char ch){
  ch =Character.toUpperCase(ch);
  switch(ch){
      case 'A': return 10;
      case 'B': return 11;
      case 'C': return 12;
      case 'D': return 13;
      case 'E': return 14;
      case 'F': return 15;
      case 'G': return 16;
      case 'H': return 17;
      case 'I': return 34;
      case 'J': return 18;
      case 'K': return 19;
      case 'L': return 20;
      case 'M': return 21;
      case 'N': return 22;
      case 'O': return 35;
      case 'P': return 23;
      case 'Q': return 24;
      case 'R': return 25;
      case 'S': return 26;
      case 'T': return 27;
      case 'U': return 28;
      case 'V': return 29;
      case 'W': return 32;
      case 'X': return 30;
      case 'Y': return 31;
      case 'Z': return 32;
  }
  return -100;
 }
 private boolean checkFormat(String id){
  if(id.length() !=10){
   System.out.println("長度不符合");
   return false;
  }
  for(int i=1; i<10; i++){
   char ch = id.charAt(i);
   if(!Character.isDigit(ch)){
    System.out.println("格式錯誤");
    return false;
   }
  }
  return true;
 }
 public boolean check(String id){
  if(!checkFormat(id)){
   return false;
  }
  char ch = id.charAt(0);
    int D0 = this.convert(ch);
 
   int D1 = Integer.parseInt(id.substring(1, 2));
   int D2 = Integer.parseInt(id.substring(2, 3));
   int D3 = Integer.parseInt(id.substring(3, 4));
   int D4 = Integer.parseInt(id.substring(4, 5));
   int D5 = Integer.parseInt(id.substring(5, 6));
   int D6 = Integer.parseInt(id.substring(6, 7));
   int D7 = Integer.parseInt(id.substring(7, 8));
   int D8 = Integer.parseInt(id.substring(8, 9));
   int D9 = Integer.parseInt(id.substring(9, 10));
   int X1 = D0/10;
   int X2 = D0%10;
   int Y = X1+ 9*X2+ 8*D1+ 7*D2+ 6*D3+ 5*D4+4*D5+ 3*D6+ 2*D7+ D8;
   int CheckCode = (10-(Y % 10)) % 10;
 
   if(CheckCode==D9) {
    return true;
   } else {
     return false;
   }
 }
}
--------------------------------------------------------------------------------------------------------

2012年12月20日 星期四

Java-StringBuffer比對內容是否對稱

public class Play3{
 public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("123345");
      System.out.println(sb);
      System.out.println("長度"+sb.length());
      System.out.println("等下要比對次數"+sb.length()/2);
      int f=0;
      int l=sb.length()-1;
      for(int i= 0 ; i<sb.length()/2;i++)
        {
         if(sb.toString().charAt(f) == sb.toString().charAt(l))
        {
            System.out.print("第"+(i+1)+"個跟後面數來第"+(i+1)+"個有一樣");
            break;
         }
         f++;
           l--;
        }
    }
}

Java-寫出一個過濾陣列的Method

  public int[] deleteSameNumber(int[] ary)
  {
    int[] a = new int[ary.length];
    int k = 0;
    for (int i = 0 ; i<ary.length ; i++)
        {
        int exist = 0;//此值用來檢查是否有重複性
        for(int j = 0; j<a.length;j++)
        {
        if(ary[i] == a[j]) exist =1;
        }
   
        if (exist == 0) a[k++]=ary[i];//表示傳進去的ary[i]在a陣列裡沒有
   
       }
       return a;
       }

2012年11月27日 星期二

手動建置一台虛擬的SwitchHub

TUN/TAP 原本就是 Linux 系統所提供的虛擬網路介面裝置,只是預設並不是每套 Linux 的發行版本都有內建此功能。

查看是否有安裝
$tunctl

操控TUN/TAP的套件名稱為"uml_utilities"
$sudo apt-get install uml-utilities


建立一張網路卡名稱為p1只有student可以用
$sudo tunctl -t p1 -u user

用BashScript寫一隻程式,可以快速建立網卡
























建立橋接裝置br01(這時候建立的只是一個空殼)
$sudo brctl addbr br01

指定一個ip給br01
$sudo ifconfig br01 192.168.10.1/24 up

這時候的bridge抽象是沒有port也就是說沒有洞讓別人聯接進來
底下手洞建立三張網卡br01p1、br0p2、br0p3
$sudo tunctl -t br0p1 -u student
$sudo tunctl -t br0p2 -u student
$sudo tunctl -t br0p3 -u student




$sudo ifconfig br0p1 up 
$sudo ifconfig br0p2 up
$sudo ifconfig br0p3 up

然後把網卡加進br01,一台虛擬的SwitchHub就完成了!!!
$sudo brctl addif br01 br0p1
$sudo brctl addif br01 br0p1
$sudo brctl addif br01 br0p1




























2012年11月20日 星期二

Linux核心虛擬系統

 KVM:Kernel-based Virtual Machine
關於KVM相關資訊:http://www-03.ibm.com/systems/virtualization/infrastructure/open/index.html

完全虛擬化(Full virtualization) 例如:VMware、VirtualBox。
    半虛擬化(Paravirtualization)此虛擬化會去改元系統的Kernel,此技術是XEN創造。

寄居架構(Hosted Architecture)
裸機架構(Bare-Metal)系統裝完就內建虛擬技術,KVM、VMware ESX

Nested VM巢狀系統

查看是否有支援VT-x ※有些硬體support but BIOS沒開
$sudo kvm-ok

探索核心模組
$modprobe -l |grep kvm 探索核心模組








kvm主要是負責虛擬CPU、Memory
quem負責周邊hard disk、N
$sudo apt-get install qemu-kvm

啟動虛擬機
 $kvm -m 128 -cdrom ISO/tsc32.iso -boot d
-m 記憶體128M
cdrom掛到/ISO/底下
這裡採用的Linux為Tiny core的Live CD版本此作業系統特色是檔案極小,大約是10MB
出現以下畫面就代表虛擬機啟動成功but 沒live cd版本















底下將建置有HardDisk的虛擬機


Step1(終端機執行)
先產生一個資料夾並產生虛擬硬碟檔


產生虛擬硬碟檔
$ kvm-img create -f raw vmdisk/tsc32.img 20m
 
 
 

啟動 Tiny Server Core 虛擬電腦
$kvm -m 128 -cdrom ISO/tsc32.iso -hda vmdisk/tsc32.img -boot d & 
     -m ─ memory
 -cdrom ─ cdrom掛載的位置
   -hda ─ Hard Disk掛載的位置
-boot d ─ 先從硬碟開始讀
      & ─ 啟動後在背後執行
 
Step2(QEMU視窗內執行)
建立分割區並格式化ext4檔
 
$sudo fdisk /dev/sda 
底下是進入硬碟分隔模式的指令 
---
  |--n 新增一個分割區
  |
  |--p (primary)建立主要分割區)
  |
  |--1
  |
  |--1
  |
  |--1
  |
  |--w 確定寫入 
 
 
$sudo mkfs.ext4 /dev/sda1
 
重開機 
$sudo reboot
 



底下用Shell Script語法寫一個檔案,使其可以快速地開啟多部虛擬機
$sudo nano myring.sh 
----------------------------------------myring.sh---------------------------------------------------
#!/bin/bash

[ ! -f vmdisk/myring01.img ] && cp vmdisk/tsc32.img vmdisk/myring01.img
 #-f=find 中括號裡判斷file是否存在 不存在時執行後面的指令
 
kvm -m 64 -hda vmdisk/myring01.img -cdrom ISO/tsc32.iso -boot d &

 
 
 [ ! -f vmdisk/myring02.img ] && cp vmdisk/tsc32.img vmdisk/myring02.img
 
 kvm -m 64 -hda vmdisk/myring02.img -cdrom ISO/tsc32.iso -boot d & 
 
 
  [ ! -f vmdisk/myring03.img ] && cp vmdisk/tsc32.img vmdisk/myring03.img
 
 kvm -m 64 -hda vmdisk/myring03.img -cdrom ISO/tsc32.iso -boot d & 
 
-----------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 



2012年11月18日 星期日

Ubuntu 12.04 server版本進階

選擇安裝的版本為 : ubuntu-12.04.1-server-amd64.iso
VM設定:VMWorksation 9
網路設定 :在VMWorksation 9網路設定為NAT

建立群組
    sudo groupadd aaa

將使用者a01 a02 加入aaa群組
sudo /etc/group 進程式底下 把a01,a02加到aaa那行最後面

讓檔案開啟所有權限
    sudo chmod 777 FileName

複製檔案
    cp Source Destination

※補充
A 拷貝到 B 假設B原來有該檔案 就會繼承B權限
             如果B沒有該檔案 就會繼承A檔案的權限,另外如果下cp 前面有加 sudo 則owner為root

改變檔案所屬群組chgrp
chgrp aaa mya01
          群組 檔案  

自創命令
---------------bashrc-----------------

alias ping='ping -c 4'
alias ipconfig='/etc/update-motd.d/50-landscape-sysinfo'
export PS1="\u@\h:"
alias bye'sudo shutdown -h +2"2分鐘後關機"'
-----------------------------------------



Sticky Bit

a01使用者到/home/student/創一個mysbit資料夾並更改權限
chmod 1777 mysbit
1.a01進到mysbit建檔Hello1.txt Hello.txt2 Hello3.txt
2三個使用者分別刪檔案
結構如下

home
  |
  |--student
  |     |
  |     |--mysbit
  |          |
  |          |-Hello1.txt    <--student 可以刪
  |          |
  |          |-Hello2.txt    <--a02     不能刪
  |          |
  |          |-Hello3.txt    <--a01     可以刪




sudo apt-get install apache2

sudo /etc/init.d/apache2 restart 啟動apache

nano /var/www/index.html  編輯網站




特殊指令
bash
pstree

----------自創程式test.sh-------------
read -p "" a
read -p "" b
declare -i sum=$(($a+$b))
echo $sum