簡明 C 程式設計 - 使用 Code::Blocks ( 附範例光碟 ) 劉立民編著 全華圖書股份有限公司印行
劉立民 現職 中原大學應用數學系副教授 世新大學資管系副教授 學歷 美國紐澤西理工學院資訊博士 (1999) 雪城大學資科所碩士 (1994) 經歷 美國紐澤西州立. 肯恩大學. 資訊科學系助理教授 Pumpkin Networks Inc., (Sunnyvale CA) 資深工程師 / 專案經理 AT&T(Middletown, NJ) 資深工程師 專長與研究領域 人工智慧 語意網路 指紋影像處理 資料庫系統 電腦輔助教學 機器學習 深度學習 學術生涯迄今發表二十餘篇期刊論文 翻譯數本機器學習 深度學習相關書籍 教授 C 程式語言設計課程有超過 20 年的經驗 ii 06356007-00文前.indd 2 2017/12/7 上午 09:36:07
約半世紀之前 計算機的發展因為新材料的陸續發明 硬體的發展速度遠比軟體 的發展來的快 早期的計算機 使用所謂的 機器語言 或 組合語言 來撰寫 系統 不同的機器 會有不同的機器語言 因此 針對甲機器所製作的程式 自然不 能在不同品牌的乙機器上使用 以這種方式來發展軟體 當然很不理想 不符合經濟 效益 C 語言在約在 1970 年代 由 Dennis Ritchie 與 Brian Kernighan 在美國 貝爾實 驗室 中被設計出來 使用這個具有 可攜性 的高階程式語言 可以在甲機器上製 作能在乙機器上執行的程式 因此 從 70 年代以來 C 語言一直在資訊界扮演一個非 常重要的角色 IEEE Spectrum 近年來會對程式語言做重要性的排名 C 這個 古老 的 語言一直都還能排在前 3 名 2016 年最新公佈的報告 下圖 C 語言甚至排在 第一名 也是前 10 名中 最老的程式語言 由此可知它的重要性 在資訊界幾乎可以 說是不可取代的 出自 http://spectrum.ieee.org/ 網站首頁 學習 C 程式語言 如果只學它的語法 結構 那一定會很繁瑣 無趣 可是在學 習基本語法的過程中 又不大可能有足夠的能力來解決企業界實際的問題 幸好過去 幾十年來 世界各地的先進 精心設計了許多不同程度的小問題 舉辦了許多的程式 設計比賽 其中也包含了深淺不一的程式問題 這些問題 雖然多半比實務問題簡單 卻足夠檢驗讀者是否消化 吸收了所學習的 C 語言機制 本書主要介紹是由西班牙 University of Valladolid 所收集的問題集 以 UVa 來編號 Valladolid 大學設計了一個 非常好用 方便的網站 https://uva.onlinejudge.org/ 讀者可以在線上提交 檢查程式 是否正確 iii 06356007-00文前.indd 3 2017/12/5 上午 10:48:03
UVa https://uva.onlinejudge.org/ 2013 Collegiate Programming Examination Committee, CPE Committee https://cpe.cse.nsysu.edu.tw/ CPE 2010 4 50 CPE https://cpe.cse.nsysu.edu.tw/ CPE C UVa http://coding-frenzy.arping.me/ CPE iv 06356007-00 文前.indd 4 2017/12/5 上午 10:48:06
http://coding-frenzy.arping.me/ 1 CPE 22 3 2-2 UVa 6 C UVa UVa UVa C pair programming v 06356007-00 文前.indd 5 2017/12/5 上午 10:48:08
https://goo.gl/2pt188 UVa problem-based learning PBL projectbased learning UVa C vi 06356007-00 文前.indd 6 2017/12/5 上午 10:48:12
0 0-1...0-2 0-2...0-4 0-3...0-7 0-4...0-8 1 C 1-1 C...1-2 1-2...1-3 1-3 C...1-6 1-4...1-9 2 2-1...2-2 2-2...2-5 2-3...2-5 2-4...2-8 2-5...2-10 3 3-1...3-2 3-2...3-4 vii 06356007-00 文前.indd 7 2017/12/5 上午 10:48:14
4 4-1...4-2 4-2...4-2 4-3 if...4-4 4-4 if (nested if)...4-6 4-5 case-switch...4-13 5 5-1 while...5-2 5-2 infinite loop...5-2 5-3...5-4 5-4 break...5-7 6 6-1...6-2 6-2 void...6-2 6-3 return...6-3 6-4...6-3 6-5...6-6 6-6 Linux Ubuntu...6-8 7 7-1 FILE...7-2 7-2...7-2 7-3 /...7-2 7-4...7-4 viii 06356007-00 文前.indd 8 2017/12/5 上午 10:48:18
7-5...7-5 7-6...7-6 7-7...7-7 8 8-1 do-while...8-2 8-2 continue...8-3 8-3 repeat-until...8-4 8-4 for...8-6 8-5 (Nested loop)...8-8 8-6...8-14 9 9-1...9-2 9-2...9-3 9-3...9-5 9-4...9-9 9-5...9-12 10 I I...10-1 ix 06356007-00 文前.indd 9 2017/12/5 上午 10:48:20
11 11-1 (array)... 11-2 11-2 one-dimentional array... 11-3 11-3... 11-6 11-4... 11-7 12 II 12-1...12-2 12-2...12-6 13 13-1 (multi-dimensional array)...13-2 13-2 two-dimensional array...13-2 13-3...13-7 13-4...13-8 14 III 14-1 Tic-Tac-Toe...14-2 14-2...14-8 x 06356007-00 文前.indd 10 2017/12/5 上午 10:48:20
15 15-1...15-2 15-2...15-4 15-3...15-7 15-4...15-8 15-5...15-13 16 IV IV...16-1 17 17-1...17-2 17-2...17-7 17-3... 17-11 18 V V...18-1 xi 06356007-00 文前.indd 11 2017/12/5 上午 10:48:20
19 19-1...19-2 19-2...19-3 19-3...19-7 20 20-1...20-2 20-2...20-4 20-3...20-9 21 21-1...21-2 21-2...21-5 21-3...21-8 22 VI VI...22-1... A-1 xii 06356007-00 文前.indd 12 2017/12/5 上午 10:48:21
7 interactive program FILE / 06356007-NE 三校 N.indd 1 2017/12/4 下午 03:09:05
7-2 C Code::Blocks 7-1 FILE C FILE FILE struct 7-2 stdio.h FILE *fopen(const char *fname, const char *mode) int fclose(file *stream) int fprintf(file *stream, const char *format,...) int fscanf(file *stream, const char *format,...) 7-3 / 7-3-1 indiretion operator * FILE * FILE * fpt1;// FILE * fpt1 fpt2 FILE * fpt1, * fpt2; C 7-3-2fopen FILE *fopen(const char *fname, const char *mode) filename mode filename mode P7-1 "r" read 06356007-NE 三校 N.indd 2 2017/12/4 下午 03:09:05
CH7 7-3 absolute path C:\ D:\ UNIX / relative path P7-1 fopen() fopen() fopen() FILE * fopen() C if 7-3-3fclose int fclose(file *stream) stream stream fopen() P7-1 1 int main() { 2 FILE * fpt1, * fpt2; // stream 3 fpt1 = fopen("data.txt","r"); 4 if (fpt1) { 5 // 6 fclose(fpt1); // 7 } else { 8 // [ ] 9 printf(" \n"); 10 } 11 return 0; 12 } P7-1 data.txt 9 7-1 06356007-NE 三校 N.indd 3 2017/12/4 下午 03:09:05
7-4 C Code::Blocks fopen() 7-3-4mode 7-1 "r" read "w" write "a" append C 7-4 7-4-1 fprintf int fprintf(file *stream, const char *format,...) fprintf() printf() fprintf() FILE *stream fpt2 result.txt fpt2 = fopen("result.txt","w"); // "w" printf("%6.2f,%6.2f,%6.2f\n", 5.3, 7.2, 9.1); result.txt fprintf(fpt2,"%6.2f,%6.2f,%6.2f\n", 5.3, 7.2, 9.1); (fclose) fflush() P7-2 data.txt ( 2 ) fprintf() 3 ( 4~6 ) 4~6 7~9 7~9 4~6 06356007-NE 三校 N.indd 4 2017/12/4 下午 03:09:05
CH7 7-5 P7-2 1 int main() { 2 FILE * fpt2; 3 fpt2 = fopen("data.txt","w"); 4 if (fpt2) { 5 fprintf(fpt2,"%6.2f,%6.2f,%6.2f\n",-8.1,6.3,4.5); 6 fprintf(fpt2,"%6.2f,%6.2f,%6.2f\n",5.3,-7.2,9.1); 7 fprintf(fpt2,"%6.2f,%6.2f,%6.2f\n",1.5,8.1,-14.); 8 printf("%6.2f,%6.2f,%6.2f\n",-8.1,6.3,4.5); 9 printf("%6.2f,%6.2f,%6.2f\n",5.3,-7.2,9.1); 10 printf("%6.2f,%6.2f,%6.2f\n",1.5,8.1,-14.); 11 fclose(fpt2); 12 } 13 return 0; 14 } P7-2 data.txt (P7_2.cbp) 7-2 7-5 7-5-1 fscanf int fscanf(file *stream, const char *format,...) printf()/fprintf() fscanf() scanf FILE *streamfscanf() stream fprintf() stream 06356007-NE 三校 N.indd 5 2017/12/4 下午 03:09:06
7-6 C Code::Blocks P7-2 data.txt 3 3 P7-3 data.txt P7-3 1 int main() { 2 FILE * fpt1; 3 fpt1 = fopen("data.txt","r"); 4 if (fpt1) { 5 float f1, f2, f3; 6 fscanf(fpt1,"%f,%f,%f",&f1, &f2, &f3); 7 printf("[%6.2f:%6.2f:%6.2f]\n",f1,f2,f3); 8 fscanf(fpt1,"%f,%f,%f",&f1, &f2, &f3); 9 printf("[%6.2f:%6.2f:%6.2f]\n",f1,f2,f3); 10 fscanf(fpt1,"%f,%f,%f",&f1, &f2, &f3); 11 printf("[%6.2f:%6.2f:%6.2f]\n",f1,f2,f3); 12 fclose(fpt1); 13 } 14 return 0; 15 } P7-3 data.txt (P7_3.cbp) 7-3 7-6 10 UVa 10812 Beat the Spread! 40 20 20 40 06356007-NE 三校 N.indd 6 2017/12/4 下午 03:09:06
CH7 7-7 2 40 20 20 40 P7-4 1 int main() { 2 FILE * fpt1; 3 fpt1 = fopen("data.txt","r"); 4 if (fpt1) { 5 int n, i=0, data1, data2; 6 fscanf(fpt1,"%d",&n); 7 while(i++ < n) { 8 fscanf(fpt1,"%d %d",&data1, &data2); 9 printf(" (%d,%d)\n", data1, data2); 10 } 11 fclose(fpt1); 12 } 13 return 0; 14 } P7-4 5 i 0 i++ 6~9 7-4 7-7 12 UVa 10929 You can say 11 0 06356007-NE 三校 N.indd 7 2017/12/4 下午 03:09:06
7-8 C Code::Blocks 7-7-1EOF End-Of-File fscanf() EOFfscanf() 16 UVa 10235 Simply Emirp P7-5 EOF <Ctrl>+<D> UNIX <Ctrl>+<Z> C EOF EOF P7-5 1 int main() { 2 FILE * fpt1; 3 fpt1 = fopen("data.txt","r"); 4 if (fpt1) { 5 while ( fscanf(fpt1,"%d",&data)!=eof ) { 6 printf(" (%d)\n", data); 7 } 8 fclose(fpt1); 9 } 10 return 0; 11 } 7-5 06356007-NE 三校 N.indd 8 2017/12/4 下午 03:09:07
1. (data.txt) 3 32 54 98-9 35 6 3 2. (data.txt) 3 3.2 54 98-9 3.5 6 3 3. 20 (0~999) 4. rand() n 0<n<=500 (0~999) 5. ( ) ( ) 1 6. 1 XXX.XX (0.00~999.99) 7. rand() n 0<n<=1000 ( ) XXX.XX (0.00~999.99) 8. ( ) ( ) 9. (data.txt) 3 ( 5 ) (data.txt) 3 1:12,12,12 2:5,5,3 : ( ) 1: 2: : 06356007-NE 三校 N.indd 9 2017/12/4 下午 03:09:07
10. 某甲號稱製作了一個程式 它能輸入三角形的三個邊長 輸出這是什麼三角形 輸入的資料檔 範例如下 1:12,12,12 2:5,5,3 : 如果你的工作是測試某甲的程式是否能正常工作 你要準備什麼樣的資料來測試某甲的程式 06356007-NE三校N.indd 10 2017/12/4 下午 03:09:07