2013-07-14 14:41:31
本章主要讨论如何根据伪代码编写程序,并用脚手架测试程序的正确性。
作者使用脚手架这个名词,刚开始看,真看不懂,估计是语言习惯的问题,看完这一章,才大致明白就是测试代码所搭建的程序。
书中的脚手架主要是采用assert进行测试,自己觉得很不习惯。
下面是自己写的二分搜索的递归以及非递归实现,并附上测试结果。
完整代码:
1 #include <iostream> 2 using namespace std; 3 4 #define SIZE 100 5 6 //递归implementation 7 //int _bsearch_recursive(const int a_unsorted[],size_t begin,size_t end,int x) //不要用size_t类型 8 int _bsearch_recursive(const int a_unsorted[],int begin,int end,int x) 9 { 10 //size_t mid = (begin + end)/2; 11 if (begin > end) 12 { 13 return -1; 14 } 15 16 int mid = (begin + end)/2; 17 18 if ( x == a_unsorted[mid] ) 19 { 20 return mid; 21 } 22 23 if ( x < a_unsorted[mid] ) 24 { 25 return _bsearch_recursive(a_unsorted,begin,mid - 1,x); 26 } 27 28 return _bsearch_recursive(a_unsorted,mid + 1,end,x); 29 } 30 31 int _bsearch_1(const int a_unsorted[],size_t n,int x) 32 { 33 if (NULL == a_unsorted || n <= 0) 34 { 35 cout<<"invalid input!"<<endl; 36 exit(0); 37 } 38 39 return _bsearch_recursive(a_unsorted,0,n - 1,x); 40 } 41 42 //非递归implementation 43 int _bsearch_2(const int a_unsorted[],size_t n,int x) 44 { 45 if (NULL == a_unsorted || n <= 0) 46 { 47 cout<<"invalid input!"<<endl; 48 exit(0); 49 } 50 51 int begin = 0; 52 int end = n - 1; 53 int mid = 0; 54 55 while (begin <= end) 56 { 57 mid = (begin + end)/2; 58 59 if (x == a_unsorted[mid]) 60 { 61 return mid; 62 } 63 64 /* if (begin == end) //不需要,在下一次循环中会因为不满足begin <= end而结束循环,返回-1 65 { 66 return -1; 67 } 68 */ 69 if (x < a_unsorted[mid]) 70 { 71 end = mid - 1; 72 } 73 74 begin = mid + 1; 75 } 76 77 return -1; 78 } 79 80 //测试程序,即“脚手架” 81 int main() 82 { 83 //int a_unsorted[SIZE] = {12,15,23,38, 48,49,52,67, 71,78,100}; 84 int a_unsorted[SIZE] = {12,15}; 85 int n = 2; 86 int x; 87 int pos; 88 89 //test _bsearch_2 90 cout<<"test _bsearch_1..."<<endl; 91 cout<<"please enter the number to find : "<<endl; 92 while (cin>>x) 93 { 94 cout<<"searching "<<x<<endl; 95 pos = _bsearch_1(a_unsorted,n,x); 96 if ( pos == -1) 97 { 98 cout<<x<<" is not found!"<<endl; 99 } 100 else 101 { 102 cout<<a_unsorted[pos]<<" is found"<<endl; 103 } 104 105 cout<<"please enter the number to find : "<<endl; 106 } 107 108 cin.clear(); 109 cin.sync(); 110 cout<<endl; 111 112 //test _bsearch_2 113 cout<<"test _bsearch_2..."<<endl; 114 cout<<"please enter the number to find : "<<endl; 115 while (cin>>x) 116 { 117 cout<<"searching "<<x<<endl; 118 pos = _bsearch_2(a_unsorted,n,x); 119 if ( pos == -1) 120 { 121 cout<<x<<" is not found!"<<endl; 122 } 123 else 124 { 125 cout<<a_unsorted[pos]<<" is found"<<endl; 126 } 127 128 cout<<"please enter the number to find : "<<endl; 129 } 130 131 return 0; 132 }
测试结果:
test _bsearch_1... please enter the number to find : 12 searching 12 12 is found please enter the number to find : 15 searching 15 15 is found please enter the number to find : 11 searching 11 11 is not found! please enter the number to find : 13 searching 13 13 is not found! please enter the number to find : 18 searching 18 18 is not found! please enter the number to find : ^Z test _bsearch_2... please enter the number to find : 12 searching 12 12 is found please enter the number to find : 15 searching 15 15 is found please enter the number to find : 11 searching 11 11 is not found! please enter the number to find : 17 searching 17 17 is not found! please enter the number to find : 13 searching 13 13 is not found! please enter the number to find : ^Z 请按任意键继续. . .