RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-553772

justSMTH's questions

Martin Hope
justSMTH
Asked: 2025-02-03 21:46:33 +0000 UTC

服务器是否应该一直连接到数据库还是仅在查询期间连接?

  • 6

我正在制作一个没有框架的小服务。现在我正在设置数据库的工作。我不明白什么才是更正确的:服务器与数据库的持续连接还是连接、执行请求然后断开连接?

计划的负载并不大,因此无论哪种选择都不会有问题。但我希望把每件事都做好。

sql
  • 2 个回答
  • 46 Views
Martin Hope
justSMTH
Asked: 2024-11-08 00:58:03 +0000 UTC

for 循环体被忽略

  • 5

程序忽略 for 循环体。迭代通过,但调试器不进入主体。 VS22 构建调试。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

typedef struct List {
    int data;
    struct List* Next;
} List;

void Add(List* ls, int data) {
    List* node = ls;
    ls = malloc(sizeof(List));
    ls->data = data;
    ls->Next = node;
}

int main() {
    List* ls = malloc(sizeof(List));
    ls->Next = NULL;
    ls->data = NULL;
    if (ls->Next == NULL) {
        ls->data = 2;
    }
    int i = 0;
    while (1) {
        char c = getchar();
        char num[16];
        if (isspace(c)) {
            if (num[0] != '\0') {
                Add(ls, atoi(num));
                for (int j = 0; j <= i; j++) { 
                    num[j] == '\0'; \\Полностью игнорируется
                }
                i=0;
            }
            continue;
        }
        else if (c == '.') {
            if (num[0] != '\0') {
                Add(ls, atoi(num));
                i++;
                for (; i <= 0; i--) {
                    num[i] == '\0';
                }
            }
            break;
        }
        else if (isdigit(c)) {
            num[i++] = c;
        }
        else {
            printf("Error");
            return 1;
        }
    }
    while (ls->Next != NULL) {
        printf("%d", ls->data);
    }
    return 0;
}
c
  • 1 个回答
  • 28 Views
Martin Hope
justSMTH
Asked: 2023-12-22 00:46:40 +0000 UTC

为什么我开始在所有具有结构的行上收到错误?

  • 4

