C 实施问题:

C 实施问题:,c,brainfuck,C,Brainfuck,我坐下来试着实施脑力操。问题似乎很简单。我很难让这个愚蠢的东西发挥作用。我做这件事已经有一段时间了;我承认我需要一些睡眠。也许这就是问题所在。解释器没有输出任何东西。我很确定问题很简单;我知道在我更好地掌握了这个程序的发展方向之后,我需要对一些函数调用进行模块化。为什么我没有得到输出 main.c #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <memory.h&g

我坐下来试着实施脑力操。问题似乎很简单。我很难让这个愚蠢的东西发挥作用。我做这件事已经有一段时间了;我承认我需要一些睡眠。也许这就是问题所在。解释器没有输出任何东西。我很确定问题很简单;我知道在我更好地掌握了这个程序的发展方向之后,我需要对一些函数调用进行模块化。为什么我没有得到输出

main.c

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <memory.h>

#include "list.h"

node file;
node flow;
node memm;

void init() {
    file.val = 1;
    file.next = 0;
    flow.val = 1;
    flow.next = 0;
    memm.val = 1;
    memm.next = 0;
}

int run = 1;
void quit(int val) {
    run = 0;
    while (file.next) pop(&file);
    while (flow.next) pop(&flow);
    while (memm.next) pop(&memm);
}

void doop() {
    switch (file.val++) {
        case '>':
            memm.val++;
            break;
        case '<':
            memm.val--;
            break;
        case '+':
            get(&memm, memm.val)->val++;
            break;
        case '-':
            get(&memm, memm.val)->val--;
            break;
        case '.':
            printf("c", get(&memm, memm.val)->val);
            fflush(stdout);
            break;
        case '[':
            if (!get(&memm, memm.val)->val)
                while (get(&file, file.val)->val != ']')
                    file.val++;
            else push(&flow, file.val);
        case ']':
            if (get(&memm, memm.val)->val)
                file.val = pop(&flow);
    }
}

int main(int argc, char** argv) {
    int flen, c, i, f_len;
    FILE *fh;
    char fh_name[] = "test";

    signal(SIGINT, quit);
    init();

    fh = fopen(fh_name, "r");
    while (run && (c = fgetc(fh)) != EOF)
        push(&file, c);
    fclose(fh);

    f_len = length(&file);
    while (file.val > 0 && file.val < f_len)
        doop();
    return (EXIT_SUCCESS);
}
list.c

#include <stdlib.h>
#include "list.h"

int length(node *m) {
    int len = 0;
    while (m->next) {
        len++;
        m = m->next;
    }
    return len;
}

void push(node *n, int i) {
    node *m = n;
    while (m->next)
        m = m->next;
    m->next = malloc(sizeof(struct node));
    m->next->val = i;
    m->next->next = 0;
}

int pop(node *n) {
    node *m = n;
    int i = length(n) - 1;
    while (i) {
        i--;
        m = m->next;
    }
    i = m->next->val;
    free(m->next);
    m->next = 0;
    return i;
}

node *get(node *n, int i) {
    node *m = n;
    while (i) {
        i--;
        if (!m->next)
            push(n, 0);
        m = m->next;
    }
    return m;
}

这是因为你需要睡觉,也因为你的代码乱七八糟,我发现了这个

printf("c", get(&memm, memm.val)->val);
它将打印一个
c
,仅此而已

printf("%c", get(&memm, memm.val)->val);
/*      ^ it's the format specifier for the argument */
我怎么这么快就发现了这一点?

  • 非常简单,我启用了编译器警告

BTW
get(&memm,memm.val)->val
的风格很糟糕,但也很糟糕。

这是因为你需要睡觉,而且你的代码很乱,我发现了这个

printf("c", get(&memm, memm.val)->val);
它将打印一个
c
,仅此而已

printf("%c", get(&memm, memm.val)->val);
/*      ^ it's the format specifier for the argument */
我怎么这么快就发现了这一点?

  • 非常简单,我启用了编译器警告
BTW
get(&memm,memm.val)->val
是非常糟糕的风格,但非常糟糕。

这行

switch (file.val++) {
不可能是对的。目前它只是增加文件链的第一个“val”,就像下面的“mem.val++”

我希望您需要去掉该行中的+,然后做一些事情,增加指向指令的指针,而不是指令本身


您的']'指令错误;即使你不想回去了,你也需要做流行音乐


您的“[”指令部分错误。如果该值从零开始,它当前将跳到第一个“]”,它将发现不匹配的“]”。

switch (file.val++) {
不可能是对的。目前它只是增加文件链的第一个“val”,就像下面的“mem.val++”

我希望您需要去掉该行中的+,然后做一些事情,增加指向指令的指针,而不是指令本身


您的']'指令错误;即使你不想回去了,你也需要做流行音乐



您的“[”指令部分错误。如果该值从零开始,它当前将跳到第一个“]”,它将发现不匹配的“]”。

@EugeneSh。是的,我现在还在做<代码>gdb和我都很好friends@MotokoKusanagi在尝试读取文件之前,请检查
fopen()
是否成功,并且解释器没有输出任何内容,没有涉及解释器。@iharob正在读取文件;通过弹出所有
文件
值,我可以从内存中反向读取文件。至于“不涉及解释器”,我可能使用了不正确的术语。@MotokoKusanagi这不是编写不安全代码的好理由,你可以习惯它。我让它工作了。它有很多错误:缺少
break
s,多个逻辑错误。@EugeneSh。是的,我现在还在做<代码>gdb和我都很好friends@MotokoKusanagi在尝试读取文件之前,请检查
fopen()
是否成功,并且解释器没有输出任何内容,没有涉及解释器。@iharob正在读取文件;通过弹出所有
文件
值,我可以从内存中反向读取文件。至于“不涉及解释器”,我可能使用了不正确的术语。@MotokoKusanagi这不是编写不安全代码的好理由,你可以习惯它。我让它工作了。它有很多错误:缺少
break
s,多个逻辑错误。