Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.0 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
//#include "function.h"

//#ifndef FUNCTION_H
#define FUNCTION_H
char expr[256];
int pos,x,y,a,b,c,d;  

typedef struct treeNode {
    char data;
    struct treeNode *left;
    struct treeNode *right;
} Node;


int printInfix(Node *root){
    if (root != NULL) {
        if(root->data=='|'){
            x=printPrefix(root->left);
            y=printPrefix(root->right);
            if((x+y)==2) y=0;
            return x+y;
        }
        else if(root->data=='&'){
            x=printPrefix(root->left);
            y=printPrefix(root->right);
            return x*y;
        }
        else if(root->data=='A'){
            return a;
        }
        else if(root->data=='B'){
            return b;
        }
        else if(root->data=='C'){
            return c;
        }
        else if(root->data=='D'){
            return d;
        }
    }
}

void constructTree(Node** head)
{
    char c;

    if((*head)==NULL)
    {
        (*head) = (Node *)malloc(sizeof(Node));
		c = getchar();

		(*head)->data = c;

        (*head)->left = (*head)->right = NULL;

        if((*head)->left == NULL && ((*head)->data=='&' || (*head)->data=='|'))
            constructTree( &(*head)->left);
        if((*head)->right == NULL && ((*head)->data=='&' || (*head)->data=='|'))
            constructTree( &(*head)->right);
    }
}

void freeTree(Node *root)
{
    if (root!=NULL)
    {
        freeTree(root->left);
        freeTree(root->right);
        free(root);
    }
}

int main(void){
    int e;
    getchar();
    Node *root=NULL;
    constructTree(&root);
    for(a=0;a<=1;a++){
        for(b=0;b<=1;b++){
            for(c=0;c<=1;c++){
                for(d=0;d<=1;d++){
                    e=printPrefix(&root);
                    printf("%d %d %d %d %d\n",a,b,c,d,e);
                }
            }
        }
    }
    freeTree(root);

    return 0;
}