我有一个包含完全工作代码的文件,而我正在另一个文件中工作。在这两个文件中的某个时刻,代码开始抱怨所有结构。首先,我删除了新文件(那里没有做太多事情),但这没有帮助。这些文件不会相互交互。一般来说,VS 仅对那些调用结构的行给出错误。这是代码,其中

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <stdio.h>
#define MAX_SIZE_OF_MATRIX 3
#define MAX_SIZE_OF_VAL 4
typedef struct STR {
    int Val;
    int Row;
    STR* Next;
} STR;
typedef struct ListSTR {
    STR* row;
    struct ListSTR* Next;
} ListSTR;
void Add(STR** point, int Val, int Row) {
    STR* t = malloc(sizeof(STR));
    t->Row = Row;
    t->Val = Val;
    t->Next = NULL;
    STR** p = point;
    if (*p)
        for (p = &(*p)->Next; *p; p = &(*p)->Next);
    *p = t;
}
void AddToIndex(ListSTR** matrix, int index, int Val, int Row) {
    ListSTR** result = matrix;
    for (int i = 1; i <= index; i++) {
        if (*result) {
            result = &(*result)->Next;
        }
        else {
            printf("Error");
            exit(1);
        }
    }
    Add(&((*result)->row), Val, Row);
}
void clean(ListSTR* matrix, int ROWS) {
    ListSTR* matrixTemp;
    for (int i = 0; i < ROWS; i++) {
        matrixTemp = matrix;
        STR* point = matrixTemp->row;
        STR* pTemp;
        while (point) {
            pTemp = point;
            if (point->Next) {
                point = point->Next;
            }
            else {
                free(pTemp);
                break;
            }
            free(pTemp);
        }
        if (matrix->Next) {
            matrix = matrix->Next;
        }
        else {
            free(matrixTemp);
            break;
        }
        free(matrixTemp);
    }
}
int isNum(char c) {
    switch (c) {
    case '1':
        return 1;
    case '2':
        return 1;
    case '3':
        return 1;
    case '4':
        return 1;
    case '5':
        return 1;
    case '6':
        return 1;
    case '7':
        return 1;
    case '8':
        return 1;
    case '9':
        return 1;
    case '0':
        return 1;
    default:
        return 0;
    }
}
int isWhitespace(char c) {
    switch (c) {
    case' ':
        return 1;
    case'\n':
        return 1;
    case'\0':
        return 1;
    default:
        return 0;
    }
}
void GetRowsAndColums(char* filename, int* ROWS, int* COLUMNS) {
    FILE* fp = fopen(filename, "r");
    char rows[MAX_SIZE_OF_MATRIX];
    char c;
    while (isWhitespace(c = getc(fp))) {}
    int i = 0;
    while (1) {
        i = 0;
        if (isNum(c)) {
            rows[i++] = c; rows[i] = 0;
        }
        else if (isWhitespace(c)) {
            if (c == '\n') {
                printf("Error");
                exit(1);
            }
            *ROWS = strtol(rows, NULL, 10);
            break;
        }
        else {
            printf("Error");
            exit(1);
        }
        c = getc(fp);
    }
    while (isWhitespace(c = getc(fp))) {}
    char column[MAX_SIZE_OF_MATRIX];
    i = 0;
    while (1) {
        i = 0;
        if (isNum(c)) {
            column[i++] = c;  column[i] = 0;
        }
        else if (isWhitespace(c)) {
            *COLUMNS = strtol(column, NULL, 10);
            break;
        }
        else {
            printf("Error");
            exit(1);
        }
        c = getc(fp);
    }
    fclose(fp);
}
void TestAdd(ListSTR* matrix) {
    for (int i = 0; i < 3; i++) {
        matrix->row = (STR*)malloc(sizeof(STR));
        matrix->row->Row = i;
        matrix->row->Val = i + 1;
        matrix->row->Next = NULL;
        matrix = matrix->Next;
    }
}
void printMatrix(ListSTR* matrix, int ROWS, int COLUMNS) {
    STR* point;
    for (int i = 0; i < ROWS; i++) {
        point = matrix->row;
        int lastRow = -1;
        for (int j = 0; j < COLUMNS; j++) {
            if (point == NULL) {
                for (int k = 1; k < COLUMNS - lastRow; k++) {
                    printf("0 ");
                }
                break;
            }
            int Val = point->Val;
            int Row = point->Row;
            if (Row == 0) {
                printf("%d ", Val);
                lastRow = 0;
            }
            else if (Row >= COLUMNS) {
                printf("Error");
                exit(1);
            }
            else if (Row > lastRow) {
                for (int k = lastRow + 1; k < Row; k++) {
                    printf("0 ");
                }
                printf("%d ", Val);
                lastRow = Row;
            }
            else if (Row <= lastRow) {
                printf("Error");
                exit(1);
            }
            else {
                printf("%d ", Val);
            }
            point = point->Next;
        }
        printf("\n");
        matrix = matrix->Next;
    }
}
void Initialize(ListSTR* matrix, int ROWS) {
    if (ROWS == 1) {
        matrix->row = NULL;
        return;
    }
    else if (ROWS < 1) {
        printf("Error");
        exit(0);
    }
    for (int i = 0; i < ROWS; i++) {
        matrix->row = NULL;
        matrix->Next = malloc(sizeof(ListSTR));
        matrix = matrix->Next;
    }
    free(matrix);
}
void ReadMatrix(ListSTR* matrix, int ROWS, int COLUMNS, char* filename) {
    FILE* fp = fopen(filename, "r");
    char c;
    int count = 1;
    while (c = getc(fp) != '\n') {}
    while (1) {
        while (1) {
            int isNULL = 0;
            char val[MAX_SIZE_OF_VAL];
            while (isWhitespace(c = getc(fp))) {}
            int i = 0;
            int value;
            int row;
            while (1) {
                if (c == '-' && i == 0) {
                    val[i++] = c; val[i] = 0;
                }
                else if (isNum(c)) {
                    val[i++] = c; val[i] = 0;
                }
                else if (isWhitespace(c)) {
                    if (c == '\n') {
                        printf("Error");
                        exit(1);
                    }
                    value = strtol(val, NULL, 10);
                    break;
                }
                else if (c == 'N') {
                    if (matrix->row != NULL) {
                        printf("Error");
                        exit(1);
                    }
                    isNULL = 1;
                    break;
                }
                else {
                    printf("Error");
                    exit(1);
                }
                c = getc(fp);
            }
            while (isWhitespace(c = getc(fp))) {}
            char rows[MAX_SIZE_OF_MATRIX];
            i = 0;
            while (1) {
                if (isNULL) {
                    if (c == 'U') {
                        for (int i = 0; i < 2; i++) {
                            c = getc(fp);
                            if (c != 'L') {
                                printf("Error");
                                exit(1);
                            }
                        }
                        break;
                    }
                }
                if (isNum(c)) {
                    rows[i++] = c; rows[i] = 0;
                }
                else if (isWhitespace(c)) {
                    row = strtol(rows, NULL, 10);
                    if (row >= ROWS) {
                        printf("Error");
                        exit(1);
                    }
                    break;
                }
                else if (c == EOF) {
                    row = strtol(rows, NULL, 10);
                    if (row >= ROWS) {
                        printf("Error");
                        exit(1);
                    }
                    break;
                }
                else {
                    printf("Error");
                    exit(1);
                }
                c = getc(fp);
            }
            if (!isNULL) {
                Add(&(matrix->row), value, row);
            }
            else {
                break;
            }
            if (c == '\n' || c == EOF) {
                break;
            }
        }
        count++;
        if (count >= ROWS) {
            break;
        }
        matrix = matrix->Next;
    }
    fclose(fp);
}
void Transpose(ListSTR* matrix, ListSTR* result, int ROWS, int COLUMNS) {
    for (int i = 0; i < ROWS; i++) {
        if (matrix->row == NULL) {
            if (i < ROWS) {
                matrix = matrix->Next;
            }
            continue;
        }
        STR* point = matrix->row;
        while (1) {
            AddToIndex(&result, point->Row, point->Val, i);
            if (point->Next != NULL) {
                point = point->Next;
            }
            else {
                break;
            }
        }
        if (i < ROWS) {
            matrix = matrix->Next;
        }
    }
}
int Trans() {
    setlocale(LC_ALL, "Rus");
    int ROWS, COLUMNS;
    char* file = "Text.txt";
    GetRowsAndColums(file, &ROWS, &COLUMNS);
    ListSTR* matrix = malloc(sizeof(ListSTR));
    Initialize(matrix, ROWS);
    ListSTR* result = malloc(sizeof(ListSTR));
    Initialize(result, COLUMNS);
    ReadMatrix(matrix, ROWS, COLUMNS, file);
    printf("Матрица \n");
    printMatrix(matrix, ROWS, COLUMNS);
    Transpose(matrix, result, ROWS, COLUMNS);
    printf("Матрица транспонированная \n");
    printMatrix(result, COLUMNS, ROWS);
    clean(matrix, ROWS); clean(result, COLUMNS);
    return 0;
}

