~複習~Review~
By Welly
基本環境
environment?
起手式
#include <iostream>//<--控制輸入輸出的函式庫
using namespace std;//<--也可以不打 但有些語法前就要加"std::"(詳情可洽教學長www)
int main(){//<--主函式(先打就對了www)
巴拉巴拉~;//<--記得打分號!!!
return 0;//<--回傳值(因為不是void型別的函式 必須有回傳值 如果看不懂一樣先打就對了:)
}編譯器(邊一氣)
compiler
編譯器(邊一氣) code::blocks

編譯器(邊一氣) Dev C++

線上編譯器
以下略......(其實我只用過第一個www
輸入/輸出
input/output
cin/cout
cin>>
cout<<
" "<--空格
endl or \n<--換行
scanf/printf
printf("請輸入數字:");
scanf("%d", &input);
ex:
printf("顯示字元 %c\n", 'A');
printf("顯示字元編碼 %d\n", 'A');
printf("顯示十進位整數 %d\n", 15);
printf("顯示科學記號 %e\n", 0.001234);
變數
variables
變數

變數
| 型態 | 中文意思 | 英文字義 | 可儲存的資料 |
|---|---|---|---|
| int | 整數 | integer | 100、-5、1246 ... |
| float | 浮點數(小數) | floating point | 3.14159、4.3、-1.1 ... |
| char | 字元(半形字) | character | 'a'、'R'、'1'、'@'、'*' ... |
| string | 字串(文句) | string | "Hello"、"^_^"、"Rock!" ... |
| bool | 布林(是非) | boolean | true、false |
運算子
operator
算數運算子
#include <iostream>
using namespace std;
int main()
{
int a=1;
int b=2;
cout<<"a+b="<<a+b;//<--加
cout<<"a-b="<<a-b;//<--減
cout<<"a*b="<<a*b;//<--乘
cout<<"a/b="<<a/b;//<--除
cout<<"a%b="<<a%b;//<--取餘數
return 0;
}
//a+b=3
//a-b=-1
//a*b=2
//a/b=0
//a%b=1邏輯運算子
#include <iostream>
using namespace std;
int main()
{
int a=1;
int b=2;
if(a==1||a==2){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
if(a==1&&a==2){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
if(a!=b){
cout<<"a!=b"<<endl;
}else{
cout<<"a==b"<<endl;
}
return 0;
}
//true
//false
//a!=b關係運算子
#include <iostream>
using namespace std;
int main()
{
int a=1;
int b=2;
if(a<b){
cout<<"a<b"<<endl;
}
if(a>b){
cout<<"a>b"<<endl;
}
if(a==b){//<--是==1不是=!!!
cout<<"a=b"<<endl;
}
return 0;
}
//a<b偷懶寫法
i=i+1//i+=1//i++(只有加一時可用)
i=i-1//i-=1//i--(只有減一時可用)
i = i + 8 // i += 8
i = i - 8 // i -= 8
i = i * 8 // i *= 8
i = i / 8 // i /= 8
i= i % 8 // i %=8
//前置的場合下會先進行運算再指派到 變數, 而後置 的 場合下則是會先指派到 變數再進行運算

++a/--a
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
a++/a--
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
#include <iostream>
using namespace std;
int main()
{
int a,x;
a=1;
x=++a;
cout<<x<<endl;
cout<<a<<endl;
return 0;
}
//2
//2#include <iostream>
using namespace std;
int main()
{
int a,x;
a=1;
x=a++;
cout<<x<<endl;
cout<<a<<endl;
return 0;
}
//1
//2條件判斷
if-else
if v.s else if
#include <iostream>
using namespace std;
int main()
{
int a=126;
if(a%2==0){
if(a%3==0){ //巢狀
cout<<"a是6的倍數"<<endl;
}
if(a%3==0&&a%7==0){
cout<<"a是21的倍數"<<endl;
}
cout<<"a是偶數"<<endl;
}
else{
cout<<"a不是2或3或6的倍數"<<endl;
}
return 0;
}
//a是6的倍數
//a是21的倍數
//a是偶數
#include <iostream>
using namespace std;
int main()
{
int a=126;
if(a%2==0){
if(a%3==0){ //巢狀
cout<<"a是6的倍數"<<endl;
}
else if(a%3==0&&a%7==0){
cout<<"a是21的倍數"<<endl;
}
cout<<"a是偶數"<<endl;
}
else{
cout<<"a不是2或3或6的倍數"<<endl;
}
return 0;
}
//a是6的倍數
//a是偶數
迴圈
loop
十十乘法表
#include <iostream>
using namespace std;
int main()
{
int a=10;
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){ //巢狀
cout<<a<<"*"<<b<<'='<<a*b<<endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a=1;
int b=1;
while(a<11){
while(b<11){ //巢狀
cout<<a<<'*'<<b<<'='<<a*b<<endl;
b+=1;
}
b=1;
a+=1;
}
return 0;
}
break
跳出這個迴圈
continue
跳過此次迴圈
#include <iostream>
using namespace std;
int main()
{
int a=10;
for(int i=0;i<a;i++){
if(i==5){
break;
}
cout<<i<<" ";
}
return 0;
}
//0 1 2 3 4#include <iostream>
using namespace std;
int main()
{
int a=10;
for(int i=0;i<a;i++){
if(i==5){
continue;
}
cout<<i<<" ";
}
return 0;
}
//0 1 2 3 4 6 7 8 9陣列
array
一維陣列
#include <iostream>
using namespace std;
int main()
{
int a[10]={1,6,5,2,8,4,3,5,2,89};
int b[10];
for(int i=0;i<10;i++){
cin>>b[i];
}
for(int j=0;j<10;j++){
cout<<a[j]<<" ";
cout<<b[j]<<" ";
}
return 0;
}
二維陣列
#include <iostream>
using namespace std;
int main()
{
int a[10][10];
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
cin>>a[i][j];
}
for(){
for(int j=0;j<10;j++){
cout<<a[i][j]<<" ";
}
}
return 0;
}
//輸入輸出字元&字串
character & string
跳脫字元
| 字元 | 意義 | 字元 | 意義 |
|---|---|---|---|
| \' | 單引號 | \t | 定位字元tab |
| \" | 雙引號" | \b | 倒退backspace |
| \\ | 反斜線\ | \n | 換行enter |
| \0 | 空字元NULL | \a | 發出一聲警告 |
from zsisc27 character string
跳脫字元
#include <iostream>
using namespace std;
int main()
{
cout << " ^ ^ "<<endl;
cout<< "(=-w-=)----?"<<endl;
cout<<" \" \" \" \" "<< endl;
return 0;
}
// ^ ^
//(=-w-=)----?
// " " " " 字元&字串
#include <iostream>
#include <string>
using namespace std;
int main()
{
char a='h';
char b[]={'z','s','i','s','c','!','!','!'};
char c[]="zsisc!!!"; //用字串宣告字元陣列會多要一個空間放\0
string hello="zsisc!!!";
cout<<a<<endl;
for(int i=0;i<8;i++){
cout<<b[i];
}cout<<", "<<sizeof(b)/sizeof(b[0])<<endl;
for(int i=0;i<9;i++){
cout<<c[i];
}cout<<", "<<sizeof(c)/sizeof(c[0])<<endl;
cout<<hello<<endl;
}
//h
//zsisc!!!, 8
//zsisc!!!, 9
//zsisc!!!相關函式
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
char a='i';
char b[10]={'z','s','i','s','c','!','!','!'};
char c[20]="zsisc28!!!";
string hello="zsisc!!!";
string hi="zsisc28!!!";
cout<<strlen(b)<<endl;
if(strcmp(b,c)==true){
cout<<"the same!"<<endl;
}
cout<<strcat(b,c)<<endl;
return 0;
}
//8
//zsisc!!!zsisc28!!!
函式
function
函式庫的函式
| 函式庫 | 函式 | 功能 | 回傳值型態 |
|---|---|---|---|
| math.h | sqrt( float x ) | 回傳 x 的開根號值 | float |
| pow( float x, float y ) | 回傳 x 的 y 次方 | float | |
| ctype.h | isalpha( char c ) | 回傳 c 是不是英文字母 | bool |
| isdigit( char c ) | 回傳 c 是不是數字 | bool | |
| string.h | strlen( char s[] ) | 回傳 s 的長度 | int |
自定義函式
回傳值型態 函式名稱(參數1型態 參數名稱1, 參數2型態 參數名稱2, ... )
{
Do anything you want...
return 回傳值;
} //函式大括號後沒有分號!
int main(){
return 0;
}
自定義函式(*注意*)
- 可沒有參數或回傳值(但只有void)
- 回傳值可能為數值(int、double)、字元(char)或是/非(bool)
- 若用void記得用還是要加小括號
- 參數間用逗號分開
- 回傳值型態要和當初定義的相同
- 每個參數都要定義型態
自定義函式
#include <iostream>
using namespace std;
int calculate(int a,char b,int c){
if(b=='+'){
return a+c;
}
else if(b=='-'){
return a-c;
}
else if(b=='*'){
return a*c;
}
else if(b=='/'){
return a/c;
}
}
int main()
{
int A,B;
char cal;
cin>>A>>cal>>B;
cout<<calculate(A,cal,B);
return 0;
}
自定義函式(void)
#include<iostream>
using namespace std;
void bird()
{
cout << " .-." << endl;
cout << " /'v'\\" << endl;
cout << " (/ \\)" << endl;
cout << "='=\"=\"===< " << endl;
cout << "mrf|_|" << endl;
}
void cat()
{
cout << " /\\_/\\" << endl;
cout << " ____/ o o \\" << endl;
cout << " /~____ =o= /" << endl;
cout << " (______)__m_m)" << endl;
}
int main()
{
bird();
cout << "麻雀:早安~" << endl;
cat();
cout << "貓:我要吃掉你!" << endl;
bird();
cout << "麻雀:不要><" << endl;
return 0;
}
/*
.-.
/'v'\
(/ \)
='="="===<
mrf|_|
麻雀:早安~
/\_/\
____/ o o \
/~____ =o= /
(______)__m_m)
貓:我要吃掉你!
.-.
/'v'\
(/ \)
='="="===<
mrf|_|
麻雀:不要><
*/遞迴
recursion
遞迴



for迴圈
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
遞迴
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
#include<iostream>
using namespace std;
int main()
{
int a=0;
for(int i=0;i<11;i++){
a=a+i;
}
cout<<a;
return 0;
}
//55#include<iostream>
using namespace std;
int sum(int a){
if(a==1){
return 1;
}
else{
return sum(a-1)+a;
}
}
int main()
{
cout<<sum(10);
return 0;
}
//55費氏數列
#include<iostream>
using namespace std;
int fibonacci(int a){
if(a==0){
return 0;
}
else if(a==1){
return 1;
}
else{
return fibonacci(a-1)+fibonacci(a-2);
}
}
int main()
{
int N;
cin>>N;
cout<<fibonacci(N);
return 0;
}//用文字來說,就是費氏數列由0和1開始,之後的費波那契數就是由之前的兩數相加而得出。輾轉相除法
#include<iostream>
using namespace std;
int euclidean_algorithm(int a,int b){
if(b==0){
return a;
}
else{
return euclidean_algorithm(b,a%b);
}
}
int main()
{
int M,N;
cin>>M>>N;
cout<<euclidean_algorithm(M,N);
return 0;
}
指標
pointer
指標(位置)
pointer:其實就是這個東東在記憶體中的位置名稱(地址->其實就是0x+十六進位的一串數字 ex.0x40f7d8)
#include <iostream>
using namespace std;
int main()
{
int a[2];
int b=2;
cout<<a;
cout<<&b;
return 0;
}
//0x2ffb38
//0x2ffb34指標變數(指標)
pointer variable:一種變數型態 其實就是拿來存指標(隔壁那個 也就是位置)的變數(就是所謂的存址型態)
#include <iostream>
using namespace std;
int main()
{
int *p;
int a=2;
p=&a;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
cout<<a<<endl;
//cout<<*a<<endl; 編譯錯誤
cout<<&a<<endl;
return 0;
}
//0x7ffc22ea9f44
//2
//0x7ffc22ea9f48
//2
//0x7ffc22ea9f44指標(位置)

指標變數(指標)

一些符號~~
- &取址符號:跟系統說我要這個東東(ex.變數)的地址
- *取值符號:跟系統說我要這個地址裡的東東
#include <iostream>
using namespace std;
int main()
{
int a[2];
int b=2;
int c;
int *p;
p=&b;
c=*p;
cout<<a<<endl;
cout<<&b<<endl;
cout<<p<<endl;
cout<<c<<endl;
return 0;
}
//0x40f67c
//0x40f678
//0x40f678
//2指標陣列
不准死掉唷~
/*註:由於指標陣列最常見的應用是動態記憶體分配,以下程式皆用動態分配示範(其實是程教長想裝逼)
若感到不適,請深吸一口氣,知道自己哪裡不懂的話盡量提!*/
動態一維指標陣列
#include <iostream>
int main(){
int *arr = new int[5]; //宣告動態陣列
for(int i=0;i<5;i++){
*(arr + i) = i * 2; //賦值的方法
}
for(int i=0;i<5;i++){
std::cout << *(arr + i) << " "; //存取值的方法
}
delete [] arr; //刪除動態陣列
return 0;
}
//0 2 4 6 8 | arr[0] | arr[1] | arr[2] | arr[3] | arr[4] |
|---|---|---|---|---|
動態二維指標陣列
#include <iostream>
int main(){
int **arr = new int *[3];
for(int i=0;i<3;i++){
arr[i] = new int[5];
} //建構動態二維陣列
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
*(*(arr + i) + j) = (i * 5) + (j + 1); //賦值
}
}
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
std::cout << *(*(arr + i) + j) << " "; //讀取
}
std::cout << "\n";
}
for(int i=0;i<3;i++){
delete [] arr[i];
}
delete [] arr; //刪除動態二維陣列
return 0;
}
/*
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
*/pass by value/reference/pointer
&
指標X函式
先別害怕 來聽個小故事~
壞壞小明想要抄同學的作業(哪泥!!!
pass by value
第一天:小華叫小明用單身十六年的手速自己抄在習作上
回家以後小明看到習作上的題目就想在習作上面畫畫
->先複製再把"值"傳入 並且不會更改到原本的值 就像小明先把答案抄到自己的習作一樣 他後來在上面畫畫並不會更改到小華的習作簿
#include<iostream>
using namespace std;
void f(int A, int B) {
int tmp = A;
A = B;
B = tmp;
cout << A << " " << B << endl; //2 1
}
int main(){
int a = 1, b = 2;
f(a, b);
cout << a << " " << b; //1 2
return 0;
}pass by pointer
第二天:小智直接把自己的習作借給小明(天阿真是個好人)回家以後小明看到習作上的題目又想在習作上面畫畫
->直接把地址傳入 因此會更改到原本的值 就像小智直接把習作給小明一樣 他後來在上面畫畫會更改到小智的習作簿
#include<iostream>
using namespace std;
void f(int *A, int *B) { //A, B 是 a, b的記憶體位址 //存址型態
int tmp = *A; //取A記憶體位址的值,也就是a的值 //取值A
*A = *B; //把A記憶體位址的值(a),改成B記憶體位址的值(b) //取值A,B
*B = tmp; //把B記憶體位址的值(b),改成tmp //取值B
cout << *A << " " << *B << endl; //2 1 //取值A,B
}
int main(){
int a = 1, b = 2;
f(&a, &b); //傳入a, b的記憶體位址 //取址a,b
cout << a << " " << b; //2 1
return 0;
}pass by reference
這個跟pass by pointer的原理基本上是一樣的 可以想成是工程師覺得太多星星(*)了都要眼冒金星了所以把pass by pointer簡化成pass by reference 實際上他們應該還有一些差別 不過我也不清楚 等到各位變成電神再來電爆我吧ww
->所以是會更改到原本的值的歐~
#include<iostream>
using namespace std;
void f(int &A, int &B) {
int tmp = A;
A = B;
B = tmp;
cout << A << " " << B <<endl;
}
int main(){
int a = 1, b = 2;
f(a, b);
cout << a << " " << b;
return 0;
}結構體
struct
結構體
就是所謂的"自定義型別" 是用一個類別(type)把好幾個型別同時包起來 就不用宣告一堆的變數~
->宣告如右邊紅色框框 輸出其中一個時記得加點點(.)(如右邊藍色框框) 賦值則如黃色底線 三個都要一次打歐~
#include <iostream>
using namespace std;
struct data{
int no;
char name[10];
int age;
}; //大括號後要加分號!
int main()
{
data list1={1,"Bob",10};
cout<<list1.no<<endl;
cout<<list1.name<<endl;
cout<<list1.age<<endl;
return 0;
}
//1
//Bob
//10結構

簡單來說就是豪華大禮包的概念www
也可以搭配指標歐~
先別急著說ㄋㄡˋ
搭配指標
其實跟普通的指標是一樣的~
只是型別改成長的有點變態的data類別 而在輸出時要改成左邊藍框框內的寫法
->然後在使用struct時 要記得每次都要打"struct data"這樣煩人的東東 (不過在C++的世界中是可以單純只寫"data"而不寫"struct"的 但在其他的編譯器中有可能會跑不動(/ω\))
#include <iostream>
using namespace std;
struct data{
int no;
char name[10];
int age;
};
int main()
{
struct data *sp;
struct data list1={1,"Bob",10};
sp=&list1;
cout<<(*sp).name<<endl;
return 0;
}
//Bob可愛的符號->
其實跟上一頁那個做的事情是一樣的 可能只是有人覺得打那一大坨太麻煩了www
->所以他們是一樣的拉~
(*sp).name==sp->name
#include <iostream>
using namespace std;
struct data{
int no;
char name[10];
int age;
};
int main()
{
struct data *sp;
struct data list1={1,"Bob",10};
sp=&list1;
cout<<sp->name<<endl;
return 0;
}
//Bob不想打那麼多字啦~
typedof函式~
typedef函式
用來幫你的一大坨東東取名子的函式~
像右邊的寫法 就是把他取名叫"t_data" 下面就可以直接用了~
#include <iostream>
using namespace std;
typedef struct {
int no;
char name[10];
int age;
} t_data;
int main()
{
t_data *sp;
t_data list1={1,"Bob",10};
sp=&list1;
cout<<(*sp).name<<endl;
return 0;
}
//Bob也可以定義指標
跟剛剛的定義方法是一樣的~ 記得加星星!(*) 接下來就可以跟一般的指標一樣使用瞜~
->第一次聽不懂是正常ㄉ 跟我一樣(/ω\)
#include <iostream>
using namespace std;
typedef struct {
int no;
char name[10];
int age;
} t_data,*pt_data;
int main()
{
pt_data pD;
t_data data={1,"Bob",11};
pD=&data;
cout<<(*pD).name<<endl;
cout<<pD->name<<endl;
return 0;
}
//Bob
//Bob對了也有陣列歐
其實就是一般的陣列用法拉~ 不用太緊張:)
#include <iostream>
using namespace std;
struct data {
int no;
char name[10];
int age;
};
int main()
{
struct data lists[3]={
{10,"Bob",10},
{11,"Andy",11},
{12,"Hanry",12}
};
for(int i=0;i<3;i++){
cout<<lists[i].name<<endl;
}
return 0;
}
//Bob
//Andy
//Henrysizeof()跟可怕ㄉ迴圈
最後了最後了 大家加油!!!
sizeof函式
一個會幫你算好你丟進去的東東(ex.型態)有多大(在記憶體中佔幾個byte)的函式~
->到目前為止都很親民~ 那要加入可怕迴圈瞜(/ω\)
#include <iostream>
int is_admin = 0;
int ary[160];
int main(){
std::cout << sizeof(char) << std::endl;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(long long) << std::endl;
std::cout << sizeof(float) << std::endl;
std::cout << sizeof(int *) << std::endl;
std::cout << sizeof(double *) << std::endl;
}
// 1 4 8 4 8 8常用的迴圈寫法
#include <iostream>
using namespace std;
struct data {
int no;
char name[10];
int age;
};
int main()
{
struct data lists[3]={
{10,"Bob",10},
{11,"Andy",11},
{12,"Hanry",12}
};
for (int i=0; i<sizeof(lists)/sizeof(lists[0]); ++i) {
cout << lists[i].name << endl;
}
return 0;
}
//Bob
//Andy
//Henry個人是覺得這樣寫的人應該是嫌自己視力太好(/ω\)
Copy of review
By welly6256
Copy of review
- 189