




已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
C+練習(xí)宋文軒 CS1011 U2010145463-1集合類的頭文件Set.h如下,請(qǐng)定義其中的函數(shù)成員。classSETint*set;/set 用于存放集合元素int card; /card 為能夠存放的元素個(gè)數(shù)int used; /used 為已經(jīng)存放的元素個(gè)數(shù)public:SET(int card);/card 為能夠存放的元素個(gè)數(shù)SET( )int size( ); /返回集合已經(jīng)存放的元素個(gè)數(shù)int insert(int v); /插入 v 成功時(shí)返回 1,否則返回 0int remove(int v); /刪除 v 成功時(shí)返回 1,否則返回 0int has(int v); /元素 v 存在時(shí)返回 1, 否則返回 0解: 程序如下:#include #include using namespace std;class SET int *set; int card; int used; public: SET(int card); SET(); int size(); int insert(int v); int remove(int v); int has(int v);SET:SET(int card) if(set=new intcard)SET:card=card; used=0;SET:SET() if(set) delete set; set=0; card=used=0; int SET:size() return used;int SET:insert(int v) if(used0) for(x=0;xused;x+) if(setx=v) used-; for(;xused;x+) setx=setx+1; return 1; return 0; return 0;int SET:has(int v) int x; for(x=0;xused;x+) if(setx=v) return 1; return 0;int main() return 0;3-5利用 C 的文件類型 FILE, 定義新的文件類 DOCU, DOCU 用構(gòu)造函數(shù)打開文件,用析構(gòu)函數(shù)關(guān)閉文件, 并提供同 fread, fwrite, ftell, fseek 類似的函數(shù)成員 read, write, tell,seek, 類 DOCU 的聲明如下. 請(qǐng)定義其中的函數(shù)成員.class DOCUchar *name;FILE *file;public:int read(void *ptr, int size, int n);int seek(long offset, int whence);int write(const void *ptr, int size, int n);long tell( );DOCU(const char *filename, const char *mode);DOCU( );解:程序如下:#include #include #include using namespace std;class DOCUchar *name;FILE *file;public:int read(void *ptr, int size, int n);int seek(long offset, int whence);int write(const void *ptr, int size, int n);long tell( );DOCU(const char *filename, const char *mode);DOCU( );DOCU:DOCU(const char *filename, const char *mode)name=new charstrlen(filename)+1;strcpy(name, filename);file=fopen(name, mode);int DOCU:read(void *ptr, int size, int n)return fread(ptr, size, n, file);int DOCU:write(const void *ptr, int size, int n)return fwrite(ptr, size, n, file);int DOCU:seek(long offset, int whence)return fseek(file, offset, whence);long DOCU:tell( )return ftell(file);int main()return 0;4-5字符串類的類型聲明如下:#include #include class STRINGchar *str;public:int strlen( )const;int strcmp(const STRING &)const;STRING &strcpy(const STRING &);STRING &strcat(const STRING &);STRING(char *);STRING( );void main(void)STRING s1(I like apple);STRING s2( and pear);STRING s3( and orange);coutLength of s1=s1.strlen( )n;s1.strcat(s2).strcat(s3);coutLength of s1=s1.strlen( )n;s3.strcpy(s2).strcpy(s1);coutLength of s3=s3.strlen( )n;試定義字符串復(fù)制及連接等函數(shù)成員, 這些函數(shù)成員調(diào)用 C 的字符串運(yùn)算函數(shù).解: STRING的函數(shù)成員定義如下:#include #include using namespace std;class STRINGchar *str;public:int strlen( )const;int strcmp(const STRING &)const;STRING &strcpy(const STRING &);STRING &strcat(const STRING &);STRING(char *);STRING( );int STRING:strlen( )const return :strlen(str); int STRING:strcmp(const STRING &s)constreturn :strcmp(str, s.str); STRING &STRING:strcat(const STRING &s)int len=:strlen(str)+:strlen(s.str)+1;char *t=str;if(str=new charlen) :strcat(:strcpy(str, t), s.str);delete t;return *this;STRING &STRING:strcpy(const STRING &s)int len=:strlen(s.str)+1;delete str;if(str=new charlen) :strcpy(str, s.str);return *this;STRING:STRING(char *s)if(str=new char:strlen(s)+1) :strcpy(str, s);STRING:STRING( ) if (str) delete str; str=0; int main(void)STRING s1(I like apple);STRING s2( and pear);STRING s3( and orange);coutLength of s1=s1.strlen( )n;s1.strcat(s2).strcat(s3);coutLength of s1=s1.strlen( )n;s3.strcpy(s2).strcpy(s1);coutLength of s3=s3.strlen( )n;return 0;4-7定義學(xué)生成績記錄,記錄包含姓名,密碼等標(biāo)示信息,以及英語,數(shù)學(xué),物理, 化學(xué)等成績信息,在查詢某個(gè)學(xué)生的某科成績時(shí),要求正確提供該學(xué)生的的密碼.解:#include #include #include using namespace std;class RECORDchar password10;int english, mathematics,physics,chemistry;public:char name10;int RECORD:* get(char *item, char * pswd);/密碼核對(duì)正確后? 返回成員指針RECORD(char *name, char *pswd, int engl, int math, int phys, int chem);RECORD:RECORD(char *name, char *pswd, int engl, int math, int phys, int chem)strcpy(RECORD:name, name);strcpy(password, pswd);english=engl;mathematics=math;physics=phys;chemistry=chem;int RECORD:* RECORD:get(char *item, char *pswd)if(strcmp(pswd, password) return 0; /在 C+中返回 0 表示空指針if(strcmp(item, english)=0) return &RECORD:english;if(strcmp(item, mathematics)=0) return &RECORD:mathematics;if(strcmp(item, physics)=0) return &RECORD:physics;if(strcmp(item, chemistry)=0) return &RECORD:chemistry;return 0; / C+提倡返回 0 表示空指針char *getpswd(const char *name)int i=0;static char pswd10;coutMr. name, please input your password: ;while(pswdi=getchar()!=n) if(i9) i+; pswdi=0;coutnn;return pswd;RECORD song(song, 123, 90, 93, 94, 97);int main(void) char *pswd=getpswd();int RECORD:*p; /定義數(shù)據(jù)成員指針 pint RECORD:*q;int RECORD:*m;int RECORD:*n;p=song.get(english, pswd);q=song.get(mathematics, pswd);m=song.get(physics, pswd);n=song.get(chemistry, pswd);if(p=0) coutPassword or inquiry item does not exist!n;return 0;coutYour english is song.*pn;coutYour mathematics is song.*qn;coutYour physics is song.*mn;coutYour chemistry is song.*nn;return 0;4-9指出如下程序中的錯(cuò)誤.struct Achar *a, b, *geta( );char A:*p;char
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 風(fēng)險(xiǎn)評(píng)估與管理的試題及答案
- 汽修技師中級(jí)考試試題及答案
- 鐵路知識(shí)考試試題及答案
- 歷年地震考試試題及答案
- 單招六類考試試題大全及答案
- 關(guān)于安全考試試題及答案
- 語音審核面試題及答案
- 海信入職考試試題及答案
- 初級(jí)理論考試試題及答案
- 組卷中考語文試題及答案
- 水電安裝施工組織設(shè)計(jì)施工組織設(shè)計(jì)
- 水滸傳名著匯報(bào)課
- 幼小銜接繪本故事推薦《一年級(jí)一點(diǎn)都不可怕!》幼兒園課件
- 移動(dòng)互聯(lián)網(wǎng)時(shí)代九大思維用戶思維
- 2021-2022物理化學(xué)試題A
- GB/T 28162.3-2011自動(dòng)操作用元器件的包裝第3部分:表面安裝元器件在連續(xù)帶上的包裝
- 消化性潰瘍英文
- 個(gè)人分期還款協(xié)議書模板(5篇)
- CNAS-CL01:2018(ISO17025:2017)改版后實(shí)驗(yàn)室首次內(nèi)審及管理評(píng)審資料匯總
- 智慧樹超星爾雅創(chuàng)新性思維和方法網(wǎng)絡(luò)通識(shí)課題庫附答案
- 05G515輕型屋面梯形鋼屋架
評(píng)論
0/150
提交評(píng)論