Microsoft PowerPoint - ch11.ppt

Similar documents
新版 明解C++入門編

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

Microsoft Word - 11.doc

c_cpp

Microsoft PowerPoint - Class5.pptx

C/C++语言 - C/C++数据

_汪_文前新ok[3.1].doc

FY.DOC

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

Microsoft PowerPoint - ch02_AEL0080.ppt

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

CH09

Microsoft PowerPoint - ch04_AEL0080.ppt

運算子多載 Operator Overloading

Microsoft PowerPoint - ch09_AEL0080.ppt

第3章.doc

untitled

untitled

!"# $% & $%%% ( ")*+,-./00-(11.-. $%! $ " # $ % & ( - ) +%23!"# $%%% %,.%,!" $%.! 1.% & /$ 3(,. ( /0% $%%% ( $%%% ( 3 5 /6%%%! ")*+,-./00-(11

!! "!! "! "!! "! "! "!!#$% & ()*+, -./!000$ 1-2$##0! 3

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

山东建筑大学学分制管理规定(试行)

Microsoft PowerPoint - 11_Templates.ppt

提问袁小兵:

Strings


<4D F736F F D B0EABB79A4E5B8D5C344BBBCB065AAA9>


康體藝術

公共圖書館利用教育方案規劃之研究

Microsoft Word - 01.DOC

Microsoft Word - 第三章第一節第二節.doc

untitled

EK-STM32F

nooog

目录


2.1 n ~á CPU q ˆ ~ µƒã î ˆ C++ à nù eô 1. (edit) 2. Í (preprocess) 3. (compile) 4. (link) 5. (load) 6. ˆ (execute) µl ô Ãs nù (editor) Í (preprocessor

Microsoft Word - CPE考生使用手冊 docx

3.1 num = 3 ch = 'C' 2

Microsoft PowerPoint - C-Ch12.ppt

投影片 1

Microsoft PowerPoint - 10 模板 Template.pptx

untitled

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

ebook15-C

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

ebook39-5

新・明解C言語入門編『索引』

Microsoft PowerPoint - ch03_AEL0080.ppt

ebook122-3

Microsoft PowerPoint - STU_C_Lang_CH01.ppt

Microsoft PowerPoint - ch12.ppt

W. Richard Stevens UNIX Sockets API echo Sockets TCP OOB IO C struct C/C++ UNIX fork() select(2)/poll(2)/epoll(4) IO IO CPU 100% libevent UNIX CPU IO

Chapter12 Derived Classes

Template

Microsoft PowerPoint - P833_Ch02.ppt

Microsoft PowerPoint - 02 C語言基本概述.ppt

Microsoft PowerPoint - 02 C語言基本概述.ppt

迅速在两个含有大量数据的文件中寻找相同的数据

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

第七讲 继承与多态

untitled

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1.

untitled

引言

第5章修改稿

Microsoft Word cppFinalSolution.doc

Microsoft Word - well_game.doc

全国计算机技术与软件专业技术资格(水平)考试

C 1

Simulator By SunLingxi 2003

bingdian001.com

Microsoft PowerPoint - string_kruse [兼容模式]

CC213

《C语言程序设计》教材习题参考答案

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e

Strings

2000 / / % 0.035% % % % 3 2 ETF creation / redemption TTT ETF 3 / -1 2

Spyder Anaconda Spyder Python Spyder Python Spyder Spyder Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Sp

,, 2

Microsoft Word - CIN-DLL.doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

Microsoft Word - C-pgm-ws2010.doc

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

ebook39-6

立 志 于 打 造 最 贴 近 考 生 实 际 的 辅 导 书 计 算 机 考 研 之 数 据 结 构 高 分 笔 记 率 辉 编 著 周 伟 张 浩 审 核 讨 论 群 :

chap07.key

摘 要 就 一 个 游 戏 而 言, 对 于 参 与 者, 需 要 研 究 不 同 的 策 略 去 达 到 胜 利, 而 对 于 游 戏 设 计 者, 则 需 要 研 究 这 个 游 戏 的 平 衡 性 与 记 分 规 则 的 合 理 性, 并 不 断 去 调 整 它 们 在 本 文 中, 我 们

第7章-并行计算.ppt

untitled

bingdian001.com

第1章

C++11概要 ライブラリ編

Microsoft Word - ACL chapter02-5ed.docx

穨2700使用手冊.doc

untitled

C++ 程式設計

Transcription:

11 前處理指令 前處理指令可以要求前處理器 (preprocessor) 在程式編譯之前, 先進行加入其它檔案的內容 文字取代以及選擇性編譯等工作 1/39 前處理指令 11.1 11.2 11.3 11.4 11.5 前處理器使用 #define 進行文字取代使用 #define 設定巨集指令條件式編譯其他與編譯器有關的前處理指令 2/39

3/39 前處理指令 #include 前處理指令不是 C++ 敘述, 不用分號 ; 做結尾 例如 : #include <iomanip> 與 #include 配合的檔案名稱有兩種語法 例如 : #include Commom.h" 附錄 C 中列出了 C++ 標準程式庫裏面常用的標檔 可以將檔案的詳細路徑寫在雙引號內 例如 : #include c:\c++test\commom.h" 使用 #define 進行文字取代 以 #define 開頭的前處理指令有四種功能 : 1. 宣告某一個代號的值 2. 產生符號常數 (symbolic constant) 3. 產生巨集 (macro) 4. 宣告某一個代號已經被設定 4/39

以 #define 開頭的前處理指令 #define 代號被取代文字 除了包括在字串 (string) 之內的文字以外, 所有吻合的文字, 都將被代號直接取代 例如 : #define and && #define or #define not! 5/39 範例程式 LogicTest.cpp // 前處理指令 #define and && #define or #define not! #include <iostream> using namespace std; 6/39

// ------ 主程式 --------------------------- int main() { float t1,t2; t1=0.5; t2=t1*0.2; cout << "((t1 > 0.8) and (t2 <0.9) or (t1>0.2))= " << ((t1 > 0.8) and (t2 <0.9) or (t1>0.2)) << endl; cout << " not(t1 > t2) = " << not(t1 > t2) << endl; return 0; 7/39 程式執行結果 ((t1> 0.8) and (t2 <0.9) or (t1>0.2))= 1 not(t1 > t2) = 0 8/39

宣告符號常數 (symbolic constant) 符號常數因為直接取代程式內的代號, 本身並不佔用記憶空間, 而每個常數在定義時即擁有自己的記憶空間 #define RAND_MAX 0x7FFFU #define PI 3.1415926 比較好的做法是使用常數 例如 : const double PI = 3.1415926; const double PI = 2.0*asin(1.0); 9/39 前處理指令內禁用的代號 代號 -- LINE -- -- FILE -- DATE -- -- -- TIME -- 意義行號檔名編譯日期編譯時間 10/39

以 #line 開頭的前處理指令 行號不會加在程式裹面, 程式的檔案名稱也沒有真的被更動, 這個指令的最大用途在於大型檔案的除錯 (debugging) #line 120 #line 120 "Test File.cpp" 11/39 範例程式 Reserved.cpp 使用保留代號 // Reserved.cpp #include <iostream> using namespace std; // ---- 主程式 ---------------------------- int main() { cout << " 行號 : " << --LINE-- << endl; cout << " 檔名 : " << --FILE-- << endl; cout << " 編譯日期 : " << --DATE-- << endl; cout << " 編譯時間 : " << --TIME-- << endl; 12/39

#line 120 "Test File.cpp" cout << " 執行 #line 120 \"Test File.cpp\" 之後 : " << endl; cout << " 行號 : " << --LINE-- << endl; cout << " 檔名 : " << --FILE-- << endl; return 0; 13/39 程式執行結果 行號 : 7 檔名 : Reserved.cpp 編譯日期 : Sep 13 2004 編譯時間 : 09:30:38 執行 #line 120 "Test File.cpp" 之後 : 行號 : 122 檔名 : Test File.cpp 14/39

使用 #define 設定巨集 (macro) 巨集指令的參數直接取代巨集本體部份的代號 例如 : #define MAX(x,y) ((x) > (y)? (x) : (y)) 它的功能相當於下列的 inline 樣版函數 : 15/39 template <class T> inline const T& MAX(const T& x, const T& y) {return x > y? x : y; 巨集本體部份的代號在執行時會被引數直接取代 如果設定了一個叫做 PRODUCT 的巨集 : #define PRODUCT(x,y) (x * y) 則 double F1 = 12.5, F2 = 8.38; cout << PRODUCT(F1+2, F2+6) << endl; 相當於執行 : double F1 = 12.5, F2 = 8.38; cout << 12.5 + 2 * 8.38 + 6 << endl; 16/39

PRODUCT 巨集和 inline 樣版函數 #define PRODUCT(x,y) ((x) * (y)) 它的功能相當於下列的 inline 樣版函數 : template <class T> inline const T& PRODUCT(const T& x, const T& y) {return x*y; 17/39 將巨集指令的參數名稱直接以字串的型式輸出 只要將巨集本體部份相對於參數的代號前加上 # 即可 例如 : #define ShowValue(x) \ { \ cerr << #x << " 的值是 : " << x << end \ 如果我們在程式中執行下列敘述 : double F1= 12.5; ShowValue(F1); 結果 : F1 的值是 : 12.5 18/39

19/39 合併巨集指令的參數 例如, 定義一個名叫 Merge 的巨集 : #define Merge(x, y) x##y 如果在程式中執行下列敘述 : double F1= 12.5; cout << " 執行 Merge(F,1) 的結果是 : " << Merge(F,1) << endl; 可以得到下列結果 : 執行 Merge(F,1) 的結果是 : 12.5 範例程式 MacroTest.cpp: 建立和使用 MAX PRODUCT Merge, 和 ShowValue 四個巨集指令 20/39 // MacroTest.cpp #include <iostream> using namespace std; // -- 巨集指令 --------------------------- #define MAX(x,y) ((x) > (y)? (x) : (y)) #define PRODUCT(x,y) ((x) * (y)) #define Merge(x, y) x##y #define ShowValue(x) \ { \ cerr << #x << " 的值是 : " << x << endl; \ cerr << " 按 Enter 鍵繼續." << endl; \ cin.get(); \

21/39 // ---- 主程式 ------------------------------- int main() { int a = 3, b = 9; double F1= 12.5, F2=8.38; cout << "MAX(a, b) = " << MAX(a, b) << endl; cout << "MAX(F1, F2) = " << MAX(F1, F2) << endl; cout << "PRODUCT(F1, F2) = " << PRODUCT(F1+2, F2+6) << endl; ShowValue(F1); cout << "\n 執行 Merge(F,2) 的結果是 :" << Merge(F,2) << endl; return 0; 程式執行結果 MAX(a, b) = 9 MAX(F1, F2) = 12.5 PRODUCT(F1, F2) = 208.51 F1 的值是 : 12.5 按 Enter 鍵繼續. 執行 Merge(F,2) 的結果是 : 8.38 22/39

條件式編譯 (conditional compilation) 可以有選擇性地執行某些前處理指令, 或有選擇性地只編譯原始程式碼的某些部份 常用的有下列數種 : #ifdef #ifndef #else #endif #if defined() #if!defined() #elif 23/39 使用 #define 設定代號 下列指令設定了代號 Mark: #define Mark 24/39

以前處理指令避免重複加入同一個標頭檔 (1) 25/39 例如 : 在標頭檔 Commom.h 內加入 : #ifndef Common_h #define Common_h // 標頭檔 Common.h 的主要內容 // 如果 Common_h 沒設定過才加入這個部份 #endif 在上式中, #ifndef Common_h 也可以寫成 #if!defined(common_h) 以前處理指令避免重複加入同一個標頭檔 (2) 標頭檔 Commom.h 還可以寫成下列格式 : #ifdef Common_h #else #define Common_h // 標頭檔 Common.h 的主要內容 // 如果 Common_h 沒設定過才加入這個部份 26/39 #endif

範例程式 Common.h 檔案 MacroTest.cpp 分成 Common.h 和 Macro2.cpp 兩個檔案 // Common.h #ifndef Common_h #define Common_h #include <iostream> using namespace std; 27/39 // -- 巨集指令 --------------------------- #define MAX(x,y) ((x) > (y)? (x) : (y)) #define PRODUCT(x,y) ((x) * (y)) #define Merge(x, y) x##y #define ShowValue(x) \ { \ cerr << #x << " 的值是 : " << x << endl; \ cerr << " 按 Enter 鍵繼續." << endl; \ cin.get(); \ #endif 28/39

範例程式 Macro2.cpp // MacroTest.cpp // Macro2.cpp #include "Common.h" // ---- 主程式 -------------------- int main() { int a = 3, b = 9; double F1= 12.5, F2=8.38; 29/39 cout << "MAX(a, b) = " << MAX(a, b)<< endl; cout << "MAX(F1, F2) = " << MAX(F1, F2) << endl; cout << "PRODUCT(F1, F2) = " << PRODUCT(F1+2, F2+6) << endl; ShowValue(F1); cout << "\n 執行 Merge(F,2) 的結果是 : " << Merge(F,2) << endl; return 0; 30/39

利用前處理指令插入除錯專用的敘述 藉由 #define 和 #undef 前處理指令來做為執行的開關 例如, 將巨集 Breakpoint 以下列的方式包裹起來 : #define Debug #if defined(debug) #define Breakpoint(x) { \ cerr << #x << " 的值是 : " << x << endl; \ cerr << " 按 Enter 鍵繼續." << endl; \ cin.get(); \ #else #define Breakpoint(x) #endif 31/39 利用前處理指令暫時忽略部份程式 藉由 #if 0 和 #endif 把要暫時忽略不編譯的區域包圍起來 如下列語法所示 : // ---- 其它敘述 ---- #if 0 // ---- 暫時不處理的區域 #endif // ---- 其它敘述 ---- 32/39

利用 assert( ) 協助除錯 標頭檔 <cassert> 提供了函數 assert() 可以使用 #define NDEBUG #undef NDEBUG 做為函數 assert() 的開關 assert(i<15); 一旦 I 大於或等於 15, 就會立即關閉程式, 並發出錯誤訊息 : Assertion failed: line 40 i < 15, file Test.cpp, 33/39 範例程式 Debug.cpp // Debug.cpp #define Debug #if defined(debug) #define Breakpoint(x) \ { \ cerr << #x << 的值是 : << x << endl; \ cerr << 按 Enter 鍵繼續. << endl; \ cin.get(); \ #else #define Breakpoint(x) #endif #undef NDEBUG 34/39

#include <iomanip> #include <iostream> #include <stdlib> #include <cassert> using namespace std; inline double Rand() // 定義 inline 函數 Rand() {return double(rand())/rand_max; 35/39 36/39 // ---- 主程式 ----------------------------- int main() { srand(int(time(0))); const int Size = 20; double Data[Size]; for (int i=0; i<size; i++) { Data[i]= 10.0*Rand()-5.0; assert(i<size); Breakpoint(Data[Size-1]); return 0;

程式執行結果 ( 因為程式會產生亂數, 每一次執行結果都不相同 ) Data[Size-1] 的值是 : 1.72658 按 Enter 鍵繼續 37/39 #pragma 前處理指令 有某些特殊的前處理指令可以被某些編譯器接受, 例如 #pragma warn -nak 38/39

#error 前處理指令 下列前處理指令 : #ifndef cplusplus #error Must use C++ for the type bcd. #endif 這個語法相當於下列指令 : #ifndef cplusplus cerr << Must use C++ for the type bcd. ; #endif 39/39