Microsoft PowerPoint - 05-func.ppt

Size: px
Start display at page:

Download "Microsoft PowerPoint - 05-func.ppt"

Transcription

1 函式 (Functions) 用途 定義語法 使用前必須先宣告 呼叫 傳回值的資料型態 參數列 引數的傳遞方式 陣列參數 預設的引數值 不定數目的參數 return 敘述 遞迴函式 行間函式 處理指令行的選項 函式指標 與其它語言寫成的函式連結 Function - 1 函式的用途 程式碼的再利用 (code reuse) 便利模組化設計的 implementation 便利程式的維護 便利程式的移植 Function - 2

2 函式的定義語法 函式傳回值的資料型態 函式名稱 參數列 type function_name ( parameter list ) // function body // 包含變數宣告與指令敘述 Function - 3 範例 : // prog.cpp #include <iostream> using namespace std; 主函式 : 程式執行時的進入點 int max (int a, int b) return (a >= b)? a : b; int main () cout << max(3, 4) << endl; return 0; 函式的定義 呼叫函式 max Function - 4

3 函式使用前必須先宣告 函式在被呼叫之前必須先定義或宣告 譬如 : 在前一頁的範例中, 函式 max 的定義寫在呼叫之前 若函式 max 的定義寫在 main 函式之後的話, 則我們必須在呼叫之前加以宣告 宣告方式如下 : int max (int a, int b); int main () int max (int a, int b) 函式宣告 (declaration) 或稱函式原型 (prototype) Function - 5 外部函式 (external function) 是指定義在其它檔案之中的函式 外部函式必須先宣告才能呼叫 其宣告方式如下 : extern type function_name ( parameter list ); 比方說, 在之前的例子中, 如果函式 max 是存在 max.cpp, 則主程式檔 prog.cpp 必須改寫成 : // prog.cpp #include <iostream> using namespace std; extern int max (int a, int b); int main () // 此處同前, 故省略 而且以下列的方式來編譯 : CC prog.cpp max.cpp -o prog Function - 6

4 外部函式通常是宣告在.h 的標頭檔之中 比方說, 我們可以為 max.cpp 寫一個對應的 max.h 檔, 其中包含以下的宣告 : extern int max (int a, int b); 則主程式檔 prog.cpp 可以改寫成 : // prog.cpp #include <iostream> #include max.h using namespace std; int main () // 此處同前, 故省略 然後用前一頁的方式來編譯 Function - 7 從前一頁的說明, 你現在應該明白為什麼使用系統函式時, 必須在程式中加入適當的系統標頭檔了吧! 譬如, 如果想使用計算字串長度的系統函式 strlen, 你就必須用以下的方法加入系統標頭檔 string.h: #include <string.h> 理由是 : 系統函式屬於外部函式, 系統標頭檔包含了它們的宣告 如果你沒有加入適當的系統標頭檔, 就呼叫某系統函式, 編譯程式會認為該函式並未事先宣告, 因而發出編譯錯誤的訊息 Function - 8

5 函式的呼叫 函式呼叫採取以下的格式 : function_name ( argument list ) 對於函式呼叫, 編譯程式會檢查下列事項 : 函式名稱是否已經定義或宣告 引數的個數是否等於參數的個數 max (3) // error: 引數只有一個 引數的資料型態是否等於或可轉換至參數的資料型態 max (3.0, 4.0) // ok: double 引數自動轉換成 int 參數 max ( one, two ) // error: char * 引數無法轉換成 int 參數 Function - 9 int add ( int x, int y ) int s; s = x + y; return s; int add2 ( int x, int y ) int s; s = add ( x+1, y+1 ); C x*=2; y*=2; s += add ( x, y ); D return s; main () int x = 3, y = 4, s; s = add2 ( x, y ); B cout << s << endl; return 0; 左邊程式的函數呼叫次序如下 : 作業系統 (OS) main () add2 () add () add () 由上圖可知函數返回的次序剛好與函數呼叫的次序相反, 似乎與堆疊 LIFO 的特性相符 事實上, 堆疊也的確用於函數呼叫中 A B C D Function - 10

6 每一個程式在執行之前, 系統會配置一個堆疊供其使用 此堆疊稱之為執行堆疊 ( run-time stack ) 其作用如下所述: 呼叫函數時 : 1. 將返回位址 (return address) push 至執行堆疊中 2. 將引數值依序 push 至執行堆疊中 3. 跳到被呼叫的函數 進入被呼叫函數時 : 1. 在執行堆疊中配置空間給局部變數 ( local variables) 2. 開始執行此函數程式碼 Function - 11 離開被呼叫函數時 : 1. 將返回位址之上所佔用的執行堆疊空間 pop 掉 2. 讀出執行堆疊最上面所存的返回位址, 然後將其 pop 掉 3. 將函數傳回值 push 置於執行堆疊中 4. 跳至返回位址繼續呼叫函數的執行 Function - 12

7 函式傳回值的資料型態 函式必須指定傳回值的資料型態 以下是允許的型態 : 基本型態, 如 int 或 double; 複合型態, 如 int& 或 double *; 自定型態, 如 enum 或 class; void, 代表函式無傳回值 範例 : class Date /* definition */ ; bool look_up (int *, int *); double calc (double); int count ( const string &, char ); Date& calendar ( const char * ); void sum (vector<int>&, int); Function - 13 函式傳回值的資料型態不可以是陣列或函式 範例 : int[10] foo_bar (); // error: 傳回值不可為陣列型態 int* foo_bar (); // ok 函式必須指定傳回值的資料型態 範例 : foo_bar (); int foo_bar (); // error: 未指定傳回值的型態 // ok Function - 14

8 函式的參數列 函式可以沒有參數, 如 : int foo_bar (); int foo_bar (void); 函式的參數必須個別地宣告, 如 : int foo_bar (int p1, int p2); // ok int foo_bar (int p1, p2); // error 函式的參數名稱必須不同, 如 : int foo_bar (int p1, int p2); // ok int foo_bar (int p1, int p1); // error Function - 15 函式宣告中的參數名稱可以省略, 如 : int foo_bar (int p1, int p2); int foo_bar (int, int); // ok // ok 函式宣告和定義中的參數名稱可以不相同, 如 : int foo_bar (int p1, int p2); int foo_bar (int x, int y) // declaration // definition 不過這樣做容易造成混淆, 應該僅量避免 函式宣告和定義中的參數資料型態必須一致, 否則會造成編譯上的錯誤 Function - 16

9 若參數不應該在函式中修改其值, 最好將其宣告為 const, 如 : int max (const int x, const int y) return (x >= y)? x : y; 宣告為 const 的參數, 不可以在函式中修改其值 ( 即出現在等號的左邊 ), 如 : void foo (int a); int bar (const int x) int y = x; x = x; x = y + 3; foo(x); foo(const_cast<int>(x)); // ok // error // error // error // ok Function - 17 引數的傳遞方式 如前所述, 呼叫函式時, 引數會透過執行堆疊傳遞至函式的對應參數 C++ 提供以下的兩種傳遞方式 : call by value( 數值傳遞 ) 傳遞引數的值至對應的參數 由於引數和參數是獨立的個體, 所以改變參數的值並不會影響到引數原有的值 call by reference ( 參照傳遞 ) 傳遞引數的 lvalue 至對應的參數 由於引數和參數是指涉同樣的記憶體區域, 所以改變參數的值等同於直接改變引數的值 Function - 18

10 Call by value( 數值傳遞 ) main () int a = 3, b = 4; cout << max(a,b) << endl; int max (int x, int y) return (x >= y)? x : y; main a 3 傳遞 b 4 傳遞 3 x max 4 y Function - 19 main () int a = 3, b = 4;... swap(a, b);... void swap (int x, int y) int temp; temp = x; x = y; y = temp; main a 3 傳遞 b 4 傳遞 3 x swap 4 y swap 只是交換 x 和 y 的值, 而沒有交換引數 a 和 b 的值 Function - 20

11 main () int a = 3, b = 4;... swap(&a, &b);... void swap (int *x, int *y) int temp; temp = *x; *x = *y; *y = temp; main a b x y swap swap 利用指標參數 x 和 y 來間接地交換引數 a 和 b 的值 Function - 21 當引數是一個佔據大量記憶體的結構或 class 時, 使用數值傳遞直接拷貝這些記憶體的內容到函式並不是一個有效率的方式 譬如在以下的例子中, 如果 Matrix 物件佔據大量記憶體的話, 則函式 operator+ 的參數定義方式 會使得引數傳遞花費掉過多的時間 : class Matrix /* 矩陣類別的定義 */; Matrix operator+ (Matrix m1, Matrix m2) Matrix result; // 計算 m1 + m2 並把結果存入 result return result; Matrix a, b, c; c = a + b; Function - 22

12 前一頁所述的問題可以用指標參數的方法來解決, 即 Matrix operator+ (Matrix *m1, Matrix *m2) Matrix result; // 計算 *m1 + *m2 並把結果存入 result return result; 不過這樣一來, 我們就不能寫下列的算式 : Matrix a, b, c, d; d = &a + &b + &c; // error: should be d = &(&a + &b) + &c 因為上式可視為 : d = operator+(operator+(&a, &b), &c); 型態為 Matrix, 而非所需的 Matrix * Function - 23 參照資料型態 參照 (reference) 又稱為別名 (alias) 是用來提供物件的另外一個名稱 就如指標一般, 我們可以利用參照來間接地存取物件的值, 不同的是 : 參照不須要如指標般的繁瑣指涉方式 參照在 C++ 中, 主要是用於提高引數傳遞的效率 參照宣告的基本語法如下 : type &name = var_name; 其中, var_name 必須是之前已經宣告為 type 型態的變數名稱 Function - 24

13 範例 : int ival; int &refval = ival; // refval is an alias of ival ival = 1024; cout << refval << endl; // print 1024 refval += 100; cout << ival << endl; // print 1124 int *ip = &refval; // ip points to refval (*ip)++; ival 或 refval cout << ival << endl; // print 1125 Function - 25 參照必須設定初值, 如 : int ival; int &refval = ival; int &refval; // ok // error 參照一旦設定之後, 不可以再重新設定參照的對象, 因此以下的指令 : refval = kval; 並不是把 refval 重新設定去參照 kval, 而是把 kval 的值存入 refval 所指涉的變數 參照不是指標, 如 : int ival; int &refval = &ival; // error: refval 是 int, 非 int * int *ip; int *&refptrval = ip; // ok Function - 26

14 一般參照不可以用來指涉不同型態的物件或常數, 如 : double dval = 3.14; int &r3 = 1024; int &r4 = dval; // error // error 不同型態的物件或常數必須使用常數參照來指涉, 如 : double dval = 3.14; const int &r1 = dval; const int &r2 = 1024; const double &dr = dval + 1.0; // ok // ok // ok 常數參照事實上是指涉一個暫存變數, 譬如 : const int &r2 = 1024; 會被編譯程式轉化成 : int tmp = 1024 const int &r2 = tmp; 不能更改常數參照的值 Function - 27 範例 : double dval = 3.14; const int &r1 = dval; const double &dr = dval; cout << r1 << endl; // print 3 dval = 5.0; cout << r1 << endl; // print 3 r1 = 5; // error cout << r1 + 2 << endl; // print 5 dr = 5; // error: 不能更改常數參照 Function - 28

15 Call by Reference 參照在 C++ 中最主要是用於 call by reference 你可以用以下的格式來宣告參照參數 : type &para_name 假定函式 foo 的宣告如下 : int foo (int &x); 則函式呼叫 foo(a) 的參數傳遞方式相當於在 foo 中宣告 int &x = a; 使得參數 x 成為引數 a 的別名 在 foo 中對 x 的修改, 等同於對 a 的修改 Function - 29 main () int a = 3, b = 4;... swap(a, b);... void swap (int &x, int &y) int temp; temp = x; x = y; y = temp; main a x swap b y 在 swap 中交換 x 和 y 的值, 等同於交換引數 a 和 b 的值 Function - 30

16 如果之前的 Matrix 加法改寫如下 : Matrix operator+ (const Matrix &m1, const Matrix &m2) Matrix result; // 計算 m1 + m2 並把結果存入 result return result; 我們就可以寫下列的算式 : Matrix a, b, c, d; d = a + b + c; 因為上式可視為 : d = operator+(operator+(a, b), c); Function - 31 對於不應該在函式中修改的參照參數, 最好用 const 將其宣告成常數參照參數 void foo (const int &x) x = 3; // error 參照參數的對應引數不可以是常數, 常數參照參數則可 void foo (int &x); void bar (const int &x); int a; foo(a); // ok foo(3); // error bar(a); // ok bar(3); // ok Function - 32

17 陣列參數 在 C/C++ 中, 陣列不是把內容採用數值傳遞的方式整個傳入函式, 而是用指標的方式把陣列第一個元素的位址傳入函式 比方說 : int a[10]; foo(a); 傳入函式 foo 的是 a[0] 的位址, 而不是 a 的 10 個元素 以下三種函式宣告的方式具有相同的意義 : void foo (int *x); void foo (int x[]); void foo (int x[10]); Function - 33 由於陣列的傳遞方式, 在函式中更改陣列參數的元素, 等同於更改陣列引數的元素 譬如 : int main () int a[] = 0, 1; foo(a); cout << a[0] = << a[0] << endl; cout << a[1] = << a[1] << endl; void foo (int x[]) x[0] = 1; x[1] = 0; 所得的輸出結果為 : a[0] = 1 a[0] = 0 Function - 34

18 由於只傳入陣列第一個元素的位址, 函式因此無法得知陣列之中有多少個元素 然而你可以用以下的幾種方法來解決這個問題 加上一個額外的參數來傳遞陣列的元素個數 int total (int x[], int size) int sum = 0; for (int k = 0; k < size; k++) sum += x[k]; return sum; int a[10] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; cout << total(a, 10) << endl; // print 45 Function - 35 在陣列的最後一元素中, 存放一個表示結束的特殊值 比方說,C 字串是一個以 null 字元 (0) 結尾的字元陣列 int string_length (char *str) if (!str) return 0; int k = 0, length = 0; while (str[k++]) length++; return length; char s[] = Hello, world! ; cout << string_length(s) << endl; // print 13 Function - 36

19 把參數宣告成陣列參照 int total (int (&x)[10]) // x 是一個 10 元素陣列的參照 int sum = 0; for (int k = 0; k < 10; k++) sum += x[k]; return sum; int a[10] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; cout << total(a) << endl; // print 45 int b[5] = 1, 2, 3, 4, 5; int s = total(b); // error Function - 37 預設的引數值 我們可以在函式宣告中指定預設的引數值 (default argument), 來簡化函式的呼叫方式 如 : char *screeninit (int height = 24, int width = 80, char bgchar = ); 其中的三個參數都指定了預設的值 以下都是合法的方式來呼叫 screeninit: screeninit(); // 等同於 screeninit(24, 80, ) screeninit(66); // 等同於 screeninit(66, 80, ) screeninit(66, 256); // 等同於 screeninit(66, 256, ) screeninit(66, 256, # ); Function - 38

20 呼叫時所缺的引數值是由後補齊 screeninit(? ); // 等同於 screeninit(?, 80, ) screeninit(,,? ); // error 函式的參數可以全部或部份給予預設的引數值 然而, 未指定預設引數值之參數的前面, 不可再宣告具有預設引數值的參數 比方說, 以下是錯誤的宣告方式 : char *screeninit (int height = 24, int width, char bgchar = ); 因為 width 之前的 height 宣告成具有預設的引數值 在設計具有預設引數值的函式時, 應該把變動性大的參數擺在前面, 變動性小的參數擺在後面 以 screeninit 為例, 設計者認為 height 的變動性最大 width 次之 bgchar 最小 Function - 39 預設的引數值不可以重複寫在函式的宣告與定義中 // ff.h int ff (int k = 0); // ff.cpp #include ff.h int ff (int k = 0) // error 一般的習慣是寫在宣告, 並擺在.h 標頭檔之中 如果擺在定義的話, 就只能在同檔案中使用預設引數值的呼叫方式 Function - 40

