安卓觀察者ContentObserver模式用正則擷取簡訊驗證碼

來源:互聯網
上載者:User

標籤:c   ext   int   a   檔案   string   



/*-------------------------包含標頭檔------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
int count=0;
/*-------------------------結構體定義部分------------------------------*/
typedef struct Node
{
    char name[10];
    int score;
    struct Node *next;
} ListNode;


/*----------------------------函式宣告部分------------------------------*/




/*---------------------------函數實現部分-------------------------------*/
/*-----------------------------建立鏈表---------------------------------*/
/*在鏈表的末端插入新的節點,建立鏈表*/
ListNode *CreateList()
{
    ListNode *head;//指向頭結點指標
    ListNode *p,*pre;
    head=(ListNode *)malloc(sizeof(ListNode));//為前端節點分配記憶體空間
    head->next=NULL;//將頭結點的指標域清空
    pre=head;//先將頭結點首地址賦給中間變數pre
    while(1)
    {
        if((p=(ListNode *)malloc(sizeof(ListNode)))==NULL)
        {
            printf("記憶體空間不足!!!\n");
            break;
        }
        count++;
        printf("input name of the %d student(input \"q\" to quit):",count);//列印出第幾個人的名字
        //記憶體空間p指向新插入結點的首地址
        scanf("%s",&p->name);//輸入姓名
        if(strcmp(p->name,"q")==0)
            break;
        printf("input score of the %d student:",count);
        scanf("%d",&p->score);//輸入分數
        pre->next=p;//將p指向新結點插入鏈表也就是頭結點指標域指向
        //下個結點
        //第一個結點就是p指向的,因為頭結點內容為空白
        pre=p;//這個起著指向下一個結點的作用
        pre->next=NULL;
    }
    return head;//返回這個鏈表的首地址
}
/*-------------------------輸出鏈表-----------------------------------*/
void PrintList(ListNode *h)
{
    ListNode *p;
    p=h->next;
    while(p)
    {
        printf("%s,%d",p->name,p->score);
        p=p->next;
        printf("\n");
    }
}
/*----------------------插入鏈表結點--------------------------*/
/*--------------------------------------------------------------------
函數名稱:InsertList(ListNode *h,int i,char name[],int e,int n)
函數功能:插入鏈表結點
入口參數: h: 頭結點地址 i:插入到第幾個結點 name:插入結點的姓名 e:插入結點的分數 n:鏈表中結點的個數除下頭結點外的個數
出口參數:
--------------------------------------------------------------------*/
void InsertList(ListNode *h,int i,char name[],int e,int n)
{
    ListNode *p,*q;  //先定義2個指向一個結點的指標
    if(i<1||i>n+1)
        printf("Error!!!\n");
    else
    {
        int j=1;
        p=h;//將指標p指向要鏈表的頭結點
        while(j<i)
        {
            p=p->next;
            j++;
        }
        if((q=(ListNode *)malloc(sizeof(ListNode)))==NULL)/*為要插入的
   結點分配記憶體空間*/
            printf("記憶體空間不足!!!\n");
        else
        {
            count++;
            strcpy(q->name,name);//將名字拷到要插入的節點內
            q->score=e;//將要插入的節點中分數賦值
            q->next=p->next;/*這個是將新插入的結點指標域指向
   上一個結點指標域指向的結點地址即為p->next*/
            p->next=q;/*將要插入結點位置前面的結點指標域
   指向現在插入的結點首地址*/
        }
    }
}


    /*--------------------------------------------------------------------
    函數名稱:DeleteList(ListNode *h, int i, int n)
    函數功能:刪除鏈表結點
    入口參數: h: 頭結點地址 i:要刪除的結點所在位置  n:鏈表中結點的個數除下頭結點外的個數
    出口參數:
    --------------------------------------------------------------------*/
    void DeleteList(ListNode *h, int i, int n)
    {
        ListNode *p,*q;//首先定義2個指向結點型結構體的指標
        char name[10];
        int score;
        if(i<1||i>count+1)
            printf("Error!!!\n");
        else
        {
            int j=1;
            p=h;//將指標指向鏈表的頭結點首地址
            while(j<i)
            {
                p=p->next;
                j++;
            }
            q=p->next;/*q指向要刪除的位置之前的那個結點指標域指向的
   地址q指向的結點就是要刪除的結點*/
            p->next=q->next;/*這個就是將要刪除的結點的前面那個結點
   的指標域指向要刪除的結點指標域中存放的下個結點的
   首地址從而實現了刪除第i個結點的作用*/
            strcpy(name,q->name);
            score=q->score;
            free(q);//釋放q指向的結點
            printf("name:%s\tscore:%d has been deleted!!!\n",name,score);
        }
    }


    /*--------------------------主函數-------------------------------*/
    int main()
    {
        ListNode *h;//h指向結構體NODE
        int i = 1, n, score;
        char name [10];


        while ( i )
        {
            /*輸入提示資訊*/
            printf("1--建立新的鏈表\n");
            printf("2--添加元素\n");
            printf("3--刪除元素\n");
            printf("4--輸出當前表中的元素\n");
            printf("0--退出\n");


            scanf("%d",&i);
            switch(i)
            {
            case 1:
                h=CreateList();/*建立鏈表*/
                printf("list elements is : \n");
                PrintList(h);
                break;


            case 2:
                printf("input the position. of insert element:");
                scanf("%d",&i);
                printf("input name of the student:");
                scanf("%s",name);
                printf("input score of the student:");
                scanf("%d",&score);
                InsertList(h,i,name,score,count);
                printf("list elements is:\n");
                PrintList(h);
                break;


            case 3:
                printf("input the position of delete element:");
                scanf("%d",&i);
                DeleteList(h,i,count);
                printf("list elements in : \n");
                PrintList(h);
                break;


            case 4:
                printf("list element is : \n");
                PrintList(h);
                break;
            case 0:
                return;
                break;
            default:
                printf("ERROR!Try again!\n");
            }
        }
        return 0;
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.