gpt4 book ai didi

c - C 中的链接结构

转载 作者:行者123 更新时间:2023-12-02 03:44:09 25 4
gpt4 key购买 nike

我有一个链接结构来解决逻辑门问题...但是,我对链接结构的功能以及如何使用它们的值有疑问

所以,这是我的代码:

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

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

并且,测试函数是:

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");
}

我只是想知道,如果有很好的方法自动获取“输出”结果,使用 AST 逻辑......例如,我输入一个包含逻辑的文件,我的程序在该文件中进行解析并生成该 ast,生成该 ast 的最佳方式是什么?

问题是野兽,但不知道如何解决,所以我来到 StackOverFlow 来解决我的疑惑,因为我不知道如何解决我的这些疑惑......

最佳答案

聪明。在此示例中,不需要 next 字段。门之间的联系是通过输入字段进行的,其中较晚的门(及时)的输入指的是较早的门的输出。

要生成抽象语法树 (AST),首先您必须定义如何在文件中存储电路的门,包括命名门和将门的输出链接到另一个门的输入的文本。然后读取该文件,构建一个 AST,然后遍历它并在模拟时间内通过大门时设置值。总体而言,这是一个大型但易于中型的项目。

但是,您可以执行您已经完成的操作,即按时间顺序列出门并让程序隐式遍历门。

关于c - C 中的链接结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18180159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com