




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 挖掘機(jī)買賣合同(20篇)
- 2025年廣州市勞動合同范文(19篇)
- 工程施工事故責(zé)任協(xié)議
- 員工挑戰(zhàn)協(xié)議書范本
- 員工雇傭協(xié)議合同填寫模板
- 商品歷史銷售合同協(xié)議
- 民宿水電維修合同協(xié)議
- 戀愛協(xié)議結(jié)婚協(xié)議書范本
- 商業(yè)土地轉(zhuǎn)租合同協(xié)議
- 櫥柜項(xiàng)目改造合同協(xié)議
- 腸外營養(yǎng)中電解質(zhì)補(bǔ)充中國專家共識(2024版)解讀
- 第六單元 有余數(shù)的除法測試卷(含答案)2024-2025學(xué)年二年級數(shù)學(xué)下冊人教版
- JJF 2186-2025激光多普勒流速儀校準(zhǔn)規(guī)范
- 銀行員工消保知識培訓(xùn)
- 100以內(nèi)加法減法口算1000題家庭練習(xí)打印
- 《鐵路線路工》 課件 項(xiàng)目七 養(yǎng)路機(jī)械
- 電梯的應(yīng)急預(yù)案培訓(xùn)
- GB/T 45166-2024無損檢測紅外熱成像檢測總則
- 某海上平臺的油氣集輸工藝設(shè)計(jì)20000字【論文】
- 脫硝催化劑環(huán)境影響評估-洞察分析
- 白細(xì)胞疾病及其檢驗(yàn)(血液學(xué)檢驗(yàn)課件)
評論
0/150
提交評論