1 / 17

程序设计导论 —— 第 26 讲

程序设计导论 —— 第 26 讲. 复习( 1 ) ——Debug. 重点内容复习 (4). 程序调试 查找字符串 子函数调用 快速排序 编写程序时遇到的问题及解决方法. 查找字符串. 从命令行读入一行字符串,并查找其中是否包含某个特定的子串,如果是则输出整个字符串,不是则输出“ Not Matched!”. 从命令行读入一行字符串. int getline(char s[], int lim) { int i=0; int c; while(--lim &gt;0 &amp;&amp; (c=getchar())!=EOF &amp;&amp; c!='<br>') s[i++]=c;

yetta-owen
Télécharger la présentation

程序设计导论 —— 第 26 讲

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. 程序设计导论——第26讲 复习(1)——Debug

  2. 重点内容复习(4) • 程序调试 • 查找字符串 • 子函数调用 • 快速排序 • 编写程序时遇到的问题及解决方法

  3. 查找字符串 • 从命令行读入一行字符串,并查找其中是否包含某个特定的子串,如果是则输出整个字符串,不是则输出“Not Matched!”

  4. 从命令行读入一行字符串 int getline(char s[], int lim) { int i=0; int c; while(--lim >0 && (c=getchar())!=EOF && c!='\n') s[i++]=c; return i; }

  5. 在一个字符串中查找目标串 int strindex(char s[], char t[]) { int i, j, k; int slen = strlen(s); int tlen = strlen(t); for (i=0; i<slen; i++) { for (j=i, k=0; s[j]==t[k]; j++, k++); if(k==tlen) return i; } return -1; }

  6. #include <iostream> using namespace std; #define MAXLINE 1000 int getline(char line[], int max); int strindex(char source[], char searchfor[]); char pattern[]="cpp"; int main() { char line[MAXLINE]; while( getline(line, MAXLINE)>0) { if (!strcmp(line, "q")) break; if (strindex(line, pattern)>=0) cout << line << endl; else cout << "Not Matched!" << endl; } return 0; }

  7. 快速排序 • 递归问题 • 回忆课本P108快速排序的思路 • 原理: • 首先根据第一个元素p将数组分成2个子列,其中一部分元素都小于p,另一部分都大于p; • 然后再对这两个子列按照同样的方法递归地进行划分,直到每个子列只含有一个元素为止。

  8. 分区处理 5<7 5>2 5>4 k 5<6 5>3 5>1 y z

  9. 分区处理的方法2 • 选一个随机元素作为基准值,并将它临时性地交换到最前边,下标0; • 扫描其他元素(i:1~n-1),把小于基准值的元素(小的)向前面交换(到位置last),大的元素向后面交换(到位置i)

  10. 在这个过程开始时,基准值被放到数组的最前端,last = 0且从i=1到n-1的元素都还没有检查,这时的状态是 • 从1到last的元素严格地小于基准值,从last+1到i- 1的元素大于或等于基准值,而从i到n-1的元素至今还没有检查过

  11. 在所有元素都划分完毕后,接着交换位置0的元素与last处的元素,把基准值放到它的最终位置,这样就维护了正确的顺序。现在数组的样子变成了在所有元素都划分完毕后,接着交换位置0的元素与last处的元素,把基准值放到它的最终位置,这样就维护了正确的顺序。现在数组的样子变成了

  12. last i 分区过程 2<5 7>5 6>5 3<5 4<5 1<5

  13. #include <iostream> using namespace std; void quicksort(int v[], int left, int right); void swap(int v[], int i, int j); int arr[]={4,6,2,3,7,8,9}; int main() { quicksort(arr, 0, 6); for (int i=0; i<=6; i++) cout << arr[i] << " "; cout << endl; return 0; }

  14. void quicksort(int v[], int left, int right) { int i, last; //分区处理 swap(v, left, (left+right)/2); last = left; for (i=left+1; i<=right; i++) if (v[i]<v[left]) swap(v, last++, i); swap(v, left, last); //递归处理 quicksort(v, left, last-1); quicksort(v, last+1, right); }

  15. void swap(int v[], int i, int j) { int temp; temp = v[i]; v[i] = v[j]; v[j] = temp; } 上述程序有几处错误,通过调试来发现错误!

  16. A sort( z,y ) z>=y z<y B C 不做事 D E F 分区处理 sort(z,m-1) sort(m+1,y) 快速排序的与或图:

  17. 编写程序时遇到的问题及解决方法 • 常见错误 • If(表达式) 表达式的写法 • “== ” 与“=” • #include < > • { }的匹配 • 变量的作用域 • 变量的名字,max, min, pow • 二维数组的行、列 • 笔误

More Related