精华内容
下载资源
问答
  • 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。 示例 1: 给定的树 s: 3 / \ 4 5 /...

    题目:

    给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。

    示例 1:
    给定的树 s:

         3
        / \
       4   5
      / \
     1   2
    

    给定的树 t:

       4 
      / \
     1   2
    

    返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。

    示例 2:
    给定的树 s:

         3
        / \
       4   5
      / \
     1   2
        /
       0
    

    给定的树 t:

       4
      / \
     1   2
    

    返回 false。
    在真实的面试中遇到过这道题?

    代码:
    在这里插入图片描述

     /**
        * 判断两树是否相等
        * @param l
        * @param r
        * @return
        */
    private boolean isEqual(TreeNode l, TreeNode r){
        if(l == null && r == null) return true;//两树均空自然相等
        if(l == null || r == null) return false;//一空一非空,自然不等
        if(l.val == r.val)//递归判断
            return isEqual(l.left,r.left) && isEqual(l.right,r.right);
        return false;
    }
    
        /**
        * 判断 t 树是否是 s 树的子树
        * @param s
        * @param t
        * @return
        */
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s == null && t == null) return true;
        if(s == null || t == null) return false;
        if(s.val == t.val){
            return isEqual(s,t) || isSubtree(s.left, t) || isSubtree(s.right, t);
        }
        // 根节点不同,那么就不同考虑s和t相等的情形了
        return isSubtree(s.left, t) || isSubtree(s.right, t);
    }
    
    
    展开全文
  • 判断是不是素数

    2018-08-18 16:42:00
    //判断一个数是不是素数#include <stdio.h>int main(){ int t; while(1) { scanf("%d",&t); int i,x,z=0; for(i=1;i<t;i++) { x=t%i; if(x==0) z++; } if(z...

    //判断一个数是不是素数
    #include <stdio.h>

    int main()
    {
        int t;
        while(1)
        {
        
        scanf("%d",&t);
        int i,x,z=0;
        for(i=1;i<t;i++)
        {
            x=t%i;
            if(x==0)
            z++;
        }
        if(z<2)
        printf("该数是素数。\n");
        else
        printf("该数不是素数。\n");
        }
     }

    转载于:https://www.cnblogs.com/xderbyy/p/9497730.html

    展开全文
  • 判断是不是LL1文法

    千次阅读 2014-03-28 17:48:07
    编最近上的编译原理课呢做实验,输入一个文法,然后判断是不是LL1文法,根据清华大学张素琴老师主编的《编译原理》第二版80页的算法,写了短程序: //compile.c #includestdio.h> #includestdlib.h> #...
    编最近上的编译原理课呢做实验,输入一个文法,然后判断是不是LL1文法,根据清华大学张素琴老师主编的《编译原理》第二版80页的算法,写了短程序:
    

    1. //compile.c

    2. #include<stdio.h>
    3. #include<stdlib.h>
    4. #include<string.h>
    5. #define length 100

    6. typedef struct {
    7.     char T[length];
    8.     char first[length][length];
    9.     int len;
    10. } ter,*Ter;


    11. typedef struct {
    12.     char N[length];
    13.     char first[length][length];
    14.     char follow[length][length];
    15.     char p_emp[length];
    16.     char n_emp[length];
    17.     int len;
    18. }*NTer,nter;

    19. //define Produce data
    20. typedef struct {
    21.     char front;
    22.     char back[length];

    23. }*Pro,prox;

    24. typedef struct {
    25.     Pro pro[length];
    26.     char string_first[length][length];
    27.     char select[length][length];
    28.     int len;

    29. }*Prod,prodx;

    30. //define G

    31. typedef struct {
    32.     Ter t_symbol;
    33.     NTer n_symbol;
    34.     Prod produce;
    35.     char start_symbol;
    36. }*G,grame;

    37. int belong_Arr(char x,char arr[])
    38. {
    39.     int i=0;
    40.     while(arr[i]!='\0') {
    41.         if(arr[i]==x)
    42.             return 1;
    43.         i++;
    44.     }
    45.     return 0;
    46. }

    47. int join(char a[],char b[],int a_len,int b_len)
    48. {
    49.     int i;

    50.     for(i=0; i<b_len; i++)
    51.         if((!belong_Arr(b[i],a))&&(b[i]!='&')) {
    52.             a[a_len]=b[i];
    53.             a_len++;
    54.         }
    55.     return a_len;

    56. }

    57. int join_arr(char a[],char b[])
    58. {
    59.     int i,len;
    60.     len=0;
    61.     while(a[len]!='\0')
    62.         len++;

    63.     i=0;
    64.     while(b[i]!='\0') {
    65.         if((!belong_Arr(b[i],a))&&(b[i]!='&')) {
    66.             a[len]=b[i];
    67.             len++;
    68.         }
    69.         i++;

    70.     }
    71.     return len;
    72. }

    73. int belong_N(char x,NTer n)
    74. {
    75.     int i=0;
    76.     for(; i<n->len; i++) {
    77.         if(x==n->N[i])
    78.             return 1;
    79.     }
    80.     return 0;
    81. }

    82. int belong_T(char x,Ter t)
    83. {
    84.     int i=0;
    85.     for(; i<t->len; i++) {
    86.         if(x==t->T[i])
    87.             return 1;
    88.     }
    89.     return 0;
    90. }


    91. typedef struct {
    92.     char syb;
    93.     int tag;
    94. }*elm,el;

    95. typedef struct {
    96.     elm elms[length];
    97.     int len;
    98. }*x_table,x_tab;





    99. void find_null(Prod pr,NTer n,Ter t)
    100. {
    101.     int i=0;
    102.     int j=0;
    103.     int k=0;
    104.     int m,a;
    105.     int b;
    106.     int q;
    107.     int loop=1;
    108.     int flag[n->len];
    109.     char temp;
    110.     //作为标记,辨别产生世是否删除
    111.     int tags[pr->len];
    112.     x_table tab=(x_table)malloc(sizeof(x_tab));
    113.     Prod p=(Prod)malloc(sizeof(prodx));


    114.     p->len=pr->len;

    115.     for(i=0; i<pr->len; i++) {
    116.         p->pro[i]=(Pro)malloc(sizeof(prox));
    117.         p->pro[i]->front=pr->pro[i]->front;
    118.         strcpy(p->pro[i]->back,pr->pro[i]->back);
    119.     }

    120.     for(i=0; i<p->len; i++) {
    121.         tags[i]=1;
    122.     }

    123.     for(i=0; i<n->len; i++) {
    124.         tab->elms[i]=(elm)malloc(sizeof(el));
    125.         tab->elms[i]->syb=n->N[i];
    126.         tab->elms[i]->tag=0;
    127.     }


    128.     tab->len=n->len;

    129.     for(i=0; i<p->len; i++) {
    130.         j=0;
    131.         while((temp=p->pro[i]->back[j++])!='#') {
    132.             if(belong_T(temp,t)) {
    133.                 tags[i]=0;
    134.                 break;
    135.             }

    136.         }
    137.     }



    138.     //确定以某一非终结符为左部的产生是的个数;判断以某一非终结符为左部的所有产生是都被删除,则将此非终结符标记否 即为 2 此为 步骤2_1



    139.     for(i=0; i<tab->len; i++) {
    140.         k=0;
    141.         m=0;
    142.         for(j=0; j<p->len; j++) {
    143.             if(tab->elms[i]->syb==p->pro[j]->front) {
    144.                 k++;
    145.                 if(tags[j]==0)
    146.                     m++;
    147.             }
    148.         }
    149.         if(k==0)
    150.             continue;
    151.         else if(k==m) {
    152.             tab->elms[i]->tag=2;

    153.         }


    154.     }


    155.     //判断 右部 是不是 yi bu xiu 是则 标记 1 , 此为步骤 2_2

    156.     for(i=0; i<p->len; i++) {
    157.         if(p->pro[i]->back[0]=='#') {
    158.             tags[i]=0;
    159.             for(j=0; j<tab->len; j++) {
    160.                 if(tab->elms[j]->syb==p->pro[i]->front) {
    161.                     tab->elms[j]->tag=1;
    162.                 }
    163.             }
    164.         }
    165.     }




    166.     k=0,j=0;
    167.     for(i=0; i<tab->len; i++) {
    168.         if(tab->elms[i]->tag==1) {
    169.             n->p_emp[k]=tab->elms[i]->syb;
    170.             k++;
    171.         } else if(tab->elms[i]->tag==2) {
    172.             n->n_emp[j]=tab->elms[i]->syb;
    173.             j++;
    174.         }


    175.     }

    176.     n->p_emp[k]='\0';
    177.     n->n_emp[j]='\0';

    178.     printf("==============================================\n");
    179.     printf("==============================================\n");
    180.     printf("U---------Unknown\n");
    181.     printf("Y---------Can produce empty set\n");
    182.     printf("N---------Can't produce empty set\n");



    183.     printf("The first time scaning\n");
    184.     for(i=0; i<tab->len; i++)
    185.         printf("%c ",tab->elms[i]->syb);
    186.     printf("\n");
    187.     for(i=0; i<tab->len; i++) {
    188.         if(tab->elms[i]->tag==0)
    189.             printf("U ");
    190.         else if(tab->elms[i]->tag==1)
    191.             printf("Y ");
    192.         else
    193.             printf("N ");
    194.     }
    195.     printf("\n");

    196.     m=0,j=0,k=0;




    197.     //步骤3_1



    198.     while(loop==1) {
    199.         loop=0;
    200.         for(j=0; j<tab->len; j++) {
    201.             flag[j]=tab->elms[j]->tag;
    202.         }


    203.         for(i=0; i<p->len; i++) {

    204.             if(tags[i]==1) {
    205.                 k=0;
    206.                 while(p->pro[i]->back[k]!='#') {
    207.                     if(belong_N(p->pro[i]->back[k],n)) {

    208.                         for(j=0; j<tab->len; j++) {
    209.                             if(tab->elms[j]->syb==p->pro[i]->back[k]) {
    210.                                 if(tab->elms[j]->tag==1) {

    211.                                     m=k;
    212.                                     while('\0'!=p->pro[i]->back[m]) {
    213.                                         p->pro[i]->back[m]=p->pro[i]->back[m+1];

    214.                                         //printf("%c\n",p->pro[i]->back[m]);
    215.                                         m++;
    216.                                     }
    217.                                     k=k-1;
    218.                                     if('#'==p->pro[i]->back[0]) {

    219.                                         for(b=0; b<tab->len; b++) {
    220.                                             if(tab->elms[b]->syb==p->pro[i]->front) {
    221.                                                 tab->elms[b]->tag=1;
    222.                                                 q=b;
    223.                                             }

    224.                                         }


    225.                                         for(b=0; b<p->len; b++) {
    226.                                             if(p->pro[b]->front==tab->elms[q]->syb) {
    227.                                                 tags[b]=0;
    228.                                             }
    229.                                         }
    230.                                     }
    231.                                 }

    232.                                 else if(tab->elms[j]->tag==2) {
    233.                                     tags[i]=0;

    234.                                     b=0;
    235.                                     for(a=0; a<p->len; a++) {
    236.                                         if((p->pro[a]->front==p->pro[i]->front)&&(tags[a]==1)) {
    237.                                             tags[a]=0;
    238.                                         }
    239.                                     }
    240.                                     for(a=0; a<p->len; a++) {
    241.                                         if((p->pro[a]->front==p->pro[i]->front)&&(tags[a]==1)) {
    242.                                             b++;
    243.                                         }

    244.                                     }

    245.                                     if(b==0) {
    246.                                         for(a=0; a<tab->len; a++) {
    247.                                             if((tab->elms[a]->syb==p->pro[i]->front)&&(tab->elms[a]->tag==0)) {
    248.                                                 tab->elms[a]->tag=2;
    249.                                             }
    250.                                         }


    251.                                     }


    252.                                 }
    253.                             }


    254.                         }


    255.                     }
    256.                     k++;
    257.                 }

    258.             }
    259.         }
    260.         for(i=0; i<tab->len; i++) {
    261.             if(tab->elms[i]->tag!=flag[i]) {
    262.                 loop=1;
    263.                 break;
    264.             }
    265.         }

    266.     }

    267.     printf("The second time scaning\n");
    268.     for(i=0; i<tab->len; i++)
    269.         printf("%c ",tab->elms[i]->syb);
    270.     printf("\n");
    271.     for(i=0; i<tab->len; i++) {
    272.         if(tab->elms[i]->tag==0)
    273.             printf("U ");
    274.         else if(tab->elms[i]->tag==1)
    275.             printf("Y ");
    276.         else
    277.             printf("N ");
    278.     }


    279.     k=0,j=0,m;
    280.     for(i=0; i<tab->len; i++) {
    281.         if(tab->elms[i]->tag==1) {
    282.             //n_pro_e[k]=tab->elms[i]->syb;
    283.             n->p_emp[k]=tab->elms[i]->syb;
    284.             k++;
    285.         } else if(tab->elms[i]->tag==2) {
    286.             //npro_e[j]=tab->elms[i]->syb;
    287.             n->n_emp[j]=tab->elms[i]->syb;
    288.             j++;
    289.         }


    290.     }
    291.     n->p_emp[k]='\0';
    292.     n->n_emp[j]='\0';




    293.     return ;
    294. }
    295. void get_first_set(Prod p,NTer n,Ter t)
    296. {

    297.     int i,j,k,temp;
    298.     int flag,a,b;
    299.     int m;
    300.     int tags[n->len];
    301.     //int lock[n->len];
    302.     int loop=1;
    303.     int judge[n->len];

    304.     printf("\n==============================================\n");
    305.     printf("\nBeging finding first set\n");




    306.     //X is included in Vt,F(X)={X};
    307.     for(i=0; i<t->len; i++) {
    308.         t->first[i][0]=t->T[i];
    309.     }

    310.     printf("The terminal symnol's First Set\n");
    311.     for(i=0; i<t->len; i++)
    312.         printf("First(%c)={%c}\n",t->T[i],t->first[i][0]);


    313.     printf("\n");

    314.     for(i=0; i<n->len; i++)
    315.         tags[i]=0;
    316.     

    317.     for(i=0; i<p->len; i++) {
    318.         //X->a..... a is included in first[X]
    319.         if(belong_T(p->pro[i]->back[0],t)) {
    320.             for(j=0; j<n->len; j++) {
    321.                 if(p->pro[i]->front==n->N[j]) {
    322.                     temp=tags[j]++;
    323.                     n->first[j][temp]=p->pro[i]->back[0];

    324.                     //lock[j]=0;
    325.                     break;
    326.                 }
    327.             }
    328.         }
    329.         //X-># & is included in First[X]
    330.         else if(p->pro[i]->back[0]=='#') {
    331.             for(j=0; j<n->len; j++) {
    332.                 if(p->pro[i]->front==n->N[j]) {
    333.                     temp=tags[j]++;
    334.                     n->first[j][temp]='&';

    335.                     //    lock[j]=0;
    336.                     break;
    337.                 }
    338.             }

    339.         }
    340.     }

    341.     while(loop==1) {
    342.         loop=0;
    343.         for(i=0; i<n->len; i++)
    344.             judge[i]=tags[i];
    345.         for(i=0; i<p->len; i++) {

    346.             //flag=0;
    347.             if(p->pro[i]->back[0]=='#')
    348.                 continue;
    349.             if(belong_T(p->pro[i]->back[0],t))
    350.                 continue;
    351.             for(a=0; a<n->len; a++)
    352.                 if(n->N[a]==p->pro[i]->front)
    353.                     break;
    354.             j=0;
    355.             while('#'!=p->pro[i]->back[j]) {
    356.                 if(belong_Arr(p->pro[i]->back[j],n->p_emp)) {
    357.                     for(b=0; b<n->len; b++)
    358.                         if(n->N[b]==p->pro[i]->back[j])
    359.                             break;
    360.                     tags[a]=join(n->first[a],n->first[b],tags[a],tags[b]);
    361.                     j++;
    362.                 } else if(belong_Arr(p->pro[i]->back[j],n->n_emp)) {
    363.                     for(b=0; b<n->len; b++)
    364.                         if(n->N[b]==p->pro[i]->back[j])
    365.                             break;
    366.                     tags[a]=join(n->first[a],n->first[b],tags[a],tags[b]);

    367.                     break;
    368.                 } else if(belong_T(p->pro[i]->back[j],t)) {
    369.                     if(!belong_Arr(p->pro[i]->back[j],n->first[a])) {
    370.                         n->first[a][tags[a]++]=p->pro[i]->back[j];
    371.                     }
    372.                     break;

    373.                 }

    374.             }
    375.             if(p->pro[i]->back[j]=='#')
    376.                 if(!belong_Arr('&',n->first[a]))
    377.                     n->first[a][tags[a]++]='&';
    378.         }

    379.         for(i=0; i<n->len; i++)
    380.             if(judge[i]!=tags[i]) {
    381.                 loop=1;
    382.                 break;
    383.             }

    384.     }

    385.     printf("The not_terminal symbol's First Set\n");
    386.     for(m=0; m<n->len; m++) {
    387.         printf("First(%c)={",n->N[m]);
    388.         //printf("%d ",tags[i]);
    389.         for(j=0; j<tags[m]; j++)
    390.             printf("%c ",n->first[m][j]);

    391.         printf("}\n");
    392.     }

    393.     printf("\n");
    394.     printf("\n");
    395.     for(i=0; i<p->len; i++) {
    396.         if(p->pro[i]->back[0]=='#') {
    397.             p->string_first[i][0]='&';
    398.         }

    399.         else if(!belong_Arr(p->pro[i]->back[0],n->p_emp))
    400.             if(belong_T(p->pro[i]->back[0],t)) {
    401.                 for(j=0; j<t->len; j++)
    402.                     if(p->pro[i]->back[0]==t->T[j])
    403.                         break;
    404.                 join_arr(p->string_first[i],t->first[j]);
    405.             } else {
    406.                 for(j=0; j<n->len; j++)
    407.                     if(p->pro[i]->back[0]==n->N[j])
    408.                         break;
    409.                 join_arr(p->string_first[i],n->first[j]);
    410.             }
    411.         else {
    412.             j=0;
    413.             while(p->pro[i]->back[j]!='#') {
    414.                 if(belong_Arr(p->pro[i]->back[j],n->p_emp)) {
    415.                     for(k=0; k<n->len; k++)
    416.                         if(n->N[k]==p->pro[i]->back[j])
    417.                             break;
    418.                     temp=join_arr(p->string_first[i],n->first[k]);
    419.                 } else {
    420.                     for(k=0; k<n->len; k++)
    421.                         if(n->N[k]==p->pro[i]->back[j])
    422.                             break;
    423.                     temp=join_arr(p->string_first[i],n->first[k]);
    424.                     break;
    425.                 }
    426.                 j++;
    427.             }
    428.             if(p->pro[i]->back[j]=='#')
    429.                 p->string_first[i][temp]='&';
    430.         }
    431.     }
    432.     printf("-----------------------------------------\n");
    433.     printf("The First Set of the right of produce\n");

    434.     for(i=0; i<p->len; i++) {

    435.         printf("First(");
    436.         j=0;
    437.         if(p->pro[i]->back[0]=='#')
    438.             printf("&");
    439.         else
    440.             while(p->pro[i]->back[j]!='#') {
    441.                 printf("%c",p->pro[i]->back[j]);
    442.                 j++;
    443.             }
    444.         printf(")={");
    445.         j=0;
    446.         while(p->string_first[i][j]!='\0') {
    447.             printf("%c ",p->string_first[i][j]);
    448.             j++;
    449.         }
    450.         printf("}\n");
    451.     }
    452.     printf("\n\n");

    453.     return ;


    454. }
    455. void get_follow_set(Prod p,NTer n,Ter t,char s_syb)
    456. {
    457.     int i,j,k,len,m,x,loop;
    458.     int tags[n->len];
    459.     int flags[n->len];
    460.     for(i=0; i<n->len; i++) {
    461.         tags[i]=0;
    462.         flags[i]=0;
    463.     }
    464.     printf("=====================================================\n");

    465.     printf("start finding follow set\n");
    466.     for(i=0; i<n->len; i++)
    467.         if(s_syb==n->N[i]) {
    468.             n->follow[i][0]='#';
    469.             tags[i]++;
    470.             break;
    471.         }
    472.     loop=1;
    473.     while(loop==1) {
    474.         loop=0;
    475.         for(i=0; i<n->len; i++)
    476.             flags[i]=tags[i];
    477.         for(i=0; i<p->len; i++) {
    478.             j=0;
    479.             while(p->pro[i]->back[j]!='#') {
    480.                 if(belong_N(p->pro[i]->back[j],n)) {
    481.                     for(k=0; k<n->len; k++)
    482.                         if(n->N[k]==p->pro[i]->back[j])
    483.                             break;
    484.                     if(p->pro[i]->back[j+1]!='#') {
    485.                         if(belong_N(p->pro[i]->back[j+1],n)) {
    486.                             for(m=0; m<n->len; m++)
    487.                                 if(n->N[m]==p->pro[i]->back[j+1])
    488.                                     break;
    489.                             tags[k]=join_arr(n->follow[k],n->first[m]);
    490.                             if(belong_Arr(n->N[m],n->p_emp)) {
    491.                                 for(x=0; x<n->len; x++)
    492.                                     if(n->N[x]==p->pro[i]->front)
    493.                                         break;
    494.                                 tags[k]=join_arr(n->follow[k],n->follow[x]);

    495.                             }
    496.                         } else if(belong_T(p->pro[i]->back[j+1],t)) {
    497.                             if(!belong_Arr(p->pro[i]->back[j+1],n->follow[k])) {
    498.                                 len=strlen(n->follow[k]);
    499.                                 n->follow[k][len]=p->pro[i]->back[j+1];
    500.                                 tags[k]++;
    501.                             }
    502.                         }

    503.                     }


    504.                     else {
    505.                         for(x=0; x<n->len; x++)
    506.                             if(n->N[x]==p->pro[i]->front)
    507.                                 break;
    508.                         tags[k]=join_arr(n->follow[k],n->follow[x]);
    509.                     }
    510.                 }
    511.                 j++;
    512.             }
    513.         }

    514.         for(i=0; i<n->len; i++)
    515.             if(flags[i]!=tags[i]) {
    516.                 loop=1;
    517.                 break;
    518.             }
    519.     }


    520.     printf("The not terminal symbols follow sets are:\n");

    521.     for(i=0; i<n->len; i++) {
    522.         j=0;
    523.         printf("Follow(%c)={",n->N[i]);
    524.         while(n->follow[i][j]!='\0') {
    525.             printf("%c ",n->follow[i][j]);
    526.             j++;
    527.         }
    528.         printf("}\n");
    529.     }
    530. }
    531. void get_select_set(Prod p, NTer n, Ter t) {
    532.     int i, j, k;
    533.     int flag = 0;
    534.     for (i = 0; i < p->len; i++) {

    535.         if (p->pro[i]->back[0] == '#') {
    536.             for (k = 0; k < n->len; k++)
    537.                 if (n->N[k] == p->pro[i]->front)
    538.                     break;

    539.             join_arr(p->select[i], n->follow[k]);
    540.             continue;
    541.         }
    542.         j = 0;
    543.         flag = 0;
    544.         while (p->pro[i]->back[j] != '#') {
    545.             if (!belong_Arr(p->pro[i]->back[j], n->p_emp)) {
    546.                 //flag=1;
    547.                 break;
    548.             }
    549.             j++;
    550.         }
    551.         if (p->pro[i]->back[j] == '#') {
    552.             flag = 1;
    553.         }
    554.         if (flag == 0) {
    555.             join_arr(p->select[i], p->string_first[i]);
    556.         } else if (flag == 1) {
    557.             join_arr(p->select[i], p->string_first[i]);
    558.             for (k = 0; k < n->len; k++)
    559.                 if (n->N[k] == p->pro[i]->front)
    560.                     break;
    561.             join_arr(p->select[i], n->follow[k]);
    562.         }

    563.     }

    564.     printf("=============================================\n");
    565.     printf("\nThe select set:\n");

    566.     for (i = 0; i < p->len; i++) {
    567.         printf("select(%c->", p->pro[i]->front);
    568.         j = 0;
    569.         while (p->pro[i]->back[j] != '#') {
    570.             printf("%c", p->pro[i]->back[j]);
    571.             j++;
    572.         }
    573.         printf(")={");
    574.         j = 0;
    575.         while (p->select[i][j] != '\0') {
    576.             printf("%c ", p->select[i][j]);
    577.             j++;
    578.         }
    579.         printf("}\n");

    580.     }
    581.     printf("=============================================\n");
    582. }
    583. int isLL1(Prod p)
    584. {
    585.     int i=0;
    586.     int j;
    587.     for(i=0;i<p->len-1;i++)
    588.         for(j=i+1;j<p->len;j++){
    589.             if(p->pro[i]->front==p->pro[j]->front)
    590.             if(strcmp(p->select[i],p->select[j])==0){
    591.                 printf("\nThis is not LL1\n");
    592.                 return 0;
    593.             }
    594.         }


    595.     printf("\nThis is LL1\n");
    596.     printf("\n===========================================\n");
    597.     printf("\n===========================================\n");
    598.     return 1;
    599. }

    600. int main()
    601. {
    602.     int i=0,j=0;
    603.     char temp;
    604.     char tem[100];

    605. //char pro_e[length],npro_e[length];//the symbol can can't produce empty

    606.     G g=(G)malloc(sizeof(grame));
    607.     g->t_symbol=(Ter)malloc(sizeof(ter));
    608.     g->n_symbol=(NTer)malloc(sizeof(nter));
    609.     g->produce=(Prod)malloc(sizeof(prodx));



    610. //get not-terminal symbol
    611.     printf("Enter not-terminal\n");
    612.     while(1) {
    613.         scanf("%c",&temp);
    614.         if(temp=='$') {
    615.             g->n_symbol->len=i;
    616.             i=0;
    617.             break;
    618.         }
    619.         g->n_symbol->N[i++]=temp;

    620.     }




    621. //get terminal symbol
    622.     getchar();
    623.     printf("Enter terminal\n");
    624.     while(1) {
    625.         scanf("%c",&temp);
    626.         if(temp=='$') {
    627.             g->t_symbol->len=i;
    628.             i=0;
    629.             break;
    630.         }
    631.         g->t_symbol->T[i++]=temp;

    632.     }

    633. //get start symbol-------->S

    634.     printf("Enter the start symbol(end pressing Enter)\n");
    635.     getchar();
    636.     scanf("%c",&temp);
    637.     g->start_symbol=temp;

    638. //get produce
    639.     j=0;
    640.     printf("Enter Produce\n");

    641.     while(1) {
    642.         scanf("%s",tem);

    643.         if(tem[0]=='$') {
    644.             g->produce->len=j;
    645.             j=0;
    646.             break;
    647.         }

    648.         g->produce->pro[j]=(Pro)malloc(sizeof(prox));
    649.         g->produce->pro[j]->front=tem[0];
    650.         for(i=3; i<sizeof(tem); i++) {
    651.             g->produce->pro[j]->back[i-3]=tem[i];
    652.         }
    653.         j++;
    654.     }

    655. //printf("%s\n",g->produce->pro[0]->back);

    656.     find_null(g->produce,g->n_symbol,g->t_symbol);
    657.     get_first_set(g->produce,g->n_symbol,g->t_symbol);
    658.     get_follow_set(g->produce,g->n_symbol,g->t_symbol,g->start_symbol);
    659.     get_select_set(g->produce,g->n_symbol,g->t_symbol);
    660.     isLL1(g->produce);
    661.     return 0;

    662. }
    这段代码呢,里面写了点注释,如果各位高手们看到我写的代码的不足之处,请之处,谢谢……

    对了,这里附上两个测试用例:
    1:EeTtF$ +*()i$ E E->Te# e->+Te# e-># T->Ft# t->*Ft# t-># F->i# F->(E)# $
    2 : SACB$ ab$ S S->Ab# S->Ba# A->aC# C->A# C-># B->a# $

    如果在Linux环境下的话,我们可以用管道命令来简化输入,把上面的两个例子分别放在两个文件两面,如果命名为test,则在终端输入:cat test|./compile
     
    声明:此程序,我是在gcc编译环境下编译的,在windows中用VC的话运行好像是有错的,可以试试用Dev
    <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
    阅读(1477) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
展开全文
  • 判断是不是PE文件

    千次阅读 2010-06-10 16:38:00
    判断是不是一个PE文件有很多种方法,我们用的方法是:先读取Dos头,判断e_magic是否等于"MZ",然后再读取PE文件头的头字节,判断是不是 "PE00"。这样就能确定是不是一个有效的PE文件。代码如下://选择文件void ...

    判断是不是一个PE文件有很多种方法,我们用的方法是:

    先读取Dos头,判断e_magic是否等于"MZ",然后再读取PE文件头的头字节,判断是不是 "PE00"。这样就能确定是不是一个有效的PE文件。代码如下:

     

    //选择文件
    void CPEDlg::OnButton1()
    {
    // TODO: Add your control notification handler code here
    TCHAR tzFilter[] = _T("可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||");
    CFileDialog dlgFile(TRUE,NULL,NULL, OFN_HIDEREADONLY,tzFilter,this);
    if (IDOK == dlgFile.DoModal())
    {
      m_strFilePath = dlgFile.GetPathName();
      UpdateData(FALSE);
    }
    }

    //判断是否是PE文件
    void CPEDlg::OnButton2()
    {
    // TODO: Add your control notification handler code here

    HANDLE    hFile = INVALID_HANDLE_VALUE; //打开的PE文件句柄
    IMAGE_DOS_HEADER DosHeader = {0};
    DWORD    dwReadLen = 0;
    DWORD    dwNTSignature = 0;

    //创建文件
    hFile = CreateFile(m_strFilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
      MessageBox("打开文件失败");
      return;
    }

    //读取文件
    if (!ReadFile(hFile,&DosHeader,sizeof(DosHeader),&dwReadLen,NULL))
    {
      CloseHandle(hFile);
      MessageBox("读取文件失败");
      return;
    }
    //判断DOS头部是否为MZ
    if (DosHeader.e_magic != IMAGE_DOS_SIGNATURE)
    {
      CloseHandle(hFile);
      MessageBox("不是PE文件");
      return;
    }

    SetFilePointer(hFile,DosHeader.e_lfanew,NULL,FILE_BEGIN);
    //读取PE的NT头字节
    ReadFile(hFile,&dwNTSignature,sizeof(dwNTSignature),&dwReadLen,NULL);
    //判断PE标志
    if (dwNTSignature != IMAGE_NT_SIGNATURE)
    {
      MessageBox("不是PE文件");
      CloseHandle(hFile);
      return;
    }

    CloseHandle(hFile);
    hFile = INVALID_HANDLE_VALUE;

    MessageBox("是PE文件");
    }
    展开全文
  • c判断是不是子串

    2016-11-07 17:26:00
    1 #include <stdio.h> 2 #include <string.h>//添加字符串头文件 ... 4 int Subsequence(char s[], char t[]) 5 { 6 int m,n,i,j; 7 n = strlen(s); //n表示序列S的长度 8 m = strl...
  • bool isSubtree(struct TreeNode* s, struct TreeNode* t)//判断t二叉树是不是s的子树 { if (t == NULL) { return true; } if (s) { if ((s->val == t->val) && (isSameTree(s, t) == true))//如果以该s...
  • i++) { switch (arrC[i]) { case 'a': case 'e': case 'i': case 'o': case 'n': case 'r': case 't': case 'l': case 's': case 'u': score = score + 1; break; case 'd': case 'G': score = score + 2; break; ...
  • java怎么判断字符串是不是中文发布时间:2020-06-10 10:15:36来源:亿速云阅读:276作者:Leahjava怎么判断字符串是不是中文?针对这个问题,今天小编总结这篇有关字符串判断的文章,希望能帮助更多想解决这个问题的...
  • 用JAVA自带的函数public static boolean isNumericZidai(String str) {for (int i = 0; i < str.length(); i++) {System.out.println(str.charAt(i));if (!Character.isDigit(str.charAt(i))) {...}}return t...
  • 输入一棵二叉树,判断该二叉树是否是平衡二叉树。平衡二叉树 每个结点的左右子树的深度相差不超过1 解法一:采用后序遍历的方式遍历整棵二叉树 只用遍历一次结点 C++: 1 class Solution { 2 private: bool...
  • 判断分享是不是微信

    2016-06-30 15:59:00
    // 检查是否是微信分享 function isWeixin(){ var ua = navigator.userAgent.toLowerCase(); //获取用户端信息 if(ua.indexOf('micromessenger')>0){ //检测特殊字符串"micromessenger"的位置 return t...
  • 1、用ASCII码判断  在 ASCII码表中,英文的范围是0-127,而汉字则是大于127,根据这个范围可以判断,具体代码如下: string text = "我去"; bool res = false;... foreach (char t in text) { ...
  • 1.判断一个构造函数是否为数组类型 let obj = [] obj.constructor == Array; //true console.log(obj instanceof Array) 2.instanceof obj instanceof Array //缺点:继承自数组 或者原型上有数组的原型也会认为是...
  • 思路1: 1 把整数反转,判断俩个数是否相等 代码; bool isPalindrome(int x) { if(x) return false; int reverse=0; int t=x; while(t){ reverse=reverse*10+t%10;
  • 判断一棵树是不是BST

    2021-03-17 20:10:50
    Status IsBSTree(BiTree t) /* 判别给定二叉树t是否为二叉排序树。*/ /* 若是,则返回TRUE,否则FALSE */ { BiTree p; if(!t || !t->lchild&&!t->rchild) return TRUE; // if(t->lchild) { p...
  • 以前写过一个方法,先去参考《T-SQL判断是否为今天》https://www.cnblogs.com/insus/archive/2012/03/22/2411209.html 今天使用一个更加简单的方法来实现: SET ANSI_NULLS ON GO SET QUOTED_...
  • JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,... 利用typeof除了array和null判断为object外,其他的都可以正常判断 alert(typeof null); // "object" alert(t...
  • 能大家知道在使用Java Socket编程的时候经常要检测网络是不是完好连接的。比如Socket服务器检测和客户端的连接是否已经断开。首先想到socket类的方法isClosed()、isConnected()、isInputStreamShutdown()、...
  • C语言中判断一个char*是不是utf8编码 里我修改了一下, 纯ASCII编码的字符串也返回true, 因为UTF8和ASCII兼容 实例代码: int utf8_check(const char* str, size_t length) { size_t i; int nBytes; unsigned ...
  • public class Text1 ... t = new HashSet&lt;Text2&gt;(); t.add(new Text2("诸葛亮",27)); t.add(new Text2("庞统",40)); t.add(new Text2("诸葛亮",27));...
  • 实现一个函数,判断一个数是不是素数(用C语言) 思路: 定义一个int 型数,使它依次递增整除所判断的数,如果余数等于0则不是素数,否则是素数。由for()语句配合if()语句不难写出。 以下代码总共定义三个函数,...
  • 判断一个对象是不是一个数组的方法: 1.验证原型对象 如果一个对象的原型对象是Array.prototype Object.getPrototypeOf(obj)==Array.prototype var bool=father.isPrototypeOf(child) var obj=[]; console....
  • System.out.println("\t\t\t\t欢迎使用富贵不能淫购物系统1.0版!");System.out.println("\t\t\t\t\t1.登录系统");System.out.println("\t\t\t\t\t2.退出");System.out.println("****...System.out.println("\t\t\t\...
  • 给你一个正整数,请你判断是不是3的倍数? 输入格式: 输入数据有一个T(T<=50),代表有T组数据 对于每组数据,一个整数n (1<=n<=10 ​1000 ​​ ) 注意数据范围 输出格式: 对于每组数据,输出一行“YES”...
  • csv.reader 是一个迭代器。 直接上代码:   import csv def readCsv(): with open( 't.csv', 'r' ) as fr : rd = csv.reader( fr ) print( 'type:' , type( rd ) ) for next1 in rd : ...
  • FreeBSD [root@mx1 ~/crontab]# date +%Y-%m-%d #当前时间 2011-08-02 [root@mx1 ~/crontab]# a=`date -v -1d +%d` #赋值变量 [root@mx1 ~/crontab]# echo $a #查看变量的值 ...[root@mx1 ~/crontab]# if [ $...t...
  • 展开全部下面是我用JavaScript写的素数32313133353236313431303231363533e78988e69d8331333365656630函数,供参考,大同小异PrimeA=function(n,nth){/*小于n的素数表参数nth指定返回第n个素数*///vart0=Time.now5();...
  • ORALE sql如何判断字段的值是不是空值 如: select (t.num_1 + t.num_2) from table t 字段num_1有可能为null ,如果该条记录的num_1为空值,则用0和num_2相加。这个sql语句怎么写。 最好oracle和sql server...
  • 输入两棵二叉树A,B,判断B是不是A的子结构。 class BinaryTree<T extends Comparable<T>>{ static class BinaryTreeNode<T>{ private T value; private BinaryTreeNode<T> leftchild;...
  • 空空如也

    空空如也

    1 2 3 4 5 ... 20
    收藏数 1,148
    精华内容 459
    热门标签
    关键字:

    判断是不是t