From f9b89bdf82be5fbddedf5d00e9525e2d368e2207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E8=BF=9B=E9=80=B8?= <15819740+flurocy@user.noreply.gitee.com> Date: Sun, 1 Jun 2025 15:58:19 +0800 Subject: [PATCH 01/40] testing --- test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.txt diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..25f3991 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +This is just a test. \ No newline at end of file -- Gitee From 7d02427e275e3d52748b5cbe0037e5fd911adec3 Mon Sep 17 00:00:00 2001 From: lihaoxuan Date: Mon, 2 Jun 2025 15:52:11 +0800 Subject: [PATCH 02/40] test --- text.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 text.txt diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..273c1a9 --- /dev/null +++ b/text.txt @@ -0,0 +1 @@ +This is a test. \ No newline at end of file -- Gitee From 286e94b7eba2d062bcca00b33a71d83f4bb6e1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Mon, 2 Jun 2025 11:24:47 +0000 Subject: [PATCH 03/40] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20te?= =?UTF-8?q?st.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test.txt diff --git a/test.txt b/test.txt deleted file mode 100644 index 25f3991..0000000 --- a/test.txt +++ /dev/null @@ -1 +0,0 @@ -This is just a test. \ No newline at end of file -- Gitee From 64ccb7b3ea25927b25acff26bda2b38e0cb0b10d Mon Sep 17 00:00:00 2001 From: Juicy <15804151+Juicy666@user.noreply.gitee.com> Date: Mon, 2 Jun 2025 19:25:08 +0800 Subject: [PATCH 04/40] pr file --- first_pr.txt | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 first_pr.txt diff --git a/first_pr.txt b/first_pr.txt new file mode 100644 index 0000000..13081e4 --- /dev/null +++ b/first_pr.txt @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +int main() +{ + char continue_flag = 'Y'; + char input_buffer[1024]; + + while (continue_flag == 'Y' || continue_flag == 'y') + { + printf("please input filenames: "); + fgets(input_buffer, sizeof(input_buffer), stdin); + input_buffer[strcspn(input_buffer, "\n")] = '\0'; // 移除换行符 + + // 处理多个文件名(支持逗号或空格分隔) + char *filename = strtok(input_buffer, ", "); + int first_file = 1; + + while (filename != NULL) + { + double digit; + pgm_to_matrix(filename, 28, 28, (double[28][28]){0}); // 初始化矩阵 + + // 调用识别函数 + recognize_digit(filename, &digit); + + // 输出结果 + if (first_file) + { + printf("%s: %.0f", filename, digit); + first_file = 0; + } + else + { + printf(", %s: %.0f", filename, digit); + } + + filename = strtok(NULL, ", "); + } + printf("\n"); + + // 检查是否继续 + printf("do you want to continue? please input [Y or N]: "); + scanf(" %c", &continue_flag); + getchar(); // 消耗缓冲区残留字符 + } + + printf("Bye\n"); + return 0; +} \ No newline at end of file -- Gitee From 4313f6a5dd6bc864ccb32ed27a5e4943267d2d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E8=BF=9B=E9=80=B8?= <15819740+flurocy@user.noreply.gitee.com> Date: Mon, 2 Jun 2025 19:29:09 +0800 Subject: [PATCH 05/40] Here are three function which can read a txt file ,add two matrices and mutiply two matrices. --- c10_group_ljy.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 c10_group_ljy.c diff --git a/c10_group_ljy.c b/c10_group_ljy.c new file mode 100644 index 0000000..5ce7bf7 --- /dev/null +++ b/c10_group_ljy.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +// subfunction declaration +void read_txt_data(const char *filename, const int n, double array[1][n]); +void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]); +void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]); + +// 4.read data in a txt file +void read_txt_data(const char *filename, const int n, double array[1][n]) +{ + FILE *fp = fopen(filename, "r"); + if (!fp) + { + printf("Error reading file %s\n", filename); + return; + } + + for (int i = 0; i < n; i++) + { + fscanf(fp, "%lf", &array[0][i]); + } + fclose(fp); +} + +// 5.add two matrices +void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]) +{ + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + result[i][j] = mat1[i][j] + mat2[i][j]; + } + } +} + +// 6.multiply two matrices +void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]) +{ + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + result[i][j] = 0.0; + for (int k = 0; k < p; k++) + { + result[i][j] += mat1[i][k] * mat2[k][j]; + } + } + } +} \ No newline at end of file -- Gitee From c103588d4fd5886a761564a75473085dbf2cdedf Mon Sep 17 00:00:00 2001 From: lihaoxuan Date: Mon, 2 Jun 2025 20:07:49 +0800 Subject: [PATCH 06/40] code --- lhx.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lhx.c diff --git a/lhx.c b/lhx.c new file mode 100644 index 0000000..1e75e21 --- /dev/null +++ b/lhx.c @@ -0,0 +1,79 @@ +void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]); +void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]); +void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]); +void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]) +{ + FILE *fp = fopen(file_path, "rb"); + if (!fp) + { + printf("invalid file.\n"); + return; + } + + char magic_number[3]; + fgets(magic_number, sizeof(magic_number), fp); + + if (strcmp(magic_number, "P5\n") == 0) + { + + char line[256]; + while (fgets(line, sizeof(line), fp) != NULL && line[0] == '#') + { + } + + int width, height, max_value; + fscanf(fp, "%d %d %d", &width, &height, &max_value); + + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + unsigned char pixel = fgetc(fp); + A[i][j] = (double)pixel / (double)max_value; + } + } + } + else + { + + char line[256]; + fgets(line, sizeof(line), fp); + fgets(line, sizeof(line), fp); + fgets(line, sizeof(line), fp); + fgets(line, sizeof(line), fp); + + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + unsigned char pixel = fgetc(fp); + A[i][j] = (double)pixel / 255.0; + } + } + } + fclose(fp); +} + + +void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]) +{ + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + vector[0][i * n_cols + j] = matrix[i][j]; + } + } +} + + +void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]) +{ + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + matrix[i][j] = vector[0][i * n_cols + j]; + } + } +} \ No newline at end of file -- Gitee From f94c74cb9d5b77d6527b780b185529966d320b05 Mon Sep 17 00:00:00 2001 From: K4nade <15828779+k4nade@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 14:14:46 +0800 Subject: [PATCH 07/40] pr file --- c10_group | 1 + first_pr.txt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 160000 c10_group diff --git a/c10_group b/c10_group new file mode 160000 index 0000000..96029d2 --- /dev/null +++ b/c10_group @@ -0,0 +1 @@ +Subproject commit 96029d27eb0428790892072b8420fd5ff883719b diff --git a/first_pr.txt b/first_pr.txt index 13081e4..8943dfc 100644 --- a/first_pr.txt +++ b/first_pr.txt @@ -14,7 +14,6 @@ int main() fgets(input_buffer, sizeof(input_buffer), stdin); input_buffer[strcspn(input_buffer, "\n")] = '\0'; // 移除换行符 - // 处理多个文件名(支持逗号或空格分隔) char *filename = strtok(input_buffer, ", "); int first_file = 1; -- Gitee From b5769ebb0edb867f6dc0b9ead6f87fb65bee1e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 07:08:08 +0000 Subject: [PATCH 08/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20lhx.c=20?= =?UTF-8?q?=E4=B8=BA=20LIhaoxuanpart2.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lhx.c => LIhaoxuanpart2.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lhx.c => LIhaoxuanpart2.c (100%) diff --git a/lhx.c b/LIhaoxuanpart2.c similarity index 100% rename from lhx.c rename to LIhaoxuanpart2.c -- Gitee From 3b5ca650fc535ce96ab92979268b0e308a3d95de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 07:08:44 +0000 Subject: [PATCH 09/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20c10=5Fgroup?= =?UTF-8?q?=5Fljy.c=20=E4=B8=BA=20Linjinyipart3.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c10_group_ljy.c => Linjinyipart3.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename c10_group_ljy.c => Linjinyipart3.c (100%) diff --git a/c10_group_ljy.c b/Linjinyipart3.c similarity index 100% rename from c10_group_ljy.c rename to Linjinyipart3.c -- Gitee From 230edf93e6361c5a28095730c5c0ee035b1993da Mon Sep 17 00:00:00 2001 From: billlin <2937505037@qq.com> Date: Tue, 3 Jun 2025 15:32:56 +0800 Subject: [PATCH 10/40] pr file --- recognize_digit.txt | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 recognize_digit.txt diff --git a/recognize_digit.txt b/recognize_digit.txt new file mode 100644 index 0000000..1ad6bf5 --- /dev/null +++ b/recognize_digit.txt @@ -0,0 +1,44 @@ +// 从 .pgm 文件识别数字的函数,使用神经网络 +int recognize_digit(const char* filename) { + // 声明前向传播中涉及的所有矩阵 + float A[ROWS_A][COLS_A]; // 28x28 图像矩阵 + float B[1][ROWS_A * COLS_A]; // 展平图像 (1x784) + float C[ROWS_A * COLS_A][COLS_C]; // 第一权重矩阵 W1 (784x128) + float D[1][COLS_C]; // B × C (1x128) + float E[1][COLS_C]; // 偏置 B1 (1x128) + float F[1][COLS_C]; // D + E (1x128) + float G[1][COLS_C]; // ReLU(F) (1x128) + float W2[COLS_C][COLS_W2]; // 第二权重矩阵 W2 (128x10) + float H[1][COLS_W2]; // G × W2 (1x10) + float B2[1][COLS_W2]; // 偏置 B2 (1x10) + float L[1][COLS_W2]; // H + B2 (1x10) + float S[1][COLS_W2]; // SoftMax(L) (1x10) + float W1_flat[ROWS_A * COLS_A * COLS_C]; // 展平 W1 (784*128 = 100352) + float W2_flat[COLS_C * COLS_W2]; // 展平 W2 (128*10 = 1280) +} + + +void process_filenames(const char* input) { + char* input_copy = strdup(input); // 复制输入以避免修改原始字符串 + char* token = strtok(input_copy, ","); // 按逗号分割 + while (token != NULL) { + // 去除首尾空格 + while (*token == ' ') token++; + char* end = token + strlen(token) - 1; + while (end > token && *end == ' ') *end-- = '\0'; + + // 检查文件名是否以 .pgm 结尾 + if (strstr(token, ".pgm") == NULL || strstr(token, ".pgm") != token + strlen(token) - 4) { + printf("无效文件\n"); + } else { + int digit = recognize_digit(token); // 识别数字 + if (digit == -1) { + printf("无效文件\n"); // 文件无法打开 + } else { + printf("%s: %d\n", token, digit); // 输出文件名和数字 + } + } + token = strtok(NULL, ","); // 获取下一个文件名 + } + free(input_copy); // 释放复制的字符串 +} -- Gitee From 01ce7e471a5f1a048537f31c59436f46da6d022d Mon Sep 17 00:00:00 2001 From: billlin <2937505037@qq.com> Date: Tue, 3 Jun 2025 15:58:50 +0800 Subject: [PATCH 11/40] recognize --- recognize_digit.txt | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/recognize_digit.txt b/recognize_digit.txt index 1ad6bf5..64834d5 100644 --- a/recognize_digit.txt +++ b/recognize_digit.txt @@ -17,7 +17,6 @@ int recognize_digit(const char* filename) { float W2_flat[COLS_C * COLS_W2]; // 展平 W2 (128*10 = 1280) } - void process_filenames(const char* input) { char* input_copy = strdup(input); // 复制输入以避免修改原始字符串 char* token = strtok(input_copy, ","); // 按逗号分割 @@ -27,18 +26,18 @@ void process_filenames(const char* input) { char* end = token + strlen(token) - 1; while (end > token && *end == ' ') *end-- = '\0'; - // 检查文件名是否以 .pgm 结尾 - if (strstr(token, ".pgm") == NULL || strstr(token, ".pgm") != token + strlen(token) - 4) { - printf("无效文件\n"); + // 检查文件名是否以 .pgm 结尾 + if (strstr(token, ".pgm") == NULL || strstr(token, ".pgm") != token + strlen(token) - 4) { + printf("无效文件\n"); + } else { + int digit = recognize_digit(token); // 识别数字 + if (digit == -1) { + printf("无效文件\n"); // 文件无法打开 } else { - int digit = recognize_digit(token); // 识别数字 - if (digit == -1) { - printf("无效文件\n"); // 文件无法打开 - } else { - printf("%s: %d\n", token, digit); // 输出文件名和数字 - } + printf("%s: %d\n", token, digit); // 输出文件名和数字 } - token = strtok(NULL, ","); // 获取下一个文件名 } - free(input_copy); // 释放复制的字符串 + token = strtok(NULL, ","); // 获取下一个文件名 +} +free(input_copy); // 释放复制的字符串 } -- Gitee From 4ecc1c599ddce1708fff6eb0d92a1ea38ba111e4 Mon Sep 17 00:00:00 2001 From: Juicy <15804151+Juicy666@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 16:04:05 +0800 Subject: [PATCH 12/40] pr file 4 --- fourthpart.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 fourthpart.c diff --git a/fourthpart.c b/fourthpart.c new file mode 100644 index 0000000..798b05f --- /dev/null +++ b/fourthpart.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]); +void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]); + +// 7. 激活函数(ReLU) +void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]) +{ + for (int i = 0; i < SIZE; i++) + { + G[0][i] = (F[0][i] > 0) ? F[0][i] : 0.0; + } +} + +// 8. SoftMax函数 +void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]) +{ + double max_val = L[0][0]; + for (int j = 0; j < cols; j++) + { + if (L[0][j] > max_val) + max_val = L[0][j]; + } + + double sum = 0.0; + for (int j = 0; j < cols; j++) + { + S[0][j] = exp(L[0][j] - max_val); + sum += S[0][j]; + } + + for (int j = 0; j < cols; j++) + { + S[0][j] /= sum; + } +} -- Gitee From c967c6ba424681d69de08ac3fc39c57b152c5127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:08:03 +0000 Subject: [PATCH 13/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20first=5Fpr.?= =?UTF-8?q?txt=20=E4=B8=BA=20part4--luosirui.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- first_pr.txt => part4--luosirui.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename first_pr.txt => part4--luosirui.txt (100%) diff --git a/first_pr.txt b/part4--luosirui.txt similarity index 100% rename from first_pr.txt rename to part4--luosirui.txt -- Gitee From 0cedc1d948a2ec830bcd569b06d18061493f4afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:08:26 +0000 Subject: [PATCH 14/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20part4--luos?= =?UTF-8?q?irui.txt=20=E4=B8=BA=20main=20part--luosirui.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- part4--luosirui.txt => main part--luosirui.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename part4--luosirui.txt => main part--luosirui.txt (100%) diff --git a/part4--luosirui.txt b/main part--luosirui.txt similarity index 100% rename from part4--luosirui.txt rename to main part--luosirui.txt -- Gitee From 21da83a40534c9afb4db584a21672ab8f051dd75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:08:55 +0000 Subject: [PATCH 15/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20fourthpart.?= =?UTF-8?q?c=20=E4=B8=BA=20part4--Zhengrenheng.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fourthpart.c => part4--Zhengrenheng.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename fourthpart.c => part4--Zhengrenheng.c (100%) diff --git a/fourthpart.c b/part4--Zhengrenheng.c similarity index 100% rename from fourthpart.c rename to part4--Zhengrenheng.c -- Gitee From cf9401c78a5ec558e6b93ebc94674fd19952054b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:09:15 +0000 Subject: [PATCH 16/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20Linjinyipar?= =?UTF-8?q?t3.c=20=E4=B8=BA=20part3--Linjinyi.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Linjinyipart3.c => part3--Linjinyi.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Linjinyipart3.c => part3--Linjinyi.c (100%) diff --git a/Linjinyipart3.c b/part3--Linjinyi.c similarity index 100% rename from Linjinyipart3.c rename to part3--Linjinyi.c -- Gitee From fcfe4476030ac12a17718eb8b875bf48bfef172b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:09:32 +0000 Subject: [PATCH 17/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20LIhaoxuanpa?= =?UTF-8?q?rt2.c=20=E4=B8=BA=20part2--Lihaoxuan.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LIhaoxuanpart2.c => part2--Lihaoxuan.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename LIhaoxuanpart2.c => part2--Lihaoxuan.c (100%) diff --git a/LIhaoxuanpart2.c b/part2--Lihaoxuan.c similarity index 100% rename from LIhaoxuanpart2.c rename to part2--Lihaoxuan.c -- Gitee From 17d1a2facfeffd3d4e2999a7488c5625f0222e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:10:41 +0000 Subject: [PATCH 18/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20recognize?= =?UTF-8?q?=5Fdigit.txt=20=E4=B8=BA=20part5--Linxizhe.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- recognize_digit.txt => part5--Linxizhe.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename recognize_digit.txt => part5--Linxizhe.txt (100%) diff --git a/recognize_digit.txt b/part5--Linxizhe.txt similarity index 100% rename from recognize_digit.txt rename to part5--Linxizhe.txt -- Gitee From fa82be90ee100cbd7f9895a181eae39fcee5dcc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:27:56 +0000 Subject: [PATCH 19/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20main=20part?= =?UTF-8?q?--luosirui.txt=20=E4=B8=BA=20main=20part--luosirui.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main part--luosirui.txt => main part--luosirui.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main part--luosirui.txt => main part--luosirui.c (100%) diff --git a/main part--luosirui.txt b/main part--luosirui.c similarity index 100% rename from main part--luosirui.txt rename to main part--luosirui.c -- Gitee From 720e094ba3bf0fbe0a2cd7070234e77ef1084298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 08:28:08 +0000 Subject: [PATCH 20/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20part5--Linx?= =?UTF-8?q?izhe.txt=20=E4=B8=BA=20part5--Linxizhe.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- part5--Linxizhe.txt => part5--Linxizhe.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename part5--Linxizhe.txt => part5--Linxizhe.c (100%) diff --git a/part5--Linxizhe.txt b/part5--Linxizhe.c similarity index 100% rename from part5--Linxizhe.txt rename to part5--Linxizhe.c -- Gitee From a19620ee54f2ba34a64f541557fcc844c2def332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:14:31 +0000 Subject: [PATCH 21/40] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20Final?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Final/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Final/.keep diff --git a/Final/.keep b/Final/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From f684170bb699801b6ad62de942beab9019ef444a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:15:47 +0000 Subject: [PATCH 22/40] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20Files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Files/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Files/.keep diff --git a/Files/.keep b/Files/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From ac8a4548b21001264714fb075d63dbc53e1cf414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:16:48 +0000 Subject: [PATCH 23/40] rename part5--Linxizhe.c to Files. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- part5--Linxizhe.c => Files | 0 Files/.keep | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename part5--Linxizhe.c => Files (100%) delete mode 100644 Files/.keep diff --git a/part5--Linxizhe.c b/Files similarity index 100% rename from part5--Linxizhe.c rename to Files diff --git a/Files/.keep b/Files/.keep deleted file mode 100644 index e69de29..0000000 -- Gitee From 823fe1caf727b8c1a9547587c4c0af65016874c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:17:43 +0000 Subject: [PATCH 24/40] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20Files=20?= =?UTF-8?q?=E4=B8=BA=20part5--Linxizhe.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Files => part5--Linxizhe.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Files => part5--Linxizhe.c (100%) diff --git a/Files b/part5--Linxizhe.c similarity index 100% rename from Files rename to part5--Linxizhe.c -- Gitee From 5cb618a8f938fb557d9f49d24e3b320b90fa59e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:17:55 +0000 Subject: [PATCH 25/40] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20Files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Files/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Files/.keep diff --git a/Files/.keep b/Files/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From b66940ae91ba275ecf8ea0ff170f7ee6960feef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:26:31 +0000 Subject: [PATCH 26/40] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Fi?= =?UTF-8?q?les/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Files/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Files/.keep diff --git a/Files/.keep b/Files/.keep deleted file mode 100644 index e69de29..0000000 -- Gitee From c82cd8ee329cf6ad18affabc5fef60049d2a9d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:28:24 +0000 Subject: [PATCH 27/40] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20Files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Files/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Files/.keep diff --git a/Files/.keep b/Files/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From e1149af124b7edf60ac9700da0a8fd22f4f78bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:28:54 +0000 Subject: [PATCH 28/40] rename main part--luosirui.c to Files/main part--luosirui.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- main part--luosirui.c => Files/main part--luosirui.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main part--luosirui.c => Files/main part--luosirui.c (100%) diff --git a/main part--luosirui.c b/Files/main part--luosirui.c similarity index 100% rename from main part--luosirui.c rename to Files/main part--luosirui.c -- Gitee From 4a14e134172fbf6e6294db8a37c8cb6d36352b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:29:34 +0000 Subject: [PATCH 29/40] rename part2--Lihaoxuan.c to Files/part2--Lihaoxuan.c. --- part2--Lihaoxuan.c => Files/part2--Lihaoxuan.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename part2--Lihaoxuan.c => Files/part2--Lihaoxuan.c (100%) diff --git a/part2--Lihaoxuan.c b/Files/part2--Lihaoxuan.c similarity index 100% rename from part2--Lihaoxuan.c rename to Files/part2--Lihaoxuan.c -- Gitee From 7cfd1cd4427b77c14fdaffb7abaea84d70e5f43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:29:56 +0000 Subject: [PATCH 30/40] rename part3--Linjinyi.c to Files/part3--Linjinyi.c. --- part3--Linjinyi.c => Files/part3--Linjinyi.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename part3--Linjinyi.c => Files/part3--Linjinyi.c (100%) diff --git a/part3--Linjinyi.c b/Files/part3--Linjinyi.c similarity index 100% rename from part3--Linjinyi.c rename to Files/part3--Linjinyi.c -- Gitee From 86f4f1a3d60128ea89b78c7b44d260a38f9a1672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:30:22 +0000 Subject: [PATCH 31/40] rename part4--Zhengrenheng.c to Files/part4--Zhengrenheng.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- part4--Zhengrenheng.c => Files/part4--Zhengrenheng.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename part4--Zhengrenheng.c => Files/part4--Zhengrenheng.c (100%) diff --git a/part4--Zhengrenheng.c b/Files/part4--Zhengrenheng.c similarity index 100% rename from part4--Zhengrenheng.c rename to Files/part4--Zhengrenheng.c -- Gitee From 88af9207aabdc90e5927339fff157ef5f269ff09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:30:42 +0000 Subject: [PATCH 32/40] rename part5--Linxizhe.c to Files/part5--Linxizhe.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- part5--Linxizhe.c => Files/part5--Linxizhe.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename part5--Linxizhe.c => Files/part5--Linxizhe.c (100%) diff --git a/part5--Linxizhe.c b/Files/part5--Linxizhe.c similarity index 100% rename from part5--Linxizhe.c rename to Files/part5--Linxizhe.c -- Gitee From d914dc300b3cc3447568bbca7d051fd5a4f4f3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:31:08 +0000 Subject: [PATCH 33/40] update Final/.keep. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- Final/{.keep => ma} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Final/{.keep => ma} (100%) diff --git a/Final/.keep b/Final/ma similarity index 100% rename from Final/.keep rename to Final/ma -- Gitee From 2b12174ae6a038b58c1a20889311d2394efa986f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:31:41 +0000 Subject: [PATCH 34/40] rename Final/ma to Final/matrix.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- Final/ma | 0 Final/matrix.h | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+) delete mode 100644 Final/ma create mode 100644 Final/matrix.h diff --git a/Final/ma b/Final/ma deleted file mode 100644 index e69de29..0000000 diff --git a/Final/matrix.h b/Final/matrix.h new file mode 100644 index 0000000..4b16c44 --- /dev/null +++ b/Final/matrix.h @@ -0,0 +1,18 @@ +#ifndef MATRIX_H +#define MATRIX_H +#define ROWS_A 28 +#define COLS_A 28 +#define COLS_C 128 +#define COLS_W2 10 + +void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]); +void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]); +void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]); +void read_txt_data(const char *filename, const int n, double array[1][n]); +void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]); +void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]); +void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]); +void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]); +int recognize_digit(const char *filename); +void process_filenames(const char *input); +#endif \ No newline at end of file -- Gitee From 0f212b0332ee65e322d9e92e33094a4ea072d8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:35:45 +0000 Subject: [PATCH 35/40] add Final/matrix.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- Final/matrix.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Final/matrix.c diff --git a/Final/matrix.c b/Final/matrix.c new file mode 100644 index 0000000..18d99b3 --- /dev/null +++ b/Final/matrix.c @@ -0,0 +1,85 @@ +#include "matrix.h" +#include +#include +#include +#include + +// 从 part2--Lihaoxuan.c +void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]) { + FILE *fp = fopen(file_path, "rb"); + if (!fp) { printf("invalid file.\n"); return; } + char magic_number[3]; fgets(magic_number, sizeof(magic_number), fp); + if (strcmp(magic_number, "P5\n") == 0) { + char line[256]; while (fgets(line, sizeof(line), fp) != NULL && line[0] == '#') {} + int width, height, max_value; fscanf(fp, "%d %d %d", &width, &height, &max_value); + for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) A[i][j] = (double)fgetc(fp) / (double)max_value; + } else { + char line[256]; fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); + for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) A[i][j] = (double)fgetc(fp) / 255.0; + } + fclose(fp); +} + +void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]) { + for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) vector[0][i * n_cols + j] = matrix[i][j]; +} + +void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]) { + for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) matrix[i][j] = vector[0][i * n_cols + j]; +} + +// 从 part3--Linjinyi.c +void read_txt_data(const char *filename, const int n, double array[1][n]) { + FILE *fp = fopen(filename, "r"); if (!fp) { printf("Error reading file %s\n", filename); return; } + for (int i = 0; i < n; i++) fscanf(fp, "%lf", &array[0][i]); fclose(fp); +} + +void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]) { + for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) result[i][j] = mat1[i][j] + mat2[i][j]; +} + +void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]) { + for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { result[i][j] = 0.0; for (int k = 0; k < p; k++) result[i][j] += mat1[i][k] * mat2[k][j]; } +} + +// 从 part4--Zhengrenheng.c +void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]) { + for (int i = 0; i < SIZE; i++) G[0][i] = (F[0][i] > 0) ? F[0][i] : 0.0; +} + +void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]) { + double max_val = L[0][0]; for (int j = 0; j < cols; j++) if (L[0][j] > max_val) max_val = L[0][j]; + double sum = 0.0; for (int j = 0; j < cols; j++) { S[0][j] = exp(L[0][j] - max_val); sum += S[0][j]; } + for (int j = 0; j < cols; j++) S[0][j] /= sum; +} + +// 从 part5--Linxizhe.c,补全 recognize_digit +int recognize_digit(const char *filename) { + float A[ROWS_A][COLS_A], B[1][ROWS_A * COLS_A], C[ROWS_A * COLS_A][COLS_C], D[1][COLS_C], E[1][COLS_C], F[1][COLS_C], G[1][COLS_C]; + float W2[COLS_C][COLS_W2], H[1][COLS_W2], B2[1][COLS_W2], L[1][COLS_W2], S[1][COLS_W2]; + float W1_flat[ROWS_A * COLS_A * COLS_C], W2_flat[COLS_C * COLS_W2]; + + pgm_to_matrix(filename, ROWS_A, COLS_A, A); + matrix_to_vector(ROWS_A, COLS_A, A, B); + read_txt_data("W1.txt", ROWS_A * COLS_A * COLS_C, (double(*)[ROWS_A * COLS_A * COLS_C])W1_flat); + vector_to_matrix(ROWS_A * COLS_A, COLS_C, (double(*)[ROWS_A * COLS_A * COLS_C])W1_flat, C); + matrix_mul(1, COLS_C, ROWS_A * COLS_A, B, C, D); + read_txt_data("B1.txt", COLS_C, (double(*)[COLS_C])E); + add_matrices(1, COLS_C, D, E, F); + active_function(COLS_C, F, G); + read_txt_data("W2.txt", COLS_C * COLS_W2, (double(*)[COLS_C * COLS_W2])W2_flat); + vector_to_matrix(COLS_C, COLS_W2, (double(*)[COLS_C * COLS_W2])W2_flat, W2); + matrix_mul(1, COLS_W2, COLS_C, G, W2, H); + read_txt_data("B2.txt", COLS_W2, (double(*)[COLS_W2])B2); + add_matrices(1, COLS_W2, H, B2, L); + soft_max(1, COLS_W2, L, S); + int max_index = 0; for (int i = 1; i < COLS_W2; i++) if (S[0][i] > S[0][max_index]) max_index = i; + return max_index; +} + +void process_filenames(const char *input) { + char *input_copy = strdup(input); char *token = strtok(input_copy, ","); + while (token != NULL) { while (*token == ' ') token++; char *end = token + strlen(token) - 1; while (end > token && *end == ' ') *end-- = '\0'; + if (strstr(token, ".pgm") == NULL || strstr(token, ".pgm") != token + strlen(token) - 4) printf("无效文件\n"); + else { int digit = recognize_digit(token); if (digit == -1) printf("无效文件\n"); else printf("%s: %d\n", token, digit); } + token = strtok(NULL, ","); } free(input_copy); } \ No newline at end of file -- Gitee From 015dfc27a26e1fdd9754257b2b04176fdf8f1370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:36:51 +0000 Subject: [PATCH 36/40] add Final/main.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- Final/main.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Final/main.c diff --git a/Final/main.c b/Final/main.c new file mode 100644 index 0000000..2cb8d1d --- /dev/null +++ b/Final/main.c @@ -0,0 +1,19 @@ +#include "matrix.h" +#include +#include + +int main() { + char continue_flag = 'Y'; + char input_buffer[1024]; + + while (continue_flag == 'Y' || continue_flag == 'y') { + printf("please input filenames: "); + fgets(input_buffer, sizeof(input_buffer), stdin); + input_buffer[strcspn(input_buffer, "\n")] = '\0'; + process_filenames(input_buffer); + printf("do you want to continue? please input [Y or N]: "); + scanf(" %c", &continue_flag); getchar(); + } + printf("Bye\n"); + return 0; +} \ No newline at end of file -- Gitee From 4a0023a5c3c818cae11e10df5cb5ccfa83a8037a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Tue, 3 Jun 2025 09:38:07 +0000 Subject: [PATCH 37/40] update Final/matrix.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- Final/matrix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Final/matrix.c b/Final/matrix.c index 18d99b3..25f8007 100644 --- a/Final/matrix.c +++ b/Final/matrix.c @@ -53,7 +53,7 @@ void soft_max(const int rows, const int cols, const double L[rows][cols], double for (int j = 0; j < cols; j++) S[0][j] /= sum; } -// 从 part5--Linxizhe.c,补全 recognize_digit +// 从 part5--Linxizhe.c, int recognize_digit(const char *filename) { float A[ROWS_A][COLS_A], B[1][ROWS_A * COLS_A], C[ROWS_A * COLS_A][COLS_C], D[1][COLS_C], E[1][COLS_C], F[1][COLS_C], G[1][COLS_C]; float W2[COLS_C][COLS_W2], H[1][COLS_W2], B2[1][COLS_W2], L[1][COLS_W2], S[1][COLS_W2]; -- Gitee From 685c9169dbd023bbaf7b1edde4b94435625a18d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Wed, 4 Jun 2025 10:16:40 +0000 Subject: [PATCH 38/40] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Fi?= =?UTF-8?q?nal/matrix.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Final/matrix.c | 85 -------------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 Final/matrix.c diff --git a/Final/matrix.c b/Final/matrix.c deleted file mode 100644 index 25f8007..0000000 --- a/Final/matrix.c +++ /dev/null @@ -1,85 +0,0 @@ -#include "matrix.h" -#include -#include -#include -#include - -// 从 part2--Lihaoxuan.c -void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]) { - FILE *fp = fopen(file_path, "rb"); - if (!fp) { printf("invalid file.\n"); return; } - char magic_number[3]; fgets(magic_number, sizeof(magic_number), fp); - if (strcmp(magic_number, "P5\n") == 0) { - char line[256]; while (fgets(line, sizeof(line), fp) != NULL && line[0] == '#') {} - int width, height, max_value; fscanf(fp, "%d %d %d", &width, &height, &max_value); - for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) A[i][j] = (double)fgetc(fp) / (double)max_value; - } else { - char line[256]; fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); - for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) A[i][j] = (double)fgetc(fp) / 255.0; - } - fclose(fp); -} - -void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]) { - for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) vector[0][i * n_cols + j] = matrix[i][j]; -} - -void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]) { - for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) matrix[i][j] = vector[0][i * n_cols + j]; -} - -// 从 part3--Linjinyi.c -void read_txt_data(const char *filename, const int n, double array[1][n]) { - FILE *fp = fopen(filename, "r"); if (!fp) { printf("Error reading file %s\n", filename); return; } - for (int i = 0; i < n; i++) fscanf(fp, "%lf", &array[0][i]); fclose(fp); -} - -void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]) { - for (int i = 0; i < n_rows; i++) for (int j = 0; j < n_cols; j++) result[i][j] = mat1[i][j] + mat2[i][j]; -} - -void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]) { - for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { result[i][j] = 0.0; for (int k = 0; k < p; k++) result[i][j] += mat1[i][k] * mat2[k][j]; } -} - -// 从 part4--Zhengrenheng.c -void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]) { - for (int i = 0; i < SIZE; i++) G[0][i] = (F[0][i] > 0) ? F[0][i] : 0.0; -} - -void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]) { - double max_val = L[0][0]; for (int j = 0; j < cols; j++) if (L[0][j] > max_val) max_val = L[0][j]; - double sum = 0.0; for (int j = 0; j < cols; j++) { S[0][j] = exp(L[0][j] - max_val); sum += S[0][j]; } - for (int j = 0; j < cols; j++) S[0][j] /= sum; -} - -// 从 part5--Linxizhe.c, -int recognize_digit(const char *filename) { - float A[ROWS_A][COLS_A], B[1][ROWS_A * COLS_A], C[ROWS_A * COLS_A][COLS_C], D[1][COLS_C], E[1][COLS_C], F[1][COLS_C], G[1][COLS_C]; - float W2[COLS_C][COLS_W2], H[1][COLS_W2], B2[1][COLS_W2], L[1][COLS_W2], S[1][COLS_W2]; - float W1_flat[ROWS_A * COLS_A * COLS_C], W2_flat[COLS_C * COLS_W2]; - - pgm_to_matrix(filename, ROWS_A, COLS_A, A); - matrix_to_vector(ROWS_A, COLS_A, A, B); - read_txt_data("W1.txt", ROWS_A * COLS_A * COLS_C, (double(*)[ROWS_A * COLS_A * COLS_C])W1_flat); - vector_to_matrix(ROWS_A * COLS_A, COLS_C, (double(*)[ROWS_A * COLS_A * COLS_C])W1_flat, C); - matrix_mul(1, COLS_C, ROWS_A * COLS_A, B, C, D); - read_txt_data("B1.txt", COLS_C, (double(*)[COLS_C])E); - add_matrices(1, COLS_C, D, E, F); - active_function(COLS_C, F, G); - read_txt_data("W2.txt", COLS_C * COLS_W2, (double(*)[COLS_C * COLS_W2])W2_flat); - vector_to_matrix(COLS_C, COLS_W2, (double(*)[COLS_C * COLS_W2])W2_flat, W2); - matrix_mul(1, COLS_W2, COLS_C, G, W2, H); - read_txt_data("B2.txt", COLS_W2, (double(*)[COLS_W2])B2); - add_matrices(1, COLS_W2, H, B2, L); - soft_max(1, COLS_W2, L, S); - int max_index = 0; for (int i = 1; i < COLS_W2; i++) if (S[0][i] > S[0][max_index]) max_index = i; - return max_index; -} - -void process_filenames(const char *input) { - char *input_copy = strdup(input); char *token = strtok(input_copy, ","); - while (token != NULL) { while (*token == ' ') token++; char *end = token + strlen(token) - 1; while (end > token && *end == ' ') *end-- = '\0'; - if (strstr(token, ".pgm") == NULL || strstr(token, ".pgm") != token + strlen(token) - 4) printf("无效文件\n"); - else { int digit = recognize_digit(token); if (digit == -1) printf("无效文件\n"); else printf("%s: %d\n", token, digit); } - token = strtok(NULL, ","); } free(input_copy); } \ No newline at end of file -- Gitee From 664aad120f3b3efb953acf6364f05f09ce44f4eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Wed, 4 Jun 2025 10:16:48 +0000 Subject: [PATCH 39/40] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Fi?= =?UTF-8?q?nal/matrix.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Final/matrix.h | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 Final/matrix.h diff --git a/Final/matrix.h b/Final/matrix.h deleted file mode 100644 index 4b16c44..0000000 --- a/Final/matrix.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MATRIX_H -#define MATRIX_H -#define ROWS_A 28 -#define COLS_A 28 -#define COLS_C 128 -#define COLS_W2 10 - -void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]); -void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]); -void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]); -void read_txt_data(const char *filename, const int n, double array[1][n]); -void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]); -void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]); -void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]); -void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]); -int recognize_digit(const char *filename); -void process_filenames(const char *input); -#endif \ No newline at end of file -- Gitee From 4678e07461d1b8242ed5438cf4518ca4355d619d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8A=E8=BD=A9?= <15817088+li-hao-xuan846@user.noreply.gitee.com> Date: Wed, 4 Jun 2025 10:18:08 +0000 Subject: [PATCH 40/40] update Final/main.c. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李昊轩 <15817088+li-hao-xuan846@user.noreply.gitee.com> --- Final/main.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 268 insertions(+), 6 deletions(-) diff --git a/Final/main.c b/Final/main.c index 2cb8d1d..80e9b20 100644 --- a/Final/main.c +++ b/Final/main.c @@ -1,19 +1,281 @@ -#include "matrix.h" #include +#include #include +#include -int main() { +// Phase I: 子函数声明 +void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]); +void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]); +void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]); +void read_txt_data(const char *filename, const int n, double array[1][n]); +void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]); +void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]); +void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]); +void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]); + +// Phase II: 识别流程函数 +void recognize_digit(const char *file_path, double *result_digit); + +int main() +{ char continue_flag = 'Y'; char input_buffer[1024]; - while (continue_flag == 'Y' || continue_flag == 'y') { + while (continue_flag == 'Y' || continue_flag == 'y') + { printf("please input filenames: "); fgets(input_buffer, sizeof(input_buffer), stdin); - input_buffer[strcspn(input_buffer, "\n")] = '\0'; - process_filenames(input_buffer); + input_buffer[strcspn(input_buffer, "\n")] = '\0'; // 移除换行符 + + // 处理多个文件名(支持逗号或空格分隔) + char *filename = strtok(input_buffer, ", "); + int first_file = 1; + + while (filename != NULL) + { + double digit; + pgm_to_matrix(filename, 28, 28, (double[28][28]){0}); // 初始化矩阵 + + // 调用识别函数 + recognize_digit(filename, &digit); + + // 输出结果 + if (first_file) + { + printf("%s: %.0f", filename, digit); + first_file = 0; + } + else + { + printf(", %s: %.0f", filename, digit); + } + + filename = strtok(NULL, ", "); + } + printf("\n"); + + // 检查是否继续 printf("do you want to continue? please input [Y or N]: "); - scanf(" %c", &continue_flag); getchar(); + scanf(" %c", &continue_flag); + getchar(); // 消耗缓冲区残留字符 } + printf("Bye\n"); return 0; +} + +// Phase I: 子函数实现 + +// 1. 读取PGM文件并转换为矩阵(支持P2和P5格式) +void pgm_to_matrix(const char *file_path, const int n_rows, const int n_cols, double A[n_rows][n_cols]) +{ + FILE *fp = fopen(file_path, "rb"); + if (!fp) + { + printf("invalid file.\n"); + return; + } + + char magic_number[3]; + fgets(magic_number, sizeof(magic_number), fp); + // 判断是否为P5格式 + if (strcmp(magic_number, "P5\n") == 0) + { + // 跳过注释行 + char line[256]; + while (fgets(line, sizeof(line), fp) != NULL && line[0] == '#') + { + } + // 读取图像宽度、高度和最大像素值 + int width, height, max_value; + fscanf(fp, "%d %d %d", &width, &height, &max_value); + // 读取像素数据并归一化 + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + unsigned char pixel = fgetc(fp); + A[i][j] = (double)pixel / (double)max_value; + } + } + } + else + { + // 跳过前四行文件头(P2格式处理逻辑) + char line[256]; + fgets(line, sizeof(line), fp); + fgets(line, sizeof(line), fp); + fgets(line, sizeof(line), fp); + fgets(line, sizeof(line), fp); + // 读取像素数据并归一化 + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + unsigned char pixel = fgetc(fp); + A[i][j] = (double)pixel / 255.0; + } + } + } + fclose(fp); +} + +// 2. 矩阵展平为向量 +void matrix_to_vector(const int n_rows, const int n_cols, const double matrix[n_rows][n_cols], double vector[1][n_rows * n_cols]) +{ + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + vector[0][i * n_cols + j] = matrix[i][j]; + } + } +} + +// 3. 向量转换为矩阵(反向操作) +void vector_to_matrix(const int n_rows, const int n_cols, const double vector[1][n_rows * n_cols], double matrix[n_rows][n_cols]) +{ + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + matrix[i][j] = vector[0][i * n_cols + j]; + } + } +} + +// 4. 读取TXT文件数据 +void read_txt_data(const char *filename, const int n, double array[1][n]) +{ + FILE *fp = fopen(filename, "r"); + if (!fp) + { + printf("Error reading file %s\n", filename); + return; + } + + for (int i = 0; i < n; i++) + { + fscanf(fp, "%lf", &array[0][i]); + } + fclose(fp); +} + +// 5. 矩阵加法 +void add_matrices(const int n_rows, const int n_cols, const double mat1[n_rows][n_cols], const double mat2[n_rows][n_cols], double result[n_rows][n_cols]) +{ + for (int i = 0; i < n_rows; i++) + { + for (int j = 0; j < n_cols; j++) + { + result[i][j] = mat1[i][j] + mat2[i][j]; + } + } +} + +// 6. 矩阵乘法 +void matrix_mul(const int m, const int n, const int p, const double mat1[m][p], const double mat2[p][n], double result[m][n]) +{ + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + result[i][j] = 0.0; + for (int k = 0; k < p; k++) + { + result[i][j] += mat1[i][k] * mat2[k][j]; + } + } + } +} + +// 7. 激活函数(ReLU) +void active_function(const int SIZE, const double F[1][SIZE], double G[1][SIZE]) +{ + for (int i = 0; i < SIZE; i++) + { + G[0][i] = (F[0][i] > 0) ? F[0][i] : 0.0; + } +} + +// 8. SoftMax函数 +void soft_max(const int rows, const int cols, const double L[rows][cols], double S[rows][cols]) +{ + double max_val = L[0][0]; + for (int j = 0; j < cols; j++) + { + if (L[0][j] > max_val) + max_val = L[0][j]; + } + + double sum = 0.0; + for (int j = 0; j < cols; j++) + { + S[0][j] = exp(L[0][j] - max_val); + sum += S[0][j]; + } + + for (int j = 0; j < cols; j++) + { + S[0][j] /= sum; + } +} + +// Phase II: 识别流程实现 +void recognize_digit(const char *file_path, double *result_digit) +{ + double A[28][28] = {0}; + double B[1][784] = {0}; + double C[784][128] = {0}; // W1矩阵 + double D[1][128] = {0}; + double E[1][128] = {0}; // B1偏置 + double F[1][128] = {0}; + double G[1][128] = {0}; + + double W1[1][784 * 128] = {0}; + double W2[1][128 * 10] = {0}; + double B1[1][128] = {0}; + double B2[1][10] = {0}; + + double H[1][10] = {0}; + double L[1][10] = {0}; + double S[1][10] = {0}; + + // 1. 读取PGM文件并展平为向量B + pgm_to_matrix(file_path, 28, 28, A); + matrix_to_vector(28, 28, A, B); + + // 2. 加载第一层权重W1(784x128) + read_txt_data("input_files/W1.txt", 784 * 128, W1); + vector_to_matrix(784, 128, W1, C); + matrix_mul(1, 128, 784, B, C, D); + + // 3. 加载第一层偏置B1并相加 + read_txt_data("input_files/B1.txt", 128, B1); + add_matrices(1, 128, D, B1, F); + + // 4. 激活函数 + active_function(128, F, G); + + // 5. 加载第二层权重W2(128x10) + read_txt_data("input_files/W2.txt", 128 * 10, W2); + double W2_matrix[128][10]; + vector_to_matrix(128, 10, W2, W2_matrix); + matrix_mul(1, 10, 128, G, W2_matrix, H); + + // 6. 加载第二层偏置B2并相加 + read_txt_data("input_files/B2.txt", 10, B2); + add_matrices(1, 10, H, B2, L); + + // 7. SoftMax计算 + soft_max(1, 10, L, S); + + // 8. 找出最大概率的索引 + int max_index = 0; + for (int j = 1; j < 10; j++) + { + if (S[0][j] > S[0][max_index]) + max_index = j; + } + *result_digit = (double)max_index; } \ No newline at end of file -- Gitee