From 15846744f41f17d83eb5e375a47dabc93fb05e99 Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:43:24 +0900 Subject: [PATCH 01/18] =?UTF-8?q?add:=E5=9B=B3=E4=BB=A5=E5=A4=96=E3=81=AEi?= =?UTF-8?q?f=E6=96=87=E8=A7=A3=E8=AA=AC=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textbook/c-lang/beginner/11--if_scope.mdx | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx diff --git a/src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx b/src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx new file mode 100644 index 0000000..900ec9e --- /dev/null +++ b/src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx @@ -0,0 +1,202 @@ +--- +title: if文と条件分岐の基本 +description: if文と条件分岐の基本スコープについて学びます! +slug: textbook/c-lang/beginner/if_scope +--- + +import { Aside } from "@astrojs/starlight/components"; + +## 1.if + +実行したい処理に条件を付けたい時は、if文を用いる。条件式が真の場合に処理を実行する。 + +```c +if (条件式) { + // 条件式が真の場合に実行される処理 +} +``` + +以下は、入力された点数が60点より大きいならば「合格」、そうでなければ「不合格」と表示するプログラムである。 + +```c +#include +int main() { + int score; + + printf("点数を入力してください: "); + scanf("%d", &score); + + if (score >= 60) { + printf("合格\n"); + } + if (score < 60) { + printf("不合格\n"); + } + + return 0; +} +``` + + +フローチャートで表すと以下のようになる。 + +; TODO 図を挿入 + + +; TODO 表の見た目が崩れているので修正する + +| 演算子 | 数学の記号 | +|:-------:|:--------:| +| `>=` | ≥ | +| `>` | > | +| `<` | < | +| `<=` | ≤ | +| `==` | = | +| `!=` | ≠ | + + + +## 2.else + +else文を使うことで、条件式が偽の場合に実行する処理を指定できる。 + +```c +if (条件式) { + // 条件式が真の場合に実行される処理 +} else { + // 条件式が偽の場合に実行される処理 +} +``` + +1.ifで示した例の、60点以上でないときの処理をelseを使って簡単に記述できる。 + +```c +#include +int main() { + int score; + + printf("点数を入力してください: "); + scanf("%d", &score); + + if (score >= 60) { + printf("合格\n"); + } else { + printf("不合格\n"); + } + + return 0; +} +``` +; TODO 図を挿入 + +## 3.else if + +else ifを使うことで、複数の条件を指定することができる。 + +```c +if (条件式1) { + // 条件式1が真の場合に実行される処理 +} else if (条件式2) { + // 条件式1が偽で条件式2が真の場合に実行される処理 +} else { + // 条件式1,2が偽の場合に実行される処理 +} +``` + +2.elseのコードに、入力された点数が100点ならば「満点」と出力させるコードを足す。 + +```c +#include +int main() { + int score; + + printf("点数を入力してください: "); + scanf("%d", &score); + + if (score == 100) { + printf("満点\n"); + } else if (score >= 60) { + printf("合格\n"); + } else { + printf("不合格\n"); + } + + return 0; +} +``` +; TODO 図を挿入 + +## 4.or and not + +複数の条件を指定する場合には、if文を複数個用いたり、以下の論理演算子を活用すると良い。 + +| 演算子 | 数学の記号 | +| `&&` | ∧(かつ) | +| `\|\|` | ∨(または)| +| `!` | ¬(否定) | + + +```c +int x = 8; +int y = 6; + +if(x > 5) { + printf("x は 5 より大きい\n"); + if(y > 5) { + printf("x と y は 5 より大きい\n"); + } +} + +if (x < 10 && y < 10) { + printf("x と y は 10 より小さい\n"); +} +if (!(y >= 10)) { + printf("y は 10 より大きくない\n"); +} +``` + +## 変数のスコープ + +変数は使用できる範囲に制限がある。これを変数のスコープという。 +変数のスコープは、`{}`で囲まれたブロック内でのみ使用できる。 +以下のコードを例に説明する。 +変数xは一番外側のブロックで宣言されているため、どこでも使用できる。 +しかし、変数yは1つ目のif文の中で宣言されているため、そのif文の中(ブロック)でしか使用できない。 + +```c +int x = 8; + +if(x > 5) { + int y = 6; + printf("x は 5 より大きい\n"); + if(y > 5) { + printf("x と y は 5 より大きい\n"); + printf("y = %d\n", y); // 変数yがスコープ内のため使用可能 + } +} +printf("x = %d\n", x); +// printf("y = %d\n", y); // 変数yがスコープ外のためエラー +``` + + + + + + + + + + From feaa47b204e4c1be6609eb0f344c9ebf817122a0 Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:37:42 +0900 Subject: [PATCH 02/18] =?UTF-8?q?add:=E3=83=95=E3=83=AD=E3=83=BC=E3=83=81?= =?UTF-8?q?=E3=83=A3=E3=83=BC=E3=83=88=E3=81=AEsvg=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/fc_1_10_for.drawio.svg | 4 ++++ public/fc_99_for.drawio.svg | 4 ++++ public/fc_exam_else.drawio.svg | 4 ++++ public/fc_exam_else_if.drawio.svg | 4 ++++ public/fc_inf_loop.drawio.svg | 4 ++++ public/fc_menu_switch.drawio.svg | 4 ++++ public/fc_sample_break.drawio.svg | 4 ++++ 7 files changed, 28 insertions(+) create mode 100644 public/fc_1_10_for.drawio.svg create mode 100644 public/fc_99_for.drawio.svg create mode 100644 public/fc_exam_else.drawio.svg create mode 100644 public/fc_exam_else_if.drawio.svg create mode 100644 public/fc_inf_loop.drawio.svg create mode 100644 public/fc_menu_switch.drawio.svg create mode 100644 public/fc_sample_break.drawio.svg diff --git a/public/fc_1_10_for.drawio.svg b/public/fc_1_10_for.drawio.svg new file mode 100644 index 0000000..d4130fb --- /dev/null +++ b/public/fc_1_10_for.drawio.svg @@ -0,0 +1,4 @@ + + + +
Start
Start
Yes
Yes
No
No
i <= 10
i <= 10
End
End
iを出力
iを出力
i ++
i ++
i = 1
i = 1
Text is not SVG - cannot display
\ No newline at end of file diff --git a/public/fc_99_for.drawio.svg b/public/fc_99_for.drawio.svg new file mode 100644 index 0000000..bdb8327 --- /dev/null +++ b/public/fc_99_for.drawio.svg @@ -0,0 +1,4 @@ + + + +
Start
Start
No
No
i <= 9
i <= 9
End
End
i = 1
i = 1
j = 1
j = 1
Yes
Yes
No
No
j <= 9
j <= 9
i * jを出力
i * jを出力
j ++
j ++
Text is not SVG - cannot display
\ No newline at end of file diff --git a/public/fc_exam_else.drawio.svg b/public/fc_exam_else.drawio.svg new file mode 100644 index 0000000..9405539 --- /dev/null +++ b/public/fc_exam_else.drawio.svg @@ -0,0 +1,4 @@ + + + +
Start
Start
Yes
Yes
No
No
score >= 60
score >= 60
End
End
不合格
不合格
合格
合格
Text is not SVG - cannot display
\ No newline at end of file diff --git a/public/fc_exam_else_if.drawio.svg b/public/fc_exam_else_if.drawio.svg new file mode 100644 index 0000000..2737f2f --- /dev/null +++ b/public/fc_exam_else_if.drawio.svg @@ -0,0 +1,4 @@ + + + +
Start
Start
Yes
Yes
score == 100
score == 100
End
End
合格
合格
満点
満点
No
No
No
No
Yes
Yes
score >= 60
score >= 60
不合格
不合格
Text is not SVG - cannot display
\ No newline at end of file diff --git a/public/fc_inf_loop.drawio.svg b/public/fc_inf_loop.drawio.svg new file mode 100644 index 0000000..49da68f --- /dev/null +++ b/public/fc_inf_loop.drawio.svg @@ -0,0 +1,4 @@ + + + +
Start
Start
End
End
num = 1
num = 1
numに数値を入力
numに数値を入力
No
No
Yes
Yes
num != 0
num != 0
numを出力
numを出力
Text is not SVG - cannot display
\ No newline at end of file diff --git a/public/fc_menu_switch.drawio.svg b/public/fc_menu_switch.drawio.svg new file mode 100644 index 0000000..066fce3 --- /dev/null +++ b/public/fc_menu_switch.drawio.svg @@ -0,0 +1,4 @@ + + + +
Start
Start
End
End
case 2 (チャーハン)
case 2 (チャーハン)
case 1(ラーメン)
case 1(ラーメン)
default
default
menu_numの値は?
menu_numの値は?
500円
500円
400円
400円
1000円
1000円
メニューに
ありません
メニューに ありません
case 3(餃子)
case 3(餃子)
menu_numを入力
menu_numを入力
Text is not SVG - cannot display
\ No newline at end of file diff --git a/public/fc_sample_break.drawio.svg b/public/fc_sample_break.drawio.svg new file mode 100644 index 0000000..cd21f52 --- /dev/null +++ b/public/fc_sample_break.drawio.svg @@ -0,0 +1,4 @@ + + + +
Start
Start
End
End
case 2
case 2
case 3
case 3
case 1
case 1
case 5
case 5
default
default
menu_numの値は?
menu_numの値は?
2か3が
入力されました
2か3が...
4が入力されました
4が入力されました
1が入力されました
1が入力されました
メニューに
ありません
メニューに ありません
numを入力
numを入力
case 4
case 4
4か5が
入力されました
4か5が...
Text is not SVG - cannot display
\ No newline at end of file From b062a1b54258ccce966f34258c91b3473a1efadf Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:39:00 +0900 Subject: [PATCH 03/18] =?UTF-8?q?fix:=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E5=90=8D=E3=81=AE=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textbook/c-lang/beginner/11--if_scope.mdx | 202 ------------------ 1 file changed, 202 deletions(-) delete mode 100644 src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx diff --git a/src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx b/src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx deleted file mode 100644 index 900ec9e..0000000 --- a/src/content/docs/textbook/c-lang/beginner/11--if_scope.mdx +++ /dev/null @@ -1,202 +0,0 @@ ---- -title: if文と条件分岐の基本 -description: if文と条件分岐の基本スコープについて学びます! -slug: textbook/c-lang/beginner/if_scope ---- - -import { Aside } from "@astrojs/starlight/components"; - -## 1.if - -実行したい処理に条件を付けたい時は、if文を用いる。条件式が真の場合に処理を実行する。 - -```c -if (条件式) { - // 条件式が真の場合に実行される処理 -} -``` - -以下は、入力された点数が60点より大きいならば「合格」、そうでなければ「不合格」と表示するプログラムである。 - -```c -#include -int main() { - int score; - - printf("点数を入力してください: "); - scanf("%d", &score); - - if (score >= 60) { - printf("合格\n"); - } - if (score < 60) { - printf("不合格\n"); - } - - return 0; -} -``` - - -フローチャートで表すと以下のようになる。 - -; TODO 図を挿入 - - -; TODO 表の見た目が崩れているので修正する - -| 演算子 | 数学の記号 | -|:-------:|:--------:| -| `>=` | ≥ | -| `>` | > | -| `<` | < | -| `<=` | ≤ | -| `==` | = | -| `!=` | ≠ | - - - -## 2.else - -else文を使うことで、条件式が偽の場合に実行する処理を指定できる。 - -```c -if (条件式) { - // 条件式が真の場合に実行される処理 -} else { - // 条件式が偽の場合に実行される処理 -} -``` - -1.ifで示した例の、60点以上でないときの処理をelseを使って簡単に記述できる。 - -```c -#include -int main() { - int score; - - printf("点数を入力してください: "); - scanf("%d", &score); - - if (score >= 60) { - printf("合格\n"); - } else { - printf("不合格\n"); - } - - return 0; -} -``` -; TODO 図を挿入 - -## 3.else if - -else ifを使うことで、複数の条件を指定することができる。 - -```c -if (条件式1) { - // 条件式1が真の場合に実行される処理 -} else if (条件式2) { - // 条件式1が偽で条件式2が真の場合に実行される処理 -} else { - // 条件式1,2が偽の場合に実行される処理 -} -``` - -2.elseのコードに、入力された点数が100点ならば「満点」と出力させるコードを足す。 - -```c -#include -int main() { - int score; - - printf("点数を入力してください: "); - scanf("%d", &score); - - if (score == 100) { - printf("満点\n"); - } else if (score >= 60) { - printf("合格\n"); - } else { - printf("不合格\n"); - } - - return 0; -} -``` -; TODO 図を挿入 - -## 4.or and not - -複数の条件を指定する場合には、if文を複数個用いたり、以下の論理演算子を活用すると良い。 - -| 演算子 | 数学の記号 | -| `&&` | ∧(かつ) | -| `\|\|` | ∨(または)| -| `!` | ¬(否定) | - - -```c -int x = 8; -int y = 6; - -if(x > 5) { - printf("x は 5 より大きい\n"); - if(y > 5) { - printf("x と y は 5 より大きい\n"); - } -} - -if (x < 10 && y < 10) { - printf("x と y は 10 より小さい\n"); -} -if (!(y >= 10)) { - printf("y は 10 より大きくない\n"); -} -``` - -## 変数のスコープ - -変数は使用できる範囲に制限がある。これを変数のスコープという。 -変数のスコープは、`{}`で囲まれたブロック内でのみ使用できる。 -以下のコードを例に説明する。 -変数xは一番外側のブロックで宣言されているため、どこでも使用できる。 -しかし、変数yは1つ目のif文の中で宣言されているため、そのif文の中(ブロック)でしか使用できない。 - -```c -int x = 8; - -if(x > 5) { - int y = 6; - printf("x は 5 より大きい\n"); - if(y > 5) { - printf("x と y は 5 より大きい\n"); - printf("y = %d\n", y); // 変数yがスコープ内のため使用可能 - } -} -printf("x = %d\n", x); -// printf("y = %d\n", y); // 変数yがスコープ外のためエラー -``` - - - - - - - - - - From 65d729f18c85def8a1bf3414be4498ea23c063d7 Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:39:14 +0900 Subject: [PATCH 04/18] =?UTF-8?q?fix:=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E5=90=8D=E3=81=AE=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textbook/c-lang/beginner/11--if-scope.mdx | 267 ++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx diff --git a/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx b/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx new file mode 100644 index 0000000..5acd81d --- /dev/null +++ b/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx @@ -0,0 +1,267 @@ +--- +title: if文と条件分岐の基本 +description: if文と条件分岐の基本スコープについて学びます! +slug: textbook/c-lang/beginner/if-scope +--- + + +import { Aside } from "@astrojs/starlight/components"; +// import Quize from "@/components/astro/Quize.astro"; +// import Anser from "@/components/astro/Anser.astro"; + +## 1.if + +実行したい処理に条件を付けたい時は、if文を用いる。条件式が真の場合に処理を実行する。 + +```c +if (条件式) { + // 条件式が真の場合に実行される処理 +} +``` + +以下は、入力された点数が60点より大きいならば「合格」、そうでなければ「不合格」と表示するプログラムである。 + +```c +#include +int main() { + int score; + + printf("点数を入力してください: "); + scanf("%d", &score); + + if (score >= 60) { + printf("合格\n"); + } + if (score < 60) { + printf("不合格\n"); + } + + return 0; +} +``` + + +#### 比較演算子 + +| 演算子 | 数学の記号 | +|:-------:|:--------:| +| `>=` | ≥ | +| `>` | > | +| `<` | < | +| `<=` | ≤ | +| `==` | = | +| `!=` | ≠ | + + + +## 2.else + +else文を使うことで、条件式が偽の場合に実行する処理を指定できる。 + +```c +if (条件式) { + // 条件式が真の場合に実行される処理 +} else { + // 条件式が偽の場合に実行される処理 +} +``` + +1.ifで示した例の、60点以上でないときの処理をelseを使って簡単に記述できる。 + +```c +#include +int main() { + int score; + + printf("点数を入力してください: "); + scanf("%d", &score); + + if (score >= 60) { + printf("合格\n"); + } else { + printf("不合格\n"); + } + + return 0; +} +``` + +フローチャートで表すと以下のようになる。 + +![フローチャート](/fc_exam_else.drawio.svg) + +## 3.else if + +else ifを使うことで、複数の条件を指定することができる。 + +```c +if (条件式1) { + // 条件式1が真の場合に実行される処理 +} else if (条件式2) { + // 条件式1が偽で条件式2が真の場合に実行される処理 +} else { + // 条件式1,2が偽の場合に実行される処理 +} +``` + +2.elseのコードに、入力された点数が100点ならば「満点」と出力させるコードを足す。 + +```c +#include +int main() { + int score; + + printf("点数を入力してください: "); + scanf("%d", &score); + + if (score == 100) { + printf("満点\n"); + } else if (score >= 60) { + printf("合格\n"); + } else { + printf("不合格\n"); + } + + return 0; +} +``` +![フローチャート](/fc_exam_else_if.drawio.svg) + +## 4.or and not + +複数の条件を指定する場合には、if文を複数個用いたり、以下の論理演算子を活用すると良い。 + + +| 演算子 | 数学の記号 | +|:-------|:--------| +| `&&` | ∧(かつ) | +| `\|\|` | ∨(または) | +| `!` | ¬(否定) | + + +```c +int x = 8; +int y = 6; + +if(x > 5) { + printf("x は 5 より大きい\n"); + if(y > 5) { + printf("x と y は 5 より大きい\n"); + } +} + +if (x < 10 && y < 10) { + printf("x と y は 10 より小さい\n"); +} +if (!(y >= 10)) { + printf("y は 10 より大きくない\n"); +} +``` + +## 変数のスコープ + +変数は使用できる範囲に制限がある。これを変数のスコープという。 +変数のスコープは、`{}`で囲まれたブロック内でのみ使用できる。 +以下のコードを例に説明する。 +変数xは一番外側のブロックで宣言されているため、どこでも使用できる。 +しかし、変数yは1つ目のif文の中で宣言されているため、そのif文の中(ブロック)でしか使用できない。 + +```c +int x = 8; + +if(x > 5) { + int y = 6; + printf("x は 5 より大きい\n"); + if(y > 5) { + printf("x と y は 5 より大きい\n"); + printf("y = %d\n", y); // 変数yがスコープ内のため使用可能 + } +} +printf("x = %d\n", x); +// printf("y = %d\n", y); // 変数yがスコープ外のためエラー +``` + + + + + + +## 理解度チェック + +{/* +{入力された値が10以上ならば「Yes」、それ以外なら「No」と出力するプログラムを書け。} + + + +int main() { + int num; + + printf("数値を入力してください: "); + scanf("%d", &num); + + if (num >= 10) { + printf("Yes\n"); + } else { + printf("No\n"); + } + + return 0; +} +``` +}> +{if_else文を使用して条件分岐ができる} + */} + +{/* +{else ifを使用して、ユーザーに数値を入力してもらい、その数値が以下の条件に当てはまるか判定してください。 +- 90以上 → "優" +- 70以上90未満 → "良" +- 50以上70未満 → "可" +- 50未満 → "不可" +} + + + + +int main() { + int score; + + printf("点数を入力してください: "); + scanf("%d", &score); + + // 条件分岐 + if (score >= 90) { + printf("評価: 優\n"); + } else if (score >= 70) { + printf("評価: 良\n"); + } else if (score >= 50) { + printf("評価: 可\n"); + } else { + printf("評価: 不可\n"); + } + + return 0; +``` +} +}> +{else if文を使用して複数の条件分岐ができる} + */} + + From 06f537b3d787ce867520174bc3301f5a83fa8b5a Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:39:43 +0900 Subject: [PATCH 05/18] =?UTF-8?q?add:for=E6=96=87=E3=81=AE=E8=A8=98?= =?UTF-8?q?=E4=BA=8B=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs/textbook/c-lang/beginner/12--for.mdx | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 src/content/docs/textbook/c-lang/beginner/12--for.mdx diff --git a/src/content/docs/textbook/c-lang/beginner/12--for.mdx b/src/content/docs/textbook/c-lang/beginner/12--for.mdx new file mode 100644 index 0000000..45636ea --- /dev/null +++ b/src/content/docs/textbook/c-lang/beginner/12--for.mdx @@ -0,0 +1,192 @@ +--- +title: for文 +description: for文について学びます! +slug: textbook/c-lang/beginner/for +--- + +import { Aside } from "@astrojs/starlight/components"; +// import Quize from "@/components/astro/Quize.astro"; +// import Anser from "@/components/astro/Anser.astro"; + +## 1.for + +for文は、繰り返し処理を行うための構文である。 +繰り返し処理は、書き間違えが無限ループの原因になるので注意が必要である。 + +以下のように記述する。 + +```c +for (初期化; 条件式; 更新式) { + // 条件式が真の場合に実行される処理 + } +``` + +基本的な構造は、以下の通りである。 +初期化には、ループ変数の初期値を設定する。 +条件式には、真の場合にループを繰り返す条件を設定する。 +更新式には、ループ変数の値を更新する処理を記述する。 + +以下は、1から10までの数字を表示するプログラムである。 + +```c +#include +int main() { + int i; + for (i = 1; i <= 10; i++) { + printf("%d\n", i); + } + return 0; +} +``` + +フローチャートで表すと以下のようになる。 +![フローチャート](/fc_1_10_for.drawio.svg) + +{/* */} +初期化,条件式,更新式は、書かなくても良い。 +しかし、初期化,条件式,更新式を省略するならば、while文を用いたほうがわかりやすい。 + +```c +#include +int main(){ + for(int i = 0;;){ + printf("%d",i); + break; + } +} +``` + + + +## 2.複合代入演算子 + +複合代入演算子は、代入演算子と算術演算子を組み合わせた演算子である。 +代入と演算のコードを省略して記述できる。 + +| 演算子 | 実際の動作 | +|:-------|:--------| +| `a += b` | `a = a + b` | +| `a -= b` | `a = a - b` | +| `a *= b` | `a = a * b` | +| `a /= b` | `a = a / b` | +| `a %= b` | `a = a % b` | + +## 3.for文のネスト + +ループの中にループを記述する構造のことをネスト(入れ子)構造と呼ぶ。 +以下は、九九の表を表示するプログラムである。 + +```c +#include +int main() { + int i, j; + for (i = 1; i <= 9; i++) { + for (j = 1; j <= 9; j++) { + printf("%d * %d = %d\n", i, j, i * j); + } + } + return 0; +} +``` + +フローチャートで表すと以下のようになる。 +![フローチャート](/fc_99_for.drawio.svg) + +## 理解度チェック + +{/* +{ +for文を使用して、1から10までの合計を求め、その値を出力しなさい。 +} + + + + +int main() { + int sum = 0; + + // 1 から 10 までの合計を求める + for (int i = 1; i <= 10; i++) { + sum += i; // sum = sum + i + } + + // 結果を表示 + printf("1 から 10 までの合計: %d\n",sum); + + return 0; +} +``` +} +}> +{+= は合計を求めるときに便利} + */} + +{/* +{ +for文を2つ使用して、ユーザーが指定した行数 N で 右上がりの三角形 を * で描画しなさい。 +実行例 +``` +行数を入力してください: 5 +* +* * +* * * +* * * * +* * * * * +``` +} + + + + +int main() { + int N; + + // ユーザーに行数を入力してもらう + printf("行数を入力してください: "); + scanf("%d", &N); + + // 三角形を描画 + for (int i = 1; i <= N; i++) { // 行のループ + for (int j = 1; j <= i; j++) { // 列のループ + printf("*"); + } + printf("\n"); // 1行描画したら改行 + } + + return 0; +} +``` +}> +{j <= i を条件式にすると、iの値が1からNまで増えるごとに、*の数が増やすことができる} + */} + + + + + + + + + + From b90f2b4169d98372fb652c8e5c45b11211bf4bb9 Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:40:03 +0900 Subject: [PATCH 06/18] =?UTF-8?q?add:while=E6=96=87=E3=81=AE=E8=A8=98?= =?UTF-8?q?=E4=BA=8B=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textbook/c-lang/beginner/13--while.mdx | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/content/docs/textbook/c-lang/beginner/13--while.mdx diff --git a/src/content/docs/textbook/c-lang/beginner/13--while.mdx b/src/content/docs/textbook/c-lang/beginner/13--while.mdx new file mode 100644 index 0000000..cf09b5c --- /dev/null +++ b/src/content/docs/textbook/c-lang/beginner/13--while.mdx @@ -0,0 +1,130 @@ +--- +title: while文 +description: while文について学びます! +slug: textbook/c-lang/beginner/while +--- + +import { Aside } from "@astrojs/starlight/components"; + +## 1.while + +while文は、条件式が真の間、繰り返し処理を行う構文である。 + +```c +while (条件式) { + // 条件式が真の場合に実行される処理 +} +``` + +以下は、1から10までの数字を表示するプログラムである。 +```c +#include +int main() { + int i = 1; + while (i <= 10) { + printf("%d\n", i); + i++; + } + return 0; +} +``` +{/* for文のフローチャートと同じもの */} +フローチャートで表すと以下のようになる。 +![フローチャート](/fc_1_10_for.drawio.svg) + + + + +## 2.do-while文 +do-while文は、while文と同様に、条件式が真の間繰り返し処理を行う構文である。 +while文との違いは、条件式の判定を処理の後に行うため、条件式が偽であっても1回は処理を実行する。 +特に理由がなければ、while文を使用することが推奨される。 + +```c +do { + // 条件式が真の場合に実行される処理 +} while (条件式); +``` + +以下は、0が入力されるまで数値を入力し続けるプログラムである。 +```c +#include + +int main() { + int num = 1; // 0以外の数値で初期化 + printf("0 を入力すると終了します。\n"); + + do { // 無限ループ + printf("数値を入力してください: "); + scanf("%d", &num); + printf("入力された数値: %d\n", num); + }while(num != 0); // 0 が入力されたらループを抜ける + + printf("終了しました。\n"); + return 0; +} +``` + +フローチャートで表すと以下のようになる。 +![フローチャート](/fc_inf_loop.drawio.svg) + +## 理解度チェック + +{/* +{ +while文を使用して、ユーザーが 0 を入力するまで数値を合計し表示しなさい +} + + + + +int main() { + int num; + int sum = 0; + + printf("数値を入力してください(0で終了):\n"); + + while (1) { // 無限ループ + scanf("%d", &num); // ユーザー入力 + if (num == 0) { // 0が入力されたらループ終了 + break; + } + sum += num; // 合計に加算 + } + + printf("合計: %d\n", sum); + return 0; +} +``` +}> +{while文の条件式に1を指定することで無限ループを作成できる} + */} \ No newline at end of file From f9ee86fdc64366a64f5de978298c29057901dc38 Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:41:11 +0900 Subject: [PATCH 07/18] =?UTF-8?q?add:switch=E6=96=87=E3=81=AE=E8=A8=98?= =?UTF-8?q?=E4=BA=8B=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textbook/c-lang/beginner/14--switch.mdx | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 src/content/docs/textbook/c-lang/beginner/14--switch.mdx diff --git a/src/content/docs/textbook/c-lang/beginner/14--switch.mdx b/src/content/docs/textbook/c-lang/beginner/14--switch.mdx new file mode 100644 index 0000000..9e68f38 --- /dev/null +++ b/src/content/docs/textbook/c-lang/beginner/14--switch.mdx @@ -0,0 +1,225 @@ +--- +title: switch文 +description: switch文について学びます! +slug: textbook/c-lang/beginner/switch +--- + +import { Aside } from "@astrojs/starlight/components"; + +## 1.switch +switch文は、条件によって処理を分岐させる構文である。 +if文と違い、条件式ではなく、式とその値によって処理を分岐させる。 + +```c +switch (式) { + case 定数1: + // 定数1の場合に実行される処理 + break; + case 定数2: + // 定数2の場合に実行される処理 + break; + default: + // どのcaseにも当てはまらない場合に実行される処理 + break; +} +``` + +以下は、選択したメニューの値段を返すプログラムである。 + +```c +#include + +int main() { + int menu_num; + + printf("メニューを選んでください:\n"); + printf("1: ラーメン\n"); + printf("2: チャーハン\n"); + printf("3: 餃子\n"); + + scanf("%d", &menu_num); + + switch (menu_num) { + case 1: + printf("1000円\n"); + break; + case 2: + printf("500円\n"); + break; + case 3: + printf("400円\n"); + break; + default: + printf("メニューにありません\n"); + break; + } + return 0; +} +``` + +フローチャートで表すと以下のようになる。 +![フローチャート](/fc_menu_switch.drawio.svg) + +break文は、実行されるとループ処理やswitch文を抜け、次の処理へ移る構文である。 +break文を省略すると、次のcase文が実行される。 + +以下は、1から5までの数字が入力されたときに、メッセージを返すコードである。 +```c +#include +int main() { + int num; + printf("数字を入力してください: "); + scanf("%d", &num); + + switch (num) { + case 1: + printf("1が入力されました\n"); + break; + + // case 2,3の処理をまとめることもできる + case 2: + case 3: + printf("2か3が入力されました\n"); + break; + + // case 4の処理後にbreakがないため、case 5の処理も実行される。 + // しかし、break文が無いコードは、可読性が低いため、 + // breakを書くか、コメント文を残すことを推奨する。 + case 4: + printf("4が入力されました\n"); + case 5: + printf("4か5が入力されました\n"); + break; + + default: + printf("不明\n"); + break; + } + return 0; +} +``` +フローチャートで表すと以下のようになる。 +![フローチャート](/fc_sample_break.drawio.svg) + +## 2.continue +continue文は、ループ処理を抜けずに次の処理へ移る構文である。 +以下は、1から100までの数字が入力されるとその数字を出力し、入力された数字が1から100まででないときは再入力させるコードである。 +```c +#include + +int main() { + int num = 1; // 0以外の数値で初期化 + + printf("0 を入力するとループを終了します。"); + + while (1) { + printf("1~100 の数値を入力してください: "); + scanf("%d", &num); + + if (num == 0) { + break; // break文はループそのものを抜ける + } else if (num < 1 || num > 100) { + printf("無効な入力です。もう一度入力してください。\n"); + continue; // continue文はループ処理を抜けずに次のループへ移る + } + + printf("入力された数値: %d\n", num); + } + return 0; +} +``` + + +## 理解度チェック + +{/* +{ +swtich文を用いて月を入力すると対応する季節を表示しなさい。 +} + + + + +#include + +int main() { + int month; + + // ユーザーに入力を求める + printf("1~12の数字を入力してください (例: 3 → 春): "); + scanf("%d", &month); + + // switch文を使用して季節を判定 + switch (month) { + case 3: + case 4: + case 5: + printf("春です!\n"); + break; + case 6: + case 7: + case 8: + printf("夏です!\n"); + break; + case 9: + case 10: + case 11: + printf("秋です!\n"); + break; + case 12: + case 1: + case 2: + printf("冬です!\n"); + break; + default: + printf("エラー: 1~12の範囲で入力してください。\n"); + } + + return 0; +} +``` +}> +{case文をまとめることで、同じ処理をまとめることができる。} + */} From f2947dc8c41e208afd90ffc81e19fcea2a94feff Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:41:41 +0900 Subject: [PATCH 08/18] =?UTF-8?q?add:=E6=A8=99=E6=BA=96=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=83=96=E3=83=A9=E3=83=AA=E3=81=AE=E8=A8=98=E4=BA=8B=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../c-lang/beginner/15--ex-library.mdx | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx diff --git a/src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx b/src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx new file mode 100644 index 0000000..8551a65 --- /dev/null +++ b/src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx @@ -0,0 +1,156 @@ +--- +title: 標準ライブラリ +description: 標準ライブラリについて学びます! +slug: textbook/c-lang/beginner/library +--- + +import { Aside } from "@astrojs/starlight/components"; + +## 1.標準ライブラリとは +標準ライブラリとは、C言語の標準で提供されているCプログラムで利用できる関数やマクロの集合のことである。 +標準ライブラリには、様々な関数が用意されており、それらを利用することで、プログラムの開発が容易になる。 +標準ライブラリは、`#include`ディレクティブを用いてプログラムに取り込むことができる。 + +## 2.stdio.h +`stdio.h`は、標準入出力を行うためのヘッダファイルである。 +ヘッダファイルとは、関数やマクロ、型定義などを記述し、他のCファイルで再利用できるようにするファイルであり、拡張子に`.h`を用いるものである。 +以下は、stdio.hで提供されている関数の一部である。 + +- `printf()` / `scanf()`: 画面への出力・キーボードからの入力 +- `fopen()` / `fclose()`: ファイルのオープン・クローズ +- `fgets()` / `fputs()`: ファイルからの読み込み・書き込み + +## 3.math.h +`math.h`は、数学関数を提供するヘッダファイルである。 +以下は、math.hで提供されている関数の一部である。 + +- `sin()`, `cos()`, `tan()`: 三角関数 +- `sqrt()`: 平方根 +- `pow()`: べき乗 +- `abs()`: 絶対値 +```c +#include +#include + +int main() { + double angle = 45.0; // 角度(度数法) + double radians = angle * M_PI / 180.0; // ラジアン変換 + + // 三角関数 + printf("sin(%.2f度) = %.5f\n", angle, sin(radians)); + printf("cos(%.2f度) = %.5f\n", angle, cos(radians)); + printf("tan(%.2f度) = %.5f\n", angle, tan(radians)); + + // 平方根 + double num = 25.0; + printf("sqrt(%.2f) = %.2f\n", num, sqrt(num)); + + // 累乗 + printf("2^3 = %.2f\n", pow(2.0, 3.0)); + + // 絶対値 + int x = -10; + printf("abs(%d) = %d\n", x, abs(x)); + return 0; +} + +``` + +## 4.string.h +`string.h`は、文字列操作を行うためのヘッダファイルである。 +以下は、string.hで提供されている関数の一部である。 + +- `strlen()`: 文字列の長さを取得 +- `strcpy()`: 文字列のコピー +- `strcat()`: 文字列の連結 +- `strcmp()`: 文字列の比較 + +```c +#include +#include + +int main() { + // 1. strlen() - 文字列の長さ + char str1[] = "Hello"; + printf("文字列: \"%s\"\n", str1); + printf("長さ: %lu\n\n", strlen(str1)); // ヌル終端を除いた長さ + + // 2. strcpy() - 文字列のコピー + char source[] = "C Programming"; + char destination[50]; // 十分なサイズの配列を確保 + strcpy(destination, source); + printf("コピー後の文字列: \"%s\"\n\n", destination); + + // 3. strcat() - 文字列の連結 + char str2[50] = "Hello, "; + char str3[] = "World!"; + strcat(str2, str3); + printf("連結結果: \"%s\"\n\n", str2); + + // 4. strcmp() - 文字列の比較 + char cmp1[] = "apple"; + char cmp2[] = "banana"; + char cmp3[] = "apple"; + + printf("strcmp(cmp1, cmp2) = %d\n", strcmp(cmp1, cmp2)); // "apple" < "banana" → 負の値 + printf("strcmp(cmp1, cmp3) = %d\n", strcmp(cmp1, cmp3)); // "apple" == "apple" → 0 + printf("strcmp(cmp2, cmp1) = %d\n", strcmp(cmp2, cmp1)); // "banana" > "apple" → 正の値 + + return 0; +} +``` +出力例 +``` +文字列: "Hello" +長さ: 5 + +コピー後の文字列: "C Programming" + +連結結果: "Hello, World!" + +strcmp(cmp1, cmp2) = -1 +strcmp(cmp1, cmp3) = 0 +strcmp(cmp2, cmp1) = 1 +``` + +## 5.stdlib.h +`stdlib.h`は、標準ライブラリの中で、乱数生成やメモリの確保・解放などを行うためのヘッダファイルである。 +以下は、stdlib.hで提供されている関数の一部である。 + +- `rand()`: 乱数の生成 +- `malloc()`: メモリの確保 +- `free()`: メモリの解放 + +```c +#include +#include +#include + +int main() { + // 1. 乱数の生成(rand()) + srand(time(NULL)); // 乱数の種を設定 + int random_number = rand() % 100; // 0〜99の乱数を生成 + printf("生成された乱数: %d\n\n", random_number); + + // 2. メモリの確保(malloc()) + int size = 5; + int *arr = (int *)malloc(size * sizeof(int)); // int型5個分のメモリを確保 + if (arr == NULL) { + printf("メモリの確保に失敗しました。\n"); + return 1; // エラー終了 + } + + // 配列に乱数を格納 + printf("確保したメモリに乱数を格納:\n"); + for (int i = 0; i < size; i++) { + arr[i] = rand() % 100; // 0〜99の乱数を代入 + printf("arr[%d] = %d\n", i, arr[i]); + } + + // 3. メモリの解放(free()) + free(arr); + printf("\n確保したメモリを解放しました。\n"); + + return 0; +} +``` \ No newline at end of file From aec4ac0fd9c28fba38ae55eb3c28ff9bb7dfd665 Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Thu, 10 Apr 2025 13:40:38 +0900 Subject: [PATCH 09/18] =?UTF-8?q?fix:=20=E5=90=84=E8=A6=8B=E5=87=BA?= =?UTF-8?q?=E3=81=97=E3=81=AB=E7=95=AA=E5=8F=B7=E3=82=92=E5=89=8A=E9=99=A4?= =?UTF-8?q?=20=E3=83=97=E3=83=AD=E3=82=B0=E3=83=A9=E3=83=A0=E3=82=92int=20?= =?UTF-8?q?main()=E3=81=AE=E7=AE=87=E6=89=80=E3=82=92int=20main(void)?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textbook/c-lang/beginner/11--if-scope.mdx | 25 +++++++++++------- .../docs/textbook/c-lang/beginner/12--for.mdx | 23 +++++++++------- .../textbook/c-lang/beginner/13--while.mdx | 18 ++++++++----- .../textbook/c-lang/beginner/14--switch.mdx | 23 ++++++++++------ .../c-lang/beginner/15--ex-library.mdx | 26 ++++++++++++------- 5 files changed, 73 insertions(+), 42 deletions(-) diff --git a/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx b/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx index 5acd81d..6edb227 100644 --- a/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx +++ b/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx @@ -9,7 +9,8 @@ import { Aside } from "@astrojs/starlight/components"; // import Quize from "@/components/astro/Quize.astro"; // import Anser from "@/components/astro/Anser.astro"; -## 1.if +## if +--- 実行したい処理に条件を付けたい時は、if文を用いる。条件式が真の場合に処理を実行する。 @@ -23,7 +24,7 @@ if (条件式) { ```c #include -int main() { +int main(void) { int score; printf("点数を入力してください: "); @@ -45,6 +46,7 @@ int main() { #### 比較演算子 +--- | 演算子 | 数学の記号 | |:-------:|:--------:| @@ -59,7 +61,8 @@ int main() { `==`と`=`は異なる演算子である。`==`は等価、`=`は代入を意味する。 -## 2.else +## else +--- else文を使うことで、条件式が偽の場合に実行する処理を指定できる。 @@ -75,7 +78,7 @@ if (条件式) { ```c #include -int main() { +int main(void) { int score; printf("点数を入力してください: "); @@ -95,7 +98,8 @@ int main() { ![フローチャート](/fc_exam_else.drawio.svg) -## 3.else if +## else if +--- else ifを使うことで、複数の条件を指定することができる。 @@ -113,7 +117,7 @@ if (条件式1) { ```c #include -int main() { +int main(void) { int score; printf("点数を入力してください: "); @@ -132,7 +136,8 @@ int main() { ``` ![フローチャート](/fc_exam_else_if.drawio.svg) -## 4.or and not +## and, or, not +--- 複数の条件を指定する場合には、if文を複数個用いたり、以下の論理演算子を活用すると良い。 @@ -164,6 +169,7 @@ if (!(y >= 10)) { ``` ## 変数のスコープ +--- 変数は使用できる範囲に制限がある。これを変数のスコープという。 変数のスコープは、`{}`で囲まれたブロック内でのみ使用できる。 @@ -200,6 +206,7 @@ printf("x = %d\n", x); ## 理解度チェック +--- {/* {入力された値が10以上ならば「Yes」、それ以外なら「No」と出力するプログラムを書け。} @@ -208,7 +215,7 @@ printf("x = %d\n", x); -int main() { +int main(void) { int num; printf("数値を入力してください: "); @@ -240,7 +247,7 @@ int main() { ```c #include -int main() { +int main(void) { int score; printf("点数を入力してください: "); diff --git a/src/content/docs/textbook/c-lang/beginner/12--for.mdx b/src/content/docs/textbook/c-lang/beginner/12--for.mdx index 45636ea..dde2836 100644 --- a/src/content/docs/textbook/c-lang/beginner/12--for.mdx +++ b/src/content/docs/textbook/c-lang/beginner/12--for.mdx @@ -8,8 +8,8 @@ import { Aside } from "@astrojs/starlight/components"; // import Quize from "@/components/astro/Quize.astro"; // import Anser from "@/components/astro/Anser.astro"; -## 1.for - +## for +--- for文は、繰り返し処理を行うための構文である。 繰り返し処理は、書き間違えが無限ループの原因になるので注意が必要である。 @@ -30,7 +30,7 @@ for (初期化; 条件式; 更新式) { ```c #include -int main() { +int main(void) { int i; for (i = 1; i <= 10; i++) { printf("%d\n", i); @@ -48,7 +48,7 @@ int main() { ```c #include -int main(){ +int main(void){ for(int i = 0;;){ printf("%d",i); break; @@ -75,7 +75,8 @@ a = i++ と a = ++i では、aの値が異なる。 -## 2.複合代入演算子 +## 複合代入演算子 +--- 複合代入演算子は、代入演算子と算術演算子を組み合わせた演算子である。 代入と演算のコードを省略して記述できる。 @@ -88,14 +89,15 @@ a = i++ と a = ++i では、aの値が異なる。 | `a /= b` | `a = a / b` | | `a %= b` | `a = a % b` | -## 3.for文のネスト - +## for文のネスト +--- + ループの中にループを記述する構造のことをネスト(入れ子)構造と呼ぶ。 以下は、九九の表を表示するプログラムである。 ```c #include -int main() { +int main(void) { int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { @@ -110,6 +112,7 @@ int main() { ![フローチャート](/fc_99_for.drawio.svg) ## 理解度チェック +--- {/* { @@ -121,7 +124,7 @@ for文を使用して、1から10までの合計を求め、その値を出力 ```c #include -int main() { +int main(void) { int sum = 0; // 1 から 10 までの合計を求める @@ -159,7 +162,7 @@ for文を2つ使用して、ユーザーが指定した行数 N で 右上がり ```c #include -int main() { +int main(void) { int N; // ユーザーに行数を入力してもらう diff --git a/src/content/docs/textbook/c-lang/beginner/13--while.mdx b/src/content/docs/textbook/c-lang/beginner/13--while.mdx index cf09b5c..9516f03 100644 --- a/src/content/docs/textbook/c-lang/beginner/13--while.mdx +++ b/src/content/docs/textbook/c-lang/beginner/13--while.mdx @@ -6,7 +6,8 @@ slug: textbook/c-lang/beginner/while import { Aside } from "@astrojs/starlight/components"; -## 1.while +## while +--- while文は、条件式が真の間、繰り返し処理を行う構文である。 @@ -19,7 +20,7 @@ while (条件式) { 以下は、1から10までの数字を表示するプログラムである。 ```c #include -int main() { +int main(void) { int i = 1; while (i <= 10) { printf("%d\n", i); @@ -35,6 +36,8 @@ int main() { -## 2.do-while文 +## do-while文 +--- + do-while文は、while文と同様に、条件式が真の間繰り返し処理を行う構文である。 while文との違いは、条件式の判定を処理の後に行うため、条件式が偽であっても1回は処理を実行する。 特に理由がなければ、while文を使用することが推奨される。 @@ -77,7 +82,7 @@ do { ```c #include -int main() { +int main(void) { int num = 1; // 0以外の数値で初期化 printf("0 を入力すると終了します。\n"); @@ -96,6 +101,7 @@ int main() { ![フローチャート](/fc_inf_loop.drawio.svg) ## 理解度チェック +--- {/* { @@ -107,7 +113,7 @@ while文を使用して、ユーザーが 0 を入力するまで数値を合計 ```c #include -int main() { +int main(void) { int num; int sum = 0; diff --git a/src/content/docs/textbook/c-lang/beginner/14--switch.mdx b/src/content/docs/textbook/c-lang/beginner/14--switch.mdx index 9e68f38..d27a419 100644 --- a/src/content/docs/textbook/c-lang/beginner/14--switch.mdx +++ b/src/content/docs/textbook/c-lang/beginner/14--switch.mdx @@ -6,7 +6,9 @@ slug: textbook/c-lang/beginner/switch import { Aside } from "@astrojs/starlight/components"; -## 1.switch +## switch +--- + switch文は、条件によって処理を分岐させる構文である。 if文と違い、条件式ではなく、式とその値によって処理を分岐させる。 @@ -29,7 +31,7 @@ switch (式) { ```c #include -int main() { +int main(void) { int menu_num; printf("メニューを選んでください:\n"); @@ -66,7 +68,7 @@ break文を省略すると、次のcase文が実行される。 以下は、1から5までの数字が入力されたときに、メッセージを返すコードである。 ```c #include -int main() { +int main(void) { int num; printf("数字を入力してください: "); scanf("%d", &num); @@ -101,13 +103,15 @@ int main() { フローチャートで表すと以下のようになる。 ![フローチャート](/fc_sample_break.drawio.svg) -## 2.continue +## continue +--- + continue文は、ループ処理を抜けずに次の処理へ移る構文である。 以下は、1から100までの数字が入力されるとその数字を出力し、入力された数字が1から100まででないときは再入力させるコードである。 ```c #include -int main() { +int main(void) { int num = 1; // 0以外の数値で初期化 printf("0 を入力するとループを終了します。"); @@ -130,6 +134,8 @@ int main() { ``` ## 理解度チェック +--- {/* { @@ -184,7 +191,7 @@ swtich文を用いて月を入力すると対応する季節を表示しなさ #include -int main() { +int main(void) { int month; // ユーザーに入力を求める diff --git a/src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx b/src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx index 8551a65..f311976 100644 --- a/src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx +++ b/src/content/docs/textbook/c-lang/beginner/15--ex-library.mdx @@ -1,17 +1,21 @@ --- -title: 標準ライブラリ +title: ex-標準ライブラリ description: 標準ライブラリについて学びます! slug: textbook/c-lang/beginner/library --- import { Aside } from "@astrojs/starlight/components"; -## 1.標準ライブラリとは +## 標準ライブラリとは +--- + 標準ライブラリとは、C言語の標準で提供されているCプログラムで利用できる関数やマクロの集合のことである。 標準ライブラリには、様々な関数が用意されており、それらを利用することで、プログラムの開発が容易になる。 標準ライブラリは、`#include`ディレクティブを用いてプログラムに取り込むことができる。 -## 2.stdio.h +## stdio.h +--- + `stdio.h`は、標準入出力を行うためのヘッダファイルである。 ヘッダファイルとは、関数やマクロ、型定義などを記述し、他のCファイルで再利用できるようにするファイルであり、拡張子に`.h`を用いるものである。 以下は、stdio.hで提供されている関数の一部である。 @@ -20,7 +24,8 @@ import { Aside } from "@astrojs/starlight/components"; - `fopen()` / `fclose()`: ファイルのオープン・クローズ - `fgets()` / `fputs()`: ファイルからの読み込み・書き込み -## 3.math.h +## math.h +--- `math.h`は、数学関数を提供するヘッダファイルである。 以下は、math.hで提供されている関数の一部である。 @@ -32,7 +37,7 @@ import { Aside } from "@astrojs/starlight/components"; #include #include -int main() { +int main(void) { double angle = 45.0; // 角度(度数法) double radians = angle * M_PI / 180.0; // ラジアン変換 @@ -56,7 +61,8 @@ int main() { ``` -## 4.string.h +## string.h +--- `string.h`は、文字列操作を行うためのヘッダファイルである。 以下は、string.hで提供されている関数の一部である。 @@ -69,7 +75,7 @@ int main() { #include #include -int main() { +int main(void) { // 1. strlen() - 文字列の長さ char str1[] = "Hello"; printf("文字列: \"%s\"\n", str1); @@ -113,7 +119,9 @@ strcmp(cmp1, cmp3) = 0 strcmp(cmp2, cmp1) = 1 ``` -## 5.stdlib.h +## stdlib.h +--- + `stdlib.h`は、標準ライブラリの中で、乱数生成やメモリの確保・解放などを行うためのヘッダファイルである。 以下は、stdlib.hで提供されている関数の一部である。 @@ -126,7 +134,7 @@ strcmp(cmp2, cmp1) = 1 #include #include -int main() { +int main(void) { // 1. 乱数の生成(rand()) srand(time(NULL)); // 乱数の種を設定 int random_number = rand() % 100; // 0〜99の乱数を生成 From 45ace52d650ae71c4b9023b7e34c2fcfda30d50b Mon Sep 17 00:00:00 2001 From: watagassa <135955629+watagassa@users.noreply.github.com> Date: Mon, 14 Apr 2025 22:15:39 +0900 Subject: [PATCH 10/18] =?UTF-8?q?add:=E5=AE=9F=E8=A1=8C=E7=B5=90=E6=9E=9C?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textbook/c-lang/beginner/11--if-scope.mdx | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx b/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx index 6edb227..87da6da 100644 --- a/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx +++ b/src/content/docs/textbook/c-lang/beginner/11--if-scope.mdx @@ -40,6 +40,25 @@ int main(void) { return 0; } ``` + +実行結果1 +``` +点数を入力してください: 50 +不合格 +``` +--- +実行結果2 +``` +点数を入力してください: 60 +合格 +``` +--- +実行結果3 +``` +点数を入力してください: 70 +合格 +``` +---