21 我們可以為現有的函式加上預設的引數值 比方說 :stdlib.h 中宣告了以下的函式 : int chmod (char *filename, int protmode); 它是用來設定檔案的讀寫模式 如果在某個應用中, 大部份檔案的讀寫模式是唯讀的話, 則我們可以重新宣告如下 : #include <stdlib.h> int chmod (char *filename, int protmode = 0444); 然後用簡化的呼叫方式 : chmod (filename); 把檔案的讀寫模式改成唯讀 Function - 41 已有預設引數值的參數, 不可再宣告預設引數值, 即使值相同也不允許 比方說, 函式 ff 的宣告如下 : 則 // ff.h void ff (int a, int b, int c = 0); #include ff.h void ff (int a, int b, int c = 0); void ff (int a, int b, int c = 1); 但可加入新的預設引數值, 譬如 : // error // error #include ff.h void ff (int a, int b = 0, int c); void ff (int a = 0, int b = 0, int c); void ff (int a = 0, int b, int c); // ok // ok // error Function - 42

22 引數的預設值除了常數以外, 也可以是一般算式或呼叫其它的函式 int adefault (); int bdefault (int); int cdefault (double = 0); int glob; ff ( int a = adefault (), int b = bdefault (glob), int c = cdefault (), int d = 2 * glob ); 預設值算式是在函式呼叫時才計算, 然後再把結果傳入函式之中 Function - 43 不定數目的參數 C/C++ 允許函式具有不定數目的參數 這類函式的宣告格式如下 : type function_name (para_list, ); 連續三個句點 para_list 代表固定數目的參數, 之後的 代表不定數目的參數 比方說, 由於標準函式 printf 除了第一個參數以外, 之後還可以再接不定數目的參數, 因此它的宣告如下 : int printf(const char *format, ); Function - 44

23 如何決定參數的個數? 在這種具有不定參數個數的函式中, 我們常用以下兩種技巧來決定有多少參數傳進來 : 把參數個數的資訊存在某個引數傳進來 比方說,printf 的第一個參數存著格式設定字串, 其中包含的格式設定數目即是之後所跟參數的數目 printf( The sum of %s is %d\n, title, total); 在最後一個引數中存放特殊值, 用來指示參數的結束 如 : average(1, 2, 3, 4, 5, -1); // -1 作為最後一個引數 Function - 45 如何取得參數? va_list typedef for pointer to list of arguments defined in STDIO.H defined in stdarg.h void va_start( va_list arg_ptr, prev_param ); va_start sets arg_ptr to the first optional argument in the list of arguments passed to the function. The argument arg_ptr must have va_list type. The argument prev_param is the name of the required parameter immediately preceding the first optional argument in the argument list. If prev_param is declared with the register storage class, the macro s behavior is undefined. va_start must be used before va_arg is used for the first time. type va_arg( va_list arg_ptr, type ); va_arg retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve arguments from the list. void va_end( va_list arg_ptr ); After all arguments have been retrieved, va_end resets the pointer to NULL. Function - 46

24 #include <stdarg.h> int average( int first,... ) average(1, 2, 3, 4, 5, -1); // Returns the average of a variable list of integers. int count = 0, sum = 0, i = first; va_list ap; va_start(ap, first); // Initialize variable arguments. while( i!= -1 ) sum += i; count++; i = va_arg(ap, int); va_end(ap); // Reset variable arguments. return( count? (sum / count) : 0 ); Function - 47 #include <stdarg.h> /* minprintf: minimal printf with variable argument list */ void minprintf(char *fmt,...) var_list ap; // points to each unnamed arg in turn char *p, *sval; int ival; float fval; va_start(ap, fmt); for (p = fmt; *p; p++) if (*p!= '%') putchar(*p); continue; // make ap point to 1st unnamed arg. The sum of %s is %d Function - 48

25 switch (*++p) case 'd': // %d ival = va_arg(ap, int); printf("%d", ival); break; case 'f': // %f fval = va_arg(ap, float); printf("%f", fval) break; case 's': // %s for (sval = va_arg(ap, char *); *sval; sval++) putchar (*sval); break; default: putchar(*p); break; va_end(ap); // clean up when done Function - 49 return 敘述 return 敘述是用來終止函式的執行, 然後返回到呼叫函式繼續往下執行 它有下列兩種形式 : return; return expression; 第一種形式用於傳回值為 void 的函式, 第二種形式則用於傳回值為其它型態的函式, 此處 expression 計算結果的型態必須能夠轉換成函式傳回值的型態 譬如 : char *foo () return 3; // error: 無法把 int 轉換成 char* Function - 50

26 範例 : void d_copy (double *src, double *dest, int sz) // copy src array into dest // it is assumed that arrays are same size if (!src!dest) return; if (src == dest) return; if (sz == 0) return; for (int ix = 0; ix < sz; ix++) dest[ix] = src[ix]; // no explicit return necessary Function - 51 函式是以傳值的方式把函式值傳回呼叫函式 譬如 : Matrix grow (Matrix &p) Matrix m; // 把計算結果存入 m return m; 執行完成之後,grow() 函式把存放在變數 m 的計算結果拷貝到呼叫函式 如果傳回值是一個龐大的結構, 就會使函式變得沒有效率 Function - 52

27 函式可以用傳回參照的方式把函式值傳回呼叫函式 譬如 : Matrix &grow (Matrix &p) Matrix *m; // 動態配置 m 以及把計算結果存入 *m return *m; 執行完成之後,grow() 函式把計算結果存放在指標 m 所指的記憶體, 然後以參照的方式把 m 傳回到呼叫函式 當傳回值是一個龐大的結構時, 傳回參照是最有效率的一種方法 Function - 53 不要傳回函式內部物件的參照 譬如 : Matrix &grow (Matrix &p) Matrix m; // 把計算結果存入 m return m; Matrix x = grow(y); // run-time error grow() 執行完成後, 以參照的方式把 m 傳回到呼叫函式 然而, 離開了 grow() 之後,m 即不存在, 因而造成執行上的錯誤 Function - 54

28 若要避免意外地更動函式的傳回值, 我們可以把參照傳回值宣告成 const 譬如: int &get_val(int a[], int ix) return a[ix]; int v[5] = 1, 2, 3, 4, 5; get_val(v, 0)++; // v[0] 變成 2 如果把 get_val 的宣告改成 : const int &get_val(int a[], int ix) 就能避免上述的問題 Function - 55 遞迴函式 以直接或間接的方式呼叫自己的函式稱之為遞迴函式 (recursive function) 譬如以下是一個用遞迴方式來計算 n 階乘 : int factorial (int n) if (n < = 0) // termination condition return 1; return n * factorial(n-1); Function - 56

29 行間函式 只要在函式的宣告之前加上關鍵字 inline 就可以得到行間函式的宣告, 亦即 : inline type function_name (parameter list) 當函式宣告成行間函式時, 編譯程式會儘量用巨集展開的方式來處理函式的呼叫, 而不是用正常的函式呼叫方式 這樣做的目的是為了避免正常函式呼叫所需的額外時間 inline 宣告只是對編譯程式的一種建議, 編譯程式會審視函式的形式, 然後採用適當的處理方式 Function - 57 舉例來說, 假定行間函式 max 的定義如下 : inline int max (int x, int y) return (x > y)? x : y; 編譯程式會把下面的指令 : int m = max (i, j); 用巨集展開的方式轉換成 : int m = (i > j)? i : j; 因此避免了函式呼叫所需的額外時間 Function - 58

30 雖然使用前一頁的 max 行間函式與使用下面的巨集定義 : #define MAX(x, y) (((x) > (y))? (x) : (y)) 會得到相同的執行結果, 但是 C++ 的行間函式通常比 C 前置處理器的巨集來得有效率, 譬如 : int m = max (i+j, i-j); int m = MAX (i+j, i-j); t1 = i + j; t2 = i - j; m = (t1 > t2)? t1 : t2; m = (((i+j) > (i-j))? (i+j) : (i-j)); 計算兩次 Function - 59 呼叫某一個行間函式時, 該行間函式必須已經定義在同檔案之中 ( 不僅僅是宣告而已 ), 編譯程式才能夠引用其內容來進行巨集展開 基於這個原因, 當多個程式檔須要共用某一個行間函式時, 該行間函式的定義應該寫在.h 標頭檔之中, 然後在這些程式檔中加入此標頭檔 譬如 : // file: foo.h inline int max (int x, int y) return (x > y)? x : y; // file: bar.cpp #include foo.h int m = max(i, j); Function - 60

