海之音
INFOR 36th 學術長 @小海_夢想特急_夢城前
從0開始的異世界生活程式人生
講師
Index說真的 我相信你們都比我清楚
從C開始從C開始機器碼
程式語言發展
組合語言
C
0000 0000 000000010000
0000 0001 000000000001
0001 0001 000000010000
0001 0001 000000000001 section .data
msg db 'Hello, world!',0xA
len equ $-msg
section .text
global _start
_start:
mov edx,len
mov ecx,msg
mov ebx,1
mov eax,4
int 0x80
mov ebx,0
mov eax,1
int 0x80#include<stdio.h>
int main(){
printf("Hello, world\n");
return 0;
}(亂找的 這不是Hello world)
低階 -> 高階
不是給人看 -> 給人看的
所以別再抱怨C/C++很難讀懂了
從C開始C
C
#include<stdio.h>
int main(){
printf("Hello, world\n");
return 0;
}#include<iostream>
int main(){
std::cout << "Hello, world" << std::endl;
return 0;
}C++
Python
print("Hello, world\n")Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello, world!");
}
}從C開始C/C++
C
C++
Python
C/C++的運作
組語
編譯
機器碼
組譯
C++部分簡報by 807⁸⁰⁷(電研學術長)
IDE(整合式開發環境)VScode(文字編輯器)
VScode編譯器(MSYS2)pacman -S mingw-w64-ucrt-x86_64-gcc編譯器(MSYS2)6. 前往環境變數(可以用左下角的搜尋)
7. 選取 系統變數 中的 Path 然後按下 編輯
8. 按 新增 然後貼上這段路徑
如果有如果有變更路徑: 去資料夾找 你改的路徑+\ucrt\bin
9. 確定 確定 確定
C:\msys2\ucrt64\bin編譯器(MSYS2)編譯器(MSYS2)1. 按 Ctrl+` 開啟終端機
2. 輸入
出現👇代表安裝成功
g++ --versiong++ (Rev10, Built by MSYS2 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.第一次接觸某程式的傳統
做就對了#include<iostream>
int main(){
std::cout << "hello, world\n";
return 0;
}Code::Blocks
VScode
程式架構#include<iostream>
#define io std::ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
/*
可能有其他函式、結構變數
*/
int main(){
io;
std::cout << "hello, world\n";
return 0;
}<-預處理
<-其他函式區
<-主函式區
Variable: 多變的 那const變數怎麼解釋
變數宣告資料型態
變數名稱
初始化
賦値a = 0;
=
變數名稱
資料
使用變數a = a + 1
=
變數名稱
資料
變數型態整數浮點數字元字串布林値const 修飾作用域實際上我更愛scanf/printf
輸出輸入優點與缺點std::ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #include <iostream>
int main(){
int a, b;
cin >> a >> b;
cout << a << ' ' << b << '\n';
return 0;
}畢竟電腦是電子計算機,會運算是很正常的
運算子邏輯運算and&& * 0 1 0 0 0 1 0 1
or&& * 0 1 0 0 1 1 1 1
xor!= * 0 1 0 0 1 1 1 0
位元運算運算子優先順序:先乘除後加減,然後是位元運算和邏輯運算
不確定時括號是你的超人
用Vector啦
一維陣列一維陣列高維陣列data[0]
data[1]
data[2]
data[3]
data[4]
一維陣列data[5]
data[0][0]
data[1][0]
data[2][0]
data[3][0]
data[4][0]
data[0][1]
data[1][1]
data[2][1]
data[3][1]
data[4][1]
data[0][2]
data[1][2]
data[2][2]
data[3][2]
data[4][2]
二維陣列data[5][3]
高維陣列(C-style)string老實說指標沒很常用 我都用參考
指標(pointer)指標(pointer)#include<iostream>
using namespace std;
int main(){
int *a = new int;
cout << a << '\n'; //0xbe1560
int b = 5;
int *c = &b;
cout << c << ' ' << *c; //0x70fdfc 5
return 0;
}指標(pointer)data[0]
data[1]
data[2]
data[3]
data[4]
類似變數存取
data
data+1
data+2
data+3
data+4
指標
&data[0]
&data[1]
&data[2]
&data[3]
&data[4]
相當於
指標(pointer)#include<iostream>
using namespace std;
int main(){
int data[5][5];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
data[i][j]=i*5+j;
}
}
/*
{
{ 0, 1, 2, 3, 4},
{ 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14},
{15, 16, 17, 18, 19},
{20, 21, 22, 23, 24}
}
*/
cout << *(*(data+2)+3); //13
return 0;
}
指標(pointer)*data
| *(*(data+1)) : 5 | *(*(data+1)+1):6 | *(*(data+1)+2):7 | *(*(data+1)+3):8 | *(*(data+1)+4):9 |
|---|
*(data+1)
| *(*data) : 0 | *((*data)+1) : 1 | *((*data)+2) : 2 | *((*data)+3) : 3 | *((*data)+3) : 4 |
|---|
...
參考(reference)#include <iostream>
using namespace std;
int main() {
int a = 5;
int &b = a;
b = 1;
cout << a; //1
return 0;
}參考(reference)if - else 應該不會有人特別去用 switch - case 啦
if-else// do something
if(condition){
// do something if true
}
else{
// do something if false
}
// do somethingif-else// ...
if(condition1){
//...
}
else{
if(condition2){
// ...
}
else{
// ...
}
}
// ...// ...
if(condition1){
// ...
}
else if(condition2){
// ...
}
else{
// ...
}
// ...三元運算子b=a>0?a:(-a);while loops// ...
while(condition){
// ...
}
// ...// ...
while(cin >> n){
// ...
}
// ...for loops// ...
for(初始化; 持續條件; 常駐步驟){
// ...
}
// ...// ...
for(int i=0; i<n; i++){
// ...
}
// ...無窮迴圈while(true){
// ...
}break, continue//範例:如果輸入=0就跳出
int n;
while(true){
cin >> n;
if(n==0) break;
// ...
}
//也可以寫成
int n;
while(cin >> n && n!=0){
// ...
}//範例:輸出輸入的數字
//除非輸入=48763則跳過
int n;
while(true){
cin >> n;
if(n==48763) continue;
cout << n << '\n';
// ...
}多層迴圈#include<iostream>
using namespace std;
int main(){
int data[5][5];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
data[i][j]=i*5+j;
}
}
/*
{
{ 0, 1, 2, 3, 4},
{ 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14},
{15, 16, 17, 18, 19},
{20, 21, 22, 23, 24}
}
*/
cout << *(*(data+2)+3); //13
return 0;
}
重新看一次這份code,應該要能知道為什麼結果是這樣
do whiledo{
// ...
} while(condition);還有基礎遞迴(recursion)
函式函式參數(可無)
回傳值(可無)
函式:處理過程
定義函式// 宣告+定義
回傳型別 函式名稱(參數型別1 參數1, 參數型別2 參數2...){
定義內容
}
// 只有宣告
回傳型別 函式名稱(參數型別1 參數1, 參數型別2 參數2...);呼叫函式參數回傳值練習:寫一個函式交換 main() 函式裡兩個整數變數的值
參考解答
#include<iostream>
using std::cout;
void swap(int &a, int &b){
int temp=a;
a=b;
b=temp;
return;
}
int main() {
int a = 5, b = 3;
swap(a, b);
cout << a << ' ' << b; // 3 5
return 0;
}遞迴(recursion)遞迴(recursion)費波那契數列(遞迴Ver.)
int fibonacci(int n) {
if (n == 1 || n == 2) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}初始狀態:f(1) = f(2) = 1
狀態轉移:f(n) = f(n-1) + f(n-2)
終止時間:n = 1 或 n = 2
一點點OOP
物件導向(OOP)淺談結構變數(struct)#include <iostream>
using namespace std;
struct person {
double height, weight;
double BMI() {
return weight / (height * height);
}
};
int main() {
person Sea;
Sea.height = 1.813;
Sea.weight = 71.4;
cout << Sea.BMI(); // 21.7221
}結構變數(struct)struct person {
double height, weight;
double BMI();
};
double person::BMI(){
return person::weight/(person::height*person::height);
}struct person {
double height, weight;
struct other{
double length_of_hair;
string favorate_food;
} other_information;
};結構變數(struct)struct complex{
int a, b;
complex(int _a, int _b){
a=_a;
b=_b;
}
};
int main(){
complex number(125, 807);
cout << number.a << ' ' << number.b; // 125 807
return 0;
}struct complex {
int a, b;
complex(int _a, int _b) {
a = _a;
b = _b;
}
complex operator+(complex another) {
return {a + another.a, b + another.b};
}
};
int main() {
complex number1(125, 807), number2(123, 456);
complex sum = number1 + number2;
cout << sum.a << ' ' << sum.b;
return 0;
}ZJ a271 (可以用struct實作一隻兔子)
寫題目的地方
What is OJ一些OJ一些OJ一些OJ一些OJ一些OJ一些OJ一些OJ#include<iostream>
using namespace std;int main(){for(int i=0;i<10000000;i++)cout<<"我是笨";}#include<iostream>
using namespace std;int main(){for(int i=0;i<10000000;i++)cout<<"我是笨";}#include<iostream>
using namespace std;int main(){for(int i=0;i<10000000;i++)cout<<"我是笨";}#include<iostream>
using namespace std;int main(){for(int i=0;i<10000000;i++)cout<<"我是笨";}#include<iostream>
using namespace std;int main(){for(int i=0;i<10000000;i++)cout<<"我是笨";}By 海之音
四校聯合放課第 0, 1 堂,C++基礎語法 其實我是直接改資讀的啦哈哈