review

周妍希 戴瑢溱 鄭云晶 廖姸惁

C++概論

C++ Introduction

#include <iostream>
using namespace std;
int main(){
	cout<<"zsisc32nd"<<endl;
    return 0;
}

標頭檔

將函式庫<iostream>引入

<bits/stdc++.h>萬用函式庫

<stdio.h>C語言函式庫

#include <iostream>
using namespace std;
int main()
{
    cout<<"zsisc"<<endl;//中山資研
    cout<<"IZCC"<<endl;
    /*
    建中資訊
    中山資研
    成功電研
    景美電資
    */
    return 0;
}

註解

//單行註解

/*

多行

註解

*/

int (整數)

long long (長整數)

佔用空間:4Bytes

範圍:-2^31~2^31-1

佔用空間:8Bytes

範圍:-2^63~2^63-1

int a=31;
long long a=100000000;

float (浮點數)

double (倍精度浮點數)

佔用空間:4Bytes

範圍:小數點後第七位

佔用空間:8Bytes

範圍:小數點後第十五位

float a=9.12;
double a=3.111111111;

char (字元)

string (字串)

佔用空間:1Byte

0~255(ASCII)

char a='?';
string a="zsisc._.31st";

bool (布林)

佔用空間:1Byte

false(0)/true(1)

bool yes=true;
bool no=false;

變數命名時禁用特殊字(int,true),開頭不能使用數字

記得每行最後都要打分號!

輸入輸出

cin cout

輸入>>

輸入一個變數

int a;
cin>>a;

輸入多個變數

int a,b,c;
cin>>a>>b>>c;

輸出<<

輸出一個值

輸出多個值

string classnum="209";
cout<<classnum<<endl;
string name;
cout<<"We are "<<name<<" !\n";

註:可藉由 endl 或 "\n" 換行

指定運算子

a = 5+10

等號右邊先進行運算

結果指定給左邊變數

算數運算子

+
-
++ 遞增1
-- 遞減1
*
/
% 取餘數
int a=10,b;
b=a+5;
cout << b;//15

簡寫

a=a+1 => a+=1

a=a-1=>a-=1

比較運算子

> 大於
>= 大於等於
< 小於
<= 小於等於
== 等於
!= 不等於
#include <iostream>
using namespace std;
int main() {
    int a=3;
    bool q;
    q=a<5;
    cout<<q;//1
    return 0;
}

邏輯運算子

&&
||
!
#include <iostream>
using namespace std;
int main() {
    int a=3;
    bool q;
    q= a!=0 && a>10;
    cout<<q;//0
    return 0;
}

條件假設

if else

if (條件) {
   // 條件成立時執行
} 
else {
    // 上述條件不成立時執行

條件判斷

if (分數 >= 90) {
    cout << "A";
} 
else if (分數 >= 80) {
    cout << "B";
} 
else {
    cout << "C";
}

ex:

邏輯運算子

>= 大於等於
<= 小於等於
!= 不相等
== 相等
|| OR(有一個真就為真)
&& AND(兩個都真才為真)
! NOT(反向)

比較運算子

continue:跳過這回合

break:中途結束流程

for (int i = 1; i <= 100; i++) {
    if (i % 7 == 0) {
        cout << i;
        break;   // 找到就結束
    }
}
for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0)
        continue;   // 偶數跳過
    cout << i << endl;
}

7

1  3 5 7 9

三元運算子

int score = 75;
cout << (score >= 60 ? "及格" : "不及格");
條件式 ? 條件式符合時執行: 條件式不符合時執行

及格

ex:

迴圈

loop

for 迴圈

次數迴圈

for (int i = 0; i < 5; i++) {
    cout << i << endl; 
}

while 迴圈

條件迴圈

int x = 0;
while (x < 5) {
    cout << x << endl;
    x++;
}

*條件成立就一直跑

do-while 迴圈

至少跑一次

int x = 0;
do {
    cout << x << endl;
    x++;
} while (x < 5);

*先執行,再判斷條件

一維陣列

array

宣告

int a[5] = {50, 10, 25, 30, 1};

陣列型態

陣列名稱

陣列大小

內容物(不一定要設)

賦值

(一)

[0] [1] [2] [3] [4]
50 10 25 30 1

a

int a[5] = {50, 10, 25, 30, 1};
int b[3] = {10};

宣告的同時賦值

陣列型態 陣列名稱[陣列大小] = {值};

[0] [1] [2]
10 0 0

b

沒有特別賦值時,為0

賦值

(二)

[0] [1] [2] [3]
100 200 300 400

a

int a[4];
a[0] = 100;
a[1] = 200;
a[2] = 300;
a[3] = 400;

陣列名稱[索引值] = 值;

賦值

(三)

[0] [1] [2]
55 55 55

a

 int a[3];
 for(int i=0 ;i<3 ;i++){
 	a[i] = 55;
 }

利用迴圈

關於陣列

int N;
cin>>N;
int array[N];
/*
進行對該陣列的操作
*/

在開陣列大小時,盡量避免把「沒有賦值過的變數」設為陣列大小,有可能會報錯

int N;
int array[N];
/*
進行對該陣列的操作
*/
cin>>N;

錯誤示範

正確示範

關於陣列

int a[] = {1,2,3,4,5,6,7,8,9,10};

也可以不用指定 arraysize,如下:

則 C++ 會自動偵測裡面的元素個數,並且制定大小為 10,同上式。

int a[5] = {};

有{ }代表有賦值,若賦值為空(empty),則預設值為0

[0] [1] [2] [3] [4]
0 0 0 0 0

a

二維陣列

array

定義及宣告

int a[2][3] = {{10, 25, 30},{20, 15, 35}}

陣列型態

陣列名稱

陣列大小

值(不一定要設)

