C语言 我收到无法关闭的错误消息

isr3a4wc  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(149)

我在这里得到了以下C代码和以下错误消息,只是不知道如何解决它:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#pragma warning (disable: 4996)

#define MAX_ROWS 20
#define MAX_COLS 30

typedef struct {
    char data[MAX_ROWS][MAX_COLS];
    int rows;
    int cols;
} Matrix;

char neighbor_count(Matrix* m, int row, int col) {
    int count = 0;
    for (int i = row - 1; i <= row + 1; i++) {
        if (i < 0 || i >= m->rows) {
            continue;
        }
        for (int j = col - 1; j <= col + 1; j++) {
            if (j < 0 || j >= m->cols) {
                continue;
            }
            if (m->data[i][j] == '*' && (i != row || j != col)) {
                count++;
            }
        }
    }
    return count;
}

void check_cell(Matrix* m, int row, int col) {
    char neighbors_count = neighbor_count(m, row, col);
    char current_cell = m->data[row][col];

    if (neighbors_count < 2) { //Die Zelle stirbt an Vereinsamung
        m->data[row][col] = ' ';
    }
    else if (neighbors_count > 3 && current_cell == '*') { //Die Zelle stirbt an Übervölkerung
        m->data[row][col] = ' ';
    }
    else if (neighbors_count == 3 && current_cell == ' ') { //Aus der toten Zelle wird eine neue lebende Zelle
        m->data[row][col] = '*';
    }
    else if (neighbors_count == 2 || neighbors_count == 3) { //Die Zelle lebt weiter
        m->data[row][col] = '*';
    }
}

void load_from_file(Matrix* m, const char* filename) {
    FILE* f = fopen(filename, "r");

    if (f == NULL) {
        printf("File not found\n");
        exit(-1);
    }

    int row_idx = 0;
    char ch;
    while ((ch = fgetc(f)) != EOF && row_idx < MAX_ROWS) {
        if (ch == '\n') {
            row_idx++;
            continue;
        }
        m->data[row_idx][m->cols] = ch;
        m->cols++;
    }
    m->rows = row_idx;
    fclose(f);
}

void randomize(Matrix* m, int percent) {
    m->rows = MAX_ROWS;
    m->cols = MAX_COLS;
    int cells = (m->rows * m->cols) * (percent / 100.0f);
    srand(time(NULL));
    while (cells > 0) {
        int row = rand() % m->rows;
        int col = rand() % m->cols;
        if (m->data[row][col] == ' ') {
            m->data[row][col] = '*';
            cells--;
        }
    }
}

void print_matrix(Matrix* m) {
    for (int i = 0; i < m->rows; i++) {
        for (int j = 0; j < m->cols; j++) {
            printf("%c", m->data[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

void step(Matrix* m) {
    Matrix m_tmp = *m;
    for (int i = 0; i < m->rows; i++) {
        for (int j = 0; j < m->cols; j++) {
            check_cell(&m_tmp, i, j);
        }
    }
    *m = m_tmp;
}

int main() {
    Matrix m;

    // Menü zur Auswahl des Startzustands
    printf("1. Aus Datei laden\n");
    printf("2. Zufallszustand generieren\n");
    int selection;
    scanf("%d", &selection);

    switch (selection) {
    case 1: // Aus Datei laden
        char  filename[20];
        printf("Bitte Dateinamen angeben: ");
        scanf("%s", filename);
        load_from_file(&m, filename);
        break;
    case 2: // Zufallszustand generieren
        int percent;
        printf("Prozentualer Anteil an lebenden Zellen: ");
        scanf("%d", &percent);
        randomize(&m, percent);
        break;
    default:
        printf("Ungültige Eingabe\n");
        return 0;
    }

    // Menü zur Auswahl der Animation
    printf("1. Schrittweise Animation\n");
    printf("2. Fließende Animation\n");
    scanf("%d", &selection);

    switch (selection) {
    case 1: // Schrittweise Animation
        while (1) {
            print_matrix(&m);
            step(&m);
            getchar();
        }
        break;
    case 2: // Fließende Animation
        while (1) {
            print_matrix(&m);
            step(&m);
            usleep(200000);;
        }
        break;
    default:
        printf("Ungültige Eingabe\n");
        break;
    }

    return 0;
}

这些错误消息
E1072声明不能有标签。CGoL Beleg程序C:\用户\d-enk\OneDrive\桌面\UNI媒体信息\程序\版本1\CGoL Beleg程序\CGoL Beleg程序\队列. c 119
E1072声明不能有标签。. CGoL标签程序C:\用户\d-enk\OneDrive\桌面\UNI媒体信息\程序\版本1\CGoL标签程序\CGoL标签程序\Quelle. c 125
LNK 2019引用函数“main”中未解析的外部符号“usleep”。CGoL Beleg Prog C:\用户\d-enk\OneDrive\桌面\UNI媒体信息\程序\版本1\CGoL Beleg Prog\CGoL Beleg Prog\问题对象1
LNK 1120 1无法解析的外部CGoL错误程序C:\用户\d-enk\OneDrive\桌面\UNI媒体信息\编程\默认值1\CGoL错误程序\x64\调试\CGoL错误程序. exe 1

kqlmhetl

kqlmhetl1#

“声明不能有标签”意味着不能在switch/case中间声明变量,可以通过添加局部复合语句来解决:

case 1: // Aus Datei laden
{
    char  filename[20];  // this one was causing the problem
    printf("Bitte Dateinamen angeben: ");
    scanf("%s", filename);
    load_from_file(&m, filename);
    break;
}

int percent;也是一样。
“引用未解析的外部符号“usleep””意味着编译器找不到usleep。很可能是因为它在Windows中已过时。请参阅此c++, usleep() is obsolete, workarounds for Windows/MingW?

相关问题