




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 承包三證齊全出租車合同
- 潤滑油購銷合同
- 工傷死亡一次性賠償協議書范本
- 買賣車輛協議書本范本
- 2025年輔導教師勞動合同
- 合伙開發石場協議書
- 2025年03月國家藥品監督管理局醫療器械技術審評中心合同制人員公開招聘2人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 高級系統架構設計師-2021年下半年《系統架構設計師》真題
- 廣東以色列理工學院《基礎和聲(二)》2023-2024學年第二學期期末試卷
- 山東英才學院《書法》2023-2024學年第一學期期末試卷
- 高低壓配電安全知識講座
- 《有機磷農藥中毒》課件
- 大地保險公司管理制度
- 幼兒園公開課:大班語言《相反國》課件(優化版)
- 2022版煤礦安全規程解讀
- 中國變應性鼻炎診斷和治療指南(2022版)解讀
- 組合電器(gis)設備解體大修作業指導書
- 復變函數與積分變換-西北工業大學中國大學mooc課后章節答案期末考試題庫2023年
- SAP各模塊常用表清單
- 天然氣管道置換記錄表
- 護士單人心肺復蘇技術操作考核評分標準
評論
0/150
提交評論