1 / 28

搜尋練習

搜尋練習. #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 100 int linearSearch( const int array[], int key, int size ); int main(){ int array[ SIZE ]; int loop; int searchKey; int element; srand(time(NULL));

verne
Download Presentation

搜尋練習

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 搜尋練習 #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 100 int linearSearch( const int array[], int key, int size ); int main(){ int array[ SIZE ]; int loop; int searchKey; int element; srand(time(NULL)); for ( loop = 0; loop < SIZE; loop++ ) array[ loop ] = rand()%200 + 1; printf( "Enter the integer search key: " ); scanf( "%d", &searchKey ); element = linearSearch( array, searchKey, SIZE );

  2. if ( element != -1 ) { printf( "Found value in element %d\n", element ); } else { printf( "Value not found\n" ); } return 0; } int linearSearch( const int array[], int key, int size ) { int n; for ( n = 0; n <= size - 1; ++n ) if ( array[ n ] == key ) return n; return -1; }

  3. Chapter 7指標

  4. 章節大綱 7.1 簡介 7.2 指標變數的定義及初始值設定 7.3 指標運算子 7.4 傳參考呼叫 7.5 const修飾詞在指標上的使用 7.6 使用傳參考呼叫的氣泡排序法 7.7 sizeof運算子 7.8 指標運算式和指標的算數運算 7.9 指標和陣列的關係 7.10 指標陣列 7.11 範例研究:洗牌和發牌的模擬 7.12 指向函式的指標

  5. count countPtr count 7 7 7.2 指標變數的定義及初始值設定 • 指標變數 • 包含變數記憶的位址與它的值 • 一般變數直接參照一個特別指定值 • 指標變數包含變數的位址與間接參照的值

  6. 7.2 指標變數的定義及初始值設定 • 指標變數的定義 • 『*』 用來定義指標變數 int *myPtr; • 定義在一個 int 型式的指標變數:『int *』 • 每一個指標變數前面都必須要有一個『 * 』,如: int *myPtr1, *myPtr2; • 可以定義任何型式的指標變數 • 可以初始化為 0、NULL 或某個位址 • 0 或 NULL 其實是一樣:不指向任何東西 ( NULL 比較好)

  7. yPtr y y 5 yPtr y的存放位址為 yptr的值 500000 600000 600000 5 7.3 指標運算子 • & 運算元(讀取位址的運算元) • 傳回位址 int y = 5; int *yPtr; yPtr = &y; /* yPtr 指向 y 的位址 */

  8. 7.3 指標運算子 • * 運算子(間接或反參照運算元) • 傳回其指向物的數值 • *yptr 即為 y (因為 yptr 指向 y) • * 也可以用來指定值 • 沿上例: *yptr = 7; /* 將 y 的值改為 7 */ • *運算元必須是左邊數值(lvalue) • * 與 & 這兩個運算元是互補的 • 同時運算互相抵銷

  9. #include <stdio.h> int main() { int a; int *aPtr; a = 7; aPtr = &a; printf( "The address of a is%p" "\nThevalue of aPtr is%p", &a, aPtr ); printf( "\n\nThevalue of a is%d" "\nThevalue of *aPtr is%d", a, *aPtr ); printf( "\n\nShowingthat * and & are inverses of “ "each other.\n&*aPtr =%p" "\n*&aPtr =%p\n", &*aPtr, *&aPtr ); return 0; } 將a的位址指定給aPtr

  10. The address of a is 0012FF88 The value of aPtr is 0012FF88 The value of a is 7 The value of *aPtr is 7 Proving that * and & are complements of each other. &*aPtr = 0012FF88 *&aPtr = 0012FF88

  11. 7.4 模擬傳參考呼叫 • 指標的傳參考呼叫 • 用 & 運算子來傳遞位址 • 可以更改呼叫者的記憶體內的數值 • 陣列的傳遞不用加上 & 運算子,因為陣列名稱已經相等於指標(陣列傳遞就已經是傳參考呼叫了) • * 運算子 • 舉例來看:函式 double void double( int *number ) { *number = 2 * ( *number ); } • 將*number 視作一般變數一樣傳遞使用

  12. #include <stdio.h> int cubeByValue( int ); int main() { int number = 5; printf( "The original value of number is %d", number ); number = cubeByValue( number ); printf( "\nThe new value of number is %d\n", number ); return 0; } int cubeByReference( int value ) { return value = value * value * value ; } The original value of number is 5 The new value of number is 125

  13. #include <stdio.h> void cubeByReference( int * ); int main() { int number = 5; printf( "The original value of number is %d", number ); cubeByReference( &number ); printf( "\nThe new value of number is %d\n", number ); return 0; } void cubeByReference( int *nPtr ) { *nPtr = (*nPtr) * (*nPtr) * (*nPtr); } The original value of number is 5 The new value of number is 125

  14. 7.5 const修飾詞在指標上的使用 • const 修飾詞 • 變數的值不可以被改變 • 使用時機: 使用函式時不允許改變變數值 • 嘗試改變一個已宣告成 const的變數會發出警告或錯誤訊息 • 常數指標(const pointer) • 指向一個 const 的記憶位置(不可改) • 必須宣告時作初始化 • int *const myPtr = &x; • 資料可改的常數指標 • const int *myPtr = &x; • 資料不可改的非常數指標 • const int *const Ptr = &x; • 資料不可改的常數指標 • x 可以被改變,但 *Ptr 不能

  15. #include <stdio.h> #include <ctype.h> void convertToUppercase( char *sPtr ); int main() { char string[] = "characters and $32.98"; printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); return 0; } string 字串已經被函式更改了!!

  16. void convertToUppercase( char *sPtr ){ while ( *sPtr != '\0' ) { if ( islower( *sPtr ) ) { *sPtr = toupper( *sPtr ); } ++sPtr; } } The string before conversion is: characters and $32.98 The string after conversion is: CHARACTERS AND $32.98 函式 islower(*sPtr) :如果字元 *sPtr 是介在 a 到 z 之間,函式會              回傳『真』,反之會回傳『偽』。 函式 toupper(*sPtr):將字元 *sPtr 轉換成相對應的大寫字母

  17. #include <stdio.h> void printCharacters( const char *sPtr ); int main() { char string[] = "print characters of a string"; printf( "The string is:\n" ); printCharacters( string ); printf( "\n" ); return 0; } void printCharacters( const char *sPtr ) { for ( ; *sPtr != '\0'; sPtr++ ) printf( "%c", *sPtr ); } The string is: print characters of a string

  18. #include <stdio.h> void f( const int *xPtr ); int main() { int y; f( &y ); return 0; } void f( const int *xPtr ) { *xPtr = 100; } Compiling... FIG07_12.c d:\books\2003\chtp4\examples\ch07\fig07_12.c(22) : error C2166: l-value specifies const object Error executing cl.exe. FIG07_12.exe - 1 error(s), 0 warning(s)

  19. Changing *ptr is allowed – x is not a constant. #include <stdio.h> int main() { int x; int y; int * const ptr = &x; *ptr = 7; ptr = &y; return 0; } Changing ptr is an error – ptr is a constant pointer. Compiling... FIG07_13.c D:\books\2003\chtp4\Examples\ch07\FIG07_13.c(15) : error C2166: l-value specifies­ const object Error executing cl.exe. FIG07_13.exe - 1 error(s), 0 warning(s)

  20. #include <stdio.h> int main() { int x = 5; int y; const int *const ptr = &x; printf( "%d\n", *ptr ); *ptr = 7; ptr = &y; return 0; } Compiling... FIG07_14.c D:\books\2003\chtp4\Examples\ch07\FIG07_14.c(17) : error C2166: l-value specifies­ const object D:\books\2003\chtp4\Examples\ch07\FIG07_14.c(18) : error C2166: l-value specifies­ const object Error executing cl.exe. FIG07_12.exe - 2 error(s), 0 warning(s)

  21. 7.6 使用傳參考呼叫的氣泡排序法 提供氣泡排序法指標的使用 • 交換兩個元素 • swap函式必須讀進陣列元素的位址(用 &) • 陣列元素必須『傳參考呼叫』 • 使用指標和 * 運算元, swap函式可以交換陣列中的元素

  22. #include <stdio.h> #define SIZE 10 void bubbleSort( int *, const int ); int main() { int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int i; printf( "Data items in original order\n" ); for ( i = 0; i < SIZE; i++ ) printf( "%4d", a[ i ] ); bubbleSort( a, SIZE ); printf( "\nData items in ascending order\n" ); for ( i = 0; i < SIZE; i++ ) printf( "%4d", a[ i ] ); printf( "\n" ); return 0; }

  23. void bubbleSort( int *array, const int size ) { void swap( int *, int * ); int pass, j; for ( pass = 0; pass < size - 1; pass++ ) for ( j = 0; j < size - 1; j++ ) if ( array[ j ] > array[ j + 1 ] ) swap( &array[ j ], &array[ j + 1 ] ); } void swap( int *element1Ptr, int *element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89

  24. 7.7 sizeof 運算子 • 一個特殊的一元運算子:sizeof() • 計算傳回編譯時所佔的位元數(byte) • 對陣列來說:(一個元素的位元大小)x(元素個數) • 例如:如果 sizeof( int ) 等於 4 bytes, 則 int myArray[ 10 ]; printf( "%d", sizeof( myArray ) ); • 會印出 40 • sizeof 也可以用在 • 變數名稱上(括號可有可無) • 型別名稱上(括號一定要有) • 常數上

  25. "\n sizeof d = %d" "\t sizeof(double) = %d" "\n sizeof ld = %d" "\t sizeof(long double) = %d" "\n sizeof array = %d" "\n sizeof ptr = %d\n", sizeof c,sizeof( char ),sizeof s, sizeof( short ),sizeof i,sizeof( int ), sizeof l,sizeof( long ),sizeof f, sizeof( float ),sizeof d,sizeof( double ), sizeof ld,sizeof( long double ), sizeof array,sizeof ptr); return 0; } #include <stdio.h> int main() { char c; short s; int i; long l; float f; double d; long double ld; int array[ 20 ], *ptr = array; printf( " sizeof c = %d" "\t sizeof(char) = %d" "\n sizeof s = %d" "\t sizeof(short) = %d" "\n sizeof i = %d" "\t sizeof(int) = %d" "\n sizeof l = %d" "\t sizeof(long) = %d" "\n sizeof f = %d" "\t sizeof(float) = %d"

  26. sizeof c = 1 sizeof(char) = 1 sizeof s = 2 sizeof(short) = 2 sizeof i = 4 sizeof(int) = 4 sizeof l = 4 sizeof(long) = 4 sizeof f = 4 sizeof(float) = 4 sizeof d = 8 sizeof(double) = 8 sizeof ld = 8 sizeof(long double) = 8 sizeof array = 80 sizeof ptr = 4

  27. 6/8 上機課 第七章考前猜題: • 為下列每一項撰寫一個敘述式,此敘述式要能執行指定的工作。假設int整數變數no1和no2已經宣告,且no1的初始值為2000。 • 定義一個 int 型別的指標變數vPtr。 • 將變數no1的位址指定給指標變數vPtr。 • 印出vPtr所指物件的值 • 將vPtr所指物件的值指定給變數no2。 • 印出no2的值。 • 印出no1的位址。 • 印出存放在vPtr裡的位址值。

  28. #include <stdio.h> void conversion( int *, int * ); int main() { int x = 7, y = 5 ; printf( "The original values of x and y are %d and %d", x , y ); conversion( &x , &y); printf( "\nThe new values of x and y are %d and %d\n", x , y ); return 0; } 將此程式補足函式的部分,使得印出的結果是 The original values of x and y are 7 and 5 The new values of x and y are 25 and 49

More Related