Compare commits

...

1 Commits

Author SHA1 Message Date
pvk3wty2e af24dc3ff9 Update README.md
2 years ago

@ -1,2 +1,66 @@
# BP_system
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float **in;
float **out;
} Sample;
int testSize; // 全局变量存储测试集文件中的数据行数
Sample *getTestData(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Encountered an error while opening the file!\n");
return NULL;
}
int count = 0;
float tempIn, tempOut;
// 逐行读取数据,计算数据行数
while (fscanf(file, "%f %f", &tempIn, &tempOut) == 2) {
count++;
}
// 返回文件指针到文件起始位置
fseek(file, 0, SEEK_SET);
// 分配内存来存储 Sample 结构
Sample *result = (Sample *)malloc(sizeof(Sample));
result->in = (float **)malloc(count * sizeof(float *));
result->out = (float **)malloc(count * sizeof(float *));
for (int i = 0; i < count; i++) {
result->in[i] = (float *)malloc(2 * sizeof(float)); // 2 是输入值的数量
result->out[i] = (float *)malloc(sizeof(float)); // 1 是输出值的数量
}
int index = 0;
// 读取数据到分配的内存中
while (fscanf(file, "%f %f", &tempIn, &tempOut) == 2) {
result->in[index][0] = tempIn;
result->in[index][1] = tempOut;
result->out[index][0] = tempOut;
index++;
}
testSize = count; // 存储数据行数到全局变量
printf("The file has been successfully read!\n");
fclose(file);
return result;
}
void printData(Sample *data, int size) {
if (data == NULL) {
printf("Sample is empty!\n");
return;
}
for (int i = 0; i < size; i++) {
printf("Input 1: %f\tInput 2: %f\tOutput: %f\n", data->in[i][0], data->in[i][1], data->out[i][0]);
}
}

Loading…
Cancel
Save