//p7a.c -- @CopyLeft by tsaiwn@cs.nctu.edu.tw // 這是個小例子, 有助於你完成身分證號碼檢查器進階版 #define GGG 1 #define YYY 2 #define GGYY 4 #define ERR_HA 8 int errTable[ ]= {1, 2, 4, 8}; // 方便用 Loop 處理 char errMsg[ ][66]={"GGGmag", "YYY--", "GGYYYYY", "HahaHeeError"}; const int nMsg = (sizeof errTable / sizeof errTable[0]); int erraa( ), errbb( ), errcc( ); // 宣告 void pError(int); int main( ) { // 大多數 main program 都這樣開頭 int er; // for error code er = erraa( ); pError(er); er = errbb( ); pError(er); pError( errcc( ) ); pError( 7 ); // 7 = 1 + 2 + 4 == 哪三種 Error ? printf("Bye bye!\nHit RETURN key ..."); getchar( ); // 企圖讀取一個 char, 讓程式停著等 User 按 RETURN 鍵 return 0; // 告知作業系統(OS)表示我們這主程式正常結束 }// main( ///////////////////////////////////////// int erraa( ) { int ans=0; ans += GGG; // OK 因為 bit 沒重疊 ans = ans + GGYY; // 也可用 | bitwise OR return ans; } int errbb( ) { return YYY | GGYY; // bitwise OR } int errcc( ) { return ERR_HA | GGG | YYY; } void pError(int n){ // n 應該叫 errno 比較好 ! static int i, count = 0; printf(" Error check at %d", ++count); if(count == 1)printf("st:\n"); else if(count == 2)printf("nd:\n"); else if(count == 3)printf("rd:\n"); else printf("-th:\n"); for(i=0; i< nMsg; ++i) { if(n & errTable[i]) printf(" %s\n", errMsg[i]); /// }//for i } //////////////////////////////////////////////////////////// /****** D:\testc>path c:\Dev-Cpp\bin;%path% D:\testc>gcc p7a.c D:\testc>a.exe Error check at 1st: GGGmag GGYYYYY Error check at 2nd: YYY-- GGYYYYY Error check at 3rd: GGGmag YYY-- HahaHeeError Error check at 4-th: GGGmag YYY-- GGYYYYY Bye bye! Hit RETURN key ... D:\testc> **********************/