定義及宣告

int a[2][3] = {{10, 25, 30},{20, 15, 35}}

0

1  

2

0

1

10

25

20

15

30

35

定義及宣告

int a[2][3] = {{10, 25, 30}{20, 15, 35}}

0

1  

2

0

1

10

25

20

15

30

35

a[0][0]=10

a[0][1]=25

a[0][2]=30

a[1][0]=20

a[1][1]=15

a[1][2]=35

賦值

#include <iostream>
using namespace std;
int main(){

    int a[2][3];
    a[0][0]=10;
    a[0][1]=25;
    a[0][2]=30;
    a[1][0]=20;
    a[1][1]=15;
    a[1][2]=35;
    
    return 0;
}
#include <iostream>
using namespace std;
int main(){
    int a[2][3];
    
    for(int i=0;i<2;i++)
    	for(int j=0;j<3;j++)
            cin >> a[i][j];
            
    return 0;
}

指定

迴圈

有兩種方式

函式 & 遞迴

function & recursion

宣告(declaration)

回傳值 函式名稱(參數);

回傳值(資料型別) Ex. int, double, char...

函式名稱 : 和變數名稱一樣,不可使用保留字,且第一個字元必須是字母

回傳值 函式名稱(參數){
   body of the function
}

定義(definition)

 告訴編譯器「這個函式存在」

實際寫出他要做的事

參數 : 可設定單一參數、多重參數等,可以說是"輸入值的變數",與變數相同,需要設定資料型態在前面,後面再輸入變數名字

body of the function : 一個函數被呼叫後,所要執行的程式碼

函式的呼叫(Function Call)

直接在主程式寫:函式名稱(參數) 就可以囉!

參數的預設值(Default Parameter)

#include <iostream>
using namespace std;
 
int sum(int a, int b=20){
  int result;
  result = a + b;
  return (result);
}
 
int main (){
   int a = 100;
   int b = 200;
   int result;
 
   // 呼叫函式增加值
   result = sum(a, b);
   cout << "Total value is :" << result << endl;
 
   // 再次呼叫函式
   result = sum(a);
   cout << "Total value is :" << result << endl;
 
   return 0;
}

我們可以為函式當中的參數設定預設值,如果我們沒有輸入值進去的話,則預設使用這個數字。

輸出結果:

Total value is :300(a=100,b=200)
Total value is :120(a=100,b=20)

注意:函式內的result與主程式內的result所代表的意義不同

不回傳值(void)

代表函式「不需要回傳任何值」到主程式,只是執行某件事。

void INFOR38th() {
    cout << "gay" << endl;
}

每當呼叫此函式,就直接輸出"gay"

傳值呼叫

傳值呼叫中,被傳遞的參數實際值會複製給形式參數,因此無論如何修改函數內的形式參數皆不會改變到實際參數值。

#include <iostream>
using namespace std;
 
// 函式定義及宣告
void swap(int x, int y){
   int temp;
   temp = x; /* 用 temp 保存 x 的值 */
   x = y;    /* 把 y 指定给 x */
   y = temp; /* 把 x值 指定给 y */
   return;
}

int main (){
   int a = 100;
   int b = 200;
   cout << a << b << endl;
   // 呼叫函數交換值
   swap(a, b);
   cout << a << b << endl;
   return 0;
}

以此程式碼為例:

文字好抽象=.=

輸出結果:

100200

100200

可見 a、b 值經交換函數後,輸出結果沒有任何變化。

為什麼?

呼叫 swap(a, b) 時,實際傳入的是 a 和 b 的「副本」,函式內部交換的是副本(形式參數),不會改變主程式中的原變數,所以最後輸出仍然是 a = 100, b = 200的結果

字串

string

字串

char ch='z'; // 存的是 ASCII 122

字元

用單引號 ' '

 string name = "ZSISC";

用雙引號 ""

#include <iostream>
using namespace std;
int main() {
	string sentence;
	getline(cin, sentence);
    string a;
	cin >> a;
    cout << sentence << a;
}

字串輸入

讀整行

遇空白停止

string a = "Hello";
string b = "World";
cout << a + b;

常見用法

字串相加 HelloWorld

string s = "APPLE";
cout << s[0];  // A

取得字元 A

常見函式

判斷空字串

string a = " ";
if (a.empty()) cout << "oh";  
else cout << "no";
string a = "ZSISC";
cout << a.length();  

取得字串長度 5

指標&參考

Pointer & Reference

指標

一個變數, 存放一個記憶體位址, 指向另一個變數.

變數型態 *變數名稱 = &另一個變數;
#include <iostream>
using namespace std;
int main(){
    int a = 10;
    int *p = &a;
    cout << p;  // 0x7fff683232a4
}

取址符號 &

#include <iostream>
using namespace std;
int main(){
    int a = 10;
    cout << a; // 10
    cout << &a; // 0x7ffec663074c
}

獲取變數的記憶體位址

取值符號 *

獲取指標存的值

#include <iostream>
using namespace std;
int main(){
    int a = 10;
    int *p = &a; // 宣告指標
    cout << p; // 0x7ffec663074c
    cout << *p; // 10
}

參考 

一個變數的別名, 可以讓一個變數等同於另一個變數.

變數型態 &變數名稱 = 原變數;
#include <iostream>
using namespace std;
int main(){
    int a = 3;
    int &r = a;
    cout << r; // 3
}

參考 

#include <iostream>
using namespace std;

void add(int a) {
    a++;
}

int main(){
	int x = 5;
	add(x);
    cout << x << endl;  // 5
}
#include <iostream>
using namespace std;

void add(int &a) {
    a++;
}

int main(){
	int x = 5;
	add(x);
    cout << x << endl;  // 6
}

Review

By zsisc32

Review

  • 8