posts - 1,  comments - 6,  trackbacks - 0

Book.h

#include<iostream>
#include<string>
#include"publish.h"
using namespace std;
#ifndef  BOOK_H
#define  BOOK_H
class Book
{
private:
 string bookID;//书的编号,不同的书不同的编号
 string bookName;//书的名称
 string author;//书的作者
 string bookType;//书的类型
 Publish *publish;//声明一个指针类
 int version;//书的版本号
 int count ;//该书的册数
public:
 Book();//无参构造函数
 void set_bookID(string id);//设置书的编号
 string get_bookID();//获取书的编号
 void set_bookName(string name);//设置书名
 string get_bookName();//获取书名
 void set_author(string author);//设置书作者
 string get_author();//获取书的作者
 void set_bookType(string type);//设置书的类型
 string get_bookType();//获取书的类型
 void set_publish(Publish &publish);//设置出版社信息
 Publish get_publish();//获取出版社信息
 void set_version(int version);//设置书的版本
 int get_version();//获取书的版本号
 void set_count(int count);//设置书的册数
 int get_count();//获取书的册数
 void addInfo();//录入相关图书信息
 bool searchInfo(string id);//查找相关图书信息 
 void modifyInfo(string id);//修改相关图书信息
    friend void sortInfo(Book book[],int n);//对相关图书排序
 void display();//输出图书信息
 Book operator =(Book temp);
    static int bookCount;
 friend bool isContinue();//声明友原函数,判断是否继续操作
};
#endif

Publish.h
#include<string>
using namespace std;
#ifndef PUBLISH_H
#define PUBLISH_H
class Publish
{
private:
 string pTime;
 string address;
 string name;

public:
 Publish();//无参构造函数
 Publish(string time,string add,string name);//带参构造函数
    void set_pTime(string time);//设置出版时间
 string get_pTime();//获取出版时间
 void set_address(string add);//设置出版社地址
 string get_address();//获取出版社地址
 void set_name(string name);//设置出版社名称
 string get_name();//获取出版社名称
};
#endif 
Book.cpp
#include<iostream>
#include<fstream>
#include<string>
#include"publish.h"
#include"book.h"
using namespace std;

