简体   繁体   English

c中的逻辑门仿真

[英]logic gate simulation in c

 struct gate
    {
        char name[10]; 
        int type; //Type of the gate : INPUT,AND, OR, NOT, FLIPFLOP
        int value;
        char inputname1[10];
        char inputname2[10];
        struct gate *input1;
        struct gate *input2;
    };

I've created a tree with a struct structure, but I can't find out how to calculate it in a recursive way, can you help? 我已经创建了一个具有结构结构的树,但我无法找到如何以递归方式计算它,你能帮忙吗?

模式

int pre(struct gate *root)        //address of root node is pass
{
    if(root->input1->type==INPUT || root->input2->type==INPUT){
        if(root->type == OR){
            root->type = INPUT;
            return root->value = gateor(root->input1->value,root->input2->value);
        }
        if(root->type == AND){
            root->type = INPUT;
            return root->value = gateand(root->input1->value,root->input2->value);
        }
        if(root->type==NOT){
            root->type=INPUT;
            return root->value = gatenot(root->input1->value);
        }
        if(root->type == FLIPFLOP){
            root->type = INPUT;
            return root->value = gateflipflop(root->input1->value,0);
        }
    }

    pre(root->input1);
    pre(root->input2);

}

i can't think recursive,I was hoping it would work. 我想不到递归,我希望它会起作用。

a,b,c,d is a struct but it only has value , type and name. a,b,c,d是一个结构,但它只有值,类型和名称。
values are 1 and 0 值为1和0
gate values = -1; 门值= -1;
I don't know if these values ​​are necessary. 我不知道这些价值是否必要。

Recursively you could solve it like this: 递归你可以像这样解决它:

int pre (struct gate *root) {
    if (root->type == INPUT)
        return root->value; // This is the recursion end
    else if (root->type == OR) {
        return gateor(pre(root->input1), pre(root->input2)); // Recurse here
    } else if (root->type == AND) {
        return gateand(pre(root->input1), pre(root->input2));
    } // Same for the other operations
}

This should give you an idea how to solve your problem. 这应该可以让您了解如何解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM