Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,61 @@ export default withMermaid({
link: "/cpp/chapter-3/problems/",
collapsed: true,
items: [
{ text: "Multiplication(★☆☆)", link: "/cpp/chapter-3/problems/multiplication" },
{ text: "Sum of n(★★☆)", link: "/cpp/chapter-3/problems/sum-of-n" },
{
text: "Multiplication(★☆☆)",
link: "/cpp/chapter-3/problems/multiplication",
},
{
text: "Sum of n(★★☆)",
link: "/cpp/chapter-3/problems/sum-of-n",
},
{ text: "4bit(★★☆)", link: "/cpp/chapter-3/problems/4bit" },
],
},
],
},
{
text: "[WIP] 4. 条件分岐",
text: "4. 条件分岐",
link: "/cpp/chapter-4/",
items: [
{ text: "[WIP] 4.1 if文", link: "/cpp/chapter-4/1" },
{ text: "[WIP] 4.2 演算子", link: "/cpp/chapter-4/2" },
{ text: "4.1 if文", link: "/cpp/chapter-4/1" },
{ text: "4.2 演算子", link: "/cpp/chapter-4/2" },
{
text: "4.Q 練習問題",
link: "/cpp/chapter-4/problems/",
collapsed: true,
items: [
{
text: "Positive or Negative(★☆☆)",
link: "/cpp/chapter-4/problems/positive-or-negative",
},
{
text: "Fraction(★★☆)",
link: "/cpp/chapter-4/problems/fraction",
},
],
},
],
},
{
text: "[WIP] 5. 型",
text: "5. 型",
link: "/cpp/chapter-5/",
items: [
{ text: "[WIP] 5.1 bool型", link: "/cpp/chapter-5/1" },
{ text: "[WIP] 5.2 double型", link: "/cpp/chapter-5/2" },
{ text: "[WIP] 5.3 string型①", link: "/cpp/chapter-5/3" },
{ text: "5.1 bool型", link: "/cpp/chapter-5/1" },
{ text: "5.2 double型", link: "/cpp/chapter-5/2" },
{ text: "5.3 string型①", link: "/cpp/chapter-5/3" },
{
text: "5.Q 練習問題",
link: "/cpp/chapter-5/problems/",
collapsed: true,
items: [
{
text: "Circle Area(★☆☆)",
link: "/cpp/chapter-5/problems/circle-area",
},
{ text: "Echo(★☆☆)", link: "/cpp/chapter-5/problems/echo" },
],
},
],
},
{
Expand Down
72 changes: 38 additions & 34 deletions docs/cpp/chapter-4/1.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# [WIP] 4.1 if文
# 4.1 if文

## 4.1.1 if

特定の条件を満たす時だけ実行したい時は、if 文を用いる。if の後に条件を記し、その後の `{}` に条件を満たしていた時だけ実行したい命令を記述する
何らかの処理を、**特定の条件を満たすときにだけ実行したい**場合は、if文と呼ばれる構文を用いる。`if`の後に**条件**を記し、その後の`{}`の中に条件を満たしていたときだけ実行したい命令を記述する

以下は、入力された点数が60点以上であれば「合格」、そうでなければ「不合格」と表示するプログラムである。

Expand All @@ -12,7 +12,7 @@ using namespace std;

int main() {
cout << "点数を入力してください。" << endl;
int score = 0;
int score;
cin >> score;

if (score >= 60) {
Expand All @@ -37,7 +37,7 @@ flowchart LR
condunder --> |No| e[End]
```

`score >= 60` は、 $score \geq 60$ と同じである。 `+-/*` と同じくして、条件を記述する演算子も存在する。演算子は以下の通り。
`score >= 60`は、$score \geq 60$と同じである。`+-/*`と同じくして、条件を記述する演算子も存在する。演算子は以下の通り。

| 演算子 | 数学の記号 |
|------|--------|
Expand All @@ -49,33 +49,32 @@ flowchart LR
| `!=` | $\neq$ |

:::warning
`==``=` を混同しないように注意。 `=` は**代入**`==` が**等価** である。
`==``=`を混同しないように注意。`=`は**代入**`==`が**等価**である。
:::

## 4.1.2 else

3.1.1. で示した例は、60点以上「でない」ときを `if (score < 60)` と記述することで実装した。ただ、実際には「そうでない時」を else
文によって簡単に記述できる。
4.1.1で示した例は、60点以上「でない」ときを`if (score < 60)`と記述することで実装した。ただ、実際には「そうでないとき」を**else文**によって簡単に記述できる。

```cpp:line-numbers
#include <iostream>
using namespace std;

int main() {
cout << "点数を入力してください。" << endl;
int score = 0;
cin >> score;
int score;
cin >> score;

if (score >= 60) {
cout << "合格です" << endl;
cout << "合格です" << endl;
} else {
cout << "不合格です" << endl;
cout << "不合格です" << endl;
}
}
```

::: tip
else 文は、if文の終わり( = `}` )の次に書く必要がある。
else文は、if文の終わり(=`}`)の次に書く必要がある。
:::

```mermaid
Expand All @@ -89,24 +88,24 @@ flowchart LR

## 4.1.3 else if

3.1.2. のコード に「満点だったら」という条件を足す。
4.1.2のコードに「満点だったら」という条件を足す。

```cpp:line-numbers
#include <iostream>
using namespace std;

int main() {
cout << "点数を入力してください。" << endl;
int score = 0;
int score;
cin >> score;

if (score == 100) {
cout << "満点です" << endl;
cout << "満点です" << endl;
} else {
if (score >= 60) {
cout << "合格です" << endl;
cout << "合格です" << endl;
} else {
cout << "不合格です" << endl;
cout << "不合格です" << endl;
}
}
}
Expand All @@ -124,23 +123,23 @@ flowchart LR
printunder --> e
```

ただ、このように書くのは冗長なので、`else if` と短縮する事が許されている。
ただ、このように書くのは冗長なので、`else if`と短縮する事が許されている。

```cpp:line-numbers
#include <iostream>
using namespace std;

int main() {
cout << "点数を入力してください。" << endl;
int score = 0;
int score;
cin >> score;

if (score == 100) {
cout << "満点です" << endl;
cout << "満点です" << endl;
} else if (score >= 60) {
cout << "合格です" << endl;
cout << "合格です" << endl;
} else {
cout << "不合格です" << endl;
cout << "不合格です" << endl;
}
}
```
Expand All @@ -149,23 +148,28 @@ int main() {

## 4.1.4 変数のスコープ

変数が使える範囲には制限があり、これを変数のスコープと呼ぶ
変数が使える範囲には制限があり、これを変数の**スコープ**と呼ぶ

具体的には、 `{}` の外からは変数にアクセスできない。
具体的には、`{}`の外からは変数にアクセスできない。

例えば次のソースコードで言えば、 変数 `z` は 4 ~ 6 行目でしか使用できない。
**8 行目は正しく実行できない。(コンパイルエラーとなる)**
例えば次のプログラムで言えば、変数`z`は8行目から10行目まででしか使用できない。**12行目は正しく実行できない。(コンパイルエラーとなる。)**

変数 `x` は 1行目から 9 行目まで好きなところで使用できる
変数`x`は5行目から13行目まで好きなところで使用できる

```cpp:line-numbers
int x = 8;
#include <iostream>
using namespace std;

int main() {
int x = 8;

if (x < 10) {
int z = 10;
cout << z << endl; // OK !
cout << x << endl; // OK !
if (x < 10) {
int z = 10;
cout << z << endl; // OK!
cout << x << endl; // OK!
}
cout << z << endl; // NG
cout << x << endl; // OK!
}
cout << z << endl; // NG
cout << x << endl; // OK !

```
37 changes: 28 additions & 9 deletions docs/cpp/chapter-4/2.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
# [WIP] 4.2 演算子
# 4.2 演算子

## 4.2.1 比較演算子

[4.1.1](1#_4-1-1-if)で説明したような、2つの値を比較する演算子のことを、**比較演算子**と呼ぶ。比較演算子を列挙した表を以下に再掲しておく。

| 演算子 | 数学の記号 |
|------|--------|
| `>=` | $\geq$ |
| `>` | $>$ |
| `<` | $<$ |
| `<=` | $\leq$ |
| `==` | $=$ |
| `!=` | $\neq$ |

## 4.2.2 論理演算子

条件が1つの変数のみに依存する時は `if``else` でわかりやすく書けた。
しかし、条件が2つ以上になるとif と else だけでは煩雑になっていく。
数学では「または」「かつ」 「でない」($\lor,\land, \lnot$) をよく使ったと思うが、プログラミングでもそれに当たるものが存在する。
条件が1つの変数のみに依存する時は`if``else`でわかりやすく書けた。では、条件が2つ以上ある場合はどうすればよいだろうか。if文の中にif文を書くことで、2つの条件の組み合わせを記述することができるが、煩雑にである

数学では「または」「かつ」「でない」($\lor,\land, \lnot$)をよく使ったと思うが、プログラミングでもそれに当たるものが存在する。これらを**論理演算子**と呼ぶ。論理演算子を用いることで、比較的簡単に、複数の条件の組み合わせを書くことができる

| 演算子 | 意味 | 意味・数学記号 |
|------|-----|-------------|
Expand All @@ -28,18 +39,26 @@ if (!(y >= 10)) {

## 4.2.3 演算子の優先順位

数学と同じように、演算子には優先順位がついている。優先順位自体は覚えなくても良いが、たまに優先順位が自分の想定と違うときがある。そのようなときは `()`
で式をくくることで、計算順序を変えられるので覚えると良い(数学と同じ)。
数学と同じように、演算子には優先順位がついている。優先順位自体は覚えなくても良いが、たまに優先順位が自分の想定と違うときがある。そのようなときは`()`で式をくくることで、計算順序を変えられる。

特に、 `<<` は比較演算子より優先順位が高い。
`cout << a <= b << endl;` と書くとコンパイルエラーとなってしまうので2行目のように書かなければならない。
特に、`<<`は比較演算子より優先順位が高い。
`cout << a <= b << endl;`と書くとコンパイルエラーとなってしまうので2行目のように書かなければならない。

```cpp:line-numbers
cout << ((a + b) * c) << endl;
cout << (a <= b) << endl;
```

::: tip
「じゃあ `<<` 演算子の優先度を一番低くすればいいじゃん」と思ったかもしれない。
「じゃあ`<<`演算子の優先度を一番低くすればいいじゃん」と思ったかもしれない。
しかし、実際には複雑な事情があるのだ……。
:::

::: tip 第4章のまとめ

* if文、else文で条件分岐ができます。
* `if(x==0)`はどのような条件を表していますか? `==`は比較、`=`は代入です。
* `<` `>` `<=` `>=`で大小の比較ができます。
* `&&` `||` `!`でAND, OR, NOTを扱えます。

:::
4 changes: 3 additions & 1 deletion docs/cpp/chapter-4/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# [WIP] 4. 条件分岐
# 4. 条件分岐

プログラムの中では、「**指定した条件を満たすときにのみ**」実行してほしい処理というものが存在する。第4章では、この「**条件分岐**」を扱う。
91 changes: 91 additions & 0 deletions docs/cpp/chapter-4/problems/fraction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 4.Q Fraction(★★☆)

## 問題

入力から整数$a,b,c,d$を受け取り、$\frac{a}{b}+\frac{c}{d}$を計算して、存在するなら分数`n/m`のような文字列として表示しよう。

結果を約分する必要はないものとします。

## 入出力例

### 例1

#### 入力

```Input:line-numbers
1 2 3 4
```

#### 出力

```Output:line-numbers
10/8
```

$\frac{1}{2} + \frac{3}{4} = \frac{10}{8}$

### 例2

#### 入力

```Input:line-numbers
1 3 5 7
```

#### 出力

```Output:line-numbers
22/21
```

$\frac{1}{3} + \frac{5}{7} = \frac{22}{21}$

### 例3

#### 入力

```Input:line-numbers
1 0 1 0
```

#### 出力

```Output:line-numbers
```

0除算は定義されないため、$\frac{1}{0}$は存在しません。

## ヒント

::: details ヒント1
今回は約分が必要ないので、最小公倍数を求める必要はありません。素直に掛け算で分母を求めてしまってよいでしょう。
:::

## 解答例

::: details 解答例

```cpp
#include <iostream>
using namespace std;

int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;

// 分母が0でないなら
if (b != 0 && d != 0) {
// 分母を計算 (今回は約分が不要なため、最小公倍数でなくともよい)
int denominator = b * d;

// 分子を計算
int numerator = a * d + c * b;

// 結果を出力
cout << numerator << "/" << denominator << endl;
}
}
```

なお、約分をしたい場合は`std::gcd`などを使って最小公倍数を得ることができます。
:::
Loading
Loading