31 哪些行間函式不會巨集展開? 大部份的編譯程式對下列三種行間函式通常不會採取巨集展開的處理方式 : 行間函式中包含迴圈, 而且迴圈的執行次數不是常數值 在行間函式中呼叫自己的遞迴函式 行間函式包含過多的指令行 存放於函式指標的行間函式 Function - 61 處理指令行的選項 在文字終端機模式下執行應用程式指令時 ( 如在 UNIX 或 DOS 之下 ), 我們通常可以在指令名稱之後加上一些指令選項 (option), 如 : prog -d -o ofile data 指令選項 作業系統會把這些指令選項傳入程式 問題是 : 程式要如何接收和處理這些指令選項呢? Function - 62

32 若要接收指令選項, 我們必須把原本沒有參數的主函式宣告 : 改成 : int main () int main (int argc, char *argv[]) 當程式開始執行時,argc 會存放指令行上的字 (word) 數, argv 是一個字串陣列, 依序地存放指令行上的字 此處的 字 是指空白( 空白字元 TAB 字元 與一些特殊字元 ) 所隔開的字串, 譬如 : prog -d -o ofile data quoted option 總共有 6 個字 Function - 63 以下面的指令行為例 : prog -d -o ofile data quoted option argc 的值是 6,argv 的內容則如下 : argv[0] argv[1] argv[2] argv[3] argv[4] argv[5] p r o g \0 - d \0 - o \0 o f i l e \0 d a t a \0 q u o t e d o p t i o n \0 值得注意的是 :argv[0] 中的字串是指令的名稱 Function - 64

33 #define MIN_ARG 3 /* minimal number of options */ int main (int argc, char *argv[]) if (argc < MIN_ARG) /* print usage message and exit*/ for (int j = 1; j < argc; j++) if (argv[j][0]!= - ) // handle options without prefix - else switch (argv[j][1]) case h : // handle -h option break; case a : // handle -a option break; default: // handle unknown options break; // begin processing... Function - 65 函式指標 函式指標是一種存放函式位址的指標 它的宣告格式如下 : type (*func_ptr) (parameter list); 比方說 : void f (string s) /* */ void (*fp) (string); fp = &f; *fp( error ); // pointer to function // fp points to function f // call f through fp 上兩行也可簡化成 : fp = f; fp( error ); Function - 66

34 函式指標只能存放形態相同的函式, 換句話說, 兩者的宣告必須滿足下面的條件 : 相同的傳回值資料型態 相同的參數個數與資料型態 比方說 : void (*fp) (char *s); void f1 (char *s); void f2 (int n); int f3 (char *s); void f4 (char *s, int n); fp = f1; fp = f2; fp = f3; fp = f4; // ok // error // error // error Function - 67 範例 : typedef int (*CFT) (const char *, const char *); int smallest (char *sa[], int sz, CFT cmp) // find the place of the smallest string if (sz == 0) return -1; char *min = sa[0]; int ix = 0; for (int j = 1; j < sz; j++) if (cmp(min, sa[j]) > 0) min = sa[j]; ix = j; return ix; Function - 68

35 extern int strcmp(const char *s, const char *t) int lencmp (const char *s, const char *t) int slen = strlen(s), tlen = strlen(t); if (slen > tlen) return 1; else if (slen == tlen) return 0; else return -1; smallest(strarray, n, strcmp ); smallest(strarray, n, lencmp ); // 找出最 小 的字串 // 找出最 短 的字串 Function - 69 函式指標陣列 函式指標陣列的宣告格式如下 : type (*fptrarray [size]) (parameter list); 或利用函式指標型態來宣告 : typedef type (*FP) (parameter list); FP fptrarray [size]; 比方說, int (*testcases [10]) (); 或可寫為 : typedef int (*FPV) (); FPV testcases [10]; Function - 70

C++ 程式設計

C++ 程式設計 C C 料, 數, - 列 串 理 列 main 數串列 什 pointer) 數, 數, 數 數 省 不 不, 數 (1) 數, 不 數 * 料 * 數 int *int_ptr; char *ch_ptr; float *float_ptr; double *double_ptr; 數 (2) int i=3; int *ptr; ptr=&i; 1000 1012 ptr 數, 數 1004

More information

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

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc 2 5 8 11 0 13 1. 13 2. 15 3. 18 1 23 1. 23 2. 26 3. 28 2 36 1. 36 2. 39 3. 42 4. 44 5. 49 6. 51 3 57 1. 57 2. 60 3. 64 4. 66 5. 70 6. 75 7. 83 8. 85 9. 88 10. 98 11. 103 12. 108 13. 112 4 115 1. 115 2.

More information

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc References (Section 5.2) Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 15-16, 2010 H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 0 / 22 Fun Time (1) What happens in memory? 1 i n t i ; 2

More information

新版 明解C++入門編

新版 明解C++入門編 511!... 43, 85!=... 42 "... 118 " "... 337 " "... 8, 290 #... 71 #... 413 #define... 128, 236, 413 #endif... 412 #ifndef... 412 #if... 412 #include... 6, 337 #undef... 413 %... 23, 27 %=... 97 &... 243,

More information

FY.DOC

FY.DOC 高 职 高 专 21 世 纪 规 划 教 材 C++ 程 序 设 计 邓 振 杰 主 编 贾 振 华 孟 庆 敏 副 主 编 人 民 邮 电 出 版 社 内 容 提 要 本 书 系 统 地 介 绍 C++ 语 言 的 基 本 概 念 基 本 语 法 和 编 程 方 法, 深 入 浅 出 地 讲 述 C++ 语 言 面 向 对 象 的 重 要 特 征 : 类 和 对 象 抽 象 封 装 继 承 等 主

More information

C 1

C 1 C homepage: xpzhangme 2018 5 30 C 1 C min(x, y) double C // min c # include # include double min ( double x, double y); int main ( int argc, char * argv []) { double x, y; if( argc!=

More information

第1章

第1章 第 8 章 函式 1 本章提要 8.1 前言 8.2 如何定義函式 8.3 函式的呼叫和返回 8.4 傳遞陣列 8.5 方法多載 8.6 遞迴 8.7 綜合練習 8.8 後記 2 8.1 前言 每一種高階程式語言都有提供函式 (Function)( 或稱函數 ) 的功能, 以便將經常使用到的程式功能包裝成函式的形式, 如此一來便能反覆地呼叫該函式來完成某件特定工作在高階程式語言中, 副程式 (Subroutine)

More information

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

新・明解C言語入門編『索引』 !... 75!=... 48 "... 234 " "... 9, 84, 240 #define... 118, 213 #include... 148 %... 23 %... 23, 24 %%... 23 %d... 4 %f... 29 %ld... 177 %lf... 31 %lu... 177 %o... 196 %p... 262 %s... 242, 244 %u... 177

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 49 [P.51] C/C++ [P.52] [P.53] [P.55] (int) [P.57] (float/double) [P.58] printf scanf [P.59] [P.61] ( / ) [P.62] (char) [P.65] : +-*/% [P.67] : = [P.68] : ,

More information

c_cpp

c_cpp C C++ C C++ C++ (object oriented) C C++.cpp C C++ C C++ : for (int i=0;i

More information

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

C/C++语言 - C/C++数据 C/C++ C/C++ Table of contents 1. 2. 3. 4. char 5. 1 C = 5 (F 32). 9 F C 2 1 // fal2cel. c: Convert Fah temperature to Cel temperature 2 # include < stdio.h> 3 int main ( void ) 4 { 5 float fah, cel ;

More information

untitled

untitled A, 3+A printf( ABCDEF ) 3+ printf( ABCDEF ) 2.1 C++ main main main) * ( ) ( ) [ ].* ->* ()[] [][] ** *& char (f)(int); ( ) (f) (f) f (int) f int char f char f(int) (f) char (*f)(int); (*f) (int) (

More information

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

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc 2 5 8 11 0 1. 13 2. 15 3. 18 1 1. 22 2. 25 3. 27 2 1. 35 2. 38 3. 41 4. 43 5. 48 6. 50 3 1. 56 2. 59 3. 63 4. 65 5. 69 13 22 35 56 6. 74 7. 82 8. 84 9. 87 10. 97 11. 102 12. 107 13. 111 4 114 1. 114 2.

More information

Microsoft PowerPoint - C-Ch10.ppt

Microsoft PowerPoint - C-Ch10.ppt 了解陣列元素的位址 陣列 指標的應用 10-1 陣列與指標的關係 可以使用位址運算子 (&) 來查詢陣列中各個元素的位址 &test[0] 這行表示陣列最前面元素的位址 &test[1] 這行表示陣列第二個元素的位址 關於陣列名稱的機制 陣列名稱可以表示陣列最前面元素的位址 #include int main(void) int test[5] = 80,60,55,22,75;

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 177 [P179] (1) - [P181] [P182] (2) - for [P183] (3) - switch [P184] [P187] [P189] [P194] 178 [ ]; : : int var; : int var[3]; var 2293620 var[0] var[1] 2293620

More information

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

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023) ( CIP) /. :, 2005. 2 ( ) ISBN 7-5624-3339-9.......... TP311. 1 CIP ( 2005) 011794 : : : : * : : 174 ( A ) :400030 : ( 023) 65102378 65105781 : ( 023) 65103686 65105565 : http: / /www. cqup. com. cn : fxk@cqup.

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 9 [P.11] : Dev C++ [P.12] : http://c.feis.tw [P.13] [P.14] [P.15] [P.17] [P.23] Dev C++ [P.24] [P.27] [P.34] C / C++ [P.35] 10 C / C++ C C++ C C++ C++ C ( ) C++

