第1个回答 2010-12-26
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class CBook;
class CLibrary;
class CPeople;
CBook *head=NULL;
static int i;
int k;
bool check(string str)
{
for(int n=0;n<str.length();n++)
{
if((str[n]>'9'||str[n]<'0')&&(str[n]!='.'))
return false;
}
return true;
}
class CBook
{
public:
CBook(){}
virtual ~CBook(){}
friend class CBooksadministrator;
friend class CLibrary;
private:
int m_no;//图书编号
string bookName;//图书书名
string author;//图书作者
string bookCategory;//图书分类
string bookSummary;//图书简介
CBook* next;
};
class CLibrary
{
public:
CLibrary(){}
virtual ~CLibrary(){}
void showbook(CBook *);
void GetTotalbooks(CBook *);
private:
static int totalbook;
};
int CLibrary::totalbook=0;
void CLibrary::showbook(CBook *head)
{
CBook *p1,*p2,*p3;
p1=new CBook;
p2=new CBook;
p3=new CBook;
p1=head;
p2=head;
p3=head;
cout<<endl;
cout<<"图书信息如下:"<<endl;
cout<<"编号"<<setw(8)<<"分类"<<setw(8)<<"书名"<<setw(8)<<"作者"<<setw(8)<<"简介"<<endl;
while(p1)
{
if(p1->bookCategory=="哲学")
cout<<p1->m_no<<setw(8)<<p1->bookCategory<<setw(8)<<p1->bookName
<<setw(8)<<p1->author<<setw(8)<<p1->bookSummary<<endl;
p1=p1->next;
}
while(p2)
{
if(p2->bookCategory=="经济")
cout<<p2->m_no<<setw(8)<<p2->bookCategory<<setw(8)<<p2->bookName
<<setw(8)<<p2->author<<setw(8)<<p2->bookSummary<<endl;
p2=p2->next;
}
while(p3)
{
if(p3->bookCategory=="政治")
cout<<p3->m_no<<setw(8)<<p3->bookCategory<<setw(8)<<p3->bookName
<<setw(8)<<p3->author<<setw(8)<<p3->bookSummary<<endl;
p3=p3->next;
}
}
void CLibrary::GetTotalbooks(CBook *head)
{
while(head)
{
totalbook++;
head=head->next;
}
cout<<totalbook<<" 本图书可供借阅."<<endl;
}
class CBooksadministrator
{
public:
CBooksadministrator(){}
virtual ~CBooksadministrator(){}
CBook* creat();
void Inquiry(CBook * ,int );
void Inquiry(CBook * ,string);
void insert(CBook * ,int ,string ,string ,string ,string );
};
CBook* CBooksadministrator::creat()
{
CBook *p1,*p2;
string str;
p1=new CBook;
head=p1;
p2=p1;
for(i=1;;i++)
{
cout<<"图书编号: "<<i<<endl;
p1->m_no=i;
cout<<"请输入图书分类(哲学/政治/经济):";
cin>>str;
p1->bookCategory=str.c_str();
cout<<"请输入图书书名:";
cin>>str;
p1->bookName=str.c_str();
cout<<"请输入作者:";
cin>>str;
p1->author=str.c_str();
cout<<"请输入图书简介:";
cin>>str;
p1->bookSummary=str.c_str();
p2=p1;
//p1=new CBook;
p2->next=p1;
cout<<"是否继续录入(y/n)"<<endl;
cin>>str;
while(str!="y"&&str!="n")
{
cout<<"输入有误,重输(y/n)"<<endl;
cin>>str;
}
if(str=="y")
cout<<endl;
else
break;
}
k=i;
delete p1;
p2->next=NULL;
return head;
}
void CBooksadministrator::insert(CBook *head,int m_no,string bookCategory,string bookName,string author,string bookSummary)
{
CBook *list=new CBook;
list->next=list;
list->m_no=m_no;
list->bookCategory=bookCategory;
list->bookName=bookName;
list->author=author;
list->bookSummary=bookSummary;
if(m_no <= head->m_no)
{
list->next=head;
::head=list;
return;
}
CBook *temp=NULL;
while((m_no > head->m_no)&&(head->next!=NULL))
{
temp=head;
head=head->next;
}
if(m_no > head->m_no)
{
head->next=list;
list->next=NULL;
}
else
{
temp->next=list;
list->next=head;
}
}
void CBooksadministrator::Inquiry(CBook *head,int num)
{
CBook* p;
if(head->m_no==num)
{
p=head;
head=head->next;
cout<<"编号"<<setw(8)<<"分类"<<setw(8)<<"书名"<<setw(8)<<"作者"<<setw(8)<<"简介"<<endl;
cout<<head->m_no<<setw(8)<<head->bookCategory<<setw(8)<<head->bookName
<<setw(8)<<head->author<<setw(8)<<head->bookSummary<<endl;
p=p->next;
return ;
}
while(head)
{
if(head->next==NULL)
{
cout<<"找不到要查询的图书"<<endl;
return;
}
if(head->next->m_no==num)
{
p=head->next;
cout<<"编号"<<setw(8)<<"分类"<<setw(8)<<"书名"<<setw(8)<<"作者"<<setw(8)<<"简介"<<endl;
cout<<head->m_no<<setw(8)<<head->bookCategory<<setw(8)<<head->bookName
<<setw(8)<<head->author<<setw(8)<<head->bookSummary<<endl;
p=p->next;
return ;
}
head=head->next;
}
cout<<"找不到要查询的编号."<<endl;
}
void CBooksadministrator::Inquiry(CBook *head,string str2)
{
CBook* p;
if(head->bookName==str2)
{
p=head;
head=head->next;
cout<<"编号"<<setw(8)<<"分类"<<setw(8)<<"书名"<<setw(8)<<"作者"<<setw(8)<<"简介"<<endl;
cout<<head->m_no<<setw(8)<<head->bookCategory<<setw(8)<<head->bookName
<<setw(8)<<head->author<<setw(8)<<head->bookSummary<<endl;
p=p->next;
return ;
}
else if(head->author==str2)
{
p=head;
head=head->next;
cout<<"编号"<<setw(8)<<"分类"<<setw(8)<<"书名"<<setw(8)<<"作者"<<setw(8)<<"简介"<<endl;
cout<<head->m_no<<setw(8)<<head->bookCategory<<setw(8)<<head->bookName
<<setw(8)<<head->author<<setw(8)<<head->bookSummary<<endl;
p=p->next;
return ;
}
while(head)
{
if(head->next==NULL)
{
cout<<"找不到要查询的图书"<<endl;
return;
}
if(head->next->bookName==str2)
{
p=head->next;
cout<<"编号"<<setw(8)<<"分类"<<setw(8)<<"书名"<<setw(8)<<"作者"<<setw(8)<<"简介"<<endl;
cout<<head->m_no<<setw(8)<<head->bookCategory<<setw(8)<<head->bookName
<<setw(8)<<head->author<<setw(8)<<head->bookSummary<<endl;
p=p->next;
return ;
}
if(head->next->author==str2)
{
p=head->next;
cout<<"编号"<<setw(8)<<"分类"<<setw(8)<<"书名"<<setw(8)<<"作者"<<setw(8)<<"简介"<<endl;
cout<<head->m_no<<setw(8)<<head->bookCategory<<setw(8)<<head->bookName
<<setw(8)<<head->author<<setw(8)<<head->bookSummary<<endl;
p=p->next;
return ;
}
head=head->next;
}
cout<<"找不到要查询的图书."<<endl;
}
int main()
{
CBook book;
CLibrary library;
CBooksadministrator booksadministrator;
string str;
begin:
cout<<"1->重建图书2->显示图书3->插入图书4->查询图书5->图书数目Q->退出"<<endl;
cin>>str;
if(str[0]=='1')
{
system("cls");
::head=booksadministrator.creat();
system("cls");
goto begin;
}
else if(str[0]=='2')
{
system("cls");
if(head==NULL)
{
cout<<"图书目录为空,请重新键入"<<endl<<"按回车键返回主菜单"<<endl;
cin.get();
cin.get();
system("cls");
goto begin;
}
library.showbook(head);
cout<<"操作完毕,按回车键返回之菜单"<<endl;
}
else if(str[0]=='3')
{
system("cls");
int m_no;
string bookCategory, bookName,author,bookSummary;
if(head==NULL)
{
cout<<"图书目录为空,请重新键入"<<endl<<"按回车键返回主菜单"<<endl;
cin.get();
cin.get();
system("cls");
goto begin;
}
for(i=k+1;;i++)
{
cout<<"图书编号: "<<i<<endl;
m_no=i;
cout<<"请输入图书分类(哲学/政治/经济):";
cin>>str;
bookCategory=str.c_str();
cout<<"请输入图书书名:";
cin>>str;
bookName=str.c_str();
cout<<"请输入作者:";
cin>>str;
author=str.c_str();
cout<<"请输入图书简介:";
cin>>str;
bookSummary=str.c_str();
booksadministrator.insert(head,m_no,bookCategory,bookName,author,bookSummary);
cout<<"是否继续录入(y/n)"<<endl;
cin>>str;
while(str!="y"&&str!="n")
{
cout<<"输入有误,重输(y/n)"<<endl;
cin>>str;
}
if(str=="y")
cout<<endl;
else
break;
}
cout<<"操作完毕,按回车键返回之菜单"<<endl;
}
else if(str[0]=='4')
{
system("cls");
string str1;
cout<<"1->按图书编号查找2->按图书书名查找3->按图书作者查找"<<endl;
cin>>str1;
if(str1[0]=='1')
{
int num;
if(head==NULL)
{
cout<<"图书目录为空,请重新键入"<<endl<<"按回车键返回主菜单"<<endl;
cin.get();
cin.get();
system("cls");
}
cout<<"请输入要查询的图书编号:";
cin>>str1;
while(!check(str1))
{
cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
cin>>str1;
}
num=atoi(str1.c_str());
booksadministrator.Inquiry(head,num);
}
else if(str1[0]=='2')
{
string BookName;
cout<<"输入你要查询的书名:";
cin>>BookName;
booksadministrator.Inquiry(head,BookName);
}
else
{
string m_author;
cout<<"输入你要查询的图书作者:";
cin>>m_author;
booksadministrator.Inquiry(head,m_author);
}
cout<<"操作完毕,按回车键返回之菜单"<<endl;
}
else if(str[0]=='5')
{
system("cls");
cout<<endl;
cout<<" 共有图书 ";
library.GetTotalbooks(head);
cout<<endl;
cout<<"操作完毕,按回车键返回之菜单"<<endl;
}
else
{
if(str[0]!='Q'&&str[0]!='q')
{
cout<<"请输入数字!按回车键返回"<<endl;
}
}
if(str[0]!='Q'&&str[0]!='q')
{
cin.get();
cin.get();
system("cls");
goto begin;
}
return 0;
}
第2个回答 2010-12-19
C++ 实现图书管理系统
2010-12-01 21:46
//book_class.h 代码如下:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
typedef struct book_class
{
string book_number;
string book_name;
string book_public_infor;
string book_type; //图书类型;
string book_pbulic_time;//出版时间;
}book;
class equal1
{
public:
string str;
equal1(string str1):str(str1)
{}
bool operator () (book & book1)
{
if(book1.book_name==str || book1.book_number==str || book1.book_public_infor==str || book1.book_type==str)
return true;
return false;
}
};
class book_manage
{
public:
list<book> book_store_list;
public:
book_manage(){}
void sort();
void show();
void insert();
void erase();
void inquire();
void save(){};
void show(list<book>::iterator itr);
friend bool operator < (const book &book1,const book &book2)
{
return book1.book_name<book2.book_name;
}
friend ostream & operator << ( ostream &out,const book &book1)
{
out<<book1.book_name<<" "<<book1.book_number<<" "
<<book1.book_pbulic_time<<" "<<book1.book_public_infor<<" "
<<book1.book_type<<endl;
return out;
}
};
//book_class.cpp 实现文件内容如下:
#include "book_class.h"
void book_manage::sort()
{
int number;
if(this->book_store_list.size()==0)
{
cout<<"no book in the store!"<<endl;
return ;
}
loop: cout<<"input the information you want the book sort by:"<<endl;
cout<<"you can input:"<<endl;
cout<<"1:book_name"<<endl;
cout<<"2:book_num"<<endl;
cout<<"3:book_public_time."<<endl;
cout<<endl;
cin>>number;
cout<<" input the infor:"<<endl;
switch(number)
{
case 1:
this->book_store_list.sort();
cout<<"the information of book had been sort by book's name!"<<endl;
break;
case 2:
break;
case 3:
break;
default:
cout<<"Ǹʲô"<<endl;
goto loop;
}
cout<<" this is the new sort!"<<endl;
}
void book_manage::show()
{
if(this->book_store_list.size()==0)
cout<<"no book in the store!"<<endl;
else
{
ostream_iterator<book> os(cout," ");
copy(this->book_store_list.begin(),this->book_store_list.end(), os);
}
system("pause");
return ;
}
void book_manage::insert()
{
book_class book1;
cout<<"input the new book's information:"<<endl;
cout<<"eg: 001 ߵѧ 2001-9-10 "<<endl;
cin>>book1.book_number>>book1.book_name>>book1.book_public_infor>>book1.book_pbulic_time>>book1.book_type;
this->book_store_list.push_back(book1);
cout<<"insert successfully!"<<endl;
system("pause");
}
void book_manage::erase()
{
string book_name;
cin>>book_name;
list<book>::iterator itr=find_if(this->book_store_list.begin(),this->book_store_list.end(),equal1(book_name));
if(itr==this->book_store_list.end())
cout<<"can't find the book!"<<endl;
else
this->book_store_list.erase(itr);
}
void book_manage::inquire()
{
string book_infor;
bool find_flag=false;
cin>>book_infor;
list<book>::iterator itr=find_if(this->book_store_list.begin(),this->book_store_list.end(),equal1 (book_infor));
if(itr==this->book_store_list.end())
{
cout<<"can't find the information you want!"<<endl;
return;
}
while(itr!=this->book_store_list.end())
{
this->show(itr);
++itr;
itr=find_if(itr,this->book_store_list.end(),equal1 (book_infor));
}
}
void book_manage::show(list<book>::iterator itr)
{
cout<<"you may need it!"<<endl;
cout<<(*itr).book_name<<" "<<(*itr).book_number<<" "
<<(*itr).book_pbulic_time<<" "<<(*itr).book_public_infor<<" "
<<(*itr).book_type<<endl;
}
//main.cpp 主文件如下:
#include "book_class.h"
using namespace std;
int main()
{
//ifstream infile("book.txt");
// book_manage store(infile);
book_manage store;
loop: cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "<<endl<<endl<<endl;
cout<<"\t welcome to sxl.book_manage 1.0"<<endl<<endl;
cout<<"\t\tthe is the menu!"<<endl<<endl;
cout<<"\t\t1: show the information of book !"<<endl;
cout<<"\t\t2: sort the book's information !"<<endl;
cout<<"\t\t3: insert some book!"<<endl;
cout<<"\t\t4: erase some book!"<<endl;
cout<<"\t\t5: inquire book!"<<endl;
cout<<"\t\t6: exit the system!"<<endl<<endl;
int chose;
cout<<"input your chose:";
cin>>chose;
switch(chose)
{
case 1:
system("cls");
store.show();
cout<<"press any key to continue!"<<endl;
system("cls");
goto loop;
case 2:
system("cls");
store.sort();
store.show();
system("cls");
goto loop;
case 3:
store.insert();
store.show();
system("cls");
goto loop;
case 4:
cout<<"input the book's name or num you want to erase:";
store.erase();
store.show();
system("cls");
goto loop;
case 5:
cout<<" you may input the book's number,or book's name or book's pulbic_type:";
cout<<" input the book's infor:"<<endl;
store.inquire();
system("pause");
system("cls");
goto loop;
case 6:
store.save();
return 0;
default:
cout<<" Ǹʲô?"<<endl;
system("pause");
cout<<"press any key to chose again!"<<endl;
system("cls");
goto loop;
}
return 0;
}
//希望你只作参考。别拿这个东西去交作业什么的。。。对你没有什么好出的。。。
代码还是得自己写,才能有收获呢。。。。祝你好运。。。 by sx_liang
第3个回答 2010-12-20
//---------------------------------------------------------------------------
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int reverse(int a)
{
int r=0;
while (a)
{
r=r*10+a%10;
a/=10;
}
return r;
}
int main(void)
{
int rnd[10];
ofstream ofs("reverse.txt",ofstream::app);
srand(time(NULL));
if (!ofs) return -1;
ofs<<"10个[0,25000]区间内的随机数:\t";
for (int i = 0; i<10; i++) {
rnd[i]=rand()%25001;
ofs<<rnd[i]<<"\t" ;
}
ofs<<endl;
ofs<<"逆序数中的奇数有:\t" ;
for (int i=0; i < 10; i++) {
int tem=reverse(rnd[i]);
if (tem%2) {
ofs<<tem<<"\t";
}
}
ofs<<endl;
ofs.close();
return 0;
}
//---------------------------------------------------------------------------本回答被网友采纳