# C/C++從入門到精通
[TOC]
程式入門須知
===
### 初學常犯der錯誤 :grinning_face_with_one_large_and_one_small_eye:
+ 常忘記在敘述行尾端打上<font color="#f11">分號(;)</font>
> cout << “hi”
+ 在不應該出現分號的地方多打
> If(a = b);
+ 大小寫沒區分
>C/C++語言規定大小寫字母是不同字元
+ 拼錯字
>像是將char打成chr,如果錯誤訊息像是( ‘ )刮起來<font color="#f11">‘ chr ‘ was not declared in this score 字樣</font>
+ 括弧未配成對
>有左括弧,就一定有右括弧(只要是括弧就一定是成對)
+ 引號未成對
>無論雙引號(用於字串)或單引號(用於字元)都要成對出現
+ 不該出現的空白符號
>像是namespace 打成name space 原本為連在一起的就不該隨意分開
cin輸入
===
`cin 物件可輸入文數字至指定的變數`
- **可輸入字元至變數a**
```cpp=
char a;
cin>>a;
cout<<"a"<<a<<endl;
```
- **可輸入整數至變數b**
```cpp=
int b;
cin>>b;
cout<<"b="<<b<<endl;
```
- **可輸入字串至變數c**
```cpp=
char c[5];
cin>>c;
cout<<"c="<<c<<endl;
```
### cin 範例程式
```cpp=!
#include<iostream>
using namespace std;
int main(){
char a; //只輸出輸入的第一個字元EX:輸入106輸出1
cin>>a;
cout<<"a="<<a<<endl;
int b;//只輸出整數若為0.2只輸出0
cin>>b;
cout<<"b="<<b<<endl;
char c[5];//可輸出多個字元EX:輸入106輸出106
cin>>c;
cout<<"c="<<c<<endl;
//下面這幾段也是同樣意思,可連續輸入3個變數EX:a 1 300 輸出a=a b=1 c=300 和前面一樣都可用連續輸入讓他輸出
cin>>a>>b>>c;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
return 0;
}
```
cout 輸出
===
`cout可輸出指定任何文數字與變數內容,且不用管變數的資料型態`
```cpp=
int a=5;
cout<<a;
//輸出5
```
##### 或是
```cpp=!
int a=5;
cout<<"a="<<a;
//其中第一個用雙引號表示字串內容,以字串方式輸出,第二個a,則是變數,將會輸出對應變數的內容也是輸出5
```
### cout 範例程式1
```cpp=
#include<iostream>
using namespace std;
int main(){
cout << "Good" <<endl;
int a =5;
cout << "a=" <<a<<endl;
return 0;
}
```
#### 若要進行輸出資料的欄位寬度、符點小數位數、輸出靠左或靠右
>##### 使用cout物件所提供的width、setf等屬性
#### width屬性可設定輸出資料的欄位寬度,預設向右靠齊
```cpp=
int a1=1 ,a2=10 ,a3=100 ,a4=1000 ,a5=10000;
cout.width(5);cout<<a1<<endl;
cout.width(5);cout<<a2<<endl;
cout.width(5);cout<<a3<<endl;
cout.width(5);cout<<a4<<endl;
cout.width(5);cout<<a5<<endl;
```
>輸出結果會是
1
10
100
1000
10000[color=#d35]
:::success
cout.width(5)
**在每個輸出前都要設定,有效期只有一次**
:::
```cpp=
cout.width(5)
cout<< a1 <<endl;
cout<< a2 <<endl;
//只對a1有效
```
### 範例程式2
```cpp=
#include<iostream>
using namespace std;
int main(){
int a1=100,a2=100;
cout.width(5);
cout << a1 <<endl;
cout << a2;
return 0;
}
```
輸出會是
100
100
只會影響到第一個的輸出位置(預設靠右)
#### **setf可輸出資料的小數點位數,資料靠左或靠右**
+ <font color="#f11">cout.setf(ios::fixed,ios::floatfield);</font>
#### `ios>>命名空間名稱__"::">>範圍運算子__ floatfield >>表示浮點數欄位`
+ <font color="#f11">cout.precision(2);</font>
##### `precision(2)>>浮點數精準度(小數後取兩位)`
+ <font color="#f11">cout.setf(ios::right,ios::adjustfield)</font>
#### `設定靠左(left)或靠右(right),adjustfield >>自動調整欄位`
### cout 範例程式3
```cpp
#include<iostream>
using namespace std;
int main(){
float b1=3.14159;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout.setf(ios::right,ios::adjustfield);
cout.width(8);
cout<<b1<<endl;
cout.width(8);
cout<<b1*20<<endl;
cout.width(8);
cout<<b1*200<<endl;
}
```
輸出結果
3.14
62.83
628.32
printf 輸出
===
`C語言採用printf()與scanf()作為輸出入函式`
<font color="#f11">printf()</font>可輸出結果,期中括號內需放置一對<font color="#f11">大括號("")</font>達成將欲輸出的資料當作引數傳給printf()函式
```cpp=
printf("a");
printf("aa");
```
>輸出結果為 aaa
##### 要跳列的話,需使用到<font color="#f11">跳脫字元 \n</font>
`通常使用換行符\n,在其他情況下則使用控制符endl。一個差別是,endl確保程序將立即顯示在屏幕上;而使用“\n”不能提供這樣的保證,這意味著在有些系統中,有時可能在您輸入信息後才會出現提示。`
```cpp=
printf("a\n");
printf("a\na");
```
>輸出結果
a
a
a
### printf 範例程式1
```cpp=
#include <iostream>
using namespace std;
int main() {
printf("a\n");
printf("a\nb\n");
return 0;
}
```
結果就是
a
a
b
#### 如果是變數的輸出,則必須在引數敘述中加一個列印格式控制輸出格式
### printf 範例程式2
```cpp=!
#include<iostream>
using namespace std;
int main(){
int a=3;
int b=5;
printf("a=%d\nb=%d",a,b);
// a,b會依序對應
// %d>>整數 %ld>>長整數 %f>>浮點數 %c>>字元 %s>>字串(字元陣列)
return 0;
}
```
結果是
a=3
b=5
scanf 輸入
===
`scanf()可輸入資料,跟printf同樣方式,兩者都需用到列印格式控制輸出入資料`
```cpp=!
輸入字元
char a;
scanf("%c,&a");
//%c>>字元 &a>>指定到a,&是取址符號用來取得欲放置輸入資料的變數位置
```
### scanf 範例程式1
```cpp=
#include <iostream>
using namespace std;
int main() {
char a;
printf("press any char: ");
scanf("%c", &a);
printf("char is %c",a);
return 0;
}
```
輸出結果>>
press any char: 1
char is 1
#### C語言的字串是由<font color="#f11">字元陣列</font>組成
#### 在輸入字串時,必須利用字元陣列完成輸入
### scanf 範例程式2
```cpp=
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
char c[6]; //一維陣列中有6個元素
printf("enter a string: ");
scanf("%s", &c); //%s>>字元陣列
printf("enter is %s", c);
return 0;
}
```
>此程式輸入是什麼輸出就是什麼
### scanf 範例程式3
```cpp=
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int a;
long b;
float c;
printf("enter an int: ");
scanf("%d", &a);
printf("enter an long int: ");
scanf("%ld", &b);
printf("enter an float: ");
scanf("%f", &c);
printf("\n your enter.... \n");
printf("int a =%d \n", a);
printf("long int b =%ld \n", b);
printf("float c =%f", c);
return 0;
}
```
#### 如果設int >>10,long int>>1.11
#### 則float會直接將長整數的0.11拿過來
輸出結果
enter an int: 10
enter an long int: 1.11
enter an float:
your enter.....
int a =10
long int b=1
float c=0.110000
string 字串
===
#### C因為要精簡編譯器,SO沒有string型態,所有字串處理都要使用字元陣列
#### C++新增string型態
`在使用前都要包含cstring巨集`
### string 範例程式
```cpp=
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string a;
cin >> a;
cout << "a=" <<a;
}
```
##### 兩個程式比對一下輸出結果na~~
```cpp=
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string b;
getline(cin,b);
cout << a<<endl;
cout << "b=" <<b ;
return 0;
}
//若要輸入含空白字串,則要使用getline()函式
```
若輸入HI HO
輸出結果
a = HI
b = HI HO
跳脫字元(Escape Sequence)
===
#### 字元中的單引號(')、雙引號(")及反斜線(\)均有定義功能
#### 若一定要使用這些字元,使用前須加反斜線(\)跳脫定義功能
_______________________
字元 | 跳脫字元序列
|:------:|:-----------:|
單引號 | \ \'
雙引號 | \ \"
反斜線 | \ \\
## 範例程式
```cpp=
#include<iostream>
#include<string>
using namespace std;
int main(){
string a= "HI" ;
cout << "a=" <<a<<endl;
string b= "\"HI\"" ;
cout <<"b=" <<b<<endl;
return 0;
}
```
輸出結果
a=HI
b="HI"
資料分類與型態
===
`下面整理C/C++語言數值資料型態,unsigned表示無號整數、即非負整數`
| 數值資料型態 | 占用記憶體大小 | 代表的數值範圍|
|:------:|:-----------:|:-----------:|
short | 16 | -32768 ~ +32767
int | 16 | -32768 ~ +32767
long | 32 | -2147483648 ~ +2147483647
unsigned short | 16 | 0 ~ 65535
unsigned int | 16 | 0 ~ 65535
unsigned long | 32 | 0 ~ 4294967295
float | 32 | 大約3.4E-38 ~ 3.4E+38(負數亦同)
double | 64 | 大約1.7E-308 ~ 1.7E+308(負數亦同)
long double | 80 | 大約3.4E-4932 ~ 3.4E+4932(負數亦同)
## 資料分類
### int(Integerts整數常數)的三種進位方式
* 十進位(Decimal)
* 十六進位(Hexadecimal),<font color="#f11">以OX、Ox開頭</font>
* 八進位(Octal),<font color="#f11">以O開頭</font>
+ OxA、OXB等類的均為16進制
+ 11、12等等的一般書寫數字方式為10進位
+ O72則為8進位
**可使用<font color="#f11">signed(有號)、unsigned(無號)</font>的short、int、long等的六種型態儲存整數**
>`整數預設型態是16bits的int型態`
________________________________________________________________________________
### float(Floating-poing literals浮點常數)
><font color="#f11">數字中含有小數點或指數的稱為浮點數</font>
以指數為例E、e表示10的次方
例如0.0023、2.3E-3、2.3e-3都是表示相同的浮點數或是2.3E+2>>230
浮點數可使用標準書寫>>24.101、1.123
科學符號>>2.3E+1、2.01e-4
**可以使用float、double、long double三種型態儲存浮點數常數**
#### <font color="#f11">double是浮點數預設值</font>,若要使用float,則應該要在數值後加上F、f
* 例如: double = 1.414,float = 1.414f
#### 而要用long double應在數值後加L、l
* 例如: long double = 1.414L
________________________________________________________________________________
### char(character literals字元常數),使用單引號(')圍住的!!單一字元!!
#### 例如'A'、'a'等
>***C/C++使用bits的ASCII,可表示256個字元包含<font color="#f11">大小寫英文字母、數字、標點符號、其他特殊符號</font>***
**char型態是8位元長**
________________________________________________________________________________
### String(字串)
#### 字串以雙引號" "圍住
>例如:"HI"、"你好"
##### 因C沒有字串型態,所以字串的使用要用字元陣列而C++新增string使用前要新增`#include<cstring>`
________________________________________________________________________________
### Boolean(布林值)
#### `使用1代表True、0代表False`
<font color="#f11">**布林型態是bool**</font>
變數宣告
===
#### `變數的功能是用來輸入、處理、儲存外界資料`
**變數在使用前要<font color="#f11">事先宣告</font>才能用**
>若變數未宣告的結果,編譯器便無法回應使用者在拼字上的錯誤,造成除錯的困難[color=#902bd9]
>>hi = hi +1;
上式若事先宣告hi
int hi;
沒宣告則編譯器遇到hi時,便會出現 hi 未宣告的錯誤訊息
**變數宣告的優點是可配置恰當的記憶體而提高資料的處理**效率[color=#901f7561]
____________________________________________
## C/C++變數宣告語法如下
資料型態 變數名稱 [=初值];
- int a; `宣告變數 a 為int型態`
- char b,c; `宣告變數b,c為char型態`
**變數宣告亦可連同初值一同設定**
- float e=1.1f; `宣告變數 e 為float型態`
- char r='n'; `宣告變數 r為 char型態,初值為n`
### 這段程式宣告bool型態
```cpp=
#include<iostream>
using namespace std;
int main(){
bool g,v;
g = 4>2; // 1 >> True
cout <<g<<endl;
v = 4<3; // 0 >>False
cout <<v<<endl;
cout << sizeof(v); //記憶體大小
return 0;
}
```
輸出結果
1
0
1
________________________________________________________
### C沒有字串型態要使用字串則要使用指標或字元陣列
**下列程式使用指標處理字串**
```cpp=
#include<iostream>
using namespace std;
int main(){
char *a="abcd";
char *t;
t="qwer";
cout<<a<<endl;//abcd
cout<<t<<endl;//qwer
a ="ok";
cout << a<<endl;
a=t;//qwer
cout <<a;
}
```
輸出結果
abcd
qwer
ok
qwer
_____________________________
**C++新增string型態**
`可宣告變數b為字串型態`
```cpp=
#include<iostream>
#include<string>
using namespace std;
int main(){
string b;
b ="QAQ";
cout <<b;
return 0;
}
```
輸出結果
QAQ
## 範例程式
```cpp=
#include<iostream>
#include<string>
using namespace std;
int main(){
int hi=0;
hi = hi + 1 ;
cout <<hi<<endl;
char *K;
char *N;
K ="OH";
N ="YA";
string S;
S = "HAHAHA";
cout << K<<endl;
cout << N<<endl;
cout << S;
return 0;
}
```
輸出結果
1
OH
YA
HAHAHA
#### 下面的程式碼出了許多錯誤,嘗試排錯先,再看答案 :smiling_face_with_smiling_eyes_and_hand_covering_mouth:
```cpp=
#include<iostream>
using namespace std;
int main(){
int hihi123;
int _a;
int %as;
int sum! ;
int Age#3;
int a c;
int c + 3;
int Score;
score =4;
int if;
return 0;
}
```
#### 找到錯誤了嗎? :serious_face_with_symbols_covering_mouth:
`下面是排錯後der程式`
```cpp=
int main(){
int hihi123;
int _a;
int as;//不能由符號開頭
int sum;//不可有!
int Age3;//不可有#
int a,c;//不可空白
int c; //不得含有加號
int Score;
Score =4; //大小寫錯誤
int hi; //if是保留字
return 0;
}
```
常數符號的宣告
===
**`常數符號需要記憶體儲存位置,與變數不同的是常數在整個程式中都不會也不會改變其值`**
### 程式設計有兩種表示常數的方式
>**文字式(Literal)**:直接以15或3.14表示某一常數[color=#a01d7961]
>**常數符號式(Symbolic)**:因有些數字在程式中會不斷重複出現,為了增加程式可讀性及減少程式鍵入麻煩,此時可以用有意義的符號代替這就是**常數符號宣告**[color=#509f7349]
#### C語言常數定義語法
`#define 常數名稱 常數值`
#### C++則新增了const宣告常數
`const 型態 常數名稱=常數值;`
## 範例程式
```cpp=
#include<iostream>
#define PIE 3.14 //常數符號通常用大寫表示
const double PI=100;
using namespace std;
int main(){
double a,b;
a=3*3*PIE; //3*3*3.14
b=5*5*PI; // 5*5*100
cout<<a<<endl<<b;
return 0;
}
```
輸出結果
28.26
2500
變數常數有效範圍
===
**`在一個大型程式中,一個程式是由一或數個函式組合而成
為了防止變數互相干擾,才有變數的有效範圍`**
#### 變數有效範圍如下>>
>任一變數的宣告,若無特殊聲明,均屬於<font color="#f11">**區域變數**</font>,有效範圍僅止於<font color="#f11">該變數所在程式區塊</font>
:::info
例如:
宣告在敘述區塊,則它的有效範圍僅止於該敘述區塊,別的敘述區塊是無法取得該區域變數的值
若宣告於函式中,則它的有效範圍僅止於該函式,其他函式均無法取得該區域變數的值
:::
:::spoiler
EX:
全域的a有效範圍是main()、aa()、bb()等全部函式
b、c的有效範圍則是main()、aa()中c、d有效範圍是aa(),但此c與main()中的c只是同名,他們各自占有不同記憶體,且其值<font color="#a09">不會互相干擾</font>
其次,bb()中的a是全域a,與main共用a,在bb()內的改變會傳回main();for敘述區塊內又宣告a,則此a僅在for內有效不會改變全域的a
但若在for敘述區塊中未宣告a,而誤用a,則a會改變全域的a;if敘述區塊也宣告a,則a僅在if內有效,不改變全域a
:::
`感覺看懂嘞對ㄅ
再來看程式碼吧!`
#### 範例程式1
```cpp=
#include<iostream>
using namespace std;
//int double PI=3.14;//全域常數
int a;//全域a
int main(){
void aa();
void bb();
int b,c;
a=10;//全域a
c=5;//main內c
aa();
cout<<"2.main的c="<<c<<endl;
bb();
cout<<"3.全域a="<<a<<endl;
for(int a=2;a<=3;a++)
cout<<"4.for內的a="<<a<<endl;
cout<<"5.for內的a="<<a<<endl;
for(a=2;a<=3;a++)//誤用
cout<<"6.for內的a="<<a<<endl;
cout<<"7.for內的a="<<a<<endl;
if(c>2){
int a;
a=1;
cout<<"8.if內a="<<a<<endl;
}
cout<<"9.for內的a="<<a<<endl;
return 0;
}
void aa()
{
int c,d;
c=3;//aa內的C
cout<<"1.aa內的c="<<c<<endl;
}
void bb()
{
a=8;//全域a
}
```
輸出結果
1.aa der c=3
2.main der c=5
3.global variable a=8
4.for der a=2
4.for der a=3
5.for der a=8
6.for der a=2
6.for der a=3
7.for der a=4
8.if der a=1
9.for der Ba=4
#### 範例程式2
```cpp=
#include<iostream>
using namespace std;
int a=1;
void aa(){
cout<<"1.a="<<a<<endl;//1
int a=2;
cout<<"2.a="<<a<<endl;//2
}
void bb(){
cout<<"4.a="<<a<<endl;//1
a=3;
}
int main(){
aa();
cout<<"3.a="<<a<<endl;//1
bb();
cout<<"5.a="<<a<<endl;//3
return 0;
}
```
輸出結果
1.a=1
2.a=2
3.a=1
4.a=1
5.a=3
變數資料型態轉換
===
`每個變數宣告後,即有屬於自己的型態,往後此變數均只能給相同型態變數儲存
若執行階段指定給不同型態的變數儲存,則稱此為型態轉換`
#### ==C/C++語言中型態轉換又可分為==
#### <font color="#c153">隱含轉換(Implicit Conversion)與強制轉換(Explicit Conversion)</font>
### 隱含轉換
`將值域小的型態轉為值域大的型態,稱為自動轉換或轉型(Convert)`
**<font color="#a13">此種轉換系統可以自動處理並確保資料不會流失</font>**
##### ==將short轉為long,因後者的值域比前者大,所以可順利轉換==
`下列敘述可將型態為long的變數a指派給型態long的變數b,且原值不會改變`
>short a=23;
long b;
b=a;
cout<<b;
結果是23
[color=#a95]
### 強制轉換
`將值域大的轉為值域小的型態,稱為強制轉換或稱鑄型(Cast)`,語法如下
>**變數1 = (變數1的型態) 變數2;**[color=#b54178]
##### 以下敘述可將型態是int的c變數指派給型態是short的d變數
>int c=23;
short d;
d=(short) c;
cout<<d;
結果是23[color=#a19]
<font color="#a09">強制轉換的風險大,可能導致資料流失或溢位</font>
##### 以下是將float型態強制轉換為short型態,將造成小數部分流失
>float e=3.4f;
short f;
f=(short) e;
cout<<f;
結果是3[color=#e54]
##### 以下敘述將long型態的g變數轉為short,將造成溢位
>long g=32769;
short h;
h=(short) g;
cout << h;
輸出為-32767[color=#b494]
#### 範例程式
```cpp=
#include<iostream>
using namespace std;
int main(){
short a=10;
long b;
b=a;
cout<<b<<endl;
char b1='a';
cout<<b1<<endl;
short b2;
b2=(short) b1;
cout<<b2<<endl;
int c=20;
short d;
d=(short) c;
cout<<d<<endl;
float e=3.4f;
short f;
f=(short) e;
cout<<f<<endl;
long g=32769;
short h;
h=(short) g;
cout<<h<<endl;
return 0;
}
```
輸出結果
10
a
97
20
3
-32767
運算子
===
`所謂運算子(Operator),指的是可以對運算元(Operand)執行特定功能的特殊符號`
>運算子一般分為五大類:
指派(Assignment)運算子
算數(Arithmetic)運算子
邏輯 (Logical) 運算子
關係(Relational)運算子
位元 (Bitwise) 運算子
[color=#b65656]
#### 每種運算子又可再細分為
+ **一元(Unary)運算子>>只需要一個運算元就可操作**
+ **二元(Binary)運算子>>需要兩個運算才可操作**
#### ==優先順序決定同一式子擁有多個運算子時,每個運算子進行運算的優先順序==
#### ==結合率決定同一敘述中,相鄰的運算子有相同優先順序的運算子執行順序==
### 指派運算子(Assignment Operator)
`指派運算子的符號為(=),其作用為將運算符號右邊運算式的值指派給運算符號左邊的運算元`
##### EX:
>int sum=0,a=3,b=5;
sum=a+b;//將a+b指派給sum[color=#b34]
***<font color="#a026">上述與數學的等號是不同的,所以不要懷疑為何0=8
其次是不能將常數放在指派運算子的左邊***</font>
##### EX:
>8=x;//不合法敘述
x=8//合法敘述
[color=#f53]
### 算數運算子(Arithmetic Operator)
`算術運算子用來執行一般數學運算`
#### 包含
:::info
+ 正負數 (+/-)
+ 加 (+)
+ 減 (-)
+ 乘 (*)
+ 除 (/)
+ 取餘數 (%)
+ 遞增 (++)
+ 遞減 (--)
:::
| 運算子 | 定義 | 優先順序 | 結合率 |
| :---------: | :------: | :-----------:| :----------:|
| ++/-- | 遞增/遞減 | 1 | 由左而右|
| +/- | 正負號,一元運算子 | 2 | 由右而左 |
| * | 乘法運算 | 4 | 由左而右 |
| / | 除法運算(商的型態同被除數) | 4 | 由左而右 |
| % | 求餘數(Modulus) | 4 | 由左而右 |
| +/- | 加法/減法運算 | 5 | 由左而右 |
#### 範例程式
```cpp=
#include<iostream>
using namespace std;
int main(){
int x=5,y=4;
float z=5;
cout<<"1="<<x+z<<endl; //5+5
cout<<"2="<<x-z<<endl; //5-5
cout<<"3="<<x*z<<endl; //5*5
cout<<"4="<<y/z<<endl; //4/5取商 //這裡的z是浮點數
cout<<"5="<<x/y<<endl; //5/4取商 //若不是浮點數不會輸出小數後位數
cout<<"6="<<x%y<<endl; //5/4取餘數
cout<<"7="<<-x+y/z<<endl; //-5+4/5
cout<<"8="<<x-y%y*z<<endl; //5-4%4*5
return 0;
}
```
>輸出結果
1=10
2=0
3=25
4=0.8
5=1
6=1
7=-4.2
8=5
[color=#f99]
<font color="#f18">**需要注意C/C++語言的算術運算子只有以上那些,若需要運算沒有運算子則需要運用到數學函式**</font>
:::warning
**例如C++沒有次方運算子(^)與開根號
要算次方的話要使用到pow()函式**
>a=pow(3,2);//include<math.h>或<cmath>[color=#a77]
**要算根號的話要使用到sqrt()函式**
>x=sqrt(9)//include<math.h>或<cmath>[color=#a77]
:::
### 遞增(++)及遞減(- -)
`這兩個運算子又分為前置與後置`
#### 前置是在運算子之前
EX:
>a=1;
++a;
cout<<a<<endl;//2[color=#f90]
#### 後置是運算子在運算元後
==不論 \++a,a++都是將值加1放回a,但是兩個執行結果還是有差異的==
EX:
>a=1;
b=++a;//先執行a=a+1;在執行b=a; 輸出 a=2 b=2[color=#a10]
#### 和
>a=1;
b=a++;先執行b=a;在執行a=a+1; 輸出 a=2 b=1[color=#f20]
**其a值均會加1,但b值會有差異,前置b值會得到+1的結果,後者能得到原a值**
#### 範例程式
```cpp=
#include<iostream>
using namespace std;
int main(){
int a=1;
int b,d,r,e;
b=++a;//a=1+1=2=b
d=++a;//此時a是2,a=2+1=d
e=a++;//a還是2,e=a=2,2+1=3=a
r=a++;//a現在是3,r=a=3,3+1=4=a
cout<<b<<endl;
cout<<d<<endl;
cout<<e<<endl;
cout<<r<<endl;
return 0;
}
```
輸出結果
2
3
3
4
### 關係運算子(Relational Operator)或稱比較運算子(Comparison Operator)
`用於資料之間的大小比較的結果可得到int型別的1(True)或0(False)`
EX:
>if(a==b);//比較ab是否相等[color=#d48]
| 運算子 | 定義 | 優先順序 | 結合率 |
| :---------: | :------: | :-----------:| :----------:|
| < | 小於 | 6 | 由左而右|
| > | 大於 | 6 | 由左而右 |
| <= | 小於等於 | 6 | 由左而右 |
| >= | 大於等於 | 6 | 由左而右 |
| == | 等於 | 7 | 由左而右 |
| != | 不等於 | 7 | 由左而右 |
#### 範例程式
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,c;
a=7>3;
cout<<a<<endl;//T
a=3>=7;
cout<<a<<endl;//F
b=7;
c=3;
a=b==c;
cout<<a<<endl;//F
b=7;
c=3;a=b!=c;
cout<<a<<endl;//T
return 0;
}
```
>輸出結果
1
0
0
1
[color=#a08]
### 邏輯運算子(Logical Operator)
`當同一個運算式要同時存在兩個以上比較運算子,每兩個比較運算子之間必須使用邏輯運算子連結`
| 運算子 | 定義 | 優先順序 | 結合率 |
| :---------: | :------: | :-----------:| :----------:|
| ! | 一元邏輯補數運算(NOT) | 2 | 由右而左 |
| & | 完全評估的AND運算 | 8 | 由左而右 |
| ^ | 完全評估的XOR運算 | 8 | 由左而右 |
|\| | 完全評估的OR運算 | 8 | 由左而右 |
| && | 快捷的AND運算 | 9 | 由左而右 |
|\|\|| 快捷的OR運算 | 9 | 由左而右 |
#### 範例程式
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,x,y;
x=5;
y=4;
a=x>y & y==3; //T & F = F
cout<<a<<endl;
b=x>3 || y<5;
cout<<b<<endl; //T | T = T
return 0;
}
```
>輸出結果
0
1
[color=#c38]
___
##### 完全評估系列的運算子==一定會把兩個關係運算子拿來運算==
***相反地***
#### 快捷系列的運算子==會先算出第一個關係運算子的值==
**如果這個值已經可以決定出整個運算式的結果**
**==快捷系統的運算子就不會對第二個關係運算子進行運算==**
EX:
>boolean1=(3>7)&(5>2);
boolean2=(3>7)&&(5>2);//比boolean1更快完成運算
[color=#f49]
## 範例實作
:::danger
1.若有一數學式,判斷x是否滿足1<x<=6,請轉換C/C++敘述
2.若有一數學式,同時判斷4個變數是否滿足(a1/a2)=(b1/b2),請轉換C/C ++敘述
3.(a1/a2)=(b1/b2)!=(c1/c2)又如何表示
(記得要自己打過程式,題目沒有標準答案der)
:::
__________________________
```cpp=
//第1題
#include<iostream>
using namespace std;
int main(){
int x;
cin>>x;
x=x>1 & x<=6;
cout<<x<<endl;
return 0;
}
```
```cpp=
//第二題
#include<iostream>
using namespace std;
int main(){
int a1,a2,b1,b2;
int A;
cin >> a1;
cin >> a2;
cin >> b1;
cin >> b2;
A=(a1/a2) == (b1/b2);
cout<<A;
}
```
```cpp=
//第3題
#include<iostream>
using namespace std;
int main(){
int a1,a2,b1,b2,c1,c2;
cin >> a1;
cin >> a2;
cin >> b1;
cin >> b2;
cin >> c1;
cin >> c2;
if((a1/a2)==(b1/b2)!=(c1/c2)){
cout<<"T";
}
else{
cout<<"F";
}
return 0;
}
```
_____________________________
### 複合指派運算子
`結合指派與算術、關係、邏輯運算子`
***這是C/C++特有的運算子***
##### EX:
>sum=sum+5;
又等於
sum+=5;[color=#c87]
| 運算子 | 定義 | 優先順序 | 結合率 |
| :---------: | :------: | :-----------:| :----------:|
| = | 指派設定內容 | 11 | 由右而左 |
| += | 相加後再設定內容 | 11 | 由右而左 |
| -= | 相減後再設定內容 | 11 | 由右而左 |
| * | 相乘後再設定內容 | 11 | 由右而左 |
| /= | 相除後再設定內容 | 11 | 由右而左 |
| &= | AND運算後再設定內容 | 11 | 由右而左 |
| \|= | OR運算後再設定內容 | 11 | 由右而左 |
| ^= | XOR運算後再設定內容 | 11 | 由右而左 |
-------------------------------
### 位元操作運算子(Bitwise Operator)
`位元(Bitwise)操作運算子是將所運算的整數,先轉為二進制,在逐位元進行運算`
**可分為**
>位移(shift)運算子與布林運算子
位移運算子是逐位元向左或是向右移
布林運算子則可以逐位元進行布林運算[color=#a347]
| 運算子 | 定義 | 優先順序 | 結合率 |
| :---------: | :------: | :-----------:| :----------:|
| ~ | 對各位元進行補數運算 | 2 | 由右而左 |
| << | 考慮正負位元的向左位元 | 5 | 由左而右|
| >> | 考慮正負位元的向右位元 | 5 | 由左而右 |
| & | 位元AND運算 | 2 | 由左而右 |
| \| | 位元OR運算 | 8 | 由左而右 |
| ^ | 位元XOR運算 | 8 | 由左而右 |
| <<= | 向左位移之後再指派內容 | 11 | 由左而右 |
| >>= | 向右位移之後再指派內容 | 11 | 由左而右 |
#### 數位邏輯幫大家整理一下
0 >>True
1 >>Falst
____________________
:::success
AND >>遇 0 則 0
NAND >>遇 0 則 1
OR >>遇 1 則 1
NOR >>遇 1 則 0
XOR >>相同為 0 不同為 1
NXOR >>相同為 1 不同為 0
:::
____________________
```cpp=
#include<iostream>
using namespace std;
int main(){
int i,j,k;
i=1; //0000 0001
i=~i;//1111 1110取2補數
cout<<"1="<<i<<endl;//-2(10進制)
i=6; //i=00000110
i=i<<2;//i=00011000
cout<<"2="<<i<<endl;
i=i>>3;
cout<<"3="<<i<<endl;//i=00000011
i=3; //i=00000011
j=2; //j=00000010
k=i & j;//k=00000010//遇0則0
cout<<"4="<<k<<endl;
k=i | j;//k=00000011//遇1則1
cout<<"5="<<k<<endl;
k=i ^ j;//k=00000001//相同為0不同為1
cout<<"6="<<k<<endl;
return 0;
}
```
輸出結果
1=-2
2=24
3=3
4=2
5=3
6=1
__________
### 運算子的優先順序
`同一敘述若同時有多個運算子,此時需定義運算子的優先順序`
>x=a+b*c;
由以上各運算子的「優先順序」可知,乘號(*)的優先順序是第4而加號(+)則是第5
**x=(a+(b*c));//和上述結果相同**
#### 同理
>x=a>b & b>z;
#### 同義
>x=(a>b) & (b>z);
#### 下面式子左右結果是相同的
| x=x+++2; | x=(x++)+2; |
| :---------: | :---------: |
| x=x+y*2+z++; | x=((x+(y*2))+(z++)); |
| z=x>2 & y>3; | z=((x>2)&(y>3)); |
### 範例程式
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,c;
int x,y,z;
x=2;
x=x+++2;
cout<<"X1="<<x<<endl;
x=1;y=3;z=5;
x=x+y*2+z++;//1+3*2+5=12,x=12=Z++
cout<<"X2="<<x<<endl;
x=1;y=4;
z=x>2 & y>3;
cout<<z<<endl;
return 0;
}
```
### 運算子的結合律
`當同一敘述,相鄰的運算子擁有相同優先順序的運算子,此時即需定運算子是左結合或右結合`
>x=z-b-c;[color=#a08]
**同是減號(-),優先順序相同,此時就要靠定義結合律,減法結合律是由左至右**
>x=((a-b)-c);[color=#a08]
###### 而
>x=y=z=2;[color=#a08]
###### 指派運算子的結合律由左至右,上式同義於
>(x=(y=(z=2)));[color=#a08]
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,c;
int x,y,z;
a=2;
b=3;
c=4;
x=a-b-c;
cout<<x<<endl;
x=y=z=2;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
return 0;
}
```
輸出結果
-5
x=2
y=2
z=2
運算式
===
`任何一個可求得一個值的式子,稱為一個運算式(Expression)
例如5+3會回傳一個數值,所以5+3是一個運算式`
**這些運算式通常放在cout<<運算式,<font color="#f18">一般而言可以放在等號右邊的東西</font>,都可以稱為運算式**
_______________
### 敘述(Statement)
#### 凡是控制執行的順序、對運算式取值或不做任何事均可稱為敘述
所有的敘述都要以分號(;)作結束,例如
>sum=sum+1;[color=#a90467]
**==於C/C++語言中,若前一個敘述未以分號結束,則錯誤訊息通常會出現在下一個敘述的開頭==**
________________
### 敘述區塊(Block Statement)或複合敘述(Compound Statement)
`在任何可以放上單一敘述的地方,你都可以放上敘述區塊,一個複合敘述是由兩個大括號組合而成`
**<font color="#a08">但大括號後不可再加分號</font>**
>{
t=a;
a=b;
b=t;
}[color=#f49]
__________________
### 註解(Comments)
`適當的程式註解才能增加程式可讀性`
> /\*我是註解*/
//我是註解[color=#c39429]
#### 上式<font color="#f18">/\*符號以後的字串視為註解,編譯程式不予處理,直到遇到*/</font>為止
#### 還有同一列中,雙斜線(//)後面也視為註解,編譯器均不予處理
>x=x+3; /* x值加3 */
>sum=sum+y; //將y值加至sum[color=#b499]
#### ==前者因為可超過兩列,較適合編寫較長的註解==
#### ==後者僅能寫在同一列==
___________________
### 縮排與空行
`除了適當的註解,程式設計應善用縮排與空行,才可以使程式讓人易於閱讀
因為有了縮排,程式才有了層次感,其次,空行則可使段落更加明顯`
#### 程式架構介紹
**C/C++程式架構如下:**
#### <font color="#f18">大致分為四大區塊,分別是</font>
+ 函式引用區
+ 宣告區(類別、函式、全域變數宣告區)
+ 主程式區
+ 類別、函式實作區
| 編號 | 標題 | 程式 |
| :--------: | :-------------------------------: | :---------- |
| 一 | 函式引用區(引入所需標頭檔) | #include <iostream>、#include<math.h> |
| 二 | 命名空間、類別、函式、全域變數宣告區 | using namespace std; <br>int add(int a,int b);<br>int c; |
| 三 | 主程式區main() | int main(){ <br>c=add(6,2);<br> cout<<c; <br>return 0;<br> } |
| 四 | 類別、函式實作區 | int add(int a,int b){<br>int c;<br> c=a+b;<br> return ( c );<br>} |
## 範例程式
```cpp=
#include <iostream>
#include<math.h>//函式引用區
using namespace std;//命名空間、類別、函式、全域變數宣告區
int add(int a,int b);//函式宣告
int c;
int main(){ //主程式區
c=add(6,4); //6+4
cout<<c; //10
return 0;
}
int add(int a,int b){//類別或函式實作區
int c;
c=a+b;
return (c);
}
```
#### 但是函式、類別實作區若與主程式區調換,則函式與類別不用宣告
```cpp=
#include <iostream>
#include<math.h>//函式引用區
using namespace std;//命名空間、類別、函式、全域變數宣告區
int add(int a,int b);//函式宣告
int c;
int add(int a,int b){//類別或函式實作區 //a,b不可省略
int c;
c=a+b;
return (c);
}
int main(){ //主程式區
c=add(6,4); //6+4
cout<<c; //10
return 0;
}
```
兩種寫法輸出都是10
實例應用
===
==**記得要自己想過寫過呦**==
#### 第一題
`試寫一程式,可輸入長方形的長與寬,並計算周長與面積`
:::info
長方形面積公式>>長乘寬
長方形周長公式>>2(長加寬)
:::
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,c,d;
cout<<"Input length and width:";
cin >>a>>b;//a為長、b為寬
c = 2*(a+b); //周長
d = a*b;//面積
cout<<"c="<<c<<endl;
cout<<"d="<<d<<endl;
return 0;
}
```
#### 第二題
`輸入長方體長寬高計算表面積與體積`
:::success
長方體表面積=(長x寬+寬x高+高x長)x2
長方體的體積公式=長×寬×高
:::
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,c;//長:a 寬:b 高:c
int x,y;
cout<<"Input length, width and height";
cin>>a>>b>>c;
x=(a*b+b*c+c*a)*2;//面積
y=a*b*c;//體積
cout<<"rectangular Volume="<<y<<endl;
cout<<"rectangular area="<<x;
return 0;
}
```
#### 第三題
**試寫一程式,滿足以下條件
1.可以輸入兩個座標
2.計算兩點座標距離
3.輸出此兩點距離**
```cpp=
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int x1,x2,y1,y2;
double d;//距離
cout<<"input x1,x2,y1,y2"<<endl;
cin>>x1>>x2>>y1>>y2;
d=sqrt(pow((x1-x2),2)+pow((y1-y2),2)); //pow是次方 sqrt是開根號
cout<<"The distance of the two points is:"<<d<<endl;
return 0;
}
```
#### 第四題
**試寫一程式,可輸入三角形三邊長a、b、c求面積**
```cpp=
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int a,b,c;
double d,e;
cout<<"Input triangle "<<endl;
cin>>a>>b>>c;
d=(a+b+c)/2;
e=sqrt(d*(d-a)*(d-b)*(d-c));
cout<<"The triangle area is:"<<e<<endl;
return 0;
}
```
==這裡補充一下演算法則==
**兩個數要交換,就如同兩杯水要交換
假設你有兩個杯子的水(分別為糖水a、鹽水b),交換其內容,方法如下**
>1.先找來空杯子t
2.將a杯子的水倒入t杯子
t=a;
3.將b杯子的水倒入a杯子
a=b;
4.將t杯子倒入b杯子,而完成兩杯子的交換
b=t;[color=#a53]
**其次,若未先找來空杯子t,而直接將b杯子的水往a杯子倒,再將a杯子的水往b杯子倒**
>a=b;
b=a;[color=#f46]
**則執行a=b時,如果這時a杯中糖水已經沒了,此時在執行b=a,會出現錯誤結果**
#### 第五題
>試寫一程式,滿足以下條件
1.可以輸入兩個數字
2.交換此兩個數字
3.輸出交換的結果
[color=#f50]
```cpp=!
#include<iostream>
using namespace std;
int main(){
int a,b,z;
//cout<<"Input 2 digit:"<<endl;
//cin>>a>>b; //試比較兩種輸入輸出
printf("input a:");
scanf("%d",&a);// %d是整數 &取得a變數位址
printf("input b:");
scanf("%d",&b);
z=a;
a=b;
b=z;
//cout<<"Exchange import numerical a,b aftermath:<<endl<<"a="<<a<<endl<<"b="<<b<<endl;
printf("after swap a=%d b=%d",a,b);
return 0;
}
```
#### 第六題
**試寫一程式,可以輸入三個數,並將1交給2、2交給3、3交給1並輸出三個數的結果**
```cpp=!
#include<iostream>
using namespace std;
int main(){
int a,b,c; //a>>1,b>>2,c>>3
cout<<"input 3 numberal:"<<endl;
cin>>a>>b>>c;
b=a;
c=b;
a=c;
printf("Exchange import numerical a,b,c aftermath: a=%d b=%d c=%d ",a,b,c);
return 0;
}
//輸出結果有接力棒的fu
```
#### 第七題
**假設某次考試成績如下55、66、77、88、99**
>1.試寫一程式輸入以上資料
2.輸入以上資料
3.計算總和
4.輸出總和與平均[color=#f568]
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,e;
double x,y;
a=55;
b=66;
c=77;
d=88;
e=99;
//cout<<"input 5 achievement"<<endl;//如果是手動輸入成績的話
//cin>>a>>b>>c>>d>>e;
x=a+b+c+d+e;
y=x/5;
cout<<"sum = "<<x<<endl;
cout<<"averagely = "<<y;
return 0;
}
```
==補充一下==
:::danger
如果遇到大量需要輸入的資料千萬不要這樣一個一個慢慢輸入呦
想知道如何撰寫這樣的程式,會在迴圈與陣列的章節說到
:::
#### 第八題
**兩點式,兩點可決定一直線(ax+by+c=0)
試寫一程式,可輸入兩個二為座標,並求出此直線方程式**
***例如:輸入(2,1)、(4、6)則直線方程式為5x-2y-8=0***
```cpp
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int x1,y1,x2,y2;
int a,b,c,x,y;
cout<<"input x1,y1"<<endl;
cin>>x1>>y1;
cout<<"input x2,y2"<<endl;
cin>>x2>>y2;
x=x2-x1;
y=y2-y1;
a=y;
b=-x;
c=-y*x1+x*y1;
cout<<a<<"x+"<<b<<"y+"<<c<<"=0";
return 0;
}
```
應用複習
==
**1.試寫一程式,可輸入一個一元二次方程式,求解(設本例假設方程式有二解)**
:::info
◎用公式法解一元二次方程式 ax^2 + bx + c = 0 的步驟如下:
(一) 找出二次項係數 a,一次項係數 b,常數項 c。
(二) 算出 b^2-4ac 的值。
(三) 若 b^2-4ac > 0,則 x =−𝑏±√𝑏^2−4𝑎𝑐/2𝑎
若 b^2-4ac = 0,則 x =−𝑏/2𝑎(重根) 。
若 b^2-4ac < 0,則 x 無解。(在國中階段無解)
:::
==範例輸出2x^2-7x+3=0,x1=3,x2=0.5==
```cpp=
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a,b,c,d;
double x1,x2,com;
cout<<"input abc "<<endl;
cin>>a>>b>>c;
// 0==(pow((a*x1),2))+(b*x2)+c;
if (b*b-4*a*c>0){
d=sqrt(b*b-4*a*c);
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
}
if (b*b-4*a*c==0){
// d2=sqrt(b*b-4*a*c);
x1=-b/(2*a);
x2=-b/(2*a);
}
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2;
}
```
**2.試寫一程式,可以輸入一個二元一次方程式,求其解(本例假設方程式洽有一解)**
==提示:使用克拉瑪公式==
```cpp=
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a1,a2,b1,b2,c1,c2,d;
double x,y;
cout<<"input a1_b1_c1_a2_b2_c2"<<endl;
cin>>a1>>b1>>c1>>a2>>b2>>c2;
// c1=((a1*x)+(b1*y));
// c2=((a2*x)+(b2*y));
d=((a1*b2)-a2*b1);
x=((c1*b2)-c2*b1)/d;
y=((a1*c2)-a2*c1)/d;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
```
**3.試寫一程式,可以輸入一個三元一次方程式,並求其解(本例假設所輸入的方程式洽有一解)**
```cpp=!
#include<iostream>
//#include<cmath>
using namespace std;
int main(){
double a1,a2,a3,b1,b2,b3,c1,c2,c3;
double d1,d2,d3,e,x,y,z;
cout<<"input a1_b1_c1_d1_a2_b2_c2_d2_a3_b3_c3_d3"<<endl;
cin>>a1>>b1>>c1>>d1>>a2>>b2>>c2>>d2>>a3>>b3>>c3>>d3;
e=((a1*b2*c3)+(a2*b3*c1)+(a3*b1*c2)-(a3*b2*c1)-(b3*c2*a1)-(c3*b1*a2));
z=((a1*b2*d3)+(b1*d2*a3)+(d1*b3*a2)-(d1*b2*a3)-(d2*b3*a1)-(d3*b1*a2))/e;
x=((d1*b2*c3)+(b1*c2*d3)+(c1*b3*d2)-(c1*b2*d3)-(c2*b3*d1)-(c3*d2*b1))/e;
y=((a1*d2*c3)+(d1*c2*a3)+(c1*d3*a2)-(c1*d2*a3)-(c2*d3*a1)-(c3*d1*a2))/e;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z;
}
```
**4.計算自由落體的距離與時間關係,公式為d=v0t+gt^2/2**
```cpp=
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double v0,t,g,d;
cout<<"input v0,t,g"<<endl;
cin>>v0>>t>>g;
d=v0*t+(g*t*t)/2;
cout<<"d="<<d;
return 0;
}
```
**5.請分別輸入1個0到9的整數,並將它合併為1個整數**
==例如:輸入1、2、3、4則輸出1234==
```cpp=
#include<iostream>
using namespace std;
int main(){
int i;
cin>>i;
cout<<"your input is:"<<i<<endl;
}
```