




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 体育赛事用帐篷购销合同
- 双方夫妻离婚协议书
- 柚子水果购销合同
- 软件和信息技术服务外包合作协议
- 离婚协议书去哪弄
- 环境监测技术设备供应协议
- 绿色出行服务平台合作协议
- 砂石场劳动合同
- 农产品电商运营推广合同
- 房产中介公司劳动合同
- 毛戈平-+毛戈平深度报告:再论毛戈平商业模式与核心壁垒:个人IP+化妆学校+线下服务
- 第二章美容手术的特点及其实施中的基本原则美容外科学概论讲解
- 山东省潍坊市2024-2025学年高三上学期1月期末考试生物试卷含答案
- 2025年“春训”学习心得体会例文(3篇)
- 中央2025年公安部部分直属事业单位招聘84人笔试历年参考题库附带答案详解
- 2025年春新外研版(三起)英语三年级下册课件 Unit4第1课时Startup
- 人教版(2025新版)七年级下册数学第七章 相交线与平行线 单元测试卷(含答案)
- 2025-2025学年度第二学期高二物理教学计划
- 幼儿园市级课一等奖-大班语言健康绘本《我的情绪小怪兽》有声绘本课件
- 地方足球协会管理制度
- 2025年度墓碑石材购销与墓碑定制服务合同3篇
评论
0/150
提交评论