```cpp // 未含錯誤 #include <iostream> using namespace std; int main(){ cout << " Hello world!" << endl; } ``` ```cpp // Syntax error #include <iostream> using namespace std; int main(){ cout << " Hello world!" << endl } ``` ```cpp // Runtime error #include <iostream> using namespace std; int main(){ int a = 0; cout << a/0 << endl; } ``` ```cpp // Segmentation fault #include <iostream> using namespace std; int main(){ char a[10]; cout << a[1000]; } ``` ```shell // -o 用來指定輸出至哪 g++ program_name.cpp -o exe ``` ```cpp // Main function int main(){ } ``` ```cpp // Variable type // 整數 int so_handsome = 0; // 浮點數 float so_nice = 0; double so_pretty = 0; // 字元 char rock = 'R'; char paper[100]; char Scissors = "I win!!" ``` ```cpp #include <iostream> ``` ```cpp int a = 0; char b = "R"; float d = 20.04; scanf("%d", &a); //輸入整數 (int) scanf("%c", &b); //輸入字元(char) scanf("%d", &d); //輸入浮點數(float) printf("%d\n", a); //輸出整數(int) printf("%c\n", b); //輸出字元(char) printf("%f\n", d); //輸出浮點數(float) ``` ```cpp using namespace std; ``` ```cpp // 由右至左 int i = 1000; int temp = i; ``` ```cpp // example program #include <iostream> using namespace std; int main(){ int i, j; // 假設輸入4 和 5 cin >> i >> j; int rock = i; i = j; cout << i << "\n" << j << "\n" << rock << endl; return 0; } ``` ```cpp // example program #include <iostream> using namespace std; int main(){ int i = 0; char j = "R"; i = j; cout << i << endl; return 0; } ``` ```cpp using namespace std; ``` ```shell git clone ... ``` ```shell node app.js ``` | 意義 | 數學表示法 | C++ 表示法 | | ---------- | ------------------- | --- | | 加 | + |+| | 減 | - |-| | 乘 | X |* | | 除 | / |/| | 求餘數 | mod |%| | 求負數 | - |-| | 平方 | \begin{aligned} \ 2^2 \end{aligned} |pow()| | 根號 | \begin{aligned} \sqrt2 \end{aligned} |sqrt()| ```cpp #include <iostream> using namespace std; int main(){ int a = 3, b = 4; printf("%d\n", a + b); printf("%d\n", a - b); printf("%d\n", a * b); // 為何是0? printf("%d\n", a / b); float c = 3, d = 4; // 小數點後2位 printf("%.2f\n", c/4); printf("%f\n", pow(a, 2)); printf("%f\n", sqrt(d)); cout << pow(a, 2) << endl; cout << sqrt(b) << endl; return 0; } ``` ```cpp // local/ global variable #include <iostream> using namespace std; // a global variable, which can be used everywhere int b = 10; int main(){ // a local variable, which can only be used in main int a = 1; printf("%d\n", a); rock(); } void rock(){ printf("%d\n", a); printf("%d\n", b); } ``` ```cpp // function(2) #include <iostream> using namespace std; int gcd(int a, int b); int main(){ gcd(123, 3); } int gcd(int a, int b){ int r = 0; while (a % b != 0){ r = a % b; a = b; b = r; } return b; } ``` ```cpp // function(1) #include <iostream> using namespace std; void rock(); int main(){ cout << pow(2, 2) << endl; rock(); return 0; } void rock(){ cout << "I like scissors" << endl; } ``` ```cpp // Return Type void rock(){ cout << "I like scissors" << endl; } int rock(){ return 0; } float rock(){ return 0.0 ; } char rock(){ return 'R'; } string rock(){ return "I like paper too"; } ``` ```cpp // functions.h #ifndef FUNCTIONS_H #define FUNCTIONS_H void printMessage(); int addNumbers(int a, int b); #endif ``` ```cpp // functions.cpp #include "myFunctions.h" // 包含相應的 header 檔案 #include <iostream> // 未包含 using namespace std; void printMessage() { // Function implementation std::cout << "Hello world!" << std::endl; } int addNumbers(int a, int b) { // Function implementation return a + b; } ``` ```cmake # 定義編譯器 CXX = g++ # 可以在此列出編譯的需求,並指定編譯器版本 CXXFLAGS = -std=c++14 -Wall # 執行檔名 TARGET = exec # 以 SRCS 代表所有 .cpp 檔案 SRCS = $(wildcard *.cpp) # 將 .cpp 替換成 .o OBJS = $(SRCS:.cpp=.o) all: $(TARGET) # 連結檔案(link) # 白話文:g++ -wall -o exec *.cpp $(TARGET): $(OBJS) $(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS) # 編譯檔案(compile) # 代表所有 .o depend on 對應的 .cpp # $@ = 目標文件, $< = 第一個依賴的檔案(應該可以用$^) %.o: %.cpp $(CXX) $(CXXFLAGS) -c $< -o $@ clean: rm -f $(OBJS) $(TARGET) ``` ```cmake # 定義編譯器 CXX = g++ # 可以在此列出編譯的需求,並指定編譯器版本 CXXFLAGS = -std=c++14 -Wall # 執行檔名 TARGET = exec # 以 SRCS 代表所有 .cpp 檔案 SRCS = $(wildcard *.cpp) # 不同檔案夾的代表方式 # SERVER_SRCS = $(wildcard server/*.cpp) # CLIENT_SRCS = $(wildcard clinet/*.cpp) # SRCS = $(SERVER_SRCS) $(CLIENT_SRCS) # 將 .cpp 替換成 .o OBJS = $(SRCS:.cpp=.o) # 編譯檔案 # 白話文:g++ -wall -o exec *.cpp $(TARGET): $(OBJS) $(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS) # 編譯檔案的另一種寫法 # 代表所有 .o depend on 對應的 .cpp # $@ = 目標文件, $< = 第一個依賴的檔案(應該可以用$^) # %.o: %.cpp # $(CXX) $(CXXFLAGS) -c $< -o $@ clean: rm -f $(OBJS) $(TARGET) ```