動態(tài)內(nèi)存分配一_第1頁
動態(tài)內(nèi)存分配一_第2頁
動態(tài)內(nèi)存分配一_第3頁
動態(tài)內(nèi)存分配一_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗十八 動態(tài)內(nèi)存分配(一)一、實(shí)驗?zāi)康? 掌握使用模板函數(shù)利用矩形法求積分1 聯(lián)系使用動態(tài)內(nèi)存分配方式實(shí)現(xiàn)字符串類mystring二、 實(shí)驗內(nèi)容1. 設(shè)計梯形積分法函數(shù)模板,求函數(shù)fx=x5+6x3+x2+9在區(qū)間0.1, 3.4的積分。#include<iostream>using namespace std;class Fpublic:double fun(double x)return (x*x*x*x*x+6*x*x*x+x*x+9);template<typename T>double integer(T f,double a,double b,int n)

2、double result,step;step=(b-a)/n;result=(f.fun(a)+f.fun(b)/2;for(int i=1;i<n;i+)result+=f.fun(a+i*step);result*=step;return result;int main()F f;double result0;int n=1000;result0=integer(f,0.1,3.4,n);cout<<"函數(shù)f(x)=x*x*x*x*x+6*x*x*x+x*x+9在區(qū)間0.1, 3.4的積分為"<<result0<<endl;r

3、eturn 0;2、仿照標(biāo)準(zhǔn)字符串類string,采用動態(tài)字符數(shù)組,改造自定義字符串類mystring,可自動適應(yīng)不同的串長度。要求新的自定義字符串類mystring如下:const int MAXSIZE = 100;class mystringchar* str; /指向存儲字符串空間的指針int maxsize; /存儲字符串空間的大小int last; /已用的存儲空間大小public:mystring(int=MAXSIZE);mystring(char *s, int=MAXSIZE); /要考慮串長mystring(mystring & st); /要考慮串長mystri

4、ng();void show(); /輸出字符串int strlen(); /字符串長度void reverse(); /反轉(zhuǎn)字符串int strchr(char c); /查找字符c首次出現(xiàn)的位置;要求:缺省情況下字符串動態(tài)存儲空間的大小為MAXSIZE;拷貝構(gòu)造函數(shù)中,maxsize的大小由參數(shù)st確定;將mystring類定義在頭文件mystring.h中,在源文件mystring.cpp中實(shí)現(xiàn)類的函數(shù)定義在主函數(shù)所在源文件中引用mystring.h,并對mystring類進(jìn)行測試#include<iostream>#include<cstring>#includ

5、e "mystring.h"using namespace std;mystring:mystring()str=NULL;maxsize=MAXSIZE;last=0;mystring:mystring(char *s) int i=-1;doi+;while(*(s+i)!='0'); if(s)if(str=new chari+1)strcpy(str,s); maxsize=MAXSIZE; last=i-1; elsestr=NULL;maxsize=MAXSIZE;last=-1;mystring:mystring(mystring &s

6、t)if(st.str)if(str=new charst.strlen()+1)strcpy(str,st.str); maxsize=st.maxsize;last=st.last;mystring:mystring()cout<<"Destructor"<<endl;deletestr;void mystring:show()for(int i=0;i<=last;i+)cout<<*(str+i)<<'0'cout<<endl;int mystring:strlen()int i=-1

7、;doi+;while(*(str+i)!='0');return i;void mystring:reverse()char a100; for(int i=0;i<=last;i+)ai=*(str+i);for(int j=0;j<=last;j+)*(str+j)=alast-j;int mystring:strchr(char c)int i=-1;int j;for(j=0;j<=last;j+)if(*(str+j)=c)i=j;else continue;if(i=-1) return -1;else return i;mystring.h:class mystringprivate:char *str;int maxsize;int last;public:mystring();mystring(char*s

溫馨提示

  • 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論