资源描述
C++练习
宋文轩 CS1011 U201014546
3-1
集合类的头文件Set.h如下,请定义其中的函数成员。
class
SET{
int
*set; //set 用于存放集合元素
int card; //card 为能够存放的元素个数
int used; //used 为已经存放的元素个数
public:
SET(int card);//card 为能够存放的元素个数
~SET( )
int size( ); //返回集合已经存放的元素个数
int insert(int v); //插入 v 成功时返回 1,否则返回 0
int remove(int v); //删除 v 成功时返回 1,否则返回 0
int has(int v); //元素 v 存在时返回 1, 否则返回 0
}
解: 程序如下:
#include <iostream>
#include <stdio.h>
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 int[card])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(used<card)
{
set[used++]=v;
return 1;
}
return 0;
}
int SET::remove(int v)
{
int x;
if(used>0)
{
for(x=0;x<used;x++)
if(set[x]==v)
{
used--;
for(;x<used;x++) set[x]=set[x+1];
return 1;
}
return 0;
}
return 0;
}
int SET::has(int v)
{
int x;
for(x=0;x<used;x++) if(set[x]==v) return 1;
return 0;
}
int main()
{
return 0;
}
3-5
利用 C 的文件类型 FILE, 定义新的文件类 DOCU, DOCU 用构造函数打开文件,用析构函数关闭文件, 并提供同 fread, fwrite, ftell, fseek 类似的函数成员 read, write, tell,seek, 类 DOCU 的声明如下. 请定义其中的函数成员.
class DOCU{
char *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 <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class DOCU{
char *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 char[strlen(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 <string.h>
#include <iostream.h>
class STRING{
char *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");
cout<<"Length of s1="<<s1.strlen( )<<"\n";
s1.strcat(s2).strcat(s3);
cout<<"Length of s1="<<s1.strlen( )<<"\n";
s3.strcpy(s2).strcpy(s1);
cout<<"Length of s3="<<s3.strlen( )<<"\n";
}
试定义字符串复制及连接等函数成员, 这些函数成员调用 C 的字符串运算函数.
解:
STRING的函数成员定义如下:
#include <string.h>
#include <iostream>
using namespace std;
class STRING{
char *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)const{return ::strcmp(str, s.str); }
STRING &STRING::strcat(const STRING &s){
int len=::strlen(str)+::strlen(s.str)+1;
char *t=str;
if(str=new char[len]) {
::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 char[len]) ::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");
cout<<"Length of s1="<<s1.strlen( )<<"\n";
s1.strcat(s2).strcat(s3);
cout<<"Length of s1="<<s1.strlen( )<<"\n";
s3.strcpy(s2).strcpy(s1);
cout<<"Length of s3="<<s3.strlen( )<<"\n";
return 0;
}
4-7
定义学生成绩记录,记录包含姓名,密码等标示信息,以及英语,数学,物理, 化学等成绩信息,在查询某个学生的某科成绩时,要求正确提供该学生的的密码.
解:
#include <string.h>
#include <iostream>
#include <stdio.h>
using namespace std;
class RECORD{
char password[10];
int english, mathematics,physics,chemistry;
public:
char name[10];
int RECORD::* get(char *item, char * pswd);//密码核对正确后? 返回成员指针
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 pswd[10];
cout<<"Mr. "<<name<<", please input your password: ";
while((pswd[i]=getchar())!='\n') if(i<9) { i++; }
pswd[i]=0;
cout<<"\n\n";
return pswd;
}
RECORD song("song", "123", 90, 93, 94, 97);
int main(void) {
char *pswd=getpswd(song.name);
int RECORD::*p; //定义数据成员指针 p
int 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) {
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 A{
char *a, b, *geta( );
char A::*p;
char *A::*q( );
char *(A::*r)( );
};
void main(void) {
A a;
a.p=&A::a;
a.p=&A::b;
a.q=&A::geta;
a.r=a.geta;
}
解:错误出现在 main 中:
1)a.p=&A::a; //不能将&A::a 的类型 char *A::*转换为 a.p 的类型 char A::*a.p=&A::b;
2)a.q=&A::geta; //a.q 既不是取函数 q 的地址? 也不是调用 q 函数
3)a.r=a.geta; //a.geta 既不是取函数 geta 的地址? 也不是调用 geta 函数
展开阅读全文