總網頁瀏覽量

2012年5月27日 星期日

指標與結構


指標整理
錯誤: int *ptr; *ptr = 35;
正確: int *ptr, number = 10; ptr = &number;

型式一:
int *ptr;
int num = 20;
ptr = &20;

型式二:
int num =20;
int *ptr = #



ptri 位址為 0012ff84ptri +1 位址改為  0012ff88
取得鄰近位址內的值 *(ptr+1)

++*ip(*ip)++,需括號,因為單元運算子是由右向左做的

int* const p: 指向const的指標 可以改值, 不可以改位址
int const *p (相等於 const int* p); 可以改變位址,但不可以改值,即不能當左值(lvalue)使用
const int* const:指向constconst指標

EX1:
    int num1 = 10;
    int* const ptr = &num1;
    printf("%d\n", *ptr);
   
    int num2 = 20;
    ptr = &num2;  錯誤 不可以改位址
    *ptr += 20;  可以改值
Ex2:
    int num1 = 10;
    int const *ptr = &num1;
    printf("%d\n", *ptr);
   
    *ptr += 20;  不可以改值
    int num2 = 20;
    ptr = &num2; 可以改位址


陣列指定給指標:
指標 int *ptr;   陣列 int array[10];
ptr = array;  或者是  ptr = &array[0]; (array&array[0]是相同的)
===> a[i]就可寫為 *(a + i)

若指標ptr為指向陣列的第一個元素array[0]
*(ptr + 1)表示指向array[1],所以i個元素 *(ptr+i) 指向 array[i]

char *array[] 指標陣列 ===> *array[i]指向文字列的第一個字元
char (*array)[] 指標指向陣列

函式返回指標 int *f(); f為函數,傳回值為指向整數的指標
函式指標     int (*f)(); f是個指標,指向一個會傳回整數的函數,因為*的優先權比括號低

char (*(*x( )) [] )( ):函數, 傳回一指標,該指標指向指標陣列, 陣列中各元素指向一個會傳回char值的函數

char (*(*x[3]) ( )) [5]: 指標陣列, 3個元素, 各指向一個會傳回另一指標的函數, 該指標是指向含有5個元素的字元陣列

指標傳遞給函數
void address(int*); //函式原型
void address(int *ptr){ //函式定義
}


Callback function:
typedef int (*fp)(char a);
fp fp = fun;   //fun為一個指向函數的函數指標(參數為char, 返回值為int)
int x = fun('x');

#include <stdio.h>
#include <stdlib.h>
typedef int (*Add)(int, int);
typedef int (*Sub)(int, int);

typedef struct cal{
    Add add;
    Sub sub;
}cal;

int fun_add(int, int);
int fun_sub(int, int);

int main(int argc, char *argv[])
{
    cal a =
    {
        .add = fun_add,
        .sub = fun_sub
    };
   
    printf("%d, %d", a.add(1, 1), a.sub(1, 1));
    system("PAUSE"); 
    return 0;
}

int fun_add(int a, int b){
    return a+b;
}
int fun_sub(int a, int b){
    return a-b;
}



記憶體配置:
配置一個浮點數指標
float *fp;
fp = (float *)malloc(sizeof(float));
free(fp);


int *num;
num = (int *)malloc(sizeof(int) * 10)
free(num)

int array[10][20]

int **array= (int **)malloc(sizeof(int*)*10);
for(i=0;i<10;i++)
   *(array+i) = (int *)malloc(sizeof(int) * 20);


Example:
struct grade
{
    int math;
    int english;
    int computer;
};
struct grade *student;
student = (struct grade *) malloc(num * sizeof(struct grade));


鏈結串列:
struct list
{
   int num; 
   char name[10];
   char address[50];
   struct list *next;
};
typedef struct list node;
typedef node *link;

ptr = ( link ) malloc(sizeof(node));
ptr->next = NULL;


#include <stdio.h>
#include <stdlib.h>

struct point{
    int x;
    int y;          
};

struct rect{
    struct point p1;
    struct point p2; 
};

