1、C+练习宋文轩 CS1011 U2010145463-1集合类的头文件 Set.h 如下,请定义其中的函数成员。classSETint*set; /set 用于存放集合元素int card; /card 为能够存放的元素个数int used; /used 为已经存放的元素个数public:SET(int card);/card 为能够存放的元素个数SET( )int size( ); /返回集合已经存放的元素个数int insert(int v); /插入 v 成功时返回 1,否则返回 0int remove(int v); /删除 v 成功时返回 1,否则返回 0int has(int v)
2、; /元素 v 存在时返回 1, 否则返回 0解: 程序如下:#include #include using namespace std;class SETint *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
3、SET:size()return used;int SET:insert(int v)if(used0)for(x=0;x#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 c
4、har *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
5、);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 STRING STRING STRING(char *);STRING( );void main(void)STRING s1(“
6、I like apple“);STRING s2(“ and pear“);STRING s3(“ and orange“);cout#include using namespace std;class STRINGchar *str;public:int strlen( )const;int strcmp(const STRING STRING STRING STRING(char *);STRING( );int STRING:strlen( )const return :strlen(str); int STRING:strcmp(const STRING STRING char *t=
7、str;if(str=new charlen) :strcat(:strcpy(str, t), s.str);delete t;return *this;STRING 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“)
8、;STRING s2(“ and pear“);STRING s3(“ and orange“);cout#include #include using namespace std;class RECORDchar password10;int english, mathematics,physics,chemistry;public:char name10;int RECORD:* get(char *item, char * pswd);/密码核对正确后? 返回成员指针RECORD(char *name, char *pswd, int engl, int math, int phys,
9、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(strc
10、mp(item, “english“)=0) return if(strcmp(item, “mathematics“)=0) return if(strcmp(item, “physics“)=0) return if(strcmp(item, “chemistry“)=0) return return 0; / C+提倡返回 0 表示空指针char *getpswd(const char *name)int i=0;static char pswd10;cout“Mr. “name“, please input your password: “;while(pswdi=getchar()!
11、=n) if(i9) i+; pswdi=0;cout“nn“;return pswd;RECORD song(“song“, “123“, 90, 93, 94, 97);int main(void) char *pswd=getpswd(song.name);int RECORD:*p; /定义数据成员指针 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(“
12、chemistry“, pswd);if(p=0) cout“Password or inquiry item does not exist!n“;return 0;cout“Your english is “song.*p“n“;cout“Your mathematics is “song.*q“n“;cout“Your physics is “song.*m“n“;cout“Your chemistry is “song.*n“n“;return 0;4-9指出如下程序中的错误.struct Achar *a, b, *geta( );char A:*p;char *A:*q( );char *(A:*r)( );void main(void) A a;a.p=a.p=a.q=a.r=a.geta;解:错误出现在 main 中:1)a.p= /不能将2)a.q= /a.q 既不是取函数 q 的地址? 也不是调用 q 函数3)a.r=a.geta; /a.geta 既不是取函数 geta 的地址? 也不是调用 geta 函数