截图有一些错误 截图有一些错误

c
  • 1 个回答
  • 24 Views
Martin Hope
justSMTH
Asked: 2023-12-06 21:15:05 +0000 UTC

列表的单链表在 C 中不起作用

  • 5

我需要在 C 中创建一个已实现的结构的列表列表,但它不会保存任何内容。每次我尝试向列表中添加新元素时,列表都会重置。其余代码工作正常。从文件中正确读取所需的数据。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <stdio.h>
#define MAX_SIZE_OF_MATRIX 3
#define MAX_SIZE_OF_VAL 4
typedef struct STR {
    int Val;
    int Row;
    struct STR* Next;
} STR;
typedef struct ListSTR {
    STR* row;
    struct ListSTR* Next;
} ListSTR;
void Add(STR** point, int Val, int Row) {
    if (*point == NULL) {
        *point = (STR*)malloc(sizeof(STR));
        (*point)->Row =Row;
        (*point)->Val = Val;
        (*point)->Next = NULL;
        return 0;
    }
    while ((*point)->Next != NULL) {
        (*point) = (*point)->Next;
    }
    (*point)->Next = (STR*)malloc(sizeof(STR));
    (*point)->Next->Val = Val;
    (*point)->Next->Row = Row;
    (*point)->Next->Next = NULL;
}
void AddToIndex(ListSTR** matrix, int index, int Val, int Row) {
    for (int i = 0; i < index; i++) {
        if ((*matrix)->Next != NULL) {
            (*matrix) = (*matrix)->Next;
        }
        else {
            printf("Error");
            exit(1);
        }
    }
    STR* point = (*matrix)->row;
    if (point == NULL) {
        point = (STR*)malloc(sizeof(STR));
        point->Row = Row;
        point->Val = Val;
        point->Next = NULL;
        return 0;
    }
    while (point->Next != NULL) {
        point = point->Next;
    }
    point->Next = (STR*)malloc(sizeof(STR));
    point->Next->Val = Val;
    point->Next->Row = Row;
    point->Next->Next = NULL;
}
void clean(STR* point) {
    STR* start = point->Next;
    while (start != NULL) {
        STR* next = start->Next;
        free(start);
        start = next;
    }
    free(point);
}
int isNum(char c) {
    switch (c) {
    case '1':
        return 1;
    case '2':
        return 1;
    case '3':
        return 1;
    case '4':
        return 1;
    case '5':
        return 1;
    case '6':
        return 1;
    case '7':
        return 1;
    case '8':
        return 1;
    case '9':
        return 1;
    case '0':
        return 1;
    default:
        return 0;
    }
}
int isWhitespace(char c) {
    switch (c) {
    case' ':
        return 1;
    case'\n':
        return 1;
    case'\0':
        return 1;
    default:
        return 0;
    }
}
void GetRowsAndColums(char* filename, int* ROWS, int* COLUMNS) {
    FILE* fp = fopen(filename, "r");
    char rows[MAX_SIZE_OF_MATRIX];
    char c;
    while (isWhitespace(c = getc(fp))) {}
    int i = 0;
    while (1) {
        i = 0;
        if (isNum(c)) {
            rows[i] = c;
            i++;
        }
        else if (isWhitespace(c)) {
            if (c == '\n') {
                printf("Error");
                exit(1);
            }
            *ROWS = strtol(rows, NULL, 10);
            break;
        }
        else {
            printf("Error");
            exit(1);
        }
        c = getc(fp);
    }
    while (isWhitespace(c = getc(fp))) {}
    char column[MAX_SIZE_OF_MATRIX];
    i = 0;
    while (1) {
        i = 0;
        if (isNum(c)) {
            column[i] = c;
            i++;
        }
        else if (isWhitespace(c)) {
            *COLUMNS = strtol(column, NULL, 10);
            break;
        }
        else {
            printf("Error");
            exit(1);
        }
        c = getc(fp);
    }
    fclose(fp);
}
void TestAdd(ListSTR* matrix) {
    for (int i = 0; i < 3; i++) {
        matrix->row = (STR*)malloc(sizeof(STR));
        matrix->row->Row = i;
        matrix->row->Val = i + 1;
        matrix->row->Next = NULL;
        matrix = matrix->Next;
    }
}
void printMatrix(ListSTR* matrix, int ROWS, int COLUMNS) {
    STR* point;
    for (int i = 0; i < ROWS; i++) {
        point = matrix->row;
        int lastRow = 0;
        for (int j = 0; j < COLUMNS; j++) {
            if (point == NULL) {
                for (int k = 1; k <= COLUMNS - lastRow; k++) {
                    printf("0 ");
                }
                break;
            }
            int Val = point->Val;
            int Row = point->Row;
            if (Row == 0) {
                printf("%d ", Val);
            }
            else if (Row >= COLUMNS) {
                printf("Error");
                exit(1);
            }
            else if (Row - lastRow >= 1) {
                for (int k = 1; k <= Row - lastRow; k++) {
                    printf("0 ");
                }
                printf("%d ", Val);
                lastRow = Row;
            }
            else if (Row <= lastRow) {
                printf("Error");
                exit(1);
            }
            else {
                printf("%d ", Val);
            }
            point = point->Next;
        }
        printf("\n");
        matrix = matrix->Next;
    }
}
void Initialize(ListSTR* matrix, int ROWS) {
    for (int i = 0; i < ROWS; i++) {
        matrix->row = NULL;
        matrix->Next = malloc(sizeof(ListSTR));
        matrix = matrix->Next;
    }
}
void ReadMatrix(ListSTR** matrix, int ROWS, int COLUMNS, char* filename) {
    FILE* fp = fopen(filename, "r");
    char c;
    int count = 0;
    while(c=getc(fp)!='\n'){}
    while (1) {
        while (1) {
            while (isWhitespace(c = getc(fp))) {}
            int i = 0;
            char val[MAX_SIZE_OF_VAL];
            int value;
            int row;
            while (1) {
                if (c == '-' && i == 0) {
                    val[i] = c;
                    i++;
                }
                else if (isNum(c)) {
                    val[i] = c;
                    i++;
                }
                else if (isWhitespace(c)) {
                    if (c == '\n') {
                        printf("Error");
                        exit(1);
                    }
                    value = strtol(val, NULL, 10);
                    break;
                }
                else {
                    printf("Error");
                    exit(1);
                }
                c = getc(fp);
            }
            while (isWhitespace(c = getc(fp))) {}
            char rows[MAX_SIZE_OF_MATRIX];
            i = 0;
            while (1) {
                if (isNum(c)) {
                    rows[i] = c;
                    i++;
                }
                else if (isWhitespace(c)) {
                    row = strtol(rows, NULL, 10);
                    if (row >= ROWS) {
                        printf("Error");
                        exit(1);
                    }
                    break;
                }
                else if (c == EOF) {
                    row = strtol(rows, NULL, 10);
                    if (row >= ROWS) {
                        printf("Error");
                        exit(1);
                    }
                    break;
                }
                else {
                    printf("Error");
                    exit(1);
                }
                c = getc(fp);
            }
            Add((*matrix)->row, value, row);
            if (c == '\n' || c==EOF) {
                break;
            }
        }
        count++;
        if (count >= ROWS) {
            break;
        }
        (*matrix) = (*matrix)->Next;
    }
    fclose(fp);
}
int Main() {
    int ROWS, COLUMNS;
    char* file = "Text.txt";
    GetRowsAndColums(file, &ROWS, &COLUMNS);
    ListSTR* matrix = malloc(sizeof(ListSTR));
    Initialize(matrix, ROWS);
    //ListSTR* result = malloc(sizeof(ListSTR));
    //Initialize(result, COLUMNS);
    ReadMatrix(&matrix, ROWS, COLUMNS, file);
    printMatrix(matrix, ROWS, COLUMNS);
    return 0;
}

这是文本文件

3 3
1 0 7 2
-3 2
10 0 4 1
c
  • 1 个回答
  • 77 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5