1 / 22

程序设计导论 —— 第 26 讲

程序设计导论 —— 第 26 讲. 补充内容以及重点内容复习 (2). freopen 函数. FILE *freopen( const char *path, const char *mode, FILE *stream ); 使用头文件 stdio.h path: 文件名 mode: 文件打开的模式(如 r, w ) stream: 一个文件,通常使用标准流文件( stdin, stdout 等) 实现输入输出重定向,把预定义的几个标准流文件 (stdin, stdout) 定向到由 path 指定的文件中. freopen 函数举例.

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讲 补充内容以及重点内容复习(2)

  2. freopen函数 • FILE *freopen( const char *path, const char *mode, FILE *stream ); • 使用头文件stdio.h • path: 文件名 • mode: 文件打开的模式(如r, w) • stream: 一个文件,通常使用标准流文件(stdin, stdout等) • 实现输入输出重定向,把预定义的几个标准流文件(stdin, stdout)定向到由path指定的文件中

  3. freopen函数举例 • freopen(“data.in”, “r”, stdin); • 把标准输入定向到文件data.in中 • freopen(“data.out”, “w”, stdout); • 把标准输出定向到文件data.out中 • 调试程序时不用每次都输入数据,将输入数据保存在data.in中即可 • 提交系统上的题目以后将逐步公开测试数据 • 可以用这种方式方便的调试程序

  4. #include <stdio.h> #include <string.h> int main() { freopen(“data.in”, “r”, stdin); //运行之后scanf从data.in中读入 freopen(“data.out”, “w”, stdout); //运行之后printf输出到data.out中 char str[600]; char s[]="RUC"; int i, j=0; scanf("%s", str); printf("%d", strlen(str)); for (i=0; i<strlen(str); i++) { if (s[j]==str[i]) j++; if (j==3) break; } if (j==3) printf("YES"); else printf("NO"); return 0; } 例:寻找RUC一题源代码

  5. 复习内容 • 指针 • 指针的定义 • 指针的使用 • 指向结构的指针 • 指向数组元素的指针 • 指向数组的指针 • 函数相关 • 函数的参数传递 • 数组作为函数的参数 • 函数指针的定义、使用 • 函数指针作为参数传递

  6. 指针的定义 • 定义一个指针变量 • 类型*指针变量名= 指针初值; • 类型是指针指向的变量的类型 • int *pNum; • float *p; • char *str; • int *pNumArray[10]; //指针数组

  7. 10 &a a &p p 指针相关的操作符 *p • & :求地址运算符 • * :间接引用操作符 int a=10, *p; p = &a; 即: (1)*p 与 a等价,都代表变量a的存储单元 (2)p 与 &a都是变量a的地址 &a

  8. 指针的使用 #include <iostream>//预编译命令 using namespace std; struct MyPoint { //定义结构类型MyPoint; float x, y; }; int main() { MyPoint point={2.0, 3.0}; MyPoint *pPoint; pPoint = &point; //指针pPoint指向结构变量point cout << "The point is ("; //输出提示信息 cout << pPoint->x << "," << (*pPoint).y<< ")" << endl; return 0; } • 指向结构的指针

  9. 指向数组元素的指针 • 当指针指向数组的首元素时,指针可等同数组变量使用。 • 数组变量本身的值是地址 • 数组是一组同类型的变量在内存中顺序存放 int scores[ MAX ]; • 数组变量代表数组整体占用的内存空间 sizeof(scores) 相等于 sizeof(int)*MAX • 数组变量代表数组的起始内存地址 • scores 等价于 &scores[0] • int *pscores; pscores = scores 等价于 pscores = &scores[0] • *scores 等价于scores[0] • 不能给数组变量赋值 scores = ……

  10. #include <iostream>//预编译命令 using namespace std; #define MAXNUM 10 int main() { int scores[MAXNUM]={90,91,92,93,94,95,96,97,98,99}; int *p1 = scores; int *p2, *p3; p2 = scores; p3 = scores; for (int i=0; i<MAXNUM; i++) //利用指针输出数组元素 { cout << p1[i] << " " << *p2 << " " << *(p3+i) << endl; p2++; //指针的加法运算 } cout << endl; return 0; }

  11. 指针的加减法运算 • 指针的加减法运算不是简单的地址值的加减法 • (pscore+1) 的值是&pscore[1] • *(pscore+1) 等价于pscore[1] • *(scores+i) 等价于scores[i] // 假定i整数变量 指针的加减法是考虑了类型大小的地址加减法 • 指针变量可以用指针运算的结果赋值 pscore = scores; pscore = pscore+1; pscore++; • 上述三条语句执行后*pscore 与scores[2] 等价

  12. 指针的加减法运算 • 两个指针变量相加是无意义的 float* pscore1 = scores; float* pscore2 = scores+1; pscore1 + pscore2; // 无意义 • 两个同类型指针变量相减是其间的元素数目 (pscore2 - pscore1)==1; // 值为真 • 两个同类型指针变量可比较大小 (pscore2 > pscore1); // 值为真 (pscore2 < pscore1); // 值为假 pscore1 == scores; // 两个指针变量所指的地址相同

  13. 指向数组的指针 #include <iostream>//预编译命令 using namespace std; int main() { int roomscores[3][3]={{90,91,92}, {80,81,82},{70,71,72}}; int (*p1)[3]; //定义一个指向数组的指针 p1 = roomscores; //等价于 p2 = &roomscores[0]; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) cout << (*p1)[j] << " "; cout << endl; p1++; } return 0; } 90 91 92 80 81 82 70 71 72

  14. 数组作为函数的参数 • 以地址方式传递参数 void myFunction( char str[], int array[], int length) { printf("string in myFunction: %s \n", str); if (strlen(str)>10) strcpy(str, "new string"); else strcpy(str, " "); for(int i=0; i<length; i++) array[i]=0; return; }

  15. 函数指针的定义 • 返回值类型 (*函数指针名)(参数表); • 例1: int square(int); int (*f)(int)=square; • 例2: void f1(int); void f2(int); void f3(int); void (*f[3])(int)={f1,f2,f3};

  16. 函数指针的使用 #include <iostream> int square(int x){cout << x; return 0;}; int (*f)(int)=square; void f1(int x){cout <<1 <<endl;}; void f2(int x){cout <<2 <<endl;}; void f3(int x){cout <<3 <<endl;}; void (*fp[3])(int)={f1,f2,f3}; void main(){ (*f)(0); (*fp[0])(1); (*fp[1])(2); (*fp[2])(3); }

  17. 函数指针作为参数传递 int sum(int a,int b,int (*term)(int)){ if(a>b) return 0; return (*term)(a) + sum(a+1,b,term); } int term(int a){ return a ; } void main(){ cout << sum(69, 90, term) << endl; int s=0; for(int i=69;i<=90;i++) s += i ; cout << s << endl; }

  18. qsort函数 • void qsort(void* base, size_t n, size_t size, int (*cmp)(const void*, const void*)) • base为要排序的数组 • n为要排序数组的长度 • size为数组元素的大小(以字节为单位) • cmp为判断大小函数的指针 • 这个函数需要自己定义, 如果a>b,函数返回1;a<b,函数返回-1;a=b,函数返回0

  19. #include <iostream>//预编译命令 #include <time.h> #include <stdlib.h> using namespace std; int compare(const void *a, const void *b); int main(){ int ary[20], i; srand((unsigned)time(NULL)); //随机产生20个数 for (i=0; i<20; i++) ary[i]=rand(); for (i=0; i<20; i++) //输出20个数 printf("%6d ", ary[i]); printf("\n\n"); qsort((void*)ary, 20, sizeof(int), compare ); //排序 for (i=0; i<10; i++) //输出排序后的20个数 printf("%6d ", ary[i]); printf("\n"); return 0; }

  20. int compare(const void *a, const void *b) { if ( *(int*)a > *(int*)b ) return 1; else if ( *(int*)a == *(int*)b ) return 0; else return -1; } 整数比较

  21. 字符串比较: int compare(const void *a, const void *b) { char *p, *q; p = (char *)a; q = (char *)b; return strcmp(p, q); }

  22. 结 束

More Related