Speaker: Liu Yu-Jiun Date: 2009/4/29 Recitation Course 0429 Speaker: Liu Yu-Jiun Date: 2009/4/29
作業補交方式 FTP After login... IP: 140.113.215.168 Port: 21(default) User_id: oop Password: oophw After login... You can see some test files and one upload example. Using your student id to create a directory. Your file name must be “hwX_studentid.cpp” Example: hw2_9876543.cpp
作業補交方式 You can use the test file to correct your program. Requirement: Pass all three test cases. Write the comments as detailed as possible. Deadline: 2009/6/10 Score: One point for one case. Two points for the comments. The highest score of hw0 is 2.5.
HW3 Recitation course 0422.ppt
Ch8
Pass the variables Call by value Call by reference Call by pointer data 1 non-constant 2 constant 3 4
Nonconstant pointer to nonconstant data Highest amount of access Data can be modified through the dereferenced pointer Pointer can be modified to point to other data Pointer arithmetic Operator ++ moves array pointer to the next element Ex: sPtr++; Its declaration does not include const qualifier 擁有最高的存取權限: 資料可以透過解參照指標被修改,並且指標也可以改指其它資料。 這種指標的宣告敘述不包含const
Outline Parameter is a nonconstant pointer to nonconstant data convertToUppercase modifies variable phrase 陣列名稱本身就是個指標,指向陣列的第一個項目。 陣列一定是以傳址的方式傳入函式。 fig08_10.cpp (1 of 2) 小寫轉大寫 8
Outline Parameter sPtr is a nonconstant pointer to nonconstant data Function islower returns true if the character is lowercase Function toupper returns corresponding uppercase character if original character is lowercase; otherwise toupper returns the original character Modify the memory address stored in sPtr to point to the next element of the array fig08_10.cpp (2 of 2) 小寫轉大寫 9
2. Nonconstant pointer to constant data Pointer can be modified to point to any appropriate data item Data cannot be modified through this pointer Provides the performance of pass-by-reference and the protection of pass-by-value 指標可以被修改,指向不同的資料,但是被指標所指的資料不可以透過該指標進行修改 可以用來接收傳遞給函式的陣列引數,讓函式處理陣列中元素,但不能用來修改陣列的資料。 擁有call by reference的效率和call by value的保護作用
Outline Parameter is a nonconstant pointer to constant data Pass pointer phrase to function printCharacters sPtr is a nonconstant pointer to constant data; it cannot modify the character to which it points Increment sPtr to point to the next character 11
3. Constant pointer to nonconstant data Always points to the same memory location Can only access other elements using subscript notation Data can be modified through the pointer Default for an array name Can be used by a function to receive an array argument Must be initialized when declared 指標不能被更改,永遠指向同一個記憶體位址,但程式可以透過該指標修改該記憶體位置上的資料內容。 是陣列名稱的預設狀態,陣列名稱就是指向陣列儲存位置起點的常數指標。 const指標必須於宣告時設定初始值。
int main(){ int x, y; int * const ptr = &x; *ptr = 7; ptr = &y; return 0; } Ok! error! 畫圖 Fig. 8.13
4. Constant pointer to constant data Least amount of access Always points to the same memory location Data cannot be modified using this pointer 存取權限最低。 只能指向固定的記憶體位址,且無法透過指標修改該位址上的資料。 只使用陣列附標來讀取陣列,而不修改其內容,就該以此種指標傳入陣列
#include <iostream> #include <cstring> using namespace std; void printChar(const char * const); int main(){ const char phrase[] = "print characters of a string"; cout << "The string is:\n"; printChar(phrase); cout << endl; return 0; } void printChar(const char * const sPtr){ for (int i = 0; i < strlen(sPtr) ; i++){ cout << *(sPtr+i); constant pointer to constant data constant data must be received in const // cout << sPtr[i];
Implement Selection Sort 34 56 4 10 77 51 93 30 5 52 4 56 34 10 77 51 93 30 5 52 4 5 34 10 77 51 93 30 56 52 4 5 10 34 77 51 93 30 56 52 4 5 10 30 77 51 93 34 56 52 4 5 10 30 34 51 93 77 56 52 4 5 10 30 34 51 52 77 56 93 4 5 10 30 34 51 52 56 77 93
17
Declare array as int *array (rather than int array[]) to indicate function selectionSort receives single-subscripted array Receives the size of the array as an argument; declared const to ensure that size is not modified 18
Arguments are passed by reference, allowing the function to swap values at the original memory locations 19
Sizeof Operator Returns size of operand in bytes at compiler-time Can be used with Variable names Type names (need parentheses) Constant values For arrays, sizeof returns ( size of 1 element ) * ( number of elements ) If sizeof( int ) returns 4 then int myArray[ 10 ]; cout << sizeof( myArray ); will print 40 For double realArray[ 22 ]; Use sizeof realArray / sizeof( double ) to calculate the number of elements in realArray Return type is size_t 是運算子不是函式 會影響編譯時間,不會影響執行時間
Tokenizing Breaking strings into tokens Tokens usually logical units, such as words (separated by spaces) Separated by delimiting characters Example "This is my string" has 4 word tokens (separated by spaces) char *strtok( char *s1, const char *s2 ) Multiple calls required First call contains two arguments, string to be tokenized and string containing delimiting characters Finds next delimiting character and replaces with null character Subsequent calls continue tokenizing Call with first argument NULL Stores pointer to remaining string in a static variable Returns pointer to current token
<cstring> contains prototype for strtok First call to strtok begins tokenization Subsequent calls to strtok with NULL as first argument to indicate continuation 22
23
exercise 改寫上面的程式 You can refer to “exercise.cpp” 由螢幕讀入字串 字串格式 (1, 2, 3, 4) 將字串切割成不同的tokens token1 = 1; toekn2 = 2; token3 = 3; token4 = 4 Hint: strtok(s, “, ”); 把所有tokens轉成整數並相加,最後印出總和 Hint: atoi(token); // ascii to integer You can refer to “exercise.cpp”