generated from kenryuS/report-temp
finished cls04
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 61 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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}
|
||||
@@ -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)
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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__
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef __UTILS_H__
|
||||
#define __UTILS_H__
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
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__
|
||||
Reference in New Issue
Block a user