int Book::bookCount=0;//书的总类数,不同编号的书代表一类
Book::Book()
{
 bookID="1";
 bookName="C++编程技术";
 author="郑立";
 bookType="IT类";
 publish=new Publish();//动态分配,后面一定要用delete,否则容易产生内存泄露
 version=1;
 count=1;
}
void Book::set_bookID(string id)
{
 bookID=id;
}
string  Book::get_bookID()
{
 return bookID;
}
void Book::set_bookName(string name)
{
 bookName=name;
}
string Book::get_bookName()
{
 return bookName;
}
void Book::set_author(string auth)
{
     author=auth;
}
string Book::get_author()
{
 return author;
}
void Book::set_bookType(string type)
{
 bookType=type;
}
string Book::get_bookType()
{
 return bookType;
}
void Book::set_publish(Publish &pub)
{
publish->set_pTime(pub.get_pTime());
publish->set_name(pub.get_name());
publish->set_address(pub.get_address());
}
Publish Book::get_publish()
{
 return *publish;
}
void Book::set_version(int ver)
{
 version=ver;
}
int Book::get_version()
{
 return version;
}
void Book::set_count(int coun)
{
 count=coun;
}
int Book::get_count()
{
 return count;
}

 void Book::display()
{
cout<<"图书编号:"<<bookID<<endl;
cout<<"图书姓名:"<<bookName<<endl;
cout<<"图书作者:";
cout<<author<<endl;
cout<<"图书类型:"<<bookType<<endl;
cout<<"出版时间:"<<publish->get_pTime();
cout<<" "<<"出版社地址:"<<publish->get_address()<<endl;
cout<<"出版社名称:";
cout<<publish->get_name()<<endl;
cout<<"版本号:"<<version<<" "<<"册数:"<<count<<endl;

}
void Book::addInfo()
{  
   
 char choice;
 cout<<"输入数据按Y,退出按N"<<endl;
 cin>>choice;
 if(choice=='Y')
 {    
 string id;
 string name;
 string author;
 string type;
 string ptime;
 string pname;
 string address;
 Publish publish;
 int version;
 int count;
 
 cout<<"请输入图书编号:"<<endl;
 cin>>id;
 set_bookID(id);
 
 

 cout<<"请输入图书名称:"<<endl;
 cin>>name;
 set_bookName(name);
 
 
 cout<<"请输入图书作者:"<<endl;
 cin>>author;
 set_author(author);
 
  
 cout<<"请输入图书类型:"<<endl;
 cin>>type;
 set_bookType(type);
 
   

 cout<<"请输入出版时间:"<<endl;
 cin>>ptime;
 publish.set_pTime(ptime);
 
   

 cout<<"请输入出版社地址:"<<endl;
 cin>>address;
 publish.set_address(address);
 
   

 cout<<"请输入出版社名称:"<<endl;
 cin>>pname;
 publish.set_name(pname);
 
   

 cout<<"请输入版本号:"<<endl;
 cin>>version;
 set_version(version);
 
  

 cout<<"请输入册数:"<<endl;
 cin>>count;
 set_count(count);
 
 cout<<endl;
 bookCount++;//总数加1

 }
 

}
bool Book::searchInfo(string id)
{
 bool flag=true;
 if(get_bookID()==id)
  flag=true;
 else
  flag=false;

 return flag;
 

}
void Book::modifyInfo(string id)
{
 int choice;//所做的选择
 
 string name,author,type,time,pname,address;
 int version,count;
 cout<<"请按序号输入你想修改的项目:"<<endl;
 cout<<"1:图书姓名  2:图书作者  3:图书类型    4:图书版本  "<<endl;
 cout<<"5:图书册数  6:出版日期  7:出版社名称  8:出版社地址 "<<endl;
 cin>>choice;
   if(choice!=0)//当choice=0时表示退出操作
 {
 if(choice==1)
 {  
  cout<<"请输入新的名称:"<<endl;
  cin>>name;
  set_bookName(name);
 }
 else if(choice==2)
 {  
  cout<<"请输入新的作者名:"<<endl;
  cin>>author;
  set_author(author);
 }
 else if(choice==3)
 {  
  cout<<"请输入新的图书类型:"<<endl;
  cin>>type;
  set_bookType(type);
 }
    else if(choice==4)
 {  
  cout<<"请输入新的版本号:"<<endl;
  cin>>version;
  set_version(version);
 }
 else if(choice==5)
 {  
  cout<<"请输入新的册数"<<endl;
  cin>>count;
  set_count(count);
 }
 else if(choice==6)
 { 
  cout<<"请输入新的出版时间:"<<endl;
  cin>>time;
  publish->set_pTime(time);
 }
 else if(choice==7)
 {
  cout<<"请输入新的出版名称:"<<endl;
  cin>>pname;
  publish->set_name(pname);
 }
 else
 {
  cout<<"请输入新的出版社地址:"<<endl;
  cin>>address;
  publish->set_address(address);
 }
   
 
 }

}


void sortInfo(Book book[],int n)
{
 int flag;
 Book temp;//临时变量
 for(int i=0;i<n-1;i++)
 {

  //如果前一种书的编号比后一种书的编号大,则flag返回正数
  flag =book[i].get_bookID().compare(book[i+1].get_bookID());
  if(flag)//如果flag为正数,就交换这两种书的位置
  {
        temp=book[i];
  book[i]=book[i+1];
  book[i+1]=temp;
  }
 }

}

Book Book::operator =(Book temp)//重载=运算符,进行深拷贝
{
 set_bookID(temp.get_bookID());
 set_bookName(temp.get_bookName());
 set_bookType(temp.get_bookType());
 set_author(temp.get_author());
 set_version(temp.get_version());
 set_count(temp.get_count());
 set_publish(temp.get_publish());

 return *this;
 
}


