freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

c程序設(shè)計(jì)語(yǔ)言揣錦華第2章函數(shù)-資料下載頁(yè)

2025-01-12 15:42本頁(yè)面
  

【正文】 double small(double x,double y) { return xy?x:y; } 考察以上兩個(gè)函數(shù),有如下特點(diǎn):只有參數(shù)類型不同,返回值類型不同,功能則完全一樣。類似這樣的情況,可以使用函數(shù)模板,從而避免函數(shù)體的重復(fù)定義。 第 2章 函數(shù) 函數(shù)模板可以用來(lái)創(chuàng)建一個(gè)通用功能的函數(shù) , 以支持多種不同形參 , 簡(jiǎn)化重載函數(shù)的函數(shù)體設(shè)計(jì) 。 它的最大特點(diǎn)是把函數(shù)所使用的數(shù)據(jù)類型作為參數(shù) 。 函數(shù)模板的定義形式如下: template typename標(biāo)識(shí)符 函數(shù)定義 第 2章 函數(shù) 【 例 213】 定義一個(gè)能交換兩個(gè)變量值的函數(shù) , 要求用模板函數(shù)實(shí)現(xiàn) 。 include template typename T void swap(T amp。x,T amp。y) { T z。 z=y。y=x。x=z。 } void main( ) 第 2章 函數(shù) { int m=1,n=8。 double u= ,v=。 coutm=m n=nendl。 coutu=u v=vendl。 swap(m,n)。 //整型 swap(u,v)。 //雙精度型 coutm與 n, u與 v交換以后: endl。 coutm=m n=nendl。 coutu=u v=vendl。 } 第 2章 函數(shù) 程序運(yùn)行結(jié)果為 m=1 n=8 u= v= m與 n, u與 v交換以后: m=8 n=1 u= v= 分析:編譯系統(tǒng)從調(diào)用 swap( )時(shí) , 根據(jù)實(shí)參的類型推導(dǎo)出函數(shù)模板的類型參數(shù) 。 第 2章 函數(shù) 于調(diào)用 swap(m, n), 由于實(shí)參 m及 n為 int類型 , 所以 , 推導(dǎo)出模板中類型參數(shù) T為 int。 當(dāng)類型參數(shù)的含義確定后 , 編譯器將以函數(shù)模板為樣板生成一個(gè)函數(shù): int swap(int amp。x,int amp。y) { int z; z=y; y=x; x=z; } 同樣,對(duì)于調(diào)用 swap(u, v),由于實(shí)參 u、 v為double類型,所以,推導(dǎo)出模板中類型參數(shù) T為 double。 第 2章 函數(shù) 接著 , 編譯器將以函數(shù)模板為樣板 , 生成如下函數(shù): double swap(double amp。x,double amp。y) { double z; z=y; y=x; x=z; } 因此 , 當(dāng)主函數(shù)第一次調(diào)用 swap( )時(shí) , 執(zhí)行的實(shí)際上是由函數(shù)模板生成的函數(shù) : int swap( int amp。x,int amp。y) 當(dāng)主函數(shù)第二次調(diào)用 swap( )時(shí) , 執(zhí)行的實(shí)際上是由函數(shù)模板生成的函數(shù) : double swap( double amp。x,double amp。y) 第 2章 函數(shù) 使用 C++系統(tǒng)函數(shù) C++不僅允許我們根據(jù)需要自定義函數(shù) , 而且 C++的系統(tǒng)庫(kù)中還提供了幾百個(gè)常用函數(shù)可供程序員使用 。如求平方根函數(shù) (sqrt)、 求浮點(diǎn)數(shù)或雙精度數(shù)的絕對(duì)值函數(shù) (fabs)、 對(duì)數(shù)函數(shù) (log)、 指數(shù)函數(shù) (exp)、 三角函數(shù)等都屬于數(shù)學(xué)函數(shù) 。 輸入 /輸出格式控制函數(shù)有 setw( )、setprecision( )等 。 第 2章 函數(shù) 由前面已學(xué)習(xí)的知識(shí)可知,調(diào)用函數(shù)之前必須先聲明函數(shù)原型。系統(tǒng)函數(shù)的原型聲明已經(jīng)全部由系統(tǒng)提供了,并且已分類存在于不同的頭文件中。程序員需要做的事情,就是用 include指令嵌入相應(yīng)的頭文件,然后便可以使用系統(tǒng)函數(shù)了。例如,要使用基本輸入 /輸出流函數(shù) cin( )/cout( )函數(shù),就必須嵌入頭文件。 第 2章 函數(shù) 若要使用數(shù)學(xué)函數(shù),如求絕對(duì)值函數(shù) abs( )、 fabs( ),三角函數(shù) sin( )、 cos( )、 tan( ),開(kāi)平方函數(shù) sqrt( ),對(duì)數(shù)值函數(shù) log( ),以 e為底的指數(shù)函數(shù) exp( ),就要嵌入頭文件 。同樣,要使用輸入 /輸出格式控制函數(shù)setw( )/setprecision( ),就要嵌入頭文件 。 第 2章 函數(shù) 【 例 214】 系統(tǒng)函數(shù)應(yīng)用例題 。 從鍵盤輸入一個(gè)角度值 , 求出該角度的正弦值 、余弦值和正切值 。 分析:系統(tǒng)函數(shù)中提供了求正弦值 、 余弦值和正切值的函數(shù) —— sin( )、 cos( )及 tan( )函數(shù)的說(shuō)明在頭文件 。 同樣 , 系統(tǒng)函數(shù)中也提供了輸入 /輸出格式控制函數(shù) , 如輸出域?qū)捒刂坪瘮?shù) setw( )、 輸出精度控制函數(shù) setprecision( ), 函數(shù)的說(shuō)明在頭文件 。因此 , 需要用到這些系統(tǒng)函數(shù)時(shí) , 就必須將該函數(shù)所屬的頭文件以 include 頭文件名 或 include“頭文件名 ” 的形式寫在程序代碼開(kāi)始部分 。 第 2章 函數(shù) 程序代碼如下: include include include const double pi=。 void main( ) { double a,b。 cina。 b=a*pi/180。 第 2章 函數(shù) coutsin(a)=setw(10)sin(b)endl。 coutcos(a)=setw(10)cos(b)endl。 couttan(a)=setw(10)tan(b)endl。 } 程序運(yùn)行結(jié)果為 輸入: 30 sin(30)= cos(30)= tan(30)= 第 2章 函數(shù) 充分利用系統(tǒng)函數(shù) , 可以大大減少編程的工作量 ,提高程序的運(yùn)行效率和可靠性 。 要使用系統(tǒng)函數(shù) , 應(yīng)該注意以下兩點(diǎn): ① 了解所使用的 C++開(kāi)發(fā)環(huán)境提供了哪些系統(tǒng)函數(shù)。不同的編譯系統(tǒng)提供的系統(tǒng)函數(shù)有所不同。 ② 確定要使用的系統(tǒng)函數(shù)的聲明在哪個(gè)頭文件中。這也可以在庫(kù)函數(shù)參考手冊(cè)或聯(lián)機(jī)幫助中查到。 第 2章 函數(shù) 例如,在 MSDN Library Visual Studio VC++:首先在“活動(dòng)子集”欄選擇 Visual C++ Documentation,然后按如下路徑選擇:Visual C++ Documentation → Using Visual C++ → Visual C++ Programmer39。s Guide → Run Time Library Reference → Run Time Routines by Category → Run Time Routines byCategory,如圖 22。該幫助系統(tǒng)中將函數(shù)按如下分類列出: 第 2章 函數(shù) 獲取參數(shù) ( Argument access) 浮點(diǎn)支持 ( Floatingpoint support) 緩沖區(qū)操作 ( Buffer manipulation) 輸入與輸出 ( Input and output) 字節(jié)分類 ( Byte classification) 國(guó)際化 ( Internationalization) 字符分類 ( Character classification) 內(nèi)存分配 ( Memory allocation) 數(shù)據(jù)轉(zhuǎn)換 (Data conversion) 第 2章 函數(shù) 處理機(jī)與環(huán)境控制 ( Process and environment control) 調(diào)試 ( Debug) 查找與排序 ( Searching and sorting) 目錄控制 ( Directory control) 字符串操作 ( String manipulation) 錯(cuò)誤處理 ( Error handling) 系統(tǒng)調(diào)用 ( System calls) 異常處理 ( Exception handling) 時(shí)間管理 ( Time management) 文件處理 ( File handling) 第 2章 函數(shù) 圖 22 MSDN Library Visual Studio
點(diǎn)擊復(fù)制文檔內(nèi)容
環(huán)評(píng)公示相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1