首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++小功课

2013-09-12 
c++小作业【要求】按以下描述和要求建立一个含有对象成员的类TeleBook,用类Record定义的数组是TeleBook的数

c++小作业
【要求】按以下描述和要求建立一个含有对象成员的类TeleBook,用类Record定义的数组是TeleBook的数据成员。写出所有定义成员函数的代码。执行主函数对其测试。 Record私有成员  string name;     //姓名 char *telnum;    //电话号码
   公有成员 Record(){name=""; telnum=NULL; } 
  string getname() ; //返回姓名 char* getnum();  //返回电话号码
   void setdata(char *a,char *b);//赋值给表的末尾项 
  TeleBook私有成员 Record tnum[100];  //电话簿存储区 int number;//已存入数据的元素个数(与表尾下标有关)
   公有成员 TeleBook(){number=0;}  //电话簿类构造函数
   void Insert();  //在表尾插入新数据项(输入一个新的姓名和电话号码)
   void Find();    //根据姓名查找电话号码,打印查找结果
   void print();   //打印电话簿清单,最后打印电话号码个数
   头文件包含语句和声明常变量语句为:
   #include <iostream>
   #include <string>
   using namespace std;
   const int LEN=13 ;//存储电话号码的数组长度
   测试程序的主函数为:
   void main(){ 
  int ch; TeleBook myfriend; 
  ch=1; 
  while(ch>0 && ch<4){ 
  cout<<"1-输入, 2-查找, 3-打印清单, 4-退出, please choose!"<<endl; 
  cin>>ch; 
  if(ch==1) myfriend.Insert();         //插入新数据项
   else if(ch==2) myfriend.Find();      //根据姓名查找电话号码 
  else if(ch==3) myfriend.print();      //打印清单 
  }
   }
   【提示】插入新元素时需要为telnum动态分配内存,长度为常变量LEN。  c++
[解决办法]

#include <iostream>
#include <string>
using namespace std;
const Len = 13;

class Record
{
public:
Record() {name = ""; telnum = NULL;}
string getname();
char* getnum();
void setdata(string, char*);
private:
string name;


char *telnum;
};

class TeleBook
{
public:
TeleBook() {number = 0;}
~TeleBook();
void Insert();
void Find();
void print();
private:
Record tnum[100];
int number;
};

int main()
{
int ch; TeleBook myfriend; 
ch=1; 
while(ch>0 && ch<4)

cout<<"1-输入, 2-查找, 3-打印清单, 4-退出, please choose!"<<endl; 
cin>>ch; 
if(ch==1) myfriend.Insert();         //插入新数据项
else if(ch==2) myfriend.Find();      //根据姓名查找电话号码 
else if(ch==3) myfriend.print();//打印清单
else break;
}

return 0;
}

string Record::getname()
{
return name;
}

char* Record::getnum()
{
return telnum;
}

void Record::setdata(string str, char *cp)
{
name = str;
telnum = cp;
}

void TeleBook::Insert()
{
string name;
char *cp = new char(Len + 1);
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入电话号码(最多"<<Len<<"位):";
cin>>cp;
tnum[number++].setdata(name, cp);
}

void TeleBook::Find()
{

string str;
bool b = false;
cout<<"请输入要查找的姓名:";
cin>>str;
for(int i = 0; i < number; ++i)
if(str == tnum[i].getname())
{
cout<<tnum[i].getname()<<"\t"<<tnum[i].getnum()<<endl;
b = true;
}
if(!b)
cout<<"未找到记录!"<<endl;
}

void TeleBook::print()
{
for(int i = 0; i< number; ++i)
cout<<"<"<<i<<">\t"<<tnum[i].getname()<<"\t"<<tnum[i].getnum()<<endl;
}

TeleBook::~TeleBook()
{
for(int i = 0; i < number; ++i)
delete []tnum[i].getnum();
}




大致是这个意思
[解决办法]
引用:
#include <iostream>
#include <string>
using namespace std;
const Len = 13;

class Record
{
public:
Record() {name = ""; telnum = NULL;}
string getname();
char* getnum();


void setdata(string, char*);
private:
string name;
char *telnum;
};

class TeleBook
{
public:
TeleBook() {number = 0;}
~TeleBook();
void Insert();
void Find();
void print();
private:
Record tnum[100];
int number;
};

int main()
{
int ch; TeleBook myfriend; 
ch=1; 
while(ch>0 && ch<4)

cout<<"1-输入, 2-查找, 3-打印清单, 4-退出, please choose!"<<endl; 
cin>>ch; 
if(ch==1) myfriend.Insert();         //插入新数据项
else if(ch==2) myfriend.Find();      //根据姓名查找电话号码 
else if(ch==3) myfriend.print();//打印清单
else break;
}

return 0;
}

string Record::getname()
{
return name;
}

char* Record::getnum()
{
return telnum;
}

void Record::setdata(string str, char *cp)
{
name = str;
telnum = cp;
}

void TeleBook::Insert()
{
string name;
char *cp = new char(Len + 1);
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入电话号码(最多"<<Len<<"位):";
cin>>cp;
tnum[number++].setdata(name, cp);
}

void TeleBook::Find()
{

string str;
bool b = false;
cout<<"请输入要查找的姓名:";
cin>>str;
for(int i = 0; i < number; ++i)
if(str == tnum[i].getname())
{
cout<<tnum[i].getname()<<"\t"<<tnum[i].getnum()<<endl;
b = true;
}
if(!b)
cout<<"未找到记录!"<<endl;
}

void TeleBook::print()
{
for(int i = 0; i< number; ++i)
cout<<"<"<<i<<">\t"<<tnum[i].getname()<<"\t"<<tnum[i].getnum()<<endl;
}

TeleBook::~TeleBook()
{
for(int i = 0; i < number; ++i)
delete []tnum[i].getnum();
}




大致是这个意思

c++小功课
CSDN,全球最大中文IT作业代写社区。

热点排行