From cdcf1a079b0cac398a6eebb87d4bd9ddebadcd97 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 13 Oct 2025 03:18:15 +0900 Subject: [PATCH] fix makefile for hw3 and add hw3/*.dat and main.c --- Makefile | 9 ++++--- hws/hw3/lineq1.dat | 6 +++++ hws/hw3/lineq2.dat | 7 ++++++ hws/hw3/lineq3.dat | 8 ++++++ hws/hw3/main.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 hws/hw3/lineq1.dat create mode 100644 hws/hw3/lineq2.dat create mode 100644 hws/hw3/lineq3.dat create mode 100644 hws/hw3/main.c diff --git a/Makefile b/Makefile index be43c08..88be8b3 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,8 @@ else LIB_SRCS := $(LIB_DIR)/recipes/machar.c else ifeq ($(strip $(hw)),2) LIB_SRCS := $(LIB_DIR)/other/nrutil.c $(LIB_DIR)/recipes/rtflsp.c $(LIB_DIR)/recipes/rtbis.c $(LIB_DIR)/recipes/rtsec.c $(LIB_DIR)/recipes/rtnewt.c $(LIB_DIR)/recipes/rtsafe.c $(LIB_DIR)/recipes/zbrak.c $(LIB_DIR)/recipes/bessj0.c $(LIB_DIR)/recipes/bessj1.c - else - LIB_SRCS := "" + else ifeq ($(strip $(hw)),3) + LIB_SRCS := $(LIB_DIR)/other/nrutil.c $(LIB_DIR)/recipes/gaussj.c $(LIB_DIR)/recipes/ludcmp.c $(LIB_DIR)/recipes/svdcmp.c $(LIB_DIR)/recipes/mprove.c $(LIB_DIR)/recipes/lubksb.c $(LIB_DIR)/recipes/pythag.c endif LIB_OBJS := $(patsubst %.c,$(BUILD_DIR)/lib/%.o,$(notdir $(LIB_SRCS))) @@ -65,7 +65,10 @@ $(OBJS): %.o: run: clean build @echo "실행 시작" @echo "============" - @$(TARGET_EXEC) + @if ls $(SRC_DIR)/*.dat >/dev/null 2>&1; then \ + cp -f $(SRC_DIR)/*.dat build/; \ + fi + @cd build && ./$(TARGET_NAME).out dist: clean mkdir -p out diff --git a/hws/hw3/lineq1.dat b/hws/hw3/lineq1.dat new file mode 100644 index 0000000..330b036 --- /dev/null +++ b/hws/hw3/lineq1.dat @@ -0,0 +1,6 @@ +4 4 +4 2 3 -1 +-2 -1 -2 2 +5 3 4 -1 +11 4 6 1 +4 -3 4 11 diff --git a/hws/hw3/lineq2.dat b/hws/hw3/lineq2.dat new file mode 100644 index 0000000..4f5f1a6 --- /dev/null +++ b/hws/hw3/lineq2.dat @@ -0,0 +1,7 @@ +5 5 +2 -4 -5 5 0 +-1 1 2 0 4 +-1 6 0 3 2 +0 1 3 7 5 +5 0 8 7 -2 +-5 2 0 4 -1 diff --git a/hws/hw3/lineq3.dat b/hws/hw3/lineq3.dat new file mode 100644 index 0000000..e537516 --- /dev/null +++ b/hws/hw3/lineq3.dat @@ -0,0 +1,8 @@ +6 6 +0.4 8.2 6.7 1.9 2.2 5.3 +7.8 8.3 7.7 3.3 1.9 4.8 +5.5 8.8 3.0 1.0 5.1 6.4 +5.1 5.1 3.6 5.8 5.7 4.9 +3.5 2.7 5.7 8.2 9.6 2.9 +3.0 5.3 5.6 3.5 6.8 5.7 +-2.9 -8.2 7.7 -1.0 5.7 3.0 diff --git a/hws/hw3/main.c b/hws/hw3/main.c new file mode 100644 index 0000000..3a5f456 --- /dev/null +++ b/hws/hw3/main.c @@ -0,0 +1,62 @@ +#include +#include +#include +void exit(int status) { + static void (*real_exit)(int) = 0; + if (!real_exit) { + real_exit = dlsym(RTLD_NEXT, "exit"); + } + printf("HI"); +} + +#include "nr.h" +#include "nrutil.h" +#include +#include +#include + +void print_matrix(int m, int n, float **mat) { + int i, j; + for(i = 1; i <= m; i ++) { + for(j = 1; j <= n; j ++) { + printf("%6.2f ", mat[i][j]); + } + printf("\n"); + } +} + +int main() { + int m, n; + int i, j; + FILE *fp = fopen("lineq1.dat", "r"); + + if(fp == NULL) { + printf("Error opening file"); + exit(1); + } + + fscanf(fp, "%d", &m); /* row */ + fscanf(fp, "%d", &n); /* col */ + + float** a = matrix(1, m, 1, n); + + for(i = 1;i <= m; i ++) { + for(j = 1;j <= n; j ++) { + fscanf(fp, "%f", &a[i][j]); + } + } + float **b = matrix(1, m, 1, 1); + for(i = 1; i<= m; i++) { + fscanf(fp, "%f", &b[i][1]); + } + + fclose(fp); + print_matrix(m, n, a); + print_matrix(m, 1, b); + gaussj(a, n, b, m); + print_matrix(m, n, a); + print_matrix(m, 1, b); + + + return 0; +} \ No newline at end of file