#include
#include
#include
#include
const int Maxr=100; //读者上限
const int Maxb=100; //图书上限
const int Maxbor=5; //每个读者最多借借5本
class Reader //读者类
{
int tag; //删除标记 1:已删,0:未删
int no; //读者编号
char name[10]; //读者姓名
int borbook[Maxbor]; //所借图书
public:
Reader() {}
char *getname() {return name;} //获取姓名
int gettag() {return tag;} //获取删除标记
int getno() {return no;} //获取读者编号
void setname(char na[]) //设置姓名
{
strcpy(name,na);
}
void delbook() {tag=1;} //设置删除标记
void addreader(int n,char *na) //增加读者
{
tag=0;
no=n;
strcpy(name,na);
for (int i=0;i
}
void borrowbook(int bookid) //借书操作
{
for (int i=0;i
if (borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
int retbook(int bookid) //还书操作
{
for (int i=0;i
if (borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void disp() //输出读者信息
{
cout<
cout<
};
class RDatabase //读者库类
{
int top; //读者记录指针
Reader read[Maxr]; //读者记录
public:
RDatabase() //构造函数,将reader.txt读到read[]中
{
Reader s;
top=-1;
fstream file("reader.txt",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file) break;
top++;
read[top]=s;
}
file.close();
}
void clear() //删除所有读者信息
{
top=-1;
}
int addreader (int n,char *na) //添加读者时先查找是否存在
{
Reader *p=query(n);
if (p==NULL)
{
top++;
read[top].addreader(n,na);
return 1;
}
return 0;
}
Reader *query(int readerid) //按编号查找
{
for (int i=0;i<=top;i++)
if (read[i].getno()==readerid && read[i].gettag()==0)
return &read[i];
return NULL;
}
void disp() //输出所有读者信息
{
for (int i=0;i<=top;i++)
read[i].disp();
}
void readerdata(); //读者库维护
~RDatabase() //析构函数,将read[]写入reader.txt文件中
{
fstream file("reader.txt",ios::out);
for (int i=0;i<=top;i++)
if (read[i].gettag()==0)
file.write((char *)&read[i],sizeof(read[i]));
file.close();
}
};
void RDatabase::readerdata()
{
int choice=1;
char rname[20];
int readerid;
Reader *r;
while (choice!=0)
{
cout<<"读者维护 1:新增 2:更改 3:删除 4:查找 5:显示 6:全删 0:退出=>";
cin>>choice;
switch (choice)
{
case 1:
cout<<"输入读者编号:";
cin>>readerid;
cout<<"输入读者姓名:";
cin>>rname;
addreader(readerid,rname);
break;
case 2:
cout<<"输入读者编号:";
cin>>readerid;
r=query(readerid);
if (r==NULL)
{
cout<<"该读者不存在"<
}
cout<<"输入新的姓名:";
cin>>rname;
r->setname(rname);
break;
case 3:
cout<<"输入读者编号:";
cin>>readerid;
r=query(readerid);
if (r==NULL)
{
cout<<"该读者不存在"<
}
r->delbook();
break;
case 4:
cout<<"输入读者编号:";
cin>>readerid;
r=query(readerid);
if (r==NULL)
{
cout<<"该读者不存在"<
}
r->disp();
break;
case 5:
disp();
break;
case 6:
clear();
break;
}
}
}
class Book //图书类
{
int tag; //删除标记 1:已删 0:未删
int no; //图书编号
char name[20]; //书名
int onshelf; //是否在架 1:在架 0:已借
public:
Book() {}
char *getname() {return name;} //获得书名
int getno() {return no;} //获得书编号
int gettag() {return tag;} //获得删除标记
void setname(char na[]) //设置书名
{
strcpy(name,na);
}
void delbook() {tag=1;} //删除图书
void addbook(int n,char *na) //增加图书
{
tag=0;
no=n;
strcpy (name,na);
onshelf=1;
}
int borrowbook() //借书操作
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void retbook() //还书操作
{
onshelf=1;
}
void disp() //输出图书
{
cout<
};
class BDatabase //图书库类
{
int top; //图书记录指针
Book book[Maxb]; //图书记录
public:
BDatabase() //构造函数,将book.txt读到book[]中
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char*)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear() //全删
{
top=-1;
}
int addbook(int n,char *na) //增加图书
{
Book *p=query(n);
if (p==NULL)
{
top++;
book[top].addbook(n,na);
return 1;
}
return 0;
}
Book *query(int bookid) //查找图书
{
for (int i=0;i<=top;i++)
if (book[i].getno()==bookid && book[i].gettag()==0)
return &book[i];
return NULL;
}
void bookdata(); //图书库维护
void disp()
{
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase() //
{
fstream file("book.txt",ios::out);
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
file.write((char*)&book[i],sizeof(book[i]));
file.close();
}
};
void BDatabase::bookdata()
{
int choice=1;
char bname[40];
int bookid;
Book *b;
while (choice!=0)
{
cout<<"图书维护 1:新增 2:更改 3:删除 4:查找 5:显示 6:全删 0:退出=>";
cin>>choice;
switch (choice)
{
case 1:
cout<<"输入图书编号:";
cin>>bookid;
cout<<"输入图书书名:";
cin>>bname;
addbook(bookid,bname);
break;
case 2:
cout<<"输入图书编号:";
cin>>bookid;
b=query(bookid);
if (b==NULL)
{
cout<<"该图书不存在"<
}
cout<<"输入新的书名:";
cin>>bname;
b->setname(bname);
break;
case 3:
cout<<"输入图书编号:";
cin>>bookid;
b=query(bookid);
if (b==NULL)
{
cout<<"该图书不存在"<
}
b->delbook();
break;
case 4:
cout<<"输入图书编号:";
cin>>bookid;
b=query(bookid);
if (b==NULL)
{
cout<<"该图书不存在"<
}
b->disp();
break;
case 5:
disp();
break;
case 6:
clear();
break;
}
}
}
void main()
{
int choice=1,bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
Book *b;
while(choice!=0)
{
cout<<"1:借书 2:还书 3:图书维护 4:读者维护 0:离开=>";
cin>>choice;
switch (choice)
{
case 1:
cout<<" 借书 读者编号:";
cin>>readerid;
cout<<" 图书编号:";
cin>>bookid;
r=ReaderDB.query(readerid);
if (r==NULL)
{
cout<<"该读者不存在,不能借书"<
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout<<"该读者不存在,不能借书"<
}
if (b->borrowbook()==0)
{
cout<<"该读者不存在,不能借书"<
}
r->borrowbook(b->getno());
break;
case 2:
cout<<"还书 读者编号:";
cin>>readerid;
cout<<" 图书编号:";
cin>>bookid;
r=ReaderDB.query(readerid);
if (r==NULL)
{
cout<<"不存在该读者,不能借书"<
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout<<"不存在该图书,不能借书"<
}
b->retbook();
r->retbook(b->getno());
break;
case 3:
BookDB.bookdata();
break;
case 4:
ReaderDB.readerdata();
break;
}
}
}
如果你家里没实力,毕业后要靠自己努力,那我劝你,自己编程。不懂的地方到图书馆查资料,问老师!大学里一定要学到一些真本事的。现在抄袭也好,作弊也罢,别以为是糊弄老师,其实是糊弄你自己。毕业之后你就发现自己吃亏的地方了。这路我走过,希望你别再走了。兄弟!!
没时间帮你写...
以前写的一小段代码..
你参考一下吧...
改改就可以了..
#include
float subtotal;
class Employee{
private:
int i;
int eid;
char name[20];
float price;
int num;
public:
void setData(){
cout<<"请输入商品编号:"<
cout<<"商品名称:"<
cout<<"商品价格:"<
cout<<"数量:"<
}
void shaow(){
i=num*price;
cout<<"商品编号:"<
}
float getsubtotal(){
return subtotal;
}
};
void main(){
Employee emp[3];
for(int i=0;i<3;i++){
emp[i].setData();
}
for(int j=0;j<3;j++){
emp[j].shaow();
}
cout<<"总计:"<
不好意思,我没有学到面对程序设计的部份呢~ 我去我的老师,稍后再回答~~~ /:P
太强了
佩服
好好学习一下