diff --git a/assets/cls03-a1.png b/assets/cls03-a1.png new file mode 100644 index 0000000..41db3b1 Binary files /dev/null and b/assets/cls03-a1.png differ diff --git a/assets/cls03-a2-4.png b/assets/cls03-a2-4.png new file mode 100644 index 0000000..2c543e7 Binary files /dev/null and b/assets/cls03-a2-4.png differ diff --git a/out/report-cls03.pdf b/out/report-cls03.pdf new file mode 100644 index 0000000..fe2b4db Binary files /dev/null and b/out/report-cls03.pdf differ diff --git a/out/report-cls03.synctex.gz b/out/report-cls03.synctex.gz new file mode 100644 index 0000000..7c622d6 Binary files /dev/null and b/out/report-cls03.synctex.gz differ diff --git a/report-cls03.tex b/report-cls03.tex new file mode 100644 index 0000000..d061b2c --- /dev/null +++ b/report-cls03.tex @@ -0,0 +1,63 @@ +\documentclass[xelatex,a4paper,11pt,ja=standard]{bxjsarticle} + +\usepackage{tex/preamble} +\usepackage{tex/detailed-title} + +\reportauthor{柴田健琉} +\reporttitle{前期第3回課題} +\reportdate{2026年}{04月}{27日} +\turnindate{2026年}{04月}{27日} +\schoolname{岐阜工業高等専門学校} +\department{電子制御工学科} +\subject{情報処理2} +\professor{遠藤 登} +\studentid{2024D14} +\seatingnum{15} + +\begin{document} + \detailedtitle{} + + \section{はじめに} + + この課題のプログラムは以下の環境での動作が確認されている: + + \begin{itemize} + \item{OS: NixOS 25.11 Xantusia, Linux Kernel 7.0.0 x86\_64} + \item{CC: GCC 15.2.0} + \item{CFLAGS: \texttt{-g -O1 -Wall -Wpedantic}} + \end{itemize} + + \section{課題1} + + 再帰関数を用いて1から$n$の自然数の和を求めるプログラム. + + \lstinputlisting[language=C,title={課題1}]{./src/cls03/a1.c} + + \subsection{実行結果} + + \begin{figure}[tbh] + \centering + \includegraphics[width=11cm]{./assets/cls03-a1.png} + \caption{課題1の実行結果} + \end{figure} + + \newpage + + \section{課題2-4} + + 課題2:ユークリッド互除法を用いた最大公約数を求める関数の作成. + + 課題3:課題2の関数を用いて最小公倍数を求める関数の作成. + + 課題4:配列として与えられた分数を約分する関数の作成. + + \lstinputlisting[language=C,title={課題2-4の統合プログラム}]{./src/cls03/a2-4.c} + + \subsection{実行結果} + + \begin{figure}[tbh] + \centering + \includegraphics[width=12cm]{./assets/cls03-a2-4.png} + \caption{実行結果} + \end{figure} +\end{document} diff --git a/src/cls03/Makefile b/src/cls03/Makefile new file mode 100644 index 0000000..a3671e7 --- /dev/null +++ b/src/cls03/Makefile @@ -0,0 +1,14 @@ +include ../common.mk + +PROJECT_NAME:=cls03 + +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/cls03/a1.c b/src/cls03/a1.c new file mode 100644 index 0000000..31926db --- /dev/null +++ b/src/cls03/a1.c @@ -0,0 +1,19 @@ +#include + +unsigned int sum_recc(unsigned int n) { + if (n == 0) return 0; + return n + sum_recc(n - 1); +} + +int main(void) { + unsigned int x = 0; + + printf("Input natural number: "); + (void)scanf("%u", &x); + + unsigned int s = sum_recc(x); + + printf("Result: %u\n", s); + + return 0; +} diff --git a/src/cls03/a2-4.c b/src/cls03/a2-4.c new file mode 100644 index 0000000..20e0602 --- /dev/null +++ b/src/cls03/a2-4.c @@ -0,0 +1,58 @@ +#include + +void getTwoInts(int* a, int* b) { + printf("1st Int: "); + (void)scanf("%d", a); + printf("2nd Int: "); + (void)scanf("%d", b); +} + +void printRatio(int r[]) { + printf("%d/%d", r[0], r[1]); +} + +int gcm(int a, int b); +int lcm(int a, int b); +void reduce(int x[]); + +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; +} + +int main(void) { + int a,b; + + getTwoInts(&a, &b); + int g = gcm(a, b); + int l = lcm(a, b); + + printf("Result:\ngcm = %d\nlcm = %d\n", g, l); + + int r[2] = {a, b}; + printf("Before: "); + printRatio(r); putchar('\n'); + reduce(r); + printf("After: "); + printRatio(r); putchar('\n'); + + return 0; +}