我需要在 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
起初我不太理解你的算法(事实上你立即创建了
Initialize()整个字符串列表,并且在文件读入时不动态添加它们ReadMatrix()),所以在调试你的代码后我意识到我错了一些评论。我仍然不明白你在函数中到底打印了什么
printMatrix(),但是通过添加我的代码(下面注释掉),我确保矩阵正在正确构建。从文件中读取数字时也会出现错误。读取最后一个数字后,您没有写下结束行的 nil。我通过在读取每个数字后将 nil 写入该行来纠正此问题。
我还重写了该函数
Add(),恕我直言,它变得更简单(至少现在没有代码重复)。我不知道你在什么操作系统中编写,但在我的(Linux)中我将其替换
Main()为main().这就是结果发生的情况(我注释掉了我替换的行 -
////):例如,我没有查看本示例中未调用的函数
AddToIndex()(即我不知道那里的代码是否正确)。使用您的数据运行的示例(以及我添加的矩阵打印,因为它内置在内存中):
希望这对您有帮助。