Загрузка данных


Отвечу как наставник по C и системному программированию School 21 с опытом подготовки проектов к автотестам и статическому анализу.

Ниже — полностью исправленные файлы, которые нужно заменить, чтобы убрать основные замечания cppcheck.

⸻

1. src/dmanager_module.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "door_struct.h"
#define DOORS_COUNT 15
#define MAX_ID_SEED 10000
void initialize_doors(struct door *doors);
void sort_doors(struct door *doors, int count);
void close_doors(struct door *doors, int count);
void output_doors(const struct door *doors, int count);
int main(void) {
    struct door doors[DOORS_COUNT];
    initialize_doors(doors);
    sort_doors(doors, DOORS_COUNT);
    close_doors(doors, DOORS_COUNT);
    output_doors(doors, DOORS_COUNT);
    return 0;
}
void sort_doors(struct door *doors, int count) {
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - i - 1; j++) {
            if (doors[j].id > doors[j + 1].id) {
                struct door temp = doors[j];
                doors[j] = doors[j + 1];
                doors[j + 1] = temp;
            }
        }
    }
}
void close_doors(struct door *doors, int count) {
    for (int i = 0; i < count; i++) {
        doors[i].status = 0;
    }
}
void output_doors(const struct door *doors, int count) {
    for (int i = 0; i < count; i++) {
        printf("%d, %d", doors[i].id, doors[i].status);
        if (i < count - 1) {
            printf("\n");
        }
    }
}
// Doors initialization function
// ATTENTION!!!
// DO NOT CHANGE!
void initialize_doors(struct door *doors) {
    srand(time(0));
    int seed = rand() % MAX_ID_SEED;
    for (int i = 0; i < DOORS_COUNT; i++) {
        doors[i].id = (i + seed) % DOORS_COUNT;
        doors[i].status = rand() % 2;
    }
}

⸻

2. src/documentation_module.h

#ifndef DOCUMENTATION_MODULE_H
#define DOCUMENTATION_MODULE_H
#define DOCUMENTS_COUNT 4
#define DOCUMENTS "Linked lists", "Queues", "Maps", "Binary Trees"
#define AVAILABLE_DOCUMENT "Binary Trees"
int *check_available_documentation_module(int (*validate)(const char *), int document_count, ...);
int validate(const char *data);
#endif

⸻

3. src/documentation_module.c

#include "documentation_module.h"
#include <stdarg.h>
#include <stdlib.h>
int validate(const char *data) {
    int result = 1;
    if (data == NULL) {
        result = 0;
    } else {
        const char available[] = AVAILABLE_DOCUMENT;
        int i = 0;
        while (data[i] != '\0' && available[i] != '\0' && result) {
            if (data[i] != available[i]) {
                result = 0;
            }
            i++;
        }
        if (data[i] != available[i]) {
            result = 0;
        }
    }
    return result;
}
int *check_available_documentation_module(int (*validate)(const char *), int document_count, ...) {
    int *result = NULL;
    if (validate != NULL && document_count > 0) {
        result = malloc(document_count * sizeof(int));
        if (result != NULL) {
            va_list documents;
            va_start(documents, document_count);
            for (int i = 0; i < document_count; i++) {
                const char *document = va_arg(documents, const char *);
                result[i] = validate(document);
            }
            va_end(documents);
        }
    }
    return result;
}

⸻

4. src/main_module_entry_point.c

#include <stdio.h>
#include <stdlib.h>
#include "documentation_module.h"
#include "print_module.h"
#ifdef DOCUMENTATION_MODE
int main(void) {
    const char *const documents[DOCUMENTS_COUNT] = {DOCUMENTS};
    int *availability = check_available_documentation_module(validate, DOCUMENTS_COUNT, DOCUMENTS);
    if (availability == NULL) {
        printf("n/a");
    } else {
        for (int i = 0; i < DOCUMENTS_COUNT; i++) {
            printf("[%15s : %s]", documents[i], availability[i] ? "available" : "unavailable");
            if (i < DOCUMENTS_COUNT - 1) {
                printf("\n");
            }
        }
    }
    free(availability);
    return 0;
}
#else
int main(void) {
    print_log(print_char, MODULE_LOAD_SUCCESS_MESSAGE);
    return 0;
}
#endif

⸻

5. src/print_module.h

#ifndef PRINT_MODULE_H
#define PRINT_MODULE_H
#define MODULE_LOAD_SUCCESS_MESSAGE "Output stream module load: success"
#define LOG_PREFIX "[LOG]"
void print_log(char (*print)(char), const char *message);
char print_char(char ch);
#endif

⸻

6. src/print_module.c

#include "print_module.h"
#include <stdio.h>
#include <time.h>
char print_char(char ch) {
    putchar(ch);
    return ch;
}
void print_log(char (*print)(char), const char *message) {
    time_t current_time = time(NULL);
    const struct tm *time_info = localtime(&current_time);
    if (print != NULL && message != NULL && time_info != NULL) {
        char buffer[9];
        strftime(buffer, sizeof(buffer), "%H:%M:%S", time_info);
        for (int i = 0; LOG_PREFIX[i] != '\0'; i++) {
            print(LOG_PREFIX[i]);
        }
        print(' ');
        for (int i = 0; buffer[i] != '\0'; i++) {
            print(buffer[i]);
        }
        print(' ');
        for (int i = 0; message[i] != '\0'; i++) {
            print(message[i]);
        }
        print('\n');
    }
}

⸻

7. src/list_test.c

#include <stdio.h>
#include "list.h"
#define SUCCESS 1
#define FAIL 0
int add_door_test(void);
int remove_door_test(void);
void print_result(const char *test_name, int result);
int main(void) {
    print_result("add_door", add_door_test());
    print_result("remove_door", remove_door_test());
    return 0;
}
int add_door_test(void) {
    int result = FAIL;
    struct door first = {1, 0};
    struct door second = {2, 1};
    struct node *root = init(first);
    if (root != NULL) {
        struct node *const added = add_door(root, second);
        if (added != NULL && root->next == added && added->data.id == 2 && added->data.status == 1 &&
            added->next == NULL) {
            result = SUCCESS;
        }
    }
    destroy(root);
    return result;
}
int remove_door_test(void) {
    int result = FAIL;
    struct door first = {1, 0};
    struct door second = {2, 0};
    struct door third = {3, 1};
    struct node *root = init(first);
    if (root != NULL) {
        struct node *second_node = add_door(root, second);
        struct node *third_node = add_door(second_node, third);
        if (second_node != NULL && third_node != NULL) {
            root = remove_door(second_node, root);
            if (root != NULL && root->next == third_node && find_door(2, root) == NULL) {
                root = remove_door(root, root);
                if (root == third_node && find_door(1, root) == NULL) {
                    root = remove_door(third_node, root);
                    if (root == NULL) {
                        result = SUCCESS;
                    }
                }
            }
        }
    }
    destroy(root);
    return result;
}
void print_result(const char *test_name, int result) {
    printf("%s: ", test_name);
    if (result == SUCCESS) {
        printf("SUCCESS");
    } else {
        printf("FAIL");
    }
    printf("\n");
}

⸻

После замены файлов выполни

Ты сейчас в src:

find . \( -name "*.c" -o -name "*.h" \) -exec clang-format -i {} \;
find . \( -name "*.c" -o -name "*.h" \) -exec clang-format -n {} \;
make clean
make all

Потом:

cppcheck --enable=all --suppress=missingIncludeSystem --suppress=unmatchedSuppression .

Если останутся только строки information: Include file <...> not found, это не критично. Если останется style:, скинь скрин — добьём последние замечания.