Untitled

 avatar
unknown
plain_text
3 years ago
1.8 kB
2
Indexable
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Node_{
    int data;
    struct Node_ *T,*F;
}Node;

char list[3005];//數字差48
int number[3005];
int pos,m;

int find(Node* root);
Node* make(char c);
Node* construct(void);

int main(void){
    int N,ans;
    Node *root=NULL;

    scanf("%s",list);
    pos=strlen(list);
    printf("pos:%d\n",pos);
    root=construct();
    printf("I find root!\n");

    scanf("%d",&N);
    for(int i=0;i<N;i++){
        printf("print me!\n");
        for(int j=1;j<=(pos/2+1);j++){
            printf(" j=%d;",j);
            scanf("%d",&number[j]);
        }

        printf("\nI eat all number!\n");
        ans=find(root);
        printf("%d\n",ans);
    }
    return 0;
}

Node* construct(void){
    Node* node=NULL;
    printf("in construct%d\n",m);

    if(m<pos){
        node=make(list[m]);
        m++;
        if(list[m]=='?'){
            m++;
            node->T=construct();
            m++;
            if(list[m]==':'){
                m++;
                node->F=construct();
            }
        }
        else{
            node->T=NULL;
            node->F=NULL;
        }
        return node;
    }
}

Node* make(char c){
    printf("in make\n");
    Node* node=(Node*)malloc(sizeof(Node));
    printf("(int)list[%d]-48=%d\n",m,(int)list[m]-48);
    node->data=(int)list[m]-48;
    node->T=NULL;
    node->F=NULL;
    return node;
}

int find(Node* root){
    printf("in the find\n");
    printf("%d\n",root->data);
    if(root->data==1){
        if(root->T!=NULL) return find(root->T);
        else return number[root->data];
    }
    else{
        if(root->F!=NULL) return find(root->F);
        else return number[root->data];
    }
}
Editor is loading...