![C語(yǔ)言中的指針和遞歸函數(shù)_第1頁(yè)](http://file4.renrendoc.com/view/51a0651281df3a5c08b8aa3df9a31d32/51a0651281df3a5c08b8aa3df9a31d321.gif)
![C語(yǔ)言中的指針和遞歸函數(shù)_第2頁(yè)](http://file4.renrendoc.com/view/51a0651281df3a5c08b8aa3df9a31d32/51a0651281df3a5c08b8aa3df9a31d322.gif)
![C語(yǔ)言中的指針和遞歸函數(shù)_第3頁(yè)](http://file4.renrendoc.com/view/51a0651281df3a5c08b8aa3df9a31d32/51a0651281df3a5c08b8aa3df9a31d323.gif)
![C語(yǔ)言中的指針和遞歸函數(shù)_第4頁(yè)](http://file4.renrendoc.com/view/51a0651281df3a5c08b8aa3df9a31d32/51a0651281df3a5c08b8aa3df9a31d324.gif)
![C語(yǔ)言中的指針和遞歸函數(shù)_第5頁(yè)](http://file4.renrendoc.com/view/51a0651281df3a5c08b8aa3df9a31d32/51a0651281df3a5c08b8aa3df9a31d325.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
RecursiveRecursion,aprocedureorsubroutine,implementedinaprogramminglanguage,whoseimplementationreferencesitself.Theimportanceoftherecursive1Sometimesrecursionhelpsyoutodesignsimplerandmorereadablecode.2Theadvantageisthatyoudonothavetopreservestateoneachiteration.3Itisespeciallyrelevantforrecursivedatastructures(liketrees)orrecursivealgorithms.UsingRecursiveinC:
Followingisthesourcecodeforafunctioncalledfactorial().Thisfunctiontakesoneparameter
numberandreturnsthenumberfactorial:
一下是factorial()調(diào)用的函數(shù)的源代碼。該函數(shù)有一個(gè)參數(shù)number,并返回number的階乘。#include"stdio.h"http://functionreturningthedata'sfactorial.intfactorial(intnumber)//factorial(5)=5*4*3*2*1{ if(number==1)
return1; else
returnnumber*factorial(number-1);}intfactorial(intnumber);//FunctionDeclarationvoidmain(void){ inti;//localvariabledeclaration printf("Pleaseinputaninteger=>"); scanf("%d",&i); printf("Theinteger'sfactorial=>%d",factorial(i));
//callingthe'factorial'functiontogetaresult. getchar();getchar();}CPointers
Apointerisavariablewhosevalueistheaddressofanothervariable.Likeanyvariableorconstant,youmustdeclareapointerbeforeyoucanworkwithit.Thegeneralformofapointervariabledeclarationis:
type*var-name;
指針是一個(gè)變量,它的值是另一個(gè)變量的地址。像任何變量或常量,你必須聲明一個(gè)指針之前,你可以使用它。一個(gè)指針變量聲明的一般形式是:類型*VAR-名稱;TheimportanceofthepointerSomeCtasksareperformedmoreeasilywithpointers,andotherCtasks,suchasdynamicmemoryallocation,cannotbeperformedwithoutthem.UsingPointersinC:#include"stdafx.h"voidmain(){ inta,*ptr;
//variabledeclaration,pointervariable
ptr=&a;
//storeaddressofainpointervariable
printf("Pleaseinputaninteger=>");
scanf("%d",&(*ptr));//=scanf("%d",ptr);//printtheaddressstoredinippointervariable printf("theintegeraddress:%p\n",ptr);
//accessthevalueattheaddressavailableinpointer printf("a=%d",*ptr);}UsingPointersandRecursiveFunctioninC//calculate:sum=n+n-1+n-2+...+1//calculateUserinputthemaxnumber'saccumulate.#include"stdafx.h"#defineMAX(a,b)(a>b?a:b)//functionreturningthemaxbetweentwonumbersintassignment(int*p,int*q);//FunctionDeclarationintsum(intm);//FunctionDeclarationvoidmain(void){ intnumber1,number2;//localvariabledeclaration printf("Pleaseinputtwointegers=>"); printf("theMaxintegeraccumulatesum:%d",sum(assignment(&number1,&number2)));
//first:calling"assignment"functiontogetaMaxvalue.
//second:calling"sum"functiontogetresult.}intassignment(int*p,int*q)//functionreturningtheUserinputMaxdata.{ scanf("%d",&(*p));//&(*p)=p scanf("%d",&(*q));//&(*q)=q returnMAX(*p,*q);//callingMAXfunctionandret
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- Unit3 Weather A let's learn(說課稿)-2023-2024學(xué)年人教PEP版英語(yǔ)四年級(jí)下冊(cè)001
- 2025寫場(chǎng)地租賃合同范文
- 2025工程建設(shè)招標(biāo)投標(biāo)合同履約銀行保證書
- Unit 1 Playtime Lesson 3(說課稿)-2023-2024學(xué)年人教新起點(diǎn)版英語(yǔ)二年級(jí)下冊(cè)
- 2023九年級(jí)歷史下冊(cè) 第一單元 殖民地人民的反抗與資本主義制度的擴(kuò)展第3課 美國(guó)內(nèi)戰(zhàn)說課稿 新人教版
- 2025泵車租賃合同
- 2024-2025學(xué)年高中歷史 專題二 近代中國(guó)資本主義的曲折發(fā)展 2.1 近代中國(guó)民族工業(yè)的興起說課稿1 人民版必修2
- 蔬菜物資發(fā)放方案
- 養(yǎng)生館前臺(tái)合同范例
- 代理經(jīng)營(yíng)店鋪合同范例
- 教學(xué)的模樣讀書分享
- 老年髖部骨折患者圍術(shù)期下肢深靜脈血栓基礎(chǔ)預(yù)防專家共識(shí)(2024版)解讀 課件
- 江蘇省無錫市2024年中考語(yǔ)文試卷【附答案】
- 五年級(jí)上冊(cè)小數(shù)脫式計(jì)算200道及答案
- 2024年秋新滬科版物理八年級(jí)上冊(cè) 第二節(jié) 測(cè)量:物體的質(zhì)量 教學(xué)課件
- 直播帶貨基本操作流程(直播帶貨流程完整版)
- 2024義務(wù)教育英語(yǔ)課程標(biāo)準(zhǔn)2022版考試題庫(kù)附含答案
- 多旋翼無人機(jī)駕駛員執(zhí)照(CAAC)備考試題庫(kù)大全-下部分
- 浙教版七年級(jí)上冊(cè)數(shù)學(xué)第4章代數(shù)式單元測(cè)試卷(含答案)
- 七年級(jí)下冊(cè)第六章《人體生命活動(dòng)的調(diào)節(jié)》作業(yè)設(shè)計(jì)
- 特種設(shè)備使用單位日管控、周排查、月調(diào)度示范表
評(píng)論
0/150
提交評(píng)論