# 大一程設教戰手則 fxxk program design --- ## 程式設計的意義 meaning of program design ---- - 簡化處理過程 - 提高效率&速度 - ~~拿到學分~~ - ~~拿到offer~~ --- ## 程式架構 Program structure ---- - top down logic - left to right - 引入函式庫 > 自定義函數及變數 > 主程式 ---- ```c= #include <stdio.h> //函式導入 double pi = 3.1415926; //自定義 void a(){ printf("haha\n"); } int main(){ //主程式 while(1) a(); } ``` --- ## 資料型態 Data type ---- ![image](https://hackmd.io/_uploads/r1rtFlsEa.png) ---- ### 全域變數 VS 區域變數 以{ }做分界 裡面的++可以++呼叫外面的 外面的++不能++呼叫裡面的 ---- ### 型態轉換 - 字元轉整數 - 小數轉整數 --- ## input / output 輸入輸出 ---- ```c= int n; long long m; char a; char str[10]; float b; double c; scanf("%d", &n); printf("%d", n); scanf("%lld", &n); printf("%lld", n); scanf("%c", &a); printf("%c", n); scanf("%s", &str); printf("%s", n); scanf("%f", &b); printf("%f", n); scanf("%lf", &c); printf("%lf", n); ``` --- ## 判斷 Selection structure ---- ### 邏輯 - 1 & 0 - 邏輯運算子(&&, ||, &, |, ^, ?, ~) - 算術運算子(+, -, *, /, %, =) - 比較運算子(>, >=, <, <=, ==) ---- ![image](https://hackmd.io/_uploads/ry3pmClHa.png) ---- ```c= int a=10, b = 5; //1010 0101 printf("%d\n", a&b); //0000 = 0 printf("%d\n", a|b); //1111 = 15 printf("%d\n", a^b); //1111 = 15 printf("%d\n", a&&b); //1 = 1 ``` ---- if(判斷式)...else if(判斷式)...else ```c= int a=10, b=20; if(a==b){ printf("yes"); }else if(a!=b){ printf("no"); }else{ printf("Idk"); } ``` ---- ~~if()...else~~ ```c= int a=10, b=20; (a==b) ? printf("yes") : printf("no"); ``` ---- switch() ```c= int a; scanf("%d", &a); switch(a){ case 1: printf("1"); break; case 2: printf("2"); break; default: printf("none"); break; } ``` --- ## 迴圈 Loop ---- - 程式的便利性所在 - 給定條件,**重複執行**到不符合 ---- ### for 格式: for(使用變數, 條件, 變數更新) 條件成立則重複執行 ```c= for(int i=0;i<10;i++){ printf("%d haha\n", i); } ``` ---- ### while 格式: while(條件) 條件成立則重複執行 ```c= int i=0; while(i<10){ printf("%d haha\n", i); i++; } ``` --- ## 陣列 Array ---- 在記憶體中切一塊連續的位置來做資料儲存 並以索引的方式做資料讀取 ![image](https://hackmd.io/_uploads/ryyyYMHNT.png =750x) ---- ```c= int array[10] = {1,2,3,4,5,6,7,8,9,0}; printf("%d\n", array[5]); for(int i=0;i<10;i++){ printf("%d ", array[i]); } ``` ---- ### 維度升級 因為陣列是call by reference,所以只要記憶體空間足夠就可以擴張維度([ ]的數量) ---- ```c= int a[10][10]; for(int i=0;i<10;i++) for(int j=0;j<10;j++) a[i][j] = (i+1)*(j+1); for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ printf("%5d", a[i][j]); } printf("\n"); } ``` --- ## 字串 String ---- - 由char組成的陣列 - ""for字串 ''for字元 - 配合<string.h>處理會快很多 - 使用gets()可讀取空格, puts()做輸出 ---- ```c= #include <string> char str[20]; scanf("%s", &str); printf("%s", str); char ss[20]; gets(ss); puts(ss); ``` ---- ### 字元操作 - \n -> 換行 - \t -> tab(空4格) - \b -> 倒退 - \r -> 回到行首(同home) --- ## 自訂函式 function ---- - 把太長的東東簡化 - 增加易讀性(不要殘害看你code的人) - 方便debug - ~~帥~~ ---- ```c= #include <stdio.h> void a(int n) { printf("%d haha\n", n); } int main() { int n = 10; while (n--) a(n); } ``` --- ## 遞迴 自訂函數的應用 ---- 藉由呼叫函數自己來重複執行動作 ```c= #include <stdio.h> long long power(int a){ return (a==1) ? 1 : a * power(a-1); } int main() { int n=5; printf("%lld", power(n)); } ``` --- ## 指標 pointer ---- - 減少記憶體的使用 - 指標可以儲存指標 - ++call by value++ VS ++call by reference++ ---- ```c= void swap(int a, int b) { int tmp = a; a=b; b=tmp; } int main() { int a=10,b=20; swap(a,b); printf("%d %d", a, b); } ``` ---- ```c= void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } int main() { int a = 10, b = 20; swap(&a, &b); printf("%d %d", a, b); } ``` ---- ![image](https://hackmd.io/_uploads/HySD752N6.png) 指標名稱和目標亂掉be like ---- ### 動態定義 當陣列放不下時就要用動態定義(malloc)的方式取得空間,同時確保陣列不會爛掉 ```c= #include <stdlib.h> int *n = (int*)malloc(sizeof(int*)*10); for(int i=0;i<10;i++) n[i] = i; for(int i=10;i>0;i--) printf("%d", n[i-1]); ``` --- ## 結構 structure ---- - 多種變數型態混合使用 - 建立新的型態 ---- ```c= #include <stdio.h> struct pair{ int x; char y; }; int main() { int a=10; char b='a'; struct pair p; p.x=a; p.y=b; printf("%d %c", p.x, p.y); } ``` --- ## 讀檔 idk when to use ---- - 最基本的c應用 - 邏輯跟IO很像,只是資料是跟txt檔做讀取 ---- ### 模式 ![](https://hackmd.io/_uploads/Bk_XQlWHp.png) ---- ### 建檔&寫入 ```c= #include <stdio.h> //建檔+寫入 int main(){ //開始使用前,設定file文件指針並以w+(讀寫)模式開檔 //不存在則新增檔案 FILE *fptr = fopen("TheTXT.txt","w+"); if(!fptr) { perror("檔案開啟失敗"); // 將訊息輸出至 stderr return -1; } //寫入檔案 fprintf(fptr,"haha"); //程式結束前閉檔 fclose(fptr); return 0; } ``` ---- ### 讀檔 ```c= #include <stdio.h> int main(){ //開始使用前,設定file文件指針並以r+(讀)模式開檔 FILE *fptr = fopen("TheTXT.txt","r+"); if(!fptr) { perror("檔案開啟失敗"); // 將訊息輸出至 stderr return -1; } char c; while ((c = fgetc(fptr)) != EOF) { putchar(c); } //程式結束前閉檔 fclose(fptr); return 0; } ``` --- ## 解題&deubg Using zerojudge ---- 1. 先讀懂題目 2. 用自己的邏輯寫出執行流程 3. 嘗試編寫code 4. debug ---- ### Debug - 檢查順序和邏輯對不對 - 檢查運算式有沒有錯 - 檢查迴圈是否正常運作 - 變數型態是否正確,會不會overflow ---- ### 會出現的問題 1. NA -> 解題邏輯或判斷式有誤 2. RE -> 陣列超出範圍或變數太多 3. CE -> 用了禁語或是使用沒定義的函式 4. TLE -> 程式複雜度太高,超時 5. MLE -> 記憶體用太多,爆了(其他人會恨你 --- ## 排版 排版 排版 排版 ---- # 對自己 ##### 的眼睛好 一 ## 點 ---- # 對自己的 ## 眼睛 ### 好一點 --- ## 可能用不到的東西 好東西 ---- - ~~python.org~~ - ~~cpluscplus.com~~ ---- ### 好用函式 | <stdlib.h> | <math.h> | <string.h> |<ctype.h>| | -------- | -------- | -------- |---| | malloc() | pow() | strlen() |isalnum()| |atoi()| sin() | strcmp() | |abs()| sqrt() || ---- ### 好用編輯器 - ~~vscode~~ - sublime - ~~devc++~~ - linux - online compiler --- ## QA 都問都問 ##### 不問沒機會 --- # 練習time!!! [link](/Eq-H9LQCTBiXpz8_u5rKOA?view#綜合練習) --- # THE END
{"title":"程設例會PPT","slideOptions":"{\"theme\":\"black\"}","description":"簡化過程","contributors":"[{\"id\":\"40338ce1-7405-4f34-9d1a-b983b1739361\",\"add\":6927,\"del\":985}]"}
    666 views