1 / 116

Programming Concept

Programming Concept. 蔡文能 tsaiwn@csie.nctu.edu.tw 交通大學資訊工程學系. http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/. 程式 隨便寫 隨便對 . ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs2/. 蔡文能 tsaiwn@csie.nctu.edu.tw 交通大學資訊工程學系. 平方根 – 簡 單演算法 九九乘法表 for Loop vs. while Loop Pascal Triangle

riva
Download Presentation

Programming Concept

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Concept 蔡文能 tsaiwn@csie.nctu.edu.tw 交通大學資訊工程學系

  2. http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/ 程式 隨便寫 隨便對 ftp://ftp.csie.nctu.edu.tw/pub/CSIE/course/cs2/ 蔡文能 tsaiwn@csie.nctu.edu.tw 交通大學資訊工程學系 • 平方根 – 簡單演算法 • 九九乘法表 • for Loop vs. while Loop • Pascal Triangle • 二進位也可以很有趣 -- 如何印出該六張卡片?

  3. sqrt 是程式庫裡的一個 函數(function, 函式) C型式的註解 印出根號 2 C++型式的註解 /* 寫個程式印出根號2 */ #include<stdio.h> double sqrt(double); // #include<math.h> int main( ) { printf(" sqrt 根號 2 =%f\n ", sqrt(2.0) ); // C++ 式的註解 comment printf("Bye bye!\n"); return 0; }

  4. 印出根號 3 /* 寫個程式印出根號3 */ #include<stdio.h> double sqrt(double); // #include<math.h> int main( ) { printf(" sqrt 根號 3 =%f\n ", sqrt(3.0) ); // C++ 式的註解 comment printf("Bye bye!\n"); return 0; } 每次要重改程式嗎?

  5. 使用變數 &y傳 address 印出根號 x /* 寫個程式印出根號 x */ #include <stdio.h> #include<math.h> double y; int main( ) { printf(" Give me x :"); scanf("%lf" , &y); printf("Sqrt(%f)=%f\n", y, sqrt(y) ); return 0; }

  6. 程式 隨便寫 求 x 的平方根 練習使用 while loop file:///c:\course\cs2\ file:///c:\course\cs2\p3.c 蔡文能 tsaiwn@csie.nctu.edu.tw 交通大學資訊工程學系

  7. p3.c – 練習 while loop #include <stdio.h> #include <math.h> int main() /* This program can find square root of any number that >=0 and will stop when you enter a value that < 0 阿版權是蔡文能的, 歡迎拷貝分享初學者! */ { float onevar; /* 變數名稱隨便我取的啦 */ double ans; /* 用來放答案 */ printf("Find square root of X. Negative value will cease the program\n"); printf("X=? "); /* 還是問"X=?" 比較簡潔有力, 不用說onevar */ scanf("%f", &onevar); while(onevar >=0 ) { /*只要onevar大於或等於零我們就一直重複做{...} */ ans=sqrt(onevar); /*其實sqrt的參數也應該為double 型別*/ printf(" SQRT(%f)=%f\n", onevar, ans); printf("X=? "); scanf("%f", &onevar); } /* while(onevar */ return 0; }

  8. 程式 隨便寫 求 x 的平方根 自己如何寫求 SQRT? 自己如何寫求 SQRT? file:///c:\course\cs2\p4.c file:///c:\course\cs2\p5.c 目的:練習 想 Algorithm 長除法求平方根? 適合手動做的方法不一定適合電腦做!

  9. p4.c – 練習 想 Algorithm(1/2) /* This program can find square root of any number that >=0 and will stop when you enter a value that < 0 */ /* This version is trying to show you that we can write our function/procedure to solve the problem, though the mysqrt() is actually calling the system standard function. We will rewrite the mysqrt() in next version. *****************************/ void hello(void); /*我會用到一個沒參數的函數叫hello() */ double mysqrt(double); /*也會叫用一個叫mysqrt()的函數,參數是一個double數 */ #include <math.h> /* sqrt()是在math.h內宣告的 */ int main() /* 主程式開始囉 */ { float x; hello(); /* give the user some message */ printf("X=? "); scanf("%f", &x); while(x >=0 ) { printf(" SQRT(%f)=%7.3f\n", x, mysqrt(x) ); printf("X=? "); scanf("%f", &x); } /* while */ return 0; } /**************主程式到此為止***********/

  10. p4.c – 練習 想 Algorithm(2/2) /*…*/ void hello() { printf("This program will ask you to input a value, say X."); printf("Then it will find the square root of X."); printf("The above process will continue ..."); printf("Negative value will cease the program."); } /* hello() */ double mysqrt(double y) { double myans; myans = -1.0; if(y < 0) printf(" can not process negative number"); else myans = sqrt(y); /* 還沒想好, 先借用程式庫的 sqrt( ) */ return myans; } /*mysqrt*/

  11. p5.c – mysqrt( ) Algorithm(1/2) void hello(void); /*我發誓我會用到一個沒參數的函數叫hello() */ double mysqrt(double); /*也會叫用一個叫mysqrt()的函數,參數是一個double數 */ int main() /* 主程式開始囉 */ { float x; hello(); /* give the user some message */ printf("X=? "); scanf("%f", &x); while(x >=0 ) { printf(" SQRT(%f)=%7.3f\n", x, mysqrt(x) ); printf("X=? "); scanf("%f", &x); } /* while */ return 0; } /**************主程式到此為止***********/ void hello() { printf("This program will ask you to input a value, say X."); printf("Then it will find the square root of X."); printf("The above process will continue ..."); printf("Negative value will cease the program."); } /* hello() */

  12. p5.c – mysqrt( ) Algorithm(2/2) double mysqrt(double x) { /* 這次真的要自己寫求平方根的部份囉! */ double myans; myans = -1; if(x < 0) printf(" can not process negative number\n"); else { myans = 0; /* we guess the root is zero */ while(myans*myans < x) myans++; /* 猜0, 1, 2,...直到太大 */ if(myans*myans>x){ myans =myans-1; /*太大了, 快減掉一 ; 可寫成myans--*/ while(myans*myans < x) myans = myans+0.1; }; if(myans*myans>x){ myans-=0.1; /* this means : myans = myans -0.1 */ while(myans*myans < x) myans += 0.01; } if(myans*myans>x){ myans -= 0.01; while(myans*myans < x) myans += 0.001; } /* I think the precison is good enought now */ } /*else*/ return myans; /** return myans value as our answer */ } /* end of mysqrt*/

  13. mysqrt( )問題與思考? • 若想要求到更準呢? • 這程式中函數mysqrt( )所求出的平方根若非剛好就會太大, 這並非最好的做法! • 應該找出最接近的答案, 例如, 求2的平方根時, 最後應考慮 1.415*1.415 與 1.414*1.414 何者較接近2, 如此比較合理! •  加一句 if 檢查看看就可以, 想一想!

  14. 程式 隨便寫 九九乘法表 file:///c:\course\cs2\p6.c 練習兩層的 for loop for(i=1; i<=9; ++i) { for(k … ) { /****/ } }

  15. 九九乘法表 p6.c 練習兩層的 for loop #include <stdio.h> int main() { int i,j; printf("\nX |"); for(i=1; i <= 9; i++) printf("%5d", i); printf("\n"); for(i=1 ; i <= 10 ; i++) printf("---- "); for(i=1 ; i<= 9 ; i++) { printf("\n%d |" , i); for(j=1 ; j <= 9 ; j++) printf("%5d", i*j); } printf("\n======\n"); return 0; }

  16. 九九乘法表 p6.c 核心 練習兩層的 for loop #include <stdio.h> int main() { int i,k; for(i=1 ; i<= 9 ; i++) { printf("第%d 列: " , i); for(k=1 ; k <= 9 ; k++) printf("%5d", i*k); printf("\n"); } printf("\nBye bye!\n"); return 0; }

  17. for Loop vs. while Loop 這三個寫法意義完全一樣 for(i=1 ; i<= 9 ; i++) { /* Loop body */ } i=1; for( ; i<= 9 ; ) { /* Loop body */ i++; } i=1; while( i<= 9 ) { /* Loop body */ i++; }

  18. for Loop vs. while Loop for( ;  ; ) ;  ; while( ) {  ; ; } == 0  != 0    

  19. while Loop 拿盤子, 拿飲料, 找好位子; while(肚子還餓) { 吃一盤; 喝一杯; 喘一口氣; } 結帳; 回家; All you can eat !

  20. while Loop (cont-1) 拿盤子, 拿飲料, 找好位子; while(肚子還餓) { 吃一盤; if(有急事 || 很飽了)break; if(不會渴) continue; 喝一杯; 喘一口氣; } 結帳; 回家; All you can eat !

  21. while Loop (cont-2) 拿盤子, 拿飲料, 找好位子; while(不夠本) { 吃一盤; if(有急事 || 很飽了)break; if(不會渴) continue; 喝一杯; 喘一口氣; } 結帳; 回家;

  22. p7.c 巴斯卡三角形練習 Array (陣列) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 By Blaise Pascal, 1653 (a+b)2 = a2+2a.b+b2 C(5,0) = 1 C(5,1) = 5 C(5,2) = 10 = C(5,3)

  23. 巴斯卡三角形, 方便寫程式 0 1 2 3 4 5 6 7 8 . . . 0 1 1 1 1 2 1 2 1 i 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 #define MAX 19 int x[19][19]; int x[MAX][MAX];

  24. p7.c 巴斯卡三角形 for(i=0; i<=n; i++) { x[i][0]=1;x[i][i] =1; /*記得宣告/定義x陣列喔*/ for(j=1; j<= i-1; j++) { x[i][j]= x[i-1][j] + x[i-1][j-1]; } for(j =0; j<=i; j++){ 1 printf("%5d", x[i][j]); 1 1 }; printf("\n"); 1 21 }; /* for i */ 1 3 31 1 4 6 41 第 j 格 第 i 列

  25. 問題與思考 (Pascal 三角形 ) 其實只用一維陣列(one dimentional array) 就可以做出上述的巴斯卡三角形 Why? 因為是橫著填 .. 填完一列就可印出 • 程式要如何修改? • 提示: 中間的要由右而左填 ! • ? 巴斯卡三角形在數學上有哪些意義?

  26. 二進位也可以很有趣  想一個 0 到 63 間的任一個數, 不要告訴我答案... 但是告訴我有沒有在以下哪幾張卡片中? 我可以很快找出來喔!

  27. 二進位也可以很有趣 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63

  28. 二進位也可以很有趣 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63

  29. 二進位也可以很有趣 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31, 40, 41, 42, 43, 44, 45, 46, 47, 56, 57, 58, 59, 60, 61, 62, 63

  30. 二進位也可以很有趣 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31, 36, 37, 38, 39, 44, 45, 46, 47, 52, 53, 54, 55, 60, 61, 62, 63

  31. 二進位也可以很有趣 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 42, 43, 46, 47, 50, 51, 54, 55, 58, 59, 62, 63

  32. 二進位也可以很有趣 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63

  33. 二進位也可以很有趣(原理) 1, 3, 5, 7, 0 0 0 0 0 1 = 1 9, 11, 13, 15, 0 0 0 0 1 0 = 2 17, 19, 21, 23, 0 0 0 0 1 1 = 3 25, 27, 29, 31, 0 0 0 1 0 0 = 4 33, 35, 37, 39, 0 0 0 1 0 1 = 5 41, 43, 45, 47, 0 0 0 1 1 0 = 6 49, 51, 53, 55, 0 0 0 1 1 1 = 7 57, 59, 61, 63 0 0 1 0 0 0 = 8

  34. 二進位也可以很有趣(原理) 2, 3, 6, 7, 0 0 0 0 0 1 = 1 10, 11, 14, 15, 0 0 0 0 1 0 = 2 18, 19, 22, 23, 0 0 0 0 1 1 = 3 26, 27, 30, 31, 0 0 0 1 0 0 = 4 34, 35, 38, 39, 0 0 0 1 0 1 = 5 42, 43, 46, 47, 0 0 0 1 1 0 = 6 50, 51, 54, 55, 0 0 0 1 1 1 = 7 58, 59, 62, 63 0 0 1 0 0 0 = 8

  35. 二進位也可以很有趣(原理) 4, 5, 6, 7, 0 0 0 0 0 1 = 1 12, 13, 14, 15, 0 0 0 0 1 0 = 2 20, 21, 22, 23, 0 0 0 0 1 1 = 3 28, 29, 30, 31, 0 0 0 1 0 0 = 4 36, 37, 38, 39, 0 0 0 1 0 1 = 5 44, 45, 46, 47, 0 0 0 1 1 0 = 6 52, 53, 54, 55, 0 0 0 1 1 1 = 7 60, 61, 62, 63 0 0 1 0 0 0 = 8

  36. 二進位也可以很有趣(原理) 8, 9, 10, 11, 0 0 0 1 1 1 = 7 12, 13, 14, 15, 0 0 1 0 0 0 = 8 24, 25, 26, 27, 0 0 1 0 0 1 = 9 28, 29, 30, 31, 0 0 1 0 1 0 = 10 40, 41, 42, 43, 0 0 1 0 1 1 = 11 44, 45, 46, 47, 0 0 1 1 0 0 = 12 56, 57, 58, 59, 0 0 1 1 0 1 = 13 60, 61, 62, 63 0 0 1 1 1 0 = 14

  37. 二進位也可以很有趣(原理) 16, 17, 18, 19, 0 1 0 0 0 0 = 16 20, 21, 22, 23, 0 1 0 0 0 1 = 17 24, 25, 26, 27, 0 1 0 0 1 0 = 18 28, 29, 30, 31, 0 1 0 0 1 1 = 19 48, 49, 50, 51, 0 1 0 1 0 0 = 20 52, 53, 54, 55, 0 1 0 1 0 1 = 21 56, 57, 58, 59, 0 1 0 1 1 0 = 22 60, 61, 62, 63 0 1 0 1 1 1 = 23

  38. 二進位也可以很有趣(原理) 32, 33, 34, 35, 1 0 0 0 0 0 = 32 36, 37, 38, 39, 1 0 0 0 0 1 = 33 40, 41, 42, 43, 1 0 0 0 1 0 = 34 44, 45, 46, 47, 1 0 0 0 1 1 = 35 48, 49, 50, 51, 1 0 0 1 0 0 = 36 52, 53, 54, 55, 1 0 0 1 0 1 = 37 56, 57, 58, 59, 1 0 0 1 1 0 = 38 60, 61, 62, 63 1 0 0 1 1 1 = 39

  39. 卡片玩法與原理 請玩者心裡想一個0到63的整數不要告訴你(廢話), 然後你依第六張、 第五張、第四張 ... 第一張拿給他, 請他告訴你上面有沒有他想的數, 為了表示你是過目不忘, 交給他之前假裝看一看, 交給他後不要立刻 拿回來, 只請他分別放不同手中作為證據。 答案: 如果六張都沒有就是0。 在每張給他之前看清楚第一個數, 只要他說有就加到答案中, 等到 最後一張給他, 等他說出有沒有後, 答案也出來了。 原理: 其實每張紙都是代表一個二進位數 (binary number)。 有就是 1, 沒有就是 0。 與上一遊戲一樣, 他已經把答案告訴你了 ! 因每張紙上第一位數就是該位的比重(weight, 加權)。

  40. 二進位也可以很有趣 /* 寫程式印出前述之六張卡片 */ for( int i=0; i <= 5; i++) { printf("二進位也可以很有趣\n\n "); /* 印出第 i 張卡片 */ printf("\n\n\n"); }

  41. ? 二進位也可以很有趣 for( int i=0; i <= 5; i++) { printf("二進位也可以很有趣\n\n "); for(k=0; k <= 63; k++) { if( k & (2的 i 次方) != 0) { printf("%2d, ", k); if(必要時) printf("\n"); } } printf("\n\n\n"); }

  42. ? int yy[ ] = { 1, 2, 4, 8, 16, 32}; for( int i=0; i <= 5; i++) { printf("二進位也可以很有趣\n\n "); for(k=0; k <= 63; k++) { if( k & yy[i] != 0) { printf("%2d, ", k); if(必要時) printf("\n"); } } printf("\n\n\n"); } 用一個陣列記住2的 i 次方

  43. ? if(必要時) printf("\n"); int np = 0; /* numbers printed */ for(k=0; k <= 63; k++) { if( k & yy[i] != 0) { printf("%2d, ", k); np++; // number printed if( np%8 == 0) printf("\n"); } }

  44. ? if(必要時) printf("\n"); int np = 0; for(k=0; k <= 63; k++) { if( k & yy[i] != 0) { printf("%2d, ", k); /// np++; // number printed if( (++np)%8 == 0) printf("\n"); // 每印完八個數就換列(new line) } }

  45. 二進位也可以很有趣完整程式 /* 寫程式印出前述之六張卡片 */ #include<stdio.h> int yy[ ] = { 1, 2, 4, 8, 16, 32}; int k, np; main( ) { for( int i=0; i <= 5; i++) { printf("二進位也可以很有趣\n\n "); np=0; /* 印出第 i 張卡片 */ printf("\n\n\n"); } } 前一頁程式碼 for . .

  46. 問題與思考 * 為什麼要依第六張(32開頭那張)、 第五張、 第四張 ... 第一張拿給玩者? * 如果不用一個table (array) 來記住2的i 次方, 那要怎麼做? 用乘的 –– 有何缺點? * 每張卡片最後一列也印了逗點, 如何改進? (hint: 加一列 if )

  47. 問題與思考 (ASCII code ) #include<stdio.h> main( ) { printf(" %c 的 ASCII code是%d\n", '0', '0'); printf(" %c 的 ASCII code是%d\n", 'A', 'A'); printf(" %c 的 ASCII code是%d\n", 'a', 'a'); } 如果系統使用EBCDIC碼呢?

  48. 問題與思考 (ASCII code ) #include<stdio.h> main( ) { int i, k=0; for(i=65; i<=122; ++i) { printf(" %c 的 ASCII code", i); printf(" 是%d", i); ++k; printf(k%3==0? "\n" : "\t"); } printf("\n" ); }

  49. 問題與思考 (中文碼?) #include<stdio.h> unsigned char x[9] = { 0 }; main( ) { int m = 0xa4, n=0x6a; x[0] = m; x[1]=n; x[2] = 0xae, x[3]=97; x[4] = 0xa6, x[5]=0x6e; printf("==%s==\n", x); }

  50. 問題與思考 (中文碼?) 先把前面程式存入 testc.c ccbsd2: tsaiwn> gcc testc.c ccbsd2: tsaiwn> ./a.out ==大家好== ccbsd2: tsaiwn> 編 譯 與 連 結 執 行

More Related