# C++ 隨手筆記#3
>前一篇 [C++ 隨手筆記#2](https://hackmd.io/@Neroal/B1gXca1HI)
[TOC]
## AND/OR
### 規則表
- AND表示: ==**&&**==
- OR表示: ==**||**==
| A | B | A且B | A或B |
| -------- | -------- | -------- |----|
| ✔ | ✔ | ✔ |✔|
| ✔ | ✖ | ✖ |✔|
| ✖ | ✔ | ✖ |✔|
| ✖ | ✖ | ✖ |✖|
### 使用方式
``` C++
#include <iostream>
using namespace std;
int main()
{
int i=1;
int j=2;
int k=3;
int u=3;
if(i == k || i==j)
{
cout << "如果印得出來我就不會打這篇筆記了" << endl;
}
if(i < j || j >k)
{
cout << "這就是OR" << endl;
}
if(k==u && k > i)
{
cout << "這就是AND" << endl;
}
system("pause");
}
```
輸出結果 :tv:

## 陣列Array
### 甚麼是陣列
>[color=red]假設今天有一萬筆資料要儲存,你可以選擇創建一萬個變數去存放。但是這個做法很笨,而且之後你要去更改數字時也會很辛苦。Array就是用在這種時候,它就類似一個銀行金庫,可以將每筆資料儲存在特定編號的櫃子裡,要取用某筆資料時,只要輸入編號即可取用該筆資料
>[color=yellow]0是Array的起始**位置(index)**,假設有個Array的大小為3,則它可以存放的位置為Array[0]、Array[1]、Array[2]
### 使用方式
>[color=orange]創建Array的方式為 ==**資料型態**== ==**變數名稱[大小]**==
``` C++
#include <iostream>
using namespace std;
int main()
{
int arrayint[10];
for(int i=0;i<10;i++)
{
arrayint[i] = i;
cout << arrayint[i] << endl;
}
system("pause");
}
```
輸出結果 :tv:

## 列舉enum
### 甚麼是enum
>[color=red]我們常常在程式中使用數字來代表某個**狀態**,雖然這非常的直覺化,但是C++提供了enum這個更有效的方法來去切換不同的狀態
>[color=orange]下方PlayerStatus中的PS_Spawn就類似數字**狀態**的0,之後的狀態都會比上一個多加1。例如: PS_Spawn=0,PS_Move=1...以此類推
### 使用方式
>[color=yellow]創建方式為 ==**enum**== ==**函數名稱**== ==**{狀態一,狀態二... };**==
``` C++
#include <iostream>
using namespace std;
enum PlayerStatus
{
PS_Spawn,
PS_Move,
PS_Stop,
PS_Died
};
int main()
{
PlayerStatus states = PS_Spawn;
if(states == PS_Spawn)
{
cout << "State is PS_Spawn" << states << endl;
}
states = PS_Move;
if(states == PS_Spawn)
{
cout << "State is PS_Spawn" << states << endl;
}
system("pause");
}
```
輸出結果 :tv:

:::info
enum也可以自行定義狀態的數值,如同下方範例:
```C++
enum PlayerStatus
{
PS_Spawn=5,
PS_Move=100,
PS_Stop=500,
PS_Died=1000
};
```
:::
:::warning
**enum**跟函數一樣,如果創建在**main**之後,編譯器會不爽呦:exclamation:
:::
## 條件判斷Switch
### 甚麼是switch
>[color=red]這就類似if的進階用法,我們可以設立多個條件,當條件符合時就會執行特定的程式
### 使用方式
>[color=orange]開頭用法為 ==**switch(變數)**==,而條件設立的方式為 ==**case 條件:執行程式**==,最後再加上一個 ==**break**== 結尾
```c++=
#include <iostream>
using namespace std;
enum PlayerStatus
{
Idle,
Move,
Run
};
int main()
{
PlayerStatus states = Move;
switch (states)
{
case Idle: cout << "Idle" << endl;
break;
case Move: cout << "Move" << endl;
break;
case Run: cout << "Run" << endl;
break;
default: cout << "都不是" << endl;
break;
}
system("pause");
}
```
結果輸出 :tv:

:::info
可以在switch中加入預設default條件,如果所有的case皆不符合時就會執行default :mega:
:::
## 結構Struct
### 甚麼是Struct
>[color=red]假設我們今天輸入一筆學生的資料,包含姓名、年級、學號。你可能會想說那用三個變數去存就好。但是問題來了,假如一整間學校的學生都要記錄的話怎麼辦呢?struct的用處就派上用場啦,因為它可以把多種變數變成一個 ==**自訂義的資料型態**== 讓開法者去使用它,解決了上述的問題
### 使用方式
>[color=orange] ==**struct**== ==**結構命名**== ==**{變數一,變數二...}**==
```c++
#include <iostream>
#include<string>
using namespace std;
struct Student
{
string name;
int grade;
int id;
};
int main()
{
Student student = {"Neroal",4,4000123};
cout << "姓名:" << student.name << "學號:"
<< student.id << "年級:" << student.grade << endl;
system("pause");
}
```
顯示結果 :tv:

### Method用法
>[color=yellow]你可以在struct裡直接加入method使用,例如印出這位學生的學號、姓名...等等
>[color=lightgreen]如下方範例 ==**student.Print()**== 的用法就是所謂的 ==**方法(method)**==
```c++
#include <iostream>
#include<string>
using namespace std;
struct Student
{
string name;
int grade;
int id;
void Print()
{
cout << "姓名:" << name << "學號:"
<< id << "年級:" << grade << endl;
}
};
int main()
{
Student student = {"Neroal",4,4000123};
student.Print();
system("pause");
}
```
顯示結果 :tv:

>下一篇 [C++ 隨手筆記#4](https://hackmd.io/@Neroal/SJrOvzMSU)
###### tags: `C++` `AND/OR` `Array` `enum` `switch` `struct`