diff --git a/assets/cls04-a1.png b/assets/cls04-a1.png new file mode 100644 index 0000000..d7c6d4d Binary files /dev/null and b/assets/cls04-a1.png differ diff --git a/assets/cls04-a2.png b/assets/cls04-a2.png new file mode 100644 index 0000000..ab28d07 Binary files /dev/null and b/assets/cls04-a2.png differ diff --git a/assets/cls04-a3.png b/assets/cls04-a3.png new file mode 100644 index 0000000..d84e88f Binary files /dev/null and b/assets/cls04-a3.png differ diff --git a/assets/cls04-a4.png b/assets/cls04-a4.png new file mode 100644 index 0000000..82bd4ea Binary files /dev/null and b/assets/cls04-a4.png differ diff --git a/out/report-cls04-opt.pdf b/out/report-cls04-opt.pdf new file mode 100644 index 0000000..78cde9e Binary files /dev/null and b/out/report-cls04-opt.pdf differ diff --git a/out/report-cls04.pdf b/out/report-cls04.pdf new file mode 100644 index 0000000..06db785 Binary files /dev/null and b/out/report-cls04.pdf differ diff --git a/out/report-cls04.synctex.gz b/out/report-cls04.synctex.gz new file mode 100644 index 0000000..5d72296 Binary files /dev/null and b/out/report-cls04.synctex.gz differ diff --git a/report-cls04.tex b/report-cls04.tex new file mode 100644 index 0000000..d8aaf55 --- /dev/null +++ b/report-cls04.tex @@ -0,0 +1,98 @@ +\documentclass[xelatex,a4paper,11pt,ja=standard]{bxjsarticle} + +\usepackage{tex/preamble} +\usepackage{tex/simple-title} + +\reportauthor{柴田健琉} +\reporttitle{情報処理2 - 前期第4回課題} +\reportdate{2026年}{05月}{11日} +\turnindate{2026年}{05月}{12日} + +\begin{document} + \simpletitle{} + + \section{はじめに} + + この課題のプログラムは以下の環境での動作が確認されている: + + \begin{itemize} + \item {OS: NixOS 25.11 Xantusia, Linux 7.0.3 x86\_64} + \item {CC: GCC 15.2.0} + \item {CFLAGS: \texttt{-g -O1 -Wall -Wpedantic}} + \item {ファイルエンコーディング: UTF-8} + \end{itemize} + + \section{補助関数郡} + + 今回の課題では前期第3回課題で作成した関数を使用する.再利用性を高めるために\texttt{reduce.h}を作成した. + + \lstinputlisting[language=C,title={\texttt{reduce.h}}]{src/cls04/reduce.h} + + また,分数の入出力等に関する関数を\texttt{utils.h}に記述した. + + \lstinputlisting[language=C,title={\texttt{utils.h}}]{src/cls04/utils.h} + + \newpage + + \section{課題1} + + \texttt{reduce}関数を用いて分数どうしの加算を行うプログラム. + + \lstinputlisting[language=C,title={課題1のプログラム}]{src/cls04/a1.c} + + \subsection{実行結果} + + \begin{figure}[tbh] + \centering + \includegraphics[width=12cm]{assets/cls04-a1.png} + \caption{課題1の実行結果} + \end{figure} + + \newpage + + \section{課題2} + + \texttt{reduce}関数を用いて分数どうしの減算を行うプログラム. + + \lstinputlisting[language=C,title={課題2のプログラム}]{src/cls04/a2.c} + + \subsection{実行結果} + + \begin{figure}[tbh] + \centering + \includegraphics[width=12cm]{assets/cls04-a2.png} + \caption{課題2の実行結果} + \end{figure} + + \newpage + + \section{課題3} + + \texttt{reduce}関数を用いて分数どうしの乗算を行うプログラム. + + \lstinputlisting[language=C,title={課題3のプログラム}]{src/cls04/a3.c} + + \subsection{実行結果} + + \begin{figure}[tbh] + \centering + \includegraphics[width=12cm]{assets/cls04-a3.png} + \caption{課題3の実行結果} + \end{figure} + + \newpage + + \section{課題4} + + 標準入力から自然数を取得する関数\texttt{getnum}を作成する. + + \lstinputlisting[language=C,title={課題4のプログラム}]{src/cls04/a4.c} + + \subsection{実行結果} + + \begin{figure}[tbh] + \centering + \includegraphics[width=12cm]{assets/cls04-a4.png} + \caption{課題4の実行結果} + \end{figure} +\end{document} diff --git a/src/cls04/Makefile b/src/cls04/Makefile new file mode 100644 index 0000000..d7b8557 --- /dev/null +++ b/src/cls04/Makefile @@ -0,0 +1,14 @@ +include ../common.mk + +PROJECT_NAME:=cls04 + +TGTS:=$(patsubst %.c,%,$(wildcard *.c)) + +all: $(TGTS) + +%: %.c + @mkdir -p $(BUILD_PATH)/$(PROJECT_NAME) + $(CC) $(CFLAGS) -I. $^ -o $(BUILD_PATH)/$(PROJECT_NAME)/$@ + +clean: + $(RM) -drf $(BUILD_PATH)/$(PROJECT_NAME) diff --git a/src/cls04/a1.c b/src/cls04/a1.c new file mode 100644 index 0000000..c19f586 --- /dev/null +++ b/src/cls04/a1.c @@ -0,0 +1,42 @@ +#include + +#include "reduce.h" +#include "utils.h" + +int addFrac(int x[], int y[]) { + if (x[1] == 0 || y[1] == 0) { + return -1; + } + + int l = lcm(x[1], y[1]); + int z[] = { + (x[0] * l)/x[1] + (y[0] * l)/y[1], + l + }; + + reduce(z); + formatNeg(z); + + x[0] = z[0]; + x[1] = z[1]; + return 0; +} + +int main(void) { + int x[2] = {0}; + int y[2] = {0}; + + inputFrac(x); + inputFrac(y); + + int ret = addFrac(x, y); + + if (ret == 0) { + printFrac(x); + putchar('\n'); + } else { + fprintf(stderr, "Error: detected zero div\n"); + } + + return 0; +} diff --git a/src/cls04/a2.c b/src/cls04/a2.c new file mode 100644 index 0000000..e0dfc44 --- /dev/null +++ b/src/cls04/a2.c @@ -0,0 +1,43 @@ +#include + +#include "reduce.h" +#include "utils.h" + +int subFrac(int x[], int y[]) { + if (x[1] == 0 || y[1] == 0) { + return -1; + } + + int l = lcm(x[1], y[1]); + int z[] = { + (x[0] * l)/x[1] - (y[0] * l)/y[1], + l + }; + + reduce(z); + formatNeg(z); + + x[0] = z[0]; + x[1] = z[1]; + + return 0; +} + +int main(void) { + int x[2] = {0}; + int y[2] = {0}; + + inputFrac(x); + inputFrac(y); + + int ret = subFrac(x, y); + + if (ret == 0) { + printFrac(x); + putchar('\n'); + } else { + fprintf(stderr, "Error: detected zero div\n"); + } + + return 0; +} diff --git a/src/cls04/a3.c b/src/cls04/a3.c new file mode 100644 index 0000000..80f4f5c --- /dev/null +++ b/src/cls04/a3.c @@ -0,0 +1,42 @@ +#include + +#include "reduce.h" +#include "utils.h" + +int mulFrac(int x[], int y[]) { + if (x[1]*y[1] == 0) { + return -1; + } + + int z[] = { + x[0] * y[0], + x[1] * y[1] + }; + + reduce(z); + formatNeg(z); + + x[0] = z[0]; + x[1] = z[1]; + + return 0; +} + +int main(void) { + int x[2] = {0}; + int y[2] = {0}; + + inputFrac(x); + inputFrac(y); + + int ret = mulFrac(x, y); + + if (ret == 0) { + printFrac(x); + putchar('\n'); + } else { + fprintf(stderr, "Error: Detected zero div\n"); + } + + return 0; +} diff --git a/src/cls04/a4.c b/src/cls04/a4.c new file mode 100644 index 0000000..b366dc2 --- /dev/null +++ b/src/cls04/a4.c @@ -0,0 +1,31 @@ +#include + +int getnum(void) { + int ch, n = 0; + + int stack[10] = {0}; // ceil(log_10(2^31)) + int i = 0; + while ((ch = getchar()) != '\n') { + int x = ch - '0'; + if (x < 0 || x > 9) { + continue; + } + if (i <= 9) stack[i++] = x; // stack push + } + + int d = 1; + for (int j = i - 1; j >= 0; j--) { + n += stack[j] * d; // stack pop + d = d * 10; + } + + return n; +} + +int main(void) { + int ret = getnum(); + + printf("got: %d\n", ret); + + return 0; +} diff --git a/src/cls04/reduce.h b/src/cls04/reduce.h new file mode 100644 index 0000000..bde0cfc --- /dev/null +++ b/src/cls04/reduce.h @@ -0,0 +1,28 @@ +#ifndef __REDUCE_H__ +#define __REDUCE_H__ + +int gcm(int a, int b) { + int x = a, y = b; + if (x < y) { + x = b; + y = a; + } + int r = a % b; + if (r == 0) return b; + return gcm(b, r); +} + +int lcm(int a, int b) { + int g = gcm(a, b); + return b * a / g; +} + +void reduce(int x[]) { + int a = x[0], b = x[1]; + int g = gcm(a, b); + x[0] = a / g; + x[1] = b / g; +} + + +#endif // __REDUCE_H__ diff --git a/src/cls04/utils.h b/src/cls04/utils.h new file mode 100644 index 0000000..ff2d02e --- /dev/null +++ b/src/cls04/utils.h @@ -0,0 +1,31 @@ +#ifndef __UTILS_H__ +#define __UTILS_H__ + +#include + +void inputFrac(int x[]) { + printf("Input fraction(a/b): "); + scanf("%d/%d", &x[0], &x[1]); +} + +void formatNeg(int x[]) { + size_t sz = sizeof(int); + int cnt = 0; + for (size_t i = 0; i < 2; i++) { + cnt += x[i] < 0 ? 1 : 0; + x[i] = x[i] < 0 ? x[i] * -1 : x[i]; + } + + if (cnt == 1) + x[0] = -1 * x[0]; +} + +void printFrac(int x[]) { + if (x[1] == 1) { + printf("%d", x[0]); + } else { + printf("%d/%d", x[0], x[1]); + } +} + +#endif // __UTILS_H__