user2672904
user2672904

Reputation: 61

Linked struct in C

I have a linked struct to solve logic gates problem... but, I have doubts about the functioning of the struct linked, and how can I use their values

So, that is my code:

typedef enum { NEG, AND, OR, NAND, NOR, XOR, XNOR }Gates;

struct AstLogic
{
    bool input1;
    bool input2;
    bool result;
    Gates gate;
    struct AstLogic *nextGate;
};

and, the test functions is:

struct AstLogic ast1, ast2, ast3;

//
//     1-|ast1|
//     1-|____|+-------+
//                     |
//                     +|ast3|
//                     +|____|+----+ OUTPUT
//                     |
//     1-|ast2|+-------+
//     0-|____|
//

void createInputsTest()
{
    ast1.gate = NOR;
    ast1.input1 = true;
    ast1.input2 = true;
    ast3.result = ~(ast1.input1|ast1.input2);

    ast2.gate = NOR;
    ast2.input1 = true;
    ast2.input2 = false;
    ast2.result = ~(ast2.input1|ast2.input2);
    ast1.nextGate = &ast2; // make the link

    ast3.gate = NOR;
    ast3.input1 = ast1.result;
    ast3.input2 = ast2.result;
    ast3.result = ~(ast3.input1|ast3.input2);

    if(ast3.result == true)
        printf("true");
    else
        printf("false");
}

I just whant to know, if have a good whay to auto-get the "output" result, using that AST logic... For example, I enter with one file with the logic, and my program make the parse in that file and generate that ast, how Is the best whay to generate that ast ?

questions are beasts, but do not know how to solve, so I came here in StackOverFlow to take my doubts, because I do not know how to solve these my doubts ...

Upvotes: 3

Views: 233

Answers (2)

JackCColeman
JackCColeman

Reputation: 3807

Clever. In this one example, there is no need for the next field. The linkage between gates is thru the input fields where the input of a later gate (in time) refers to the output of a gate earlier in time.

To generate an Abstract Syntax Tree (AST), first you will have to define how to store the gates for your circuit on a file, including naming the gates and having text that links the output from gates to the input of another. Then read that file, build an AST and then traverse it setting the values as you pass thru the gates in simulated time. Overall a large but easy to medium project.

However, you can do what you have done which is list the gates in time order and let the program implicitly traverse the gates.

Upvotes: 1

mortdeus
mortdeus

Reputation: 197

Your createInputsTest() was wrong. Here is the correct version.

struct AstLogic ast1, ast2, ast3;

//
//     1-|ast1|
//     1-|____|+-------+
//                     |
//                     +|ast3|
//                     +|____|+----+ OUTPUT
//                     |
//     1-|ast2|+-------+
//     0-|____|
//

void createInputsTest()
{
    ast1.gate = NOR;
    ast1.input1 = true;
    ast1.input2 = true;
    ast1.nextGate = &ast3

    ast2.gate = NOR;
    ast2.input1 = true;
    ast2.input2 = false;
    ast2.nextGate = &ast3;

    ast3.gate = NOR;
    ast1.nextGate->input1 = ~(ast1.input1|ast1.input2);
    ast2.nextGate->input2 = ~(ast2.input1|ast2.input2);
    ast3.result = ~(ast3.input1|ast3.input2);

    if(ast3.result == true)
        printf("true");
    else
        printf("false");
}

Upvotes: 0

Related Questions