




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
5.11PointerstoFunctionspointerstofunctions:canbeassigned,placedinarrays,passedtofunctions,returnedbyfunctions,…example: #include<stdio.h> #include<string.h> #defineMAXLINES5000/*maxlinestobesorted*/ char*lineptr[MAXLINES];/*pointerstotextlines*/ intreadlines(char*lineptr[],intnlines);
voidqsort(void*lineptr[],intleft,intright,int(*comp)(void*,void*)); intnumcomp(char*,char*); /*sortinputlines*/5.11PointerstoFunctionsmain(intargc,char*argv[]) { intnlines; /*numberofinputlinesread*/ intnumeric=0; /*1ifnumericsort*/ if(argc>1&&strcmp(argv[1],“-n”)==0)numeric=1; if((nlines=readlines(lineptr,MAXLINES))>=0){ qsort((void**)lineptr,0,nlines-1,
(int(*)(void*,void*))(numeric?numcmp:strcmp)); writelines(lineptr,nlines); return0; }else{printf(“inputtoobigtosort\n”); return1; }}5.11PointerstoFunctions/*qsort:sortv[left]…v[right]intoincreasingorder*/voidqsort(void*v[],intleft,intright,int(*comp)(void*,void*)){ inti,last; voidswap(void*v[],intint); if(left>=right)return; swap(v,left,(left+right)/2); last=left; for(i=left+1;i<=right;i++)
if((*comp)(v[i],v[left])<0)swap(v,++last,i); swap(v,left,last); qsort(v,left,last-1,comp); qsort(v,last+1,right,comp);}voidswap(void*v[],inti,intj){ void*temp; temp=v[i]; v[i]=v[j]; v[j]=temp;}int*comp(void*,void*))intnumcmp(char*s1,char*s2){doublev1,v2;v1=atof(s1);v2=atof(s2);if(v1<v2)return-1;elseif(v1>v2)return1;elsereturn0;}5.12ComplicatedDeclarationsdeclarationstoworddescriptions:int*f();f:functionreturningpointertointint(*f)()f:pointertofunctionreturningintchar**argvargv:pointertopointertocharint(*daytab)[13]daytab:pointertoarray[13]ofintvoid*comp()comp:functionreturningpointertovoidvoid(*comp)()comp:pointertofunctionreturningvoidchar(*(*x())[])()x:functionreturningpointertoarray[]ofpointertofunctionreturningcharchar(*(*x[3])())[5]x:array[3]ofpointertofunctionreturningpointertoarray[5]ofchar5.12ComplicatedDeclarationsconvertCdeclarationtoaworddescription:grammar:example; (*pfa[])() ( * pfa [] ) () name dir-dcl dir-dcl dcl dir-dcl dir-dcl dcldcl: optional*’sdirect-dcldirect-dcl: name (dcl) direct-dcl() direct-dcl[optionalsize]5.12ComplicatedDeclarationsprogramsthatconvert: /*dcl:parseadeclarator*/ voiddcl(void) { intns; for(ns=0;gettoken()==‘*’;)ns++;/*count*’s*/ dirdcl(); while(ns-->0)strcat(out,“pointerto”); } /*dirdcl:parseadirectdeclaration*/5.12ComplicatedDeclarations voiddirdct(void) {inttype; if(tokentype==‘(‘){ dcl(); if(tokentype!=‘)’)printf(“error:missing)\n); }elseif(tokentype==NAME)strcpy(name,token); elseprintf(“error:expectednameor(dcl)\n”); while((type=gettoken())==PARENS||type==BRACKETS) if(type==PARENS)strcat(out,“functionreturning”); else{strcat(out,“array”); strcat(out,token); strcat(out,“of”); } }5.12ComplicatedDeclarations#include<stdio.h>#include<string.h>#include<ctype.h>#defineMAXTOKEN100enum{NAME,PARENS,BRACKETS};voiddcl(void);voiddirdcl(void);intgettoken(void);inttokentype; /*typeoflasttoken*/5.12ComplicatedDeclarationschartoken[MAXTOKEN];/*lasttokenstring*/charname[MAXTOKEN];/*identifiername*/chardatatype[MAXTOKEN];/*datatype=char,int,ect.*/charout[1000];/*outputstring*/main()/*convertdeclarationtowords*/{ while(gettoken()!=EOF){ /*1sttokenonline*/ strcpy(datatype,token); /*isthedatatype*/ out[0]=‘\0’; dcl(); if(tokentype!=‘\n’)printf(“syntaxerror\n”); printf(“%s:%s\n”,name,out,datatype); } return0;}5.12ComplicatedDeclarationsintgettoken(void) /*returnnexttoken*/{ intc,getch(void); voidungetch(int); char*p=token; while((c=getch())==‘‘||c==‘\t’); if(c==‘(‘){ if((c=getch())==‘)’){ strcpy(token,”()”); returntokentype=PARENS; }else{ungetch(c); returntokentype=‘(‘;} }5.12ComplicatedDeclarationselseif(c==‘[‘){ for(*p++=c;(*p++=getch())!=‘]’;); *p=‘\0’; returntokentype=BRACKETS; }elseif(isalpha(c)){ for(*p++=c;isalnum(c=getch());} *p++=c; *p=‘\0’; ungetch(c); returntokentype=NAME; }elsereturntokentype=c;}5.12ComplicatedDeclarationsundcl:x()*[]*()char==>char(*(*x())[])() /*undcl:convertworddescriptiontodeclaration*/main(){ inttype; chartemp[MAXTOKEN]; while(gettoken()!=EOF){ strcpy(out,token); while((type=gettoken())!=‘\n’) if((type==PARENS||type==BRACKETS) strcat(out,token); e
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024抚州职业技术学院辅导员招聘笔试真题
- 征求意见表2:文成县糯米山药种植与加工技术规程
- 2025年应急救生系统项目合作计划书
- Recycle 1(第1课时) 教案人教pep英语六年级上册
- 绘画语言及其在创作过程中的思想研究
- 6~9的加、减法第4课时 练一练 教案 2025人教版数学一年级上册
- 在班队活动中重塑“爱的教育”
- 2024年深圳市光明区水务局招聘专干真题
- 2025年江苏省第十届大学生就业创业知识竞赛考试练习题库(150题)【答案】
- 2025年江油市招聘属公费师范毕业生考试试题【答案】
- 幼儿园中班下家长会课件
- 2025北京市职业病防治院第二批招聘19人笔试参考题库附答案解析
- 2025年度上半年校园安全工作总结及下半年工作计划
- 美国博物馆向中方归还楚帛书
- 线下潮人项目活动策划与执行方案
- 仓库库存预警管理制度
- 景区吊桥设施管理制度
- 2025年高考数学全国新课标Ⅱ卷试卷评析及备考策略(课件)
- 供水水费收缴管理制度
- 房产中介店经营管理制度
- 《2025版防范电信网络诈骗宣传手册》专题讲座
评论
0/150
提交评论