int main(int argc, char *argv[]){
    struct rect screen;
    screen.p1.x = 10;
    screen.p1.y = 15;
    screen.p2.x = 20;
    screen.p2.y = 25;
   
    printf("%d, %d\n", screen.p1.x, screen.p1.y); //10, 15
    printf("%d, %d\n", screen.p2.x, screen.p2.y); //20, 25
    system("PAUSE"); 
    return 0;
}

struct point *pp
宣告pp為一個指標,指向一個point結構
(*pp).x, (*pp).y 取值 或寫為 pp->x, pp->y

typedef int Length Length定義成與int同義的字
typedef char *String String定義成指向文字的指標
typedef char Bullow[4]; Bullow x; 定義xchar x[4]

Struct status{
    unsigned sex:1;
    unsigned marriage:1;
    unsigned age:7;
}
:1表示佔1位元
:7表示佔7位元


#define B "b"
char a[] = "a"B"c";
printf("%s", a);  //abc

換行使用\


#include <stdlib.h>

struct point{
    int x;
    int y;          
};

struct rect{
    struct point p1;
    struct point p2; 
};

int main(int argc, char *argv[]){
    struct rect screen;
    screen.p1.x = 10;
    screen.p1.y = 15;
    screen.p2.x = 20;
    screen.p2.y = 25;
   
    printf("%d, %d\n", screen.p1.x, screen.p1.y); //10, 15
    printf("%d, %d\n", screen.p2.x, screen.p2.y); //20, 25
    system("PAUSE"); 
     return 0;
}

struct point *pp
宣告pp為一個指標,指向一個point結構
(*pp).x, (*pp).y 取值 或寫為 pp->x, pp->y

typedef int Length Length定義成與int同義的字
typedef char *String String定義成指向文字的指標



2012年5月24日 星期四

JAVA 正則表示式


正則表達式: 符合一定規則的表達式
用於專門操作字串,用一些特定的符號來表示一些代碼操作,簡化程式碼
//對號碼進行校驗
//長度5~15, 0不可第一位, 只可數字
class Hello{

    public static void main(String[] args){

       String num = "123448940";

       int len = num.length();

       if(len >= 5 && len <= 15){

           if(!num.startsWith("0")){

              char[] arr = num.toCharArray();

              boolean flag = true;

              for(int x = 0; x < arr.length; x++){

                  if(!(arr[x] >= '0' && arr[x] <= '9')){

                     flag = false;

                     break;

                  }

              }

              if(flag){

                  System.out.println("num: " + num);

              }else{

                  System.out.println("請輸入數字");

              }

           }else{

              System.out.println("不可0開頭");

           }

       }else{

           System.out.println("長度不符");

       }

    }

}

改良:
//對號碼進行校驗
//長度5~15, 0不可第一位, 只可數字
class Hello{

    public static void main(String[] args){   

       checkNum();

    }

    public static void checkNum(){

       String num = "151651651";

       //規則 第一位[1-9], 第二位以後[0-9] 出現次數{4,14}

       String regex = "[1-9][0-9]{4,14}";

       boolean flag = num.matches(regex);

       if(flag){

           System.out.println("OK");

       }else{

           System.out.println("No");

       }

    }

}


操作功能
1.  匹配: String matches方法
[bcd] 第一個字元只能是b, c, d,並且只能有一個字元
[a-zA-Z][0-9] 第一位為字母,第二位為數字
[^abc]第一位不可a, b, c
[a-c[e-g]] aceg
[a-g&&[b-d]] b,c,d 交集

\d 也可以表示輸入為[0-9],使用時要注意特殊符號
\D 表示[^0-9]
\s 表示空白字元
\S表示非空白字元
\w 表示數字或英文字

\W表示非數字或非英文字

public class Main{

    public static void main(String[] args){

       demo();

    }

    public static void demo(){

       String str = "b5";

       String reg = "[bcd][\\d]";

       boolean b = str.matches(reg);

       System.out.println(b);//ture

    }

}

Greedy
X?:  X值出現一次或零次(完全沒有, 空字元)
X*:  X值出現零次或多次
X+:  X值出現一次或多次
X{n}:  X恰好n
X{n, }: X至少n
X{n,m}:X至少n次,但不超過m

    public static void testString(){
    String a = "11 2";
    String b = "2";
    String c = "22";
    String reg1 = "2?";
    p(a.matches(reg1));//false
    p(b.matches(reg1));//true
    p(c.matches(reg1));//false
   
    String reg2 = "2*";
    p(a.matches(reg2));//false
    p(b.matches(reg2));//true
    p(c.matches(reg2));//true

    String reg3 = "2+";
    p(a.matches(reg3));//false
    p(b.matches(reg3));//true
    p(c.matches(reg3));//true
   
    String reg4 = "2{1}";
    p(a.matches(reg4));//false
    p(b.matches(reg4));//true
    p(c.matches(reg4));//false
    }


2.  切割 split
public class Main{

    public static void main(String[] args){

       demo();

    }

    public static void demo(){

       String str = "a,b,c,d,e,f";

       String reg = ",";

       String[] strArray = str.split(reg);

       for(String s: strArray){

           System.out.println(s);

       }

    }

}

空格切割:
String str = "sqss   erfr    rferfer";
String reg = " +";  //空格出現一次或多次!

.切割
String str = "dwdaqwd.d.wd.wf.wqfq.wf";
String reg = "\\."; //.是特殊符號,必須先轉譯

特殊:
String str = "c:\\dwdqw\\dqda\\a.txt";
String reg = "\\\\";

特殊: 以疊字分割
String str = "daggegffftyvvvrs";
String reg = "(.)\\1+";
讓這個規則被蟲用,可以將規則封裝成一組,用()完成,組的出現會有編號,從1開始,想要使用已有的組可以通過 \n(n就是組的編號)的形式來獲取

3.替換 replaceAllreplace(String類別)
public class Main{

    public static void main(String[] args){

       demo();

    }

    public static void demo(){

       String str = "swdwdw46846232def48ce8f4";

       String reg = "\\d{5,}";//替換規則

       String newStr = "#";//替換為此字串

      

       String resultString = str.replaceAll(reg, newStr);

       System.out.println(resultString);

    }

}


將疊字換成單個字母 zzz -> z
String str = "swwwffrffevvvtthjyjyrrr";
String reg = "(.)\\1+";//替換規則
String newStr = "$1";//替換為此字串 $使用前一組的規則
String resultString = str.replaceAll(reg, newStr);

4.獲取:將字串中符合規則的子字串取出
將正則表達式封裝成物件
讓正則物件和要操作的字串相關聯
關聯後,獲取正則匹配引擎
通過引擎對符合規則的子字串進行操作,例如取出
class Hello{

    public static void main(String[] args){   

       demo();

    }

    public static void demo(){

       String str = "kab jio jeioj joijio skl";

       String reg = "\\b[a-z]{3}\\b";

       //將規則封裝成物件

       Pattern p = Pattern.compile(reg);

       //讓正則物件與要作用的字串相關聯

       Matcher m = p.matcher(str);

       //System.out.println(m.matches());

       //String類中的matches方法,即使用PatternMatcher類的matcher方法

       //只不過String方法封裝後, 使用較簡單, 但功能一致

      

       //將規則作用到字串上, 並進行符合規則的子串查找

       while(m.find()){

           System.out.print(m.group() + " ");//用於獲取匹配後結果

           //kab //jio  //skl

           System.out.println(m.start() + "..." + m.end());

           //0...3  //4...7  /21...24

       }

    }

}

1.  如果只想知道該字串是對或錯,使用匹配
2.  想要將已有的字串轉變成另一個字串,替換
3.  想要按照自定的方式將字串變成多個字串,切割。獲取規則外的子串
4.  想要拿到符合需求的字串中的子串,獲取。獲取符合規則的子串
class Hello{

    public static void main(String[] args){   

       demo();

    }

    public static void demo(){

       String str = "aaa..aa...ab...bb..bbb..ccc..cc.ddd...dd.d..ee";

       str = str.replaceAll("\\.+", "");

       System.out.println(str);//aaaaaabbbbbbcccccddddddee

       str = str.replaceAll("(.)\\1+", "$1");//去疊字

       System.out.println(str);//abcde

    }

}

檢查mail是否符合規則
class Hello{

    public static void main(String[] args){   

       checkMail();

    }

    public static void checkMail(){

       String mail = "abcdefg@google.com.te";

       String reg = "[a-zA-Z0-9_]{6,12}@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";

       System.out.println(mail.matches(reg));

    }

}