More information

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

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 2013 18 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp, Compilation Error cin scanf Time Limit Exceeded 1: A 5 B 5 C 5 D 5 E 5 F 5 1 2013 C 1 # include 2 int main ( void ) 3 { 4 int cases, a, b,

More information

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

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 201 201 21 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 1 B 1 C 5 D RPG 10 E 10 F 1 G II 1 1 201 201 C 1 # include 2 int main ( void

More information

Microsoft PowerPoint - C-Ch08.ppt

Microsoft PowerPoint - C-Ch08.ppt 函數 8-1 函數 函數 (function) 可以整合某些特定的處理 整合好的處理可以隨時呼叫使用 C 語言的程式本身也是一個函數, 也就是 main() 函數 使用函數可簡化程式 提款的處理 1. 將提款卡插入自動提款機當中 2. 輸入個人密碼 3. 指定提款金額 4. 領取款項 5. 確認款項與提款卡 提款處理 8-2 函數的定義與呼叫 定義函數的語法 : 傳回值的型態函數名稱 ( 引數列表

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 函數樣板 (Function Template) 與 類別樣板 (Class Template) 講師 : 洪安 1 資料結構與 C++ 程式設計進階班 為何需要通用函數? (1/2) int abs(int x) { return (x>0)?x:-x; 取名困難不好記 float fabs(float x) { return (x>0)?x:-x; complex cabs(complex x)

More information

C/C++ 语言 - 循环

C/C++ 语言 - 循环 C/C++ Table of contents 7. 1. 2. while 3. 4. 5. for 6. 8. (do while) 9. 10. (nested loop) 11. 12. 13. 1 // summing.c: # include int main ( void ) { long num ; long sum = 0L; int status ; printf

More information

untitled

untitled 3 C++ 3.1 3.2 3.3 3.4 new delete 3.5 this 3.6 3.7 3.1 3.1 class struct union struct union C class C++ C++ 3.1 3.1 #include struct STRING { typedef char *CHARPTR; // CHARPTR s; // int strlen(

More information

untitled

untitled 1 Outline 料 類 說 Tang, Shih-Hsuan 2006/07/26 ~ 2006/09/02 六 PM 7:00 ~ 9:30 聯 ives.net@gmail.com www.csie.ntu.edu.tw/~r93057/aspnet134 度 C# 力 度 C# Web SQL 料 DataGrid DataList 參 ASP.NET 1.0 C# 例 ASP.NET 立

More information

Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien

Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien Effective Modern C++ C++ C++ C++11/C++14 C++ Scott Meyers Gerhard Kreuzer Siemens AG Effective Modern C++ Effective Modern C++ Andrei Alexandrescu Facebook Modern C++ Design C++ C++ Nevin Liber DRW Trading

More information

nooog

nooog C : : : , C C,,, C, C,, C ( ), ( ) C,,, ;,, ; C,,, ;, ;, ;, ;,,,, ;,,, ; : 1 9, 2 3, 4, 5, 6 10 11, 7 8, 12 13,,,,, 2008 1 1 (1 ) 1.1 (1 ) 1.1.1 ( ) 1.1.2 ( ) 1.1.3 ( ) 1.1.4 ( ) 1.1.5 ( ) 1.2 ( ) 1.2.1

More information

untitled

untitled 1 Outline 數 料 數 數 列 亂數 練 數 數 數 來 數 數 來 數 料 利 料 來 數 A-Z a-z _ () 不 數 0-9 數 不 數 SCHOOL School school 數 讀 school_name schoolname 易 不 C# my name 7_eleven B&Q new C# (1) public protected private params override

More information

第3章.doc

第3章.doc 3 3 3 3.1 3 IT Trend C++ Java SAP Advantech ERPCRM C++ C++ Synopsys C++ NEC C C++PHP C++Java C++Java VIA C++ 3COM C++ SPSS C++ Sybase C++LinuxUNIX Motorola C++ IBM C++Java Oracle Java HP C++ C++ Yahoo

More information

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO Car DVD New GUI IR Flow User Manual V0.1 Jan 25, 2008 19, Innovation First Road Science Park Hsin-Chu Taiwan 300 R.O.C. Tel: 886-3-578-6005 Fax: 886-3-578-4418 Web: www.sunplus.com Important Notice SUNPLUS

More information

Microsoft PowerPoint - 07-overloaded.ppt

Microsoft PowerPoint - 07-overloaded.ppt Overloaded Functions 前言 處理多載函式宣告的規則 處理多載函式呼叫的規則 多載函式與 scope 函式呼叫的議決 前言 C 語言規定 : 函式的名稱不可相同 這樣的規定使得我們必須為功能相近但參數型態相異的函式取不同的名稱, 譬如 : int imax (int, int); double dmax (double, double ); // max function for

More information

Microsoft PowerPoint - ch11.ppt

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

More information

Microsoft PowerPoint - string_kruse [兼容模式]

Microsoft PowerPoint - string_kruse [兼容模式] Strings Strings in C not encapsulated Every C-string has type char *. Hence, a C-string references an address in memory, the first of a contiguous set of bytes that store the characters making up the string.

More information

chap07.key

chap07.key #include void two(); void three(); int main() printf("i'm in main.\n"); two(); return 0; void two() printf("i'm in two.\n"); three(); void three() printf("i'm in three.\n"); void, int 标识符逗号分隔,

More information

四川省普通高等学校

四川省普通高等学校 四 川 省 普 通 高 等 学 校 计 算 机 应 用 知 识 和 能 力 等 级 考 试 考 试 大 纲 (2013 年 试 行 版 ) 四 川 省 教 育 厅 计 算 机 等 级 考 试 中 心 2013 年 1 月 目 录 一 级 考 试 大 纲 1 二 级 考 试 大 纲 6 程 序 设 计 公 共 基 础 知 识 6 BASIC 语 言 程 序 设 计 (Visual Basic) 9

More information

untitled

untitled Fortran Chapter 7 Subroutine ( ) and Function 7-1 subroution 行 不 行 來 行 The general form of a subroutine is subroutine subroutine_name ( argument_list) (Declaration section) (Execution section) retrun end

More information

C/C++语言 - 分支结构

C/C++语言 - 分支结构 C/C++ Table of contents 1. if 2. if else 3. 4. 5. 6. continue break 7. switch 1 if if i // colddays.c: # include int main ( void ) { const int FREEZING = 0; float temperature ; int cold_ days

More information

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

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 Chapter 02 變數與運算式 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.2 2.2.1 2.2.2 2.2.3 type 2.2.4 2.3 2.3.1 print 2.3.2 input 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 + 2.4.6 Python Python 2.1 2.1.1 a p p l e b e a r c 65438790

More information

PowerPoint Presentation

PowerPoint Presentation 第六章簡介運算子超載 (Operator Overloading) 6-1 運算子超載的基礎 6-2 超載二元運算子 6-3 超載邏輯與關係運算子 6-4 超載一元運算子 6-5 使用夥伴函數 6-6 細部檢視指定運算子 6-7 超載註標運算子 6-1 運算子超載的基礎 甚麼是運算子超載? 讓運算子 ( 符號 ) 有不同的意義 EX: 運算子的預設意義 ( 以 + 與 = 為例 ) class frac

More information

untitled

untitled 1 5 IBM Intel 1. IBM 第 1/175 页 第 2/175 页 第 3/175 页 80 第 4/175 页 2. IBM 第 5/175 页 3. (1) 第 6/175 页 第 7/175 页 第 8/175 页 = = 第 9/175 页 = = = = = 第 10/175 页 = = = = = = = = 3. (2) 第 11/175 页 第 12/175 页 第 13/175

More information

ebook39-5

ebook39-5 5 3 last-in-first-out, LIFO 3-1 L i n e a r L i s t 3-8 C h a i n 3 3. 8. 3 C + + 5.1 [ ] s t a c k t o p b o t t o m 5-1a 5-1a E D 5-1b 5-1b E E 5-1a 5-1b 5-1c E t o p D t o p D C C B B B t o p A b o

More information

概述

概述 OPC Version 1.6 build 0910 KOSRDK Knight OPC Server Rapid Development Toolkits Knight Workgroup, eehoo Technology 2002-9 OPC 1...4 2 API...5 2.1...5 2.2...5 2.2.1 KOS_Init...5 2.2.2 KOS_InitB...5 2.2.3

More information

1 4 1.1 4 1.2..4 2..4 2.1..4 3.4 3.1 Java.5 3.1.1..5 3.1.2 5 3.1.3 6 4.6 4.1 6 4.2.6 5 7 5.1..8 5.1.1 8 5.1.2..8 5.1.3..8 5.1.4..9 5.2..9 6.10 6.1.10

1 4 1.1 4 1.2..4 2..4 2.1..4 3.4 3.1 Java.5 3.1.1..5 3.1.2 5 3.1.3 6 4.6 4.1 6 4.2.6 5 7 5.1..8 5.1.1 8 5.1.2..8 5.1.3..8 5.1.4..9 5.2..9 6.10 6.1.10 Java V1.0.1 2007 4 10 1 4 1.1 4 1.2..4 2..4 2.1..4 3.4 3.1 Java.5 3.1.1..5 3.1.2 5 3.1.3 6 4.6 4.1 6 4.2.6 5 7 5.1..8 5.1.1 8 5.1.2..8 5.1.3..8 5.1.4..9 5.2..9 6.10 6.1.10 6.2.10 6.3..10 6.4 11 7.12 7.1

More information

Template

Template 第十一章 ( 上篇 ) 函數樣板 (Function Template) 與 類別樣板 (Class Template) 建立通用函數 (Generic Functions) & 通用類別 (Generic Classes) - Code Reuse 的另一種發揮 - 煩人的事經歷一次就夠了 1 為何需要通用函數? int abs(int x) { return (x>0)?x:-x; int abs(int

More information

Strings

Strings Inheritance Cheng-Chin Chiang Relationships among Classes A 類 別 使 用 B 類 別 學 生 使 用 手 機 傳 遞 訊 息 公 司 使 用 金 庫 儲 存 重 要 文 件 人 類 使 用 交 通 工 具 旅 行 A 類 別 中 有 B 類 別 汽 車 有 輪 子 三 角 形 有 三 個 頂 點 電 腦 內 有 中 央 處 理 單 元 A

More information

Microsoft PowerPoint - 20-string-s.pptx

Microsoft PowerPoint - 20-string-s.pptx String 1 String/ 1.: char s1[10]; char *s2; char s3[] = "Chan Tai Man"; char s4[20] = "Chan Siu Ming"; char s5[]={'h','e','l','l','o','\0'; 0 1 2 3 4 5 6 7 8 9 10 11 12 s3 C h a n T a i \0 M a n \0 printf

More information

untitled

untitled 1 Outline 流 ( ) 流 ( ) 流 ( ) 流 ( ) 流 ( ) 狀 流 ( ) 利 來 行流 if () 立 行 ; else 不 立 行 ; 例 sample2-a1 (1) 列 // 料 Console.Write(""); string name = Console.ReadLine(); Console.WriteLine(" " + name + "!!"); 例 sample2-a1

More information

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2 PowerBuilder 9 PowerBuilder Native Interface(PBNI) PowerBuilder 9 PowerBuilder C++ Java PowerBuilder 9 PBNI PowerBuilder Java C++ PowerBuilder NVO / PowerBuilder C/C++ PowerBuilder 9.0 PowerBuilder Native

More information

untitled

untitled 不 料 料 例 : ( 料 ) 串 度 8 年 數 串 度 4 串 度 數 數 9- ( ) 利 數 struct { ; ; 數 struct 數 ; 9-2 數 利 數 C struct 數 ; C++ 數 ; struct 省略 9-3 例 ( 料 例 ) struct people{ char name[]; int age; char address[4]; char phone[]; int

More information

Microsoft Word - About_C_PointerAdvanced.doc

Microsoft Word - About_C_PointerAdvanced.doc (*) 如何宣告或解讀某一個資料型態的指標變數? 在變數名稱前加上一個 * 號 ( 陣列也可算成是指標只是其值不能被更改!) 反過來在解讀變數的型態時 : 先找到變數名稱, 再看其左邊是否有星號 ( 至多取一個 ), 若有表示這是一個指標變數, 否則就是一般的變數 至於資料型態的部份, 只要將變數或連同 * 號移去後, 剩下的部份就是此變數或指標的資料型態 (*) 優先順序 : 運算子的優先順序 5

More information

C

C C 2017 4 1 1. 2. while 3. 4. 5. for 6. 2/161 C 7. 8. (do while) 9. 10. (nested loop) 11. 12. 3/161 C 1. I 1 // summing.c: 2 #include 3 int main(void) 4 { 5 long num; 6 long sum = 0L; 7 int status;

More information

epub 33-8

epub 33-8 8 1) 2) 3) A S C I I 4 C I / O I / 8.1 8.1.1 1. ANSI C F I L E s t d i o. h typedef struct i n t _ f d ; i n t _ c l e f t ; i n t _ m o d e ; c h a r *_ n e x t ; char *_buff; /* /* /* /* /* 1 5 4 C FILE

More information

投影片 1

投影片 1 資料庫管理程式 ( 補充教材 -Part2) 使用 ADO.NET 連結資料庫 ( 自行撰寫程式碼 以實現新增 刪除 修改等功能 ) Private Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click ' 宣告相關的 Connection

More information

3.1 num = 3 ch = 'C' 2

3.1 num = 3 ch = 'C' 2 Java 1 3.1 num = 3 ch = 'C' 2 final 3.1 final : final final double PI=3.1415926; 3 3.2 4 int 3.2 (long int) (int) (short int) (byte) short sum; // sum 5 3.2 Java int long num=32967359818l; C:\java\app3_2.java:6:

More information

提纲 1 2 OS Examples for 3

提纲 1 2 OS Examples for 3 第 4 章 Threads2( 线程 2) 中国科学技术大学计算机学院 October 28, 2009 提纲 1 2 OS Examples for 3 Outline 1 2 OS Examples for 3 Windows XP Threads I An Windows XP application runs as a seperate process, and each process may

More information

华恒家庭网关方案

华恒家庭网关方案 LINUX V1.5 1 2 1 2 LINUX WINDOWS PC VC LINUX WINDOWS LINUX 90% GUI LINUX C 3 REDHAT 9 LINUX PC TFTP/NFS http://www.hhcn.com/chinese/embedlinux-res.html minicom NFS mount C HHARM9-EDU 1 LINUX HHARM9-EDU

More information

untitled

untitled 1 DBF (READDBF.C)... 1 2 (filetest.c)...2 3 (mousetes.c)...3 4 (painttes.c)...5 5 (dirtest.c)...9 6 (list.c)...9 1 dbf (readdbf.c) /* dbf */ #include int rf,k,reclen,addr,*p1; long brec,erec,i,j,recnum,*p2;

More information

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40 C C trio@seu.edu.cn C C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40 Week3 C Week5 Week5 Memory & Pointer

More information

C语言的应用.PDF

C语言的应用.PDF AVR C 9 1 AVR C IAR C, *.HEX, C,,! C, > 9.1 AVR C MCU,, AVR?! IAR AVR / IAR 32 ALU 1KBytes - 8MBytes (SPM ) 16 MBytes C C *var1, *var2; *var1++ = *--var2; AVR C 9 2 LD R16,-X ST Z+,R16 Auto (local

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 多型 Polymorphism 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 子類別與父類別物件間的指定 (assignment)

More information

科学计算的语言-FORTRAN95

科学计算的语言-FORTRAN95 科 学 计 算 的 语 言 -FORTRAN95 目 录 第 一 篇 闲 话 第 1 章 目 的 是 计 算 第 2 章 FORTRAN95 如 何 描 述 计 算 第 3 章 FORTRAN 的 编 译 系 统 第 二 篇 计 算 的 叙 述 第 4 章 FORTRAN95 语 言 的 形 貌 第 5 章 准 备 数 据 第 6 章 构 造 数 据 第 7 章 声 明 数 据 第 8 章 构 造

More information

Microsoft PowerPoint - Fig03_Stack.ppt [相容模式]

Microsoft PowerPoint - Fig03_Stack.ppt [相容模式] 四 堆疊與佇列 (Stack & Queue) 4-. 串列及鏈結串列 4-. 用陣列結構實作堆疊 4-3. 用鏈結串列實作堆疊 4-4. 堆疊的應用 4-5. 佇列 4-6. 用陣列結構實作佇列 4-7 7. 用鏈結串列實作佇列 堆疊的基本觀念. 定義 : 4- 堆疊 當將東西疊成一堆, 而取用的時候由上方來取出. 特性 : 先進後出, 後進先出 ( 號球先放, 但 3 號球會先拿出 ) 3 3

More information

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

全国计算机技术与软件专业技术资格(水平)考试 全 国 计 算 机 技 术 与 软 件 专 业 技 术 资 格 ( 水 平 ) 考 试 2008 年 上 半 年 程 序 员 下 午 试 卷 ( 考 试 时 间 14:00~16:30 共 150 分 钟 ) 试 题 一 ( 共 15 分 ) 阅 读 以 下 说 明 和 流 程 图, 填 补 流 程 图 中 的 空 缺 (1)~(9), 将 解 答 填 入 答 题 纸 的 对 应 栏 内 [ 说 明

More information

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

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE Project Properties IDE makefile 1. Oracle Solaris Studio 12.2 IDE 2010 9 2 8 9 10 11 13 20 26 28 30 32 33 Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1. "File" > "New

More information

第5章修改稿

第5章修改稿 (Programming Language), ok,, if then else,(), ()() 5.0 5.0.0, (Variable Declaration) var x : T x, T, x,,,, var x : T P = x, x' : T P P, () var x:t P,,, yz, var x : int x:=2. y := x+z = x, x' : int x' =2

More information

Microsoft PowerPoint - Class5.pptx

Microsoft PowerPoint - Class5.pptx C++ 程式初探 V 2015 暑期 ver. 1.0.1 C++ 程式語言 大綱 1. 大量檔案讀取 & 計算 2. 指標 3. 動態記憶體 & 動態陣列 4. 標準函式庫 (STL) vector, algorithm 5. 結構與類別 2 大量檔案讀取 & 計算 若目前有一個程式將讀取純文字文件 (.txt) 中的整數, 並將該文件中的整數有小到大排序後, 儲存到另外一個新的純文字件中 假設有

More information

穨control.PDF

穨control.PDF TCP congestion control yhmiu Outline Congestion control algorithms Purpose of RFC2581 Purpose of RFC2582 TCP SS-DR 1998 TCP Extensions RFC1072 1988 SACK RFC2018 1996 FACK 1996 Rate-Halving 1997 OldTahoe

More information

1 2005 9 2005,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : 100081 : : 7871092 1 /16 : 8. 75 : 96 : 2005 11 1 : 2005 11 1 : ISBN 7

1 2005 9 2005,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : 100081 : : 7871092 1 /16 : 8. 75 : 96 : 2005 11 1 : 2005 11 1 : ISBN 7 1 2005 9 2005,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : 100081 : : 7871092 1 /16 : 8. 75 : 96 : 2005 11 1 : 2005 11 1 : ISBN 7-80097 - 564-9 /TP 8 : 10. 00 ,,,, 1994 NCRE,,, ( ),,,,,

More information

Microsoft Word - p11.doc

Microsoft Word - p11.doc () 11-1 ()Classification Analysis( ) m() p.d.f prior (decision) (loss function) Bayes Risk for any decision d( ) posterior risk posterior risk Posterior prob. j (uniform prior) where Mahalanobis Distance(M-distance)

More information

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("%

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(% 2013 ( 28 ) ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 10 B 1 C 1 D 5 E 5 F 1 G II 5 H 30 1 2013 C 1 #include 2 int main(void) 3

More information

ebook8-30

ebook8-30 3 0 C C C C C C++ C + + C++ GNU C/C++ GNU egcs UNIX shell s h e l l g a w k P e r l U N I X I / O UNIX shell awk P e r l U N I X C C C C C C U N I X 30.1 C C U N I X 70 C C U N I X U N I X U N I X C Dennis

More information

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File 51 C 51 51 C C C C C C * 2003-3-30 pnzwzw@163.com C C C C KEIL uvision2 MCS51 PLM C VC++ 51 KEIL51 KEIL51 KEIL51 KEIL 2K DEMO C KEIL KEIL51 P 1 1 1 1-1 - 1 Project New Project 1 2 Windows 1 3 N C test

More information

Microsoft Word - CPE考生使用手冊160524.docx

Microsoft Word - CPE考生使用手冊160524.docx 大 學 程 式 能 力 檢 定 (CPE) 考 生 使 用 手 冊 2016 年 5 月 24 日 這 份 手 冊 提 供 給 參 加 CPE 檢 定 考 試 的 考 生 內 容 包 含 考 試 環 境 的 使 用, 以 及 解 題 時 所 使 用 I/O 的 基 本 知 識 1. 如 欲 報 名 參 加 CPE 考 試, 請 先 於 CPE 網 站 完 成 帳 號 註 冊, 然 後 再 報 名 該

More information

Microsoft Word - 01.DOC

Microsoft Word - 01.DOC 第 1 章 JavaScript 简 介 JavaScript 是 NetScape 公 司 为 Navigator 浏 览 器 开 发 的, 是 写 在 HTML 文 件 中 的 一 种 脚 本 语 言, 能 实 现 网 页 内 容 的 交 互 显 示 当 用 户 在 客 户 端 显 示 该 网 页 时, 浏 览 器 就 会 执 行 JavaScript 程 序, 用 户 通 过 交 互 式 的

More information

Microsoft Word - PHP7Ch01.docx

Microsoft Word - PHP7Ch01.docx PHP 01 1-6 PHP PHP HTML HTML PHP CSSJavaScript PHP PHP 1-6-1 PHP HTML PHP HTML 1. Notepad++ \ch01\hello.php 01: 02: 03: 04: 05: PHP 06:

More information

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更 AX5000 Version 1.0 2006 年 9 錄...1 說...2 說...3...4 說...5 六...6 6.1 率...7 6.2 POST PAY...8 6.3 PREPAY DEPOSIT...9 6.4...10 6.5...11 更...12...12 LCD IC LED Flash 更 兩 RJ11 ( ) DC ON OFF ON 狀 狀 更 OFF 復 狀 說

More information

提问袁小兵:

提问袁小兵: C++ 面 试 试 题 汇 总 柯 贤 富 管 理 软 件 需 求 分 析 篇 1. STL 类 模 板 标 准 库 中 容 器 和 算 法 这 部 分 一 般 称 为 标 准 模 板 库 2. 为 什 么 定 义 虚 的 析 构 函 数? 避 免 内 存 问 题, 当 你 可 能 通 过 基 类 指 针 删 除 派 生 类 对 象 时 必 须 保 证 基 类 析 构 函 数 为 虚 函 数 3.

More information

ebook14-4

ebook14-4 4 TINY LL(1) First F o l l o w t o p - d o w n 3 3. 3 backtracking parser predictive parser recursive-descent parsing L L ( 1 ) LL(1) parsing L L ( 1 ) L L ( 1 ) 1 L 2 L 1 L L ( k ) k L L ( 1 ) F i r s

More information

mvc

mvc Build an application Tutor : Michael Pan Application Source codes - - Frameworks Xib files - - Resources - ( ) info.plist - UIKit Framework UIApplication Event status bar, icon... delegation [UIApplication

More information

untitled

untitled 1 1.1 1.2 1.3 1.4 1.5 ++ 1.6 ++ 2 BNF 3 4 5 6 7 8 1.2 9 1.2 IF ELSE 10 1.2 11 1.2 12 1.3 Ada, Modula-2 Simula Smalltalk-80 C++, Objected Pascal(Delphi), Java, C#, VB.NET C++: C OOPL Java: C++ OOPL C# C++

More information

untitled

untitled (encapsulation) 例 類 說 類 料 來 料 information hiding 念 (inheritance) 來說 類 類 類 類 類 類 行 利 來 (polymorphism) 不 類 數 不 1 2 3 4 類 類 不 類 不 類 5 6 7 // virtual 不見了 #include #include using namespace

More information

学习MSP430单片机推荐参考书

学习MSP430单片机推荐参考书 MSP430 16 MSP430 C MSP430 C MSP430 FLASH 16 1 CPU 16 ALU 16 PC SP SR R4~R15 2 3 00-FFH 100-1FFH 4 5 1 2 51 24 27 6 1 2 3 4 5 6 4 12 SR SP SR CPU SR CPU C Z N GIE CPUOff CPU OscOff SCG0 SCG1 CPU EXIT SP

More information

INTRODUCTION TO COM.DOC

INTRODUCTION TO COM.DOC How About COM & ActiveX Control With Visual C++ 6.0 Author: Curtis CHOU mahler@ms16.hinet.net This document can be freely release and distribute without modify. ACTIVEX CONTROLS... 3 ACTIVEX... 3 MFC ACTIVEX

More information

Microsoft Word - 物件導向編程精要.doc

Microsoft Word - 物件導向編程精要.doc Essential Object-Oriented Programming Josh Ko 2007.03.11 object-oriented programming C++ Java OO class object OOP Ruby duck typing complexity abstraction paradigm objects objects model object-oriented

More information

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++;

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++; Memory & Pointer trio@seu.edu.cn 2.1 2.1.1 1 int *p int a 0x00C7 0x00C7 0x00C7 2.1.2 2 int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++; 2.1.3 1. 2. 3. 3 int A,

More information

Microsoft PowerPoint - 11_Templates.ppt

Microsoft PowerPoint - 11_Templates.ppt 1 1. 上機考 20% 期末考 6/23( 四 ) 晚 6:30~8:30 範圍 : 第 7, 8, 9, 10 章實習內容 按座位坐, 隨機抽兩題 2. 紙上測驗 20% 6/21( 二 ) :9:30~11:00 課本 7-11, 13 章內容 2 第 11 章樣版 (Templates) 11.1 簡介 11.2 函式樣版 11.3 多載函式樣版 11.4 類別樣版 11.5 類別樣版與無型

More information

Fuzzy Highlight.ppt

Fuzzy Highlight.ppt Fuzzy Highlight high light Openfind O(kn) n k O(nm) m Knuth O(n) m Knuth Unix grep regular expression exact match Yahoo agrep fuzzy match Gais agrep Openfind gais exact match fuzzy match fuzzy match O(kn)

More information

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit 6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C51 6.1 C51 6.1.1 C51 C51 ANSI C MCS-51 C51 ANSI C C51 6.1 6.1 C51 bit Byte bit sbit 1 0 1 unsigned char 8 1 0 255 Signed char 8 11 128

More information

Microsoft PowerPoint - ch09_AEL0080.ppt

Microsoft PowerPoint - ch09_AEL0080.ppt 9 字 串 子曰 : 質勝文則野, 文勝質則史 文質彬彬, 然後君子 論語論語.雍也第六雍也第六 標準的 C++ 提供了方便的程式庫, 讓我們能將 字串 視為獨立的單元, 以進行各種存取和剪接的處理 1/36 字串 9.1 9.2 9.3 9.4 9.5 字串的基本概念字串的輸入與輸出字串的處理字串的指標陣列字串處理在編碼上的應用 2/36 字串的基本概念 字串 (string) 是由雙引號 所包括起來的一串文字

More information

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft PowerPoint - L17_Inheritance_v4.pptx C++ Programming Lecture 17 Wei Liu ( 刘 威 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology May. 2015 Lecture 17 Chapter 20. Object-Oriented Programming: Inheritance

More information

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

_汪_文前新ok[3.1].doc 普 通 高 校 本 科 计 算 机 专 业 特 色 教 材 精 选 四 川 大 学 计 算 机 学 院 国 家 示 范 性 软 件 学 院 精 品 课 程 基 金 青 年 基 金 资 助 项 目 C 语 言 程 序 设 计 (C99 版 ) 陈 良 银 游 洪 跃 李 旭 伟 主 编 李 志 蜀 唐 宁 九 李 涛 主 审 清 华 大 学 出 版 社 北 京 i 内 容 简 介 本 教 材 面 向

More information

热设计网

热设计网 例 例 Agenda Popular Simulation software in PC industry * CFD software -- Flotherm * Advantage of Flotherm Flotherm apply to Cooler design * How to build up the model * Optimal parameter in cooler design

More information

The golden pins of the PCI card can be oxidized after months or years

The golden pins of the PCI card can be oxidized after months or years Q. 如何在 LabWindows/CVI 編譯 DAQ Card 程式? A: 請參考至下列步驟 : 步驟 1: 安裝驅動程式 1. 安裝 UniDAQ 驅動程式 UniDAQ 驅動程式下載位置 : CD:\NAPDOS\PCI\UniDAQ\DLL\Driver\ ftp://ftp.icpdas.com/pub/cd/iocard/pci/napdos/pci/unidaq/dll/driver/

More information

Microsoft Word - C-pgm-ws2010.doc

Microsoft Word - C-pgm-ws2010.doc Information and Communication Technology 資訊與通訊科技 Loops (while/for) C 廻路 姓名 : 班別 : ( ) CS C Programming #1 Functions 函數 : 1 若 n=14, 求以下表示式的值 Expressions 表示式 Value 值 Expressions 表示式 Value 值 A 20 2 * (n /

More information

Microsoft PowerPoint - 04-array_pointer.ppt

Microsoft PowerPoint - 04-array_pointer.ppt Array 與 Pointer Array Dynamical Memory Allocation Array( 陣列 ) 陣列是用來存放同樣型態的資料陣列的大小必須在程式中預先設定在程式執行中, 陣列的大小無法改變陣列中的資料是透過索引 (index) 來存取 一維陣列的宣告 type array_name[array_size]; int iarray[100]; /* an integer array

More information

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

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1 C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1 1 CARDGAME 1 CardGame 题目描述 桌上有一叠牌, 从第一张牌 ( 即位于顶面的牌 ) 开始从上往下依次编号为 1~n 当至少还剩两张牌时进行以下操作 : 把第一张牌扔掉, 然后把新的第一张放到整叠牌的最后 请模拟这个过程, 依次输出每次扔掉的牌以及最后剩下的牌的编号 输入 输入正整数 n(n

More information

ebook15-C

ebook15-C C 1 1.1 l s ( 1 ) - i i 4. 14 - d $ l s -ldi /etc/. /etc/.. - i i 3077 drwxr-sr-x 7 bin 2048 Aug 5 20:12 /etc/./ 2 drwxr-xr-x 13 root 512 Aug 5 20:11 /etc/../ $ls -ldi /. /..... i 2 2 drwxr-xr-x 13 root

More information

上海市本科教学质量年度报告

上海市本科教学质量年度报告 上 海 市 本 科 教 学 质 量 年 度 报 告 数 据 内 涵 说 明 V2.0 版 上 海 市 教 委 高 教 处 上 海 喆 思 (2015.07.02) 目 录 一 基 本 统 计 挃 标 说 明... 4 二 挃 标 解 释... 4 1. 全 日 制 在 校 本 科 生 数 及 占 在 校 生 总 数 的 比 例 ( 学 年 )... 4 2. 当 年 本 科 招 生 与 业 总 数

More information

Ps22Pdf

Ps22Pdf C ( CIP) C /. :, 2001. 7 21 ISBN 7-5624 -2355-5. C........ C. TP312 CIP ( 2001 ) 034496 C * * : 7871092 1 /16 : 14. 25 : 356 20017 1 20017 1 : 1 6 000 ISBN 7-5624-2355-5 / TP311 : 21. 00 C, C,,,, C,, (

More information

51 C 51 isp 10 C PCB C C C C KEIL

51 C 51 isp 10   C   PCB C C C C KEIL http://wwwispdowncom 51 C " + + " 51 AT89S51 In-System-Programming ISP 10 io 244 CPLD ATMEL PIC CPLD/FPGA ARM9 ISP http://wwwispdowncom/showoneproductasp?productid=15 51 C C C C C ispdown http://wwwispdowncom

More information