# <span style="font-weight:300">程式設計培訓</span> ## <span style="color:cyan"><i class="fa fa-3x fa-code"></i></span> ## <span style="font-weight:800">Learn C Programming</span> <span style="color:gray;font-weight:100">2022 / 12</span> --- <img src="https://i.imgur.com/AUeyfbC.png" style="border: none; box-shadow: none; background: none; max-width: 30%;" /> # 林彥宏 Kyle Lin <span style="color:lightgray;font-weight:100">創科資訊股份有限公司 - 總監</span> <span style="color:lightgray;font-weight:100">Monospace 共同工作空間 - 創辦人</span> ---- ## 程式設計<span style="color:yellow">教學</span> <span style="color:lightgray;font-weight:100">C / Java / Python / Android / C# / PHP / JavaScript</span> -- ## <span style="color:cyan">軟體開發</span>顧問 <span style="color:lightgray;font-weight:100">AWS / Azure / GCP / DevOps / Agile / IoT</span> ---- ![](https://i.imgur.com/yu01gRB.jpg) --- # 認證指南 ---- ## TQC+ 程式語言C認證 * CSF 電腦技能基金會 * TQC+ 軟體設計領域 ---- ## 測驗方式 * 共九大題,全部皆為操作題 * 第一到七類為除錯題 * 第八到九類為綜合應用題 ---- # 計分方式 * 第一到七類,除錯題每題5分 * 第八類,綜合應用題30分 * 第九類,綜合應用題35分 * 作答時間共100分鐘 * 滿分為100分,70分以上及格 ---- # 課程進度 * Day 1 (5/1) <br/> 第一至第五類 * Day 2 (5/2) <br /> 第六至第九類 --- # 如何,<br/> 學好<span style="color:cyan">程式設計</span> * 邏輯思考 * 團隊協作 * 自學能力 * 解決問題能力 --- # 開發工具 C/C++ IDEs * Code::Blocks http://www.codeblocks.org/ 通用型編輯器 * Sublime https://www.sublimetext.com * Atom https://atom.io * VSCode https://code.visualstudio.com ---- # Visual Studio Code ![](https://i.imgur.com/ZRDeOdO.png) --- # C / C++ 原始碼<br/>如何被執行? ---- ![](https://i.imgur.com/bYdxdMo.png) ---- ![](https://i.imgur.com/V4WFv3j.png) ---- ## 編譯器在哪裏? > C:\Dev-Cpp\bin ---- ## 設定環境變數 ```shell= set path=%path%;C:\Dev-Cpp\bin echo %path% gcc -v ``` ---- ## 開新檔案 ```shell= notepad main.c ``` ---- ## 撰寫程式 ```clike= #include <stdio.h> int main() { printf("Hello World\n"); } ``` ---- ## 編譯 ```shell= gcc -o main.exe main.c ``` ---- ## 執行 ```shell= main.exe ``` ---- ## 建立 `say.c` ```shell= notepad say.c ``` ## 修改 `say.c` ```clike= void say(char* words) { printf(words); printf("\n"); } ``` ---- 修改 `main.c` ```clike= #include <stdio.h> int main() { say("Hello"); } ``` ---- 編譯成目的檔再進行連結。 ```shell= gcc -c main.c gcc -c say.c gcc -o main.exe main.o say.o ``` ---- ## 深入淺出 Hello World * `stdio.h` ? * `main()` ? * `return 0` ? ---- ## 錯誤代碼 ```shell= echo %ERRORLEVEL% # 根據程式執行結果決定是否執行下一個指令 main.exe && echo ok main.exe if %ERRORLEVEL%==9 echo Error: Input Not Found ``` ---- ## C語言標準 ```shell= gcc -ansi -o main main.c gcc -std=c89 -o main main.c gcc -std=c99 -o main main.c gcc -std=c11 -o main main.c ``` --- # 第一類 ---- ## 102、基本認識 ```clike= #include <stdio.h> #include <stdlib.h> int main() { double a, b; double total; printf("請輸入兩個浮點數:"); scanf("%lf %lf", &a, &b); total = a + b; printf("total=%f\n", total); system("PAUSE"); return 0; } ``` ---- ## 104、基本認識 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int unit; double price = 23.34; double total; printf("請問您要買幾瓶蘋果汁? "); scanf("%d", &unit); total = unit * price; printf("我買了%d瓶100%%的蘋果汁\n", unit); printf("花了%f元", total); system("PAUSE"); return 0; } ``` ---- ## 106、基本認識 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int a, b, c, d; printf("請輸入第一個整數? "); scanf("%d", &a); printf("請輸入第二個整數? "); scanf("%d", &b); printf("請輸入第三個整數? "); scanf("%d", &c); printf("請輸入第四個整數? "); scanf("%d", &d); printf("此式的餘數為%d\n", ((a+b)/2-(c+d)/2) % 2); system("PAUSE"); return 0; } ``` ---- ## 110、基本認識 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int a, b, c; printf("請輸入變數a的值: "); scanf("%d", &a); printf("請輸入變數b的值: "); scanf("%d", &b); printf("請輸入變數c的值: "); scanf("%d", &c); //判斷a是否大於等於60且小於100,若是,則輸出1, 否則,輸出0 printf("%d\n", a >= 60 && a < 100); //先將b加1後,再除以10. printf("%f\n", ++b/10.); //判斷a是否大於c,若是,則印出a,否則,印出c printf("%d\n", a>c ? a : c); system("PAUSE"); return 0; } ``` --- # 第二類 ---- ## 202、選擇敘述與迴圈 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int score, adjust; printf("請輸入分數:"); scanf("%d", &score); if (score > 60) { adjust = score + 10; } else { adjust = score + 5; } printf("調整後的分數為%d\n", adjust); system("PAUSE"); return 0; } ``` ---- ## 204、選擇敘述與迴圈 ```clike= #include <stdio.h> #include <stdlib.h> int main() { char ch; printf("請問您的身份:以字母表示分別如下:\n"); printf("U:表示大學生,M:表示研究生,P:表示博士生 "); scanf("%c", &ch); switch (ch) { case 'u': case 'U': printf("您是大學生\n"); break; case 'm': case 'M': printf("您是研究生\n"); break; case 'p': case 'P': printf("您是博士生\n"); break; default: printf("您輸入身份不正確\n"); } system("PAUSE"); return 0; } ``` ---- ## 206、選擇敘述與迴圈 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int i = 1, total = 0; while (i <= 100) { total += i++; } printf("1加到100的總和:%d\n", total); system("PAUSE"); return 0; } ``` ---- ## 208、選擇敘述與迴圈 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int i, total = 0; for (i = 1; i <= 100; i++) { total += i % 2 ? 0 : i; } printf("1到100的偶數和: %d\n", total); system("PAUSE"); return 0; } ``` ## 210、選擇敘述與迴圈 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int i = 2, total = 0; do { total += i; i += 2; } while (i <= 100); printf("1到100的偶數和: %d\n", total); system("PAUSE"); return 0; } ``` --- # 第三類 ---- ## 302、函數與陣列 ```clike= #include <stdio.h> #include <stdlib.h> int adjust(int); int main() { int score, final; printf("請輸入您的分數: "); scanf("%d", &score); final = adjust(score); printf("調整後的分數: %d", final); //system("PAUSE"); return 0; } // int adjust(int score) // { // return score >= 60 ? score + 5 : score + 10; // } int adjust(int score) { int temp; if (score >= 60) temp = score + 5; else temp = score + 10; return temp; } ``` ---- ## 304、函數與陣列 ```clike= #include <stdio.h> #include <stdlib.h> void callFun(); int main() { int i; for (i = 1; i <= 5; i++) { callFun(); } system("PAUSE"); return 0; } void callFun() { int ai = 100; static int si = 100; ai++; si++; printf("ai=%d, si=%d\n", ai, si); } ``` ---- ## 306、函數與陣列 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int arr[6]={10, 20, 30, 40, 50, 60}; int i, total=0; for (i=0; i<6; i++) { total += arr[i]; } printf("總和為%d\n", total); //system("PAUSE"); return 0; } ``` ### 308、函數與陣列 ```clike= #include <stdio.h> #include <stdlib.h> double sum(double a[], int n); int main() { double arr[5], total; int i; for (i = 0; i < 5; i++) { printf("請輸入陣列arr[%d]元素值: ", i); scanf("%lf", &arr[i]); } for (i = 0; i < 5; i++) { printf("arr[%d]=%.2f\n", i, arr[i]); } total = sum(arr, 5); printf("此陣列的總和為%.2f\n", total); system("PAUSE"); return 0; } double sum(double a[], int n) { int k; double tot = 0; for (k = 0; k < n; k++) { tot += a[k]; } return tot; } ``` ---- ### 310、函數與陣列 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int arr[5] = {100, 200, 300, 400, 500}; int i; printf("\n陣列元素的值如下:\n"); for (i=0; i<5; i++) { printf("arr[%d]=%d\n", i, arr[i]); } printf("\n陣列元素的位址如下:\n"); for (i=0; i<5; i++) { printf("&arr[%d]=%p\n", i, &arr[i]); } printf("\n陣列元素的位址如下:\n"); for (i=0; i<5; i++) { printf("&arr[%d]=%p\n", i, arr+i); } system("PAUSE"); return 0; } ``` --- # 第四類 ---- ## 402、指標 ```clike= #include <stdio.h> #include <stdlib.h> int main() { int num=100; int *pointer = &num; printf("num=%d, *pointer=%d\n", num, *pointer); //system("PAUSE"); return 0; } ``` ---- ## 406、指標 ```clike= #include <stdio.h> #include <stdlib.h> void change(int *, int *); int main() { int i = 100, j = 200; printf("交換前i與j的值: \n"); printf("i=%d, j=%d\n", i, j); change(&i, &j); printf("交換後i與j的值: \n"); printf("i=%d, j=%d\n", i, j); system("PAUSE"); return 0; } void change(int *x, int *y) { int temp; temp=*x; *x=*y; *y=temp; } ``` ---- ## 408、指標 ```clike= #include <stdio.h> #include <stdlib.h> int Max(int *, int n); int main ( ) { int arr[5]; int maximum, i; for (i=0; i<5; i++) { printf("請輸入arr[%d]元素值: ", i); scanf("%d", &arr[i]); } printf("\n陣列的元素值分別如下:\n"); for (i=0; i<5; i++) { printf("arr[%d]=%d\n", i, arr[i]); } maximum = Max(arr, 5); printf("\n此陣列的最大值為%d\n", maximum); //system("PAUSE"); return 0; } int Max(int *p, int n) { int i, maxi_value = *p; for (i=0; i<n; i++) { if (maxi_value < *(p+i)) { maxi_value = *(p+i); } } return maxi_value; } ``` ---- ## 410、指標 ```clike= #include <stdio.h> #include <stdlib.h> int Max(int p[2][3], int n, int m); int main() { int arr2[2][3]; int maximum, i, j; for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { printf("請輸入arr[%d][%d]: ", i, j); scanf("%d", &arr2[i][j]); } } printf("\n陣列之值如下:\n"); for (i=0; i<2; i++) { for (j=0; j<3; j++) { printf("arr[%d][%d]=%d\n", i, j, arr2[i][j]); } } maximum = Max(arr2, 2, 3); printf("\n此陣列的最大值為%d\n", maximum); system("PAUSE"); return 0; } int Max(int p[2][3], int x, int y) { int i, j, maxi_value = p[0][0]; for (i=0; i<x; i++) { for (j=0; j<y; j++) { if (maxi_value < p[i][j]) { maxi_value = p[i][j]; } } } return maxi_value; } ``` ---- ## 工具箱 * [C Tutor - Visualize C code execution to learn C online](http://pythontutor.com/c.html) 指標練習範例。 ```clike= int main() { int a = 3; int b = 6; int *c; int *d; int **e; int ***f; c = &a; d = c + 1; *d = 7; e = &d; f = &e; printf("%d %d %d %d %d %d %d %d\n", a, b, *c, *c+1, *(c+1), *d, **e, ***f); return 0; } ``` 理解指標與陣列。 ```clike= int main() { int arr[5] = {1, 2, 3, 4, 5}; int *p = arr; int *p2 = arr + 2; printf("%d %d %d %d\n", arr[2], *(arr + 2), *(p + 2), *p2); return 0; } ``` 串列練習。 ```clike= #include <stdio.h> #include <stdlib.h> struct circle { int x, y; int radius; struct circle *next; }; int main() { struct circle c1 = {2, 3, 10, NULL}; struct circle c2 = {3, 5, 8, NULL}; struct circle c3 = {4, 6, 12, NULL}; c1.next = &c2; c2.next = &c3; struct circle *current; current = &c1; while (current != NULL) { printf("Circle (%d, %d) %d\n", current->x, current->y, current->radius); current = current->next; } return 0; } ``` 字元與 ASCII 代碼。 ```clike= char ch, ch2; ch = 'a'; ch2 = 'A'; printf("%c\n", ch); printf("%c\n", ch2); printf("%d\n", ch); printf("%d\n", ch2); printf("%d\n", ch - ch2); ``` 字串處理。 ```clike= #include <stdio.h> int main() { char str[100] = "abc"; int i; printf("%d\n", sizeof(str)); for (i = 0; i < sizeof(str); i++) { if (str[i] == 0) { break; } putchar(str[i]); } system("pause"); } ``` --- # 第五類 ---- ## 502、結構 ```clike= #include <stdio.h> #include <stdlib.h> int main () { struct circle { int x, y; double radius; }; double area; struct circle c1={10, 10, 2.5}; area = c1.radius*c1.radius*3.14159; printf("此圓的圓心為(%d, %d), 面積為%.2f\n", c1.x, c1.y, area); system("PAUSE"); return 0; } ``` ## 504、結構 ```clike= struct rect { int length, width; }; int callarea(struct rect *pr); #include <stdio.h> #include <stdlib.h> int main () { int area; struct rect r1= {20, 18}; area = callarea(&r1); printf("此矩形的長為%d, 寬為%d, 面積為%d\n", r1.length, r1.width, area); system("PAUSE"); return 0; } int callarea(struct rect *pr) { return pr->length * pr->width; } ``` ---- ## 506、結構 ```clike= struct circle { int x, y; double radius; }; double callarea(struct circle *pr); #include <stdio.h> #include <stdlib.h> int main() { double area; struct circle c1={10, 10, 8.5}; area = callarea(&c1); printf("此圓的圓心為(%d, %d), 半徑為%.2f, 面積為%.2f\n", c1.x, c1.y, c1.radius, area); //system("PAUSE"); return 0; } double callarea(struct circle *pr) { return pr->radius * pr->radius * 3.14159; } ``` ---- ![](https://i.imgur.com/WBH6b1L.png) ---- ## 508、結構 ```clike= #include <stdio.h> #include <stdlib.h> int main() { struct company { char name[10]; int hour; double pay; }; struct company employee[5]; double salary[5]; int i; for (i=0; i<5; i++) { printf("請輸入第%d位員工的姓名: ", i+1); scanf("%s", employee[i].name); printf("請輸入第%d位員工的工作時數: ", i+1); scanf("%d", &employee[i].hour); printf("請輸入第%d位員工一小時的工資: ", i+1); scanf("%lf", &employee[i].pay); } for (i=0; i<5; i++) { salary[i]=employee[i].hour*employee[i].pay; printf("%-10s 的薪水為 %.2f\n", employee[i].name, salary[i]); } //system("PAUSE"); return 0; } ``` ---- ## 510、結構 ```clike= #include <stdio.h> #include <stdlib.h> int main() { struct circle { int x, y; int radius; struct circle *next; }; struct circle *a, *b, *c, *current; a = (struct circle *)malloc(sizeof(struct circle)); printf("請輸入第一個圓的圓心(x, y): "); scanf("%d %d", &a->x, &a->y); printf("請輸入第一個圓的半徑: "); scanf("%d", &a->radius); a->next = NULL; b = (struct circle *)malloc(sizeof(struct circle)); printf("請輸入第二個圓的圓心(x, y): "); scanf("%d %d", &b->x, &b->y); printf("請輸入第二個圓的半徑: "); scanf("%d", &b->radius); b->next = NULL; a->next= b; c = (struct circle *)malloc(sizeof(struct circle)); printf("請輸入第三個圓的圓心(x, y): "); scanf("%d %d", &c->x, &c->y); printf("請輸入第三個圓的半徑: "); scanf("%d", &c->radius); c->next=NULL; b->next=c; current = a; int i = 1; while (current != NULL) { printf("第%d個圓的圓心為(%d, %d), 半徑為%d\n", i, current->x, current->y, current->radius); current = current->next; i++; } free(a); free(b); free(c); system("PAUSE"); return 0; } ``` ---- ![](https://i.imgur.com/v8dExpQ.png) ---- ## 補充練習 ```clike= #include <stdio.h> #include <stdlib.h> struct list { int n; struct list *next; }; int main() { int i; struct list *curr; struct list *temp; struct list *prev = NULL; for (i = 0; i < 30; i++) { temp = (struct list *)malloc(sizeof(struct list)); temp->n = (i + 1) * 10; temp->next = NULL; if (prev != NULL) { prev->next = temp; } else { curr = temp; } prev = temp; } while (curr != NULL) { printf("%d\n", curr->n); curr = curr->next; } system("pause"); return 0; } ``` --- # 第六類 ---- ## 602、字元字串與檔案處理 ```clike= #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char str2[]="Apple iPod"; char str4[]="Apple iPad"; int n; n = strcmp(str2, str4); if (n > 0) { printf("%s大於%s\n", str2, str4); } else if (n == 0) { printf("%s等於%s\n", str2, str4); } else { printf("%s小於%s\n", str2, str4); } n = strncmp(str2, str4, 5); if (n > 0) { printf("%s前五個字元大於%s前五個字元\n", str2, str4); } else if (n == 0) { printf("%s前五個字元等於%s前五個字元\n", str2, str4); } else { printf("%s前五個字元小於%s前五個字元\n", str2, str4); } system("PAUSE"); return 0; } ``` ---- ## 604、字元字串與檔案處理 ```clike= #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { char ch, ch2; printf("請輸入一小寫的英文字母: "); ch = getchar(); getchar(); //將下列的字元轉為大寫 ch2 = toupper(ch); printf("%c的大寫是%c\n", ch, ch2); /* while (ch != '\n') { continue; } */ printf("請輸入一大寫的英文字母: "); ch = getchar(); getchar(); //將下列的字元轉為小寫 ch2 = tolower(ch); printf("%c的小寫是%c\n", ch, ch2); //system("PAUSE"); return 0; } ``` ---- ## 606、字元字串與檔案處理 ```clike= #include <stdio.h> #include <stdlib.h> #include <stdlib.h> int main() { FILE *fptr; char ch; fptr = fopen("character.dat", "w"); printf("請輸入一字元: "); scanf("%c", &ch); //將一行多餘的字元丟掉 while (getchar() != '\n') { continue; } while (ch != '*') { fprintf(fptr, "%c", ch); printf("請輸入一字元: "); scanf("%c", &ch); //將一行多餘的字元丟掉 while (getchar() != '\n') { continue; } } fclose(fptr); fptr = fopen("character.dat", "r"); printf("\n以下是您輸入的資料:\n"); while (fscanf(fptr, "%c", &ch) != EOF) { printf("%3c\n", ch); } fclose(fptr); system("PAUSE"); return 0; } ``` ---- ## 608、字元字串與檔案處理 ```clike= #include <stdio.h> #include <stdlib.h> int main() { char str[]="Apple iPhone 4"; char sttr[]={'i', 'P', 'a', 'd', '\0'}; char *pstr="Apple iPod"; printf("str字串如下: %s\n", str); printf("sttr字串如下: %s\n", sttr); printf("pstr字串如下: %s\n", pstr); //system("PAUSE"); return 0; } ``` ---- ## 610、字元字串與檔案處理 ```clike= #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *fptr; char name[20]; int score; fptr = fopen("score.dat", "w"); printf("請輸入姓名: "); scanf("%s", name); printf("請輸入分數: "); scanf("%d", &score); while (score != -100) { fprintf(fptr, "%s %d\n", name, score); printf("請輸入姓名: "); scanf("%s", name); printf("請輸入分數: "); scanf("%d", &score); } fclose(fptr); fptr = fopen("score.dat", "r"); printf("\n以下是您輸入的資料:\n"); while (fscanf(fptr, "%s %d", name, &score) != EOF) { printf("%-10s %3d\n", name, score); } fclose(fptr); system("PAUSE"); return 0; } ``` --- # 第七類 ---- ## 702、其他論題 ```clike= #include <stdio.h> #include <stdlib.h> int main() { char string[20]; int output; printf("請輸入一數字的字串: "); scanf("%s", string); output = atoi(string); printf("%s轉換後的整數為%d\n", string, output); //system("PAUSE"); return 0; } ``` ---- ## 704、其他論題 ```clike= #define TRIPLE(x) (x)*(x)*(x) #include <stdio.h> #include <stdlib.h> int main() { int num, triple_num; printf("請輸入一個整數: "); scanf("%d", &num); triple_num = TRIPLE(num); printf("%d的三次方為%d\n", num, triple_num); triple_num = TRIPLE(4+1); printf("5的三次方為%d\n", triple_num); //system("PAUSE"); return 0; } ``` ---- ## 706、其他論題 ```clike= #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int x, y; double result; printf("請輸入x, y的值: "); scanf("%d %d", &x, &y); result = exp(5) * sqrt(pow(x, y) + log(100)) / pow(x, 2) * pow(y, 3); printf("result=%f\n", result); //system("PAUSE"); return 0; } ``` ---- ## 708、其他論題 ```clike= #include <stdio.h> #include <stdlib.h> #include <time.h> #define random(x) rand()%x+1 int main() { int i, j; int x; //srand(time(NULL)); printf("隨機數的最大值為: "); scanf("%d", &x); for (i=0; i<5; i++) { for (j=0; j<4; j++) { printf("%5d ", random(x)); } printf("\n"); } system("PAUSE"); return 0; } ``` ----- ## 710、其他論題 ```clike= #include <stdio.h> #include <stdlib.h> /* 若沒有定義Knum, 則加以定義為1000 */ #ifndef Knum #define Knum 1000 #endif /* 將Knum解除定義, 之後再定義為200 */ #undef Knum #define Knum 200 int main () { /* 印出最後的Knum值 */ printf("Knum = %d\n", Knum); /* 下列的變數d和i共用8個Bytes, 不是12個Bytes */ typedef union { double d; int i; } dataType; dataType dT; printf("請輸入d的變數值: "); scanf("%lf", &dT.d); printf("%f\n", dT.d); printf("請輸入i的變數值: "); scanf("%d", &dT.i); printf("%d\n", dT.i); system("PAUSE"); return 0; } ``` --- # 第八類 ---- ## 802、不定數迴圈 ```clike= #include <stdio.h> int main() { int score = 0; int grade; while (score >= 0 && score <= 100) { printf("please input score:"); scanf("%d", &score); if (score >= 0 && score <= 100) { grade = score / 10; switch (grade) { case 10: printf("grade:A+\n"); break; case 9: printf("grade:A\n"); break; case 8: printf("grade:B\n"); break; case 7: printf("grade:C\n"); break; case 6: printf("grade:D\n"); break; default: printf("grade:E\n"); } } } system("PAUSE"); return 0; } ``` ---- ## 804、平均值計算 ```clike= #include <stdio.h> #include <stdlib.h> float average(float[], int); int main() { int i; float data[6]; for (i = 0; i < 6; i++) { printf("請輸入第%d個浮點數:", i + 1); scanf("%f", data+i); } printf("您輸入的陣列值如下\n"); for (i = 0; i < 6; i++) { printf("data[%d]:%.3f\n", i, data[i]); } printf("平均:%.4f\n", average(data, 6)); system("pause"); return 0; } float average(float arr2[], int n) { float sum = 0; int i; for (i = 0; i < n; i++) { sum += arr2[i]; } return sum / n; } ``` ---- ## 806、九九乘法表 ```clike= #include <stdio.h> #include <stdlib.h> void printStar(int); void multiply(int, int); int main() { int n1, n2; printf("請輸入您要幾成幾的乘法表(最多10):"); scanf("%d", &n1); printf("請輸入您要多少個星星(*):"); scanf("%d", &n2); printStar(n2); if (n1 <= 10) { multiply(n1, n2); } system("pause"); return 0; } void multiply(int n1, int n2) { int i, j; for (i = 1; i <= n1; i++) { for (j = 1; j <= n1; j++) { printf("%2d*%2d=%2d ", i, j, i * j); } printf("\n"); printStar(n2); } } void printStar(int n) { int i; for(i = 0; i < n; i++) { printf("*"); } printf("\n"); } ``` ---- ## 808、氣泡排序 ```clike= #include <stdio.h> #include <stdlib.h> void sorting(int[], int); int main() { int i; int scoredata[10] = {3,1,6,9,5,2,7,10,4,8}; printf("請輸入十個數...\n"); for (i = 0; i < 10; i++) { printf("第 %d 個: ", i + 1); scanf("%d", &scoredata[i]); } printf("排序前: "); for (i = 0; i < 10; i++) { printf("%d ", scoredata[i]); } printf("\n"); sorting(scoredata, 10); printf("排序後: "); for (i = 0; i < 10; i++) { printf("%d ", scoredata[i]); } printf("\n"); system("pause"); return 0; } void sorting(int data2[], int n) { int i, j, temp, count = 0; for (i=0; i < n-1; i++) { for (j=0; j < n-i-1; j++) { if (data2[j] > data2[j+1]) { temp = data2[j]; data2[j] = data2[j+1]; data2[j+1] = temp; count++; } } } printf("排序次數: %d\n", count); } ``` ---- ## 810、矩陣乘積 ```clike= #include<stdio.h> #include<stdlib.h> #include <math.h> int main() { int i, j, m1[3][3], m2[3][3], m3[3][3]; printf("請輸入矩陣一...\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("m1[%d][%d]:", i, j); scanf("%d", &m1[i][j]); } } printf("請輸入矩陣二...\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("m2[%d][%d]:", i, j); scanf("%d", &m2[i][j]); } } //計算矩陣三 for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { m3[i][j] = pow(m1[i][j], m2[i][j]); } } printf("矩陣一\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%3d ", m1[i][j]); } printf("\n"); } printf("矩陣二\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%3d ", m2[i][j]); } printf("\n"); } printf("矩陣三\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%3d ", m3[i][j]); } printf("\n"); } system("PAUSE"); return 0; } ``` --- # 第九類 ---- ## 904、成績檔案讀寫 ```clike= #include <stdio.h> #include <stdlib.h> int main() { FILE *fp = fopen("score.dat", "w"); int score = 0; char name[32]; while (score >= 0) { printf("請輸入學生的姓名(分數輸入負的分數時結束):"); scanf("%s", name); printf("請輸入此學生C語言分數:"); scanf("%d", &score); fprintf(fp, "%s\n", name); fprintf(fp, "%d\n", score); } fclose(fp); system("pause"); return 0; } ``` ```clike= #include <stdio.h> #include <stdlib.h> int main() { FILE *fp = fopen("score.dat", "r"); int score = 0; char name[32]; while (score >= 0) { fscanf(fp, "%s", name); fscanf(fp, "%d", &score); if (score >= 0) { printf("%s的C語言分數是%d\n", name, score); } } fclose(fp); system("pause"); return 0; } ``` ---- ## 906、學生成績 ```clike= #include <stdio.h> #include <stdlib.h> struct student { char name[32]; int score; }; int main() { int i; struct student data[5]; for (i=0; i<5; i++) { printf("請輸入第%d位同學的姓名:", i+1); scanf("%s", data[i].name); printf("請輸入第%d位同學C語言的分數:", i+1); scanf("%d", &data[i].score); } for (i=0; i<5; i++) { printf("%-20s %3d\n", data[i].name, data[i].score); } system("pause"); return 0; } ``` ---- ## 908、亂數 ```clike= #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int i, j, rnd; srand(time(NULL)); for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { rnd = rand() % 1000 + 1; printf("%4d ", rnd); } putchar('\n'); } system("pause"); return 0; } ``` ---- ## 910、動態記憶體配置 ```clike= #include<stdlib.h> #include<stdio.h> struct student { char name[32]; int score; struct student* next; }; int main() { struct student *a, *b, *c, *current; a = malloc(sizeof(struct student)); printf("請輸入第一個學生姓名: "); scanf("%s", a->name); printf("分數: "); scanf("%d", &a->score ); a->next = NULL; b = malloc(sizeof(struct student)); printf("請輸入第二個學生姓名: "); scanf("%s", b->name); printf("分數: "); scanf("%d", &b->score ); b->next = NULL; c = malloc(sizeof(struct student)); printf("請輸入第三個學生姓名: "); scanf("%s", c->name); printf("分數: "); scanf("%d", &c->score ); c->next = NULL; a->next = b; b->next = c; printf("\n輸出...\n"); current = a; while (current!=NULL) { printf("學生: %s\n", current->name); printf("分數: %d\n\n", current->score); current = current->next; } free(a); free(b); free(c); system("PAUSE"); return 0; } ``` --- # 延伸閱讀 * [你腦袋的C更新了嗎?](https://www.ithome.com.tw/voice/108806) * 書:[21 世紀 C 語言](https://www.tenlong.com.tw/products/9789863470403) * 書:[精通C程式設計](https://www.tenlong.com.tw/products/9789864766437) * 書:[無瑕的程式碼-敏捷軟體開發技巧守則](https://www.tenlong.com.tw/products/9789862017050) Clean Code * 書:[無瑕的程式碼 番外篇-專業程式設計師的生存之道](https://www.tenlong.com.tw/products/9789862017883) Clean Coder * 經典書:The C programming language、C Primer Plus * [視覺化氣泡排序法](https://visualgo.net/en/sorting) --- Kyle <i class="fa fa-paper-plane"></i> lyhcode@gmail.com
{"metaMigratedAt":"2023-06-15T07:08:14.977Z","metaMigratedFrom":"YAML","title":"程式設計培訓 - C 語言","breaks":true,"slideOptions":"{\"theme\":\"black\",\"transition\":\"fade\"}","contributors":"[{\"id\":\"6470514f-e18d-4518-9436-f9a1219cbf2b\",\"add\":32671,\"del\":3845}]","description":"<span style=\"color:gray;font-weight:100\">2022 / 12</span>"}
    1022 views