




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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屆江西省九江市高二下化學期末教學質量檢測模擬試題含解析
- 2024年黑龍江省公安廳招聘警務輔助人員考試真題
- 景區吊橋設施管理制度
- 2025年高考數學全國新課標Ⅱ卷試卷評析及備考策略(課件)
- 2025-2030年中國寫字樓行業市場深度調研及前景趨勢與投資研究報告
- 2025-2030中國壓縮空氣儲能產業現狀調查及項目投資策略建議報告
- 棗莊機場建設投資有限公司招聘筆試真題2024
- 【伊春】2025年黑龍江伊春市紀委監委所屬事業單位公開招聘工作人員57人筆試歷年典型考題及考點剖析附帶答案詳解
- 山東省淄博市臨淄中學2024-2025學年高二下學期期末考前綜合訓練政治試題二(含答案)
- 2024-2025學年湘美版一年級下冊美術期末考試卷及參考答案 (三套)
- T/CPFIA 0011-2024磷石膏無害化處理指南(試行)
- 動車組受電弓途中故障應急處理于正航00課件
評論
0/150
提交評論