bool isContinue()
{
 char choice;
 cout<<"是否继续操作(Y/N)?"<<endl;
 cin>>choice;
 if(choice=='Y')
  return true;
 else
  return false;
}

Publish.cpp
#include<iostream>
#include<string>
#include"publish.h"
using namespace std;

Publish::Publish()
{
 pTime="2008-12-27";
 address="北京";
 name="清华大学出版社";
}
Publish::Publish(string time,string add,string name)
{
 pTime=time;
 address=add;
 name=name;
}

void Publish::set_pTime(string time)
{
 pTime=time;
}
string Publish::get_pTime()
{
 return pTime;
}
void Publish::set_address(string add)
{
 address=add;
}
string Publish::get_address()
{
 return address;
}
void Publish::set_name(string nam)
{
 name=nam;
}
string Publish::get_name()
{
 return name;
}
 
main.cpp
#include<iostream>
#include<string>
#include"book.h"
#include"publish.h"
using namespace std;
void main()
{
  
    int choice;  
 int i;
    int n;
 
 Book *book;//动态创建对象数组
 cout<<"                  欢迎进入图书信息系统!        "<<endl;
    cout<<"请输入你想加入的书的总类数:";
 cin>>n;
 cout<<endl;
 cout<<"============================================================"<<endl;
 cout<<"请根据序号选择相关操作"<<endl;   
 cout<<"============================================================"<<endl;
 cout<<endl;
 cout<<"                  1:录入图书信息!"<<endl;
 cout<<"                  2:查询图书信息!"<<endl;
 cout<<"                  3:修改图书信息!"<<endl;
 cout<<"                  4:对图书排序!"<<endl;
    cout<<"                  5:删除图书信息!"<<endl;
 cout<<"                  6:输出图书信息!"<<endl; 
 cout<<"                  7:退出系统!"<<endl;
 cout<<"============================================================"<<endl;
   
 book=new Book[n];
 cin>>choice;
 cout<<endl;
 while(choice!=0)
 {
  
 if(choice==1)
 {  
    
  for(i=0;i<n;i++)
  {
  cout<<"第"<<i<<"种书:"<<endl;
  book[i].addInfo();
  }
 
 }


 else if(choice==2)
 {
        string id;
  cout<<"请输入你想查找的图书编号:";
  cin>>id;
  for(i=0;i<n;i++)
  {
  if(book[i].searchInfo(id))
   book[i].display();
  }
 }
  
   else if(choice==3)
   {   
     string id;
  cout<<"请输入你想修改的图书编号:";
  cin>>id;
  for(i=0;i<n;i++)
  {
  if(book[i].get_bookID()==id)
  book[i].modifyInfo(id);  
  }
   }
 

 else if(choice==4)
 {
  sortInfo(book,n); 
 }
 

 else if(choice==5)
 {
        string id;
  cout<<"请输入你想删除的图书编号:";
  cin>>id;
       for(i=0;i<n;i++)
     if(book[i].get_bookID()==id)
     {
      int temp;
      temp=i;
     for(int j=i+1;j<n;j++)
     {  
      book[temp]=book[j];
               temp=j;

     } 
     n--;//删除了一种数,就减少了一种。
     }  
     
     
 }
 
  
 else if(choice==6)
 {
  for(i=0;i<n;i++)
  book[i].display();
 }
 
  
    else
 {
  exit(1);
  break;
 }
   if(isContinue())//选择是否继续操作
  {
     cout<<"请根据序号选择操作:";
  cin>>choice;
  }
  else break;//否则退出系统
 
 }

    
return ;
}


posted on 2009-01-11 16:58 黄琴 阅读(301) 评论(0)  编辑 收藏 引用 所属分类: C++
<2025年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(1)

随笔档案

文章分类

文章档案

相册

搜索

  •  

最新评论