01 //p7a.c -- @CopyLeft by tsaiwn@cs.nctu.edu.tw 02 // 這是個小例子, 有助於你完成身分證號碼檢查器進階版 03 #define GGG 1 04 #define YYY 2 05 #define GGYY 4 06 #define ERR_HA 8 07 int errTable[ ]= {1, 2, 4, 8}; // 方便用 Loop 處理 08 char errMsg[ ][66]={"GGGmag", "YYY--", "GGYYYYY", "HahaHeeError"}; 09 const int nMsg = (sizeof errTable / sizeof errTable[0]); 10 int erraa( ), errbb( ), errcc( ); // 宣告 11 void pError(int); 12 int main( ) { // 大多數 main program 都這樣開頭 13 int er; // for error code 14 er = erraa( ); 15 pError(er); 16 er = errbb( ); 17 pError(er); 18 pError( errcc( ) ); 19 pError( 7 ); // 7 = 1 + 2 + 4 == 哪三種 Error ? 20 printf("Bye bye!\nHit RETURN key ..."); 21 getchar( ); // 企圖讀取一個 char, 讓程式停著等 User 按 RETURN 鍵 22 return 0; // 告知作業系統(OS)表示我們這主程式正常結束 23 }// main( 24 ///////////////////////////////////////// 25 int erraa( ) { 26 int ans=0; 27 ans += GGG; // OK 因為 bit 沒重疊 28 ans = ans + GGYY; // 也可用 | bitwise OR 29 return ans; 30 } 31 int errbb( ) { 32 return YYY | GGYY; // bitwise OR 33 } 34 int errcc( ) { 35 return ERR_HA | GGG | YYY; 36 } 37 void pError(int n){ // n 應該叫 errno 比較好 ! 38 static int i, count = 0; 39 printf(" Error check at %d", ++count); 40 if(count == 1)printf("st:\n"); 41 else if(count == 2)printf("nd:\n"); 42 else if(count == 3)printf("rd:\n"); 43 else printf("-th:\n"); 44 for(i=0; i< nMsg; ++i) { 45 if(n & errTable[i]) printf(" %s\n", errMsg[i]); /// 46 }//for i 47 } 48 //////////////////////////////////////////////////////////// 49 /****** 50 D:\testc>path c:\Dev-Cpp\bin;%path% 51 D:\testc>gcc p7a.c 52 D:\testc>a.exe 53 Error check at 1st: 54 GGGmag 55 GGYYYYY 56 Error check at 2nd: 57 YYY-- 58 GGYYYYY 59 Error check at 3rd: 60 GGGmag 61 YYY-- 62 HahaHeeError 63 Error check at 4-th: 64 GGGmag 65 YYY-- 66 GGYYYYY 67 Bye bye! 68 Hit RETURN key ... 69 70 D:\testc> 71 **********************/