C语言回溯实验室

问题描述 投票:0回答:0

我在为我的 C 实验室编写代码时收到两个错误。

错误1是

./main.c:54:22: error: use of undeclared identifier 'matrix'
            int it = matrix[row][col];

错误2是

./main.c:100:24: error: use of undeclared identifier 'ICAN'
           setting.c_lflag |= ICAN;

我在错误所在的行添加了注释部分 //Error # 需要更改什么才能成功运行代码?

下面是实验室的C代码

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

#define ESC 27
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define MSIZE 20

typedef struct Node {
    int row;
    int col;
    int distance;
    int direction;
    struct Node* next;
} Node;

Node* root = NULL;

void add_node(int r, int c, int dist, int dir) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->row = r;
    new_node->col = c;
    new_node->distance = dist;
    new_node->direction = dir;
    new_node->next = NULL;
    if (root == NULL) {
        root = new_node;
    } else {
        Node* current = root;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_node;
    }
}

void print_list() {
    Node* current = root;
    while (current != NULL) {
        printf("(%d, %d) %d %d\n", current->row, current->col, current->distance, current->direction);
        current = current->next;
    }
}

void print_matrix() {
    printf("%c[1;1H", ESC); // cursor to top-left corner
    for (int row = 0; row < MSIZE; row++) {
        for (int col = 0; col < MSIZE; col++) {
            printf("%c[44m", ESC); // blue
            int it = matrix[row][col];
            if (it == 0)
                printf("%c[42m", ESC); //green
            if (it == 2)
                printf("%c[43m", ESC); //gold
            if (it == 3)
                printf("%c[48;2;120;20;220m", ESC); //purple RGB
            printf(" %d ", it);
        } // for col
        printf("\n"); // marks end of a row
    } // for row
} // print_matrix

void fillup(int* p, int val) {
    for (int i = 0; i < MSIZE * MSIZE; i++)
        *(p + i) = val;
}

void wipe() {
    printf("%c[44m", ESC); // blue
    printf("%c[1D", ESC);   // go back to wipe previous
    printf(".");           // wipe
}

void pinkblue() {
    printf("%c[45m", ESC); // pink
    printf("*");
    printf("%c[44m", ESC); // blue
}

void setup() {
    struct termios setting;
    tcgetattr(STDIN_FILENO, &setting);
    setting.c_lflag &= ~ICANON; //disable cannonical input mode with newline
    setting.c_lflag &= ~ECHO;   // disable echo of input
    tcsetattr(STDIN_FILENO, TCSANOW, &setting);

    printf("%c[?1049h", ESC); // alternate buffer mode
    printf("%c[2J", ESC);     // clear the screen
    printf("%c[1;1H", ESC);   // move the cursor to the top-left corner
    printf("%c[44m", ESC);    // set the background color to blue
}

void shutdown() {
    struct termios setting;
    tcgetattr(STDIN_FILENO, &setting);
    setting.c_lflag |= ICAN;
return 0;
}

在出现 3 个错误之前,第三个是说缺少一个;在第二个错误所在的第 100 行,我不知道这是否会有所作为。

c computer-science
© www.soinside.com 2019 - 2024. All rights reserved.