一、實驗結論:
1. 二分查找:
補足程序ex1_1.cpp
// 練習:使用二分查找,在一組有序元素中查找數據項 // 形參是數組,實參是數組名 #include <stdio.h> const int N=5; int binarySearch(int x[], int n, int item); int main() {int a[N]={6,16,20,52,66};int i,index, key;printf("數組a中的數據:\n");for(i=0;i<N;i++)printf("%d ",a[i]);printf("\n");printf("輸入待查找的數據項: ");scanf("%d", &key);// 調用函數binarySearch()在數組a中查找指定數據項item,并返回查找結果給index // 補足代碼① // ××× index=binarySearch(a,N,key);if(index>=0) printf("%d在數組中,下標為%d\n", key, index);elseprintf("%d不在數組中\n", key); return 0; }//函數功能描述: //使用二分查找算法在數組x中查找特定值item,數組x大小為n // 如果找到,返回其下標 // 如果沒找到,返回-1 int binarySearch(int x[], int n, int item) {int low, high, mid;low = 0;high = n-1;while(low <= high) {mid = (low+high)/2;if (item == x[mid])return mid;else if(item<x[mid])high = mid - 1;elselow = mid + 1;}return -1; }
補足程序ex1_2.cpp
// 練習:使用二分查找,在一組有序元素中查找數據項 // 形參是指針變量,實參是數組名 #include <stdio.h> const int N=5; int binarySearch(int *x, int n, int item); int main() {int a[N]={3,4,5,15,26};int i,index, key;printf("數組a中的數據:\n");for(i=0;i<N;i++)printf("%d ",a[i]);printf("\n");printf("輸入待查找的數據項: ");scanf("%d", &key);// 調用函數binarySearch()在數組a中查找指定數據項item,并返回查找結果// 補足代碼①// ××× index=binarySearch(a, N, key);if(index>=0) printf("%d在數組中,下標為%d\n", key, index);elseprintf("%d不在數組中\n", key); return 0; }//函數功能描述: //使用二分查找算法在x指向的數據項開始的n個數據中,查找item // 如果找到,返回其位置 // 如果沒找到,返回-1 int binarySearch(int *x, int n, int item) {int low, high, mid;low = 0;high = n-1;while(low <= high) {mid = (low+high)/2;if (item == *(x+mid))return mid;else if(item < *(x+mid))high = mid - 1;elselow = mid + 1;}return -1; }
2. 選擇法排序
補足程序ex2_2.cpp
// 練習:使用選擇法對字符串按字典序排序 #include <stdio.h> #include <string.h> void selectSort(char str[][20], int n ); // 函數聲明,形參str是二維數組名 int main() {char name[][20] = {"Yang", "Hai", "Yi", "Huang", "Yu","Ting"};int i;printf("輸出初始名單:\n");for(i=0; i<6; i++)printf("%s\n", name[i]);selectSort(name, 6); // 調用選擇法對name數組中的字符串排序 printf("按字典序輸出名單:\n");for(i=0; i<6; i++)printf("%s\n", name[i]);return 0; } // 函數定義 // 函數功能描述:使用選擇法對二維數組str中的n個字符串按字典序排序 void selectSort(char str[][20], int n) {int i,j;char t[20];for(i=0;i<n-1;i++){for(j=0;j<n-i-1;j++){strcmp(str[j],str[j+1]);if(strcmp(str[j],str[j+1])>0){strcpy(t,str[j]);strcpy(str[j],str[j+1]);strcpy(str[j+1],t);}}}}
3. 用指針處理字符串
練習2(黃色為修改部分)
// 用指針變量處理字符串練習2 // 刪除中間和末尾的* (即除了前導*,刪除字符串中其它全部*) #include <stdio.h> void delStarButPrefix(char []); // 函數聲明(函數聲明中可以省略數組名不寫) int main() {char string[80];printf("輸入一個字符串:\n");gets(string);printf("\n刪除<中間和末尾的*>之前的字符串:\n");puts(string);delStarButPrefix(string); // 調用函數,刪除中間和末尾的*; 注意實參的寫法 printf("\n刪除<中間和末尾的*>之后的字符串:\n");puts(string);return 0; } // 函數定義 // 函數功能描述 // 刪除字符數組s中除了前導*以外的所有*(即刪除字符串中間和末尾出現的*) void delStarButPrefix(char s[]) {int i=0; // i用于記錄字符在字符數組s中的下標 char *p = s;// 跳過前導*,i記錄字符在字符數組s中的下標,p記錄首個非*字符的位置 while(*p == '*') { //while(*p && *p == '*') 不太懂什么意思 就去掉了*p p++;i++;}// 從p指向的字符開始,把遇到的*刪除 while(*p) {if(*p != '*') {s[i] = *p;i++;p++;}elsep++; //開始不太懂p++不在if里面 就分開寫了 } s[i] = '\0'; // *p=0是就結束循環了 s[i]沒有被賦結束符號 }
練習1、3未改動
?
二、實驗總結:
1.數組名作為參數 vs. 指針變量作為參數在形參、實參寫法
?數組名作為函數參數,傳遞的值是地址。
?case1: 實參—數組名,形參—數組名?
… void f( int [] , int ); int main() {int a[5];…f(a,5);… } void f( int x[], int n ){… }
case2: 實參—數組名,形參——指針變量
… void f (int *, int ); int main() {int a[10];…f(a,10);… } void f (int *p, int n ) {… }
case3: 實參—指針變量,形參—數組名
… void f( int [] , int ); int main() {int a[5], *p;p = a;…f(p,5);… } void f( int x[], int n ){… }
case4: 實參—指針變量,形參—指針變量
… void f( int [] , int ); int main() {int a[5], *p;p = a;…f(p,5);… } void f( int *q, int n ){… }
?
2.常用指針定義小結
3.使用選擇法對字符串排序時注意事項:
? 注意字符串的比較和賦值,不能直接使用關系運算符和賦值運算符,要借助字符串處理函數。?
4.使用指針變量對字符串進行處理?
?