繁体   English   中英

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

[英]I get error messages that I can't turn off

我在这里得到以下 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;
}

和这个错误信息

  1. E1072 声明不能有 label。CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\CGoL Beleg Prog\Quelle.c 119

2.E1072 声明不能有 label.. CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\CGoL Beleg Prog\Quelle.c 125

  1. LNK2019 引用 function“main”中未解析的外部符号“usleep”。 CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\CGoL Beleg Prog\Quelle.obj 1

4.LNK1120 1 未解析的外部 CGoL Beleg Prog C:\Users\d-enk\OneDrive\Desktop\UNI MEDIENINFORMATIK\Programmierung\Vorlesung 1\CGoL Beleg Prog\x64\Debug\CGoL Beleg Prog.exe 1

再次将错误作为图片

Ich habe schon probiert die Anweisungen zu befolgen aber ich sehe wahrscheinlich den Baum vor lauter Wald nicht

“声明不能有标签”意味着你不能在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() 已过时,Windows/MingW 的解决方法? .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM