generated from kenryuS/report-temp
finished cls13
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 103 KiB |
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,60 @@
|
|||||||
|
\documentclass[xelatex,a4paper,11pt,ja=standard]{bxjsarticle}
|
||||||
|
|
||||||
|
\usepackage{tex/preamble}
|
||||||
|
\usepackage{tex/simple-title}
|
||||||
|
|
||||||
|
\reportauthor{柴田健琉}
|
||||||
|
\reporttitle{情報処理2 - 第13回課題}
|
||||||
|
\reportdate{2026年}{07月}{19日}
|
||||||
|
\turnindate{2026年}{07月}{19日}
|
||||||
|
|
||||||
|
\begin{document}
|
||||||
|
\simpletitle
|
||||||
|
|
||||||
|
\section{はじめに}
|
||||||
|
|
||||||
|
この課題のプログラムは以下の環境での動作が確認されている.
|
||||||
|
|
||||||
|
\begin{itemize}
|
||||||
|
\item{OS: NixOS 26.05 Yarara, GNU/Linux 7.1.3 x86\_64}
|
||||||
|
\item{CC: GCC 15.2.0}
|
||||||
|
\item{CFLAGS: \texttt{-g -O1 -Wall -Wpedantic}}
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
\section{課題1}
|
||||||
|
|
||||||
|
単純選択ソートと単純挿入ソートの2通りで授業サンプルで示された\texttt{Student}構造体の\texttt{height}を昇順で並べ替えるプログラムを作成する.
|
||||||
|
|
||||||
|
\lstinputlisting[language=C,title={課題1のソースコード}]{./src/cls13/main.c}
|
||||||
|
|
||||||
|
\subsection{実行結果}
|
||||||
|
|
||||||
|
\begin{figure}[tbh]
|
||||||
|
\centering
|
||||||
|
\includegraphics[width=0.8\textwidth]{./assets/cls13.png}
|
||||||
|
\caption{課題1の実行結果}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
\subsection{処理の流れ}
|
||||||
|
|
||||||
|
\begin{enumerate}
|
||||||
|
\item{プログラムの引数の数を確認し, 2以下であればエラー終了させる}
|
||||||
|
\item{関数ポインタ\texttt{alg}と関数の配列\texttt{algs}を宣言・初期化する}
|
||||||
|
\item{プログラムの第1引数の1文字目が数であれば\texttt{argv[1]}の値によって\texttt{algs}からどれか一つを\texttt{alg}に代入させ, そうでなければプログラムをエラー終了させる}
|
||||||
|
\item{\texttt{alg}が未だ\texttt{NULL}を指していれば, プログラムをエラー終了させる}
|
||||||
|
\item{構造体の配列\texttt{students}を宣言・初期化する}
|
||||||
|
\item{標準出力にソート前の配列の状態を表示させる}
|
||||||
|
\item{\texttt{alg}が指している関数を引数とともに呼び出す}
|
||||||
|
\item{ソート後の配列の状態を標準出力に表示させる}
|
||||||
|
\item{プログラムを正常終了させる}
|
||||||
|
\end{enumerate}
|
||||||
|
|
||||||
|
\subsection{書く際の難点}
|
||||||
|
|
||||||
|
シンプルなプログラムだったので特に難しいと感じずにプログラムを組めた.
|
||||||
|
|
||||||
|
\subsection{想定されるバグ}
|
||||||
|
|
||||||
|
第1引数の1文字目のみをテストしているので後続の文字列は何でも良いことになっているが, 他の使用者からすれば意図しない動作だと感じられてしまう.
|
||||||
|
|
||||||
|
\end{document}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
include ../common.mk
|
||||||
|
|
||||||
|
PROJECT_NAME:=cls13
|
||||||
|
|
||||||
|
TGTS:=$(patsubst %.c,%,$(wildcard *.c))
|
||||||
|
|
||||||
|
all: $(TGTS)
|
||||||
|
|
||||||
|
%: %.c
|
||||||
|
@mkdir -p $(BUILD_PATH)/$(PROJECT_NAME)
|
||||||
|
$(CC) $(CFLAGS) $^ -o $(BUILD_PATH)/$(PROJECT_NAME)/$@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) -drf $(BUILD_PATH)/$(PROJECT_NAME)
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define STUDENT_NUM 5
|
||||||
|
#define NAME_LEN 64
|
||||||
|
|
||||||
|
struct Student {
|
||||||
|
char name[NAME_LEN];
|
||||||
|
int height;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*sort_alg)(struct Student[], int);
|
||||||
|
|
||||||
|
void insertion_sort(struct Student students[], int n) {
|
||||||
|
struct Student tmp = {0};
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
if (students[j].height > students[i].height) {
|
||||||
|
tmp = students[i];
|
||||||
|
students[i] = students[j];
|
||||||
|
students[j] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void selection_sort(struct Student students[], int n) {
|
||||||
|
struct Student tmp = {0};
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int min_idx = i;
|
||||||
|
for (int j = i; j < n; j++) {
|
||||||
|
if (students[j].height < students[min_idx].height) {
|
||||||
|
min_idx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmp = students[i];
|
||||||
|
students[i] = students[min_idx];
|
||||||
|
students[min_idx] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Please specify sort algorithm (0: insertion, 1: selection)\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sort_alg alg = NULL;
|
||||||
|
sort_alg algs[3] = {insertion_sort, selection_sort, NULL};
|
||||||
|
|
||||||
|
if (isdigit(argv[1][0])) {
|
||||||
|
int idx = argv[1][0] - '0';
|
||||||
|
alg = algs[idx < 2 ? idx : 2];
|
||||||
|
} else {
|
||||||
|
printf("Please input correct value (0: insertion, 1: selection)\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (alg == NULL) {
|
||||||
|
printf("Please input correct value (0: insertion, 1: selection)\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Student students[STUDENT_NUM] = {
|
||||||
|
{"Sato", 178},
|
||||||
|
{"Sanaka", 175},
|
||||||
|
{"Takao", 173},
|
||||||
|
{"Mike", 165},
|
||||||
|
{"Masaki", 179}
|
||||||
|
};
|
||||||
|
|
||||||
|
printf("Initial State:\n");
|
||||||
|
for (int i = 0; i < STUDENT_NUM; i++) {
|
||||||
|
printf("%2d: %-8s%4d\n", i+1, students[i].name, students[i].height);
|
||||||
|
}
|
||||||
|
|
||||||
|
(*alg)(students, STUDENT_NUM);
|
||||||
|
|
||||||
|
printf("Sorted State:\n");
|
||||||
|
for (int i = 0; i < STUDENT_NUM; i++) {
|
||||||
|
printf("%2d: %-8s%4d\n", i+1, students[i].name, students[i].height);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user