| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4439 人关注过本帖
标题:支持模糊查询方式,在查询时只需输入书名的一部分,所有符合这一条件的图书 ...
只看楼主 加入收藏
q740940561
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2013-3-16
结帖率:85.71%
收藏
已结贴  问题点数:4 回复次数:2 
支持模糊查询方式,在查询时只需输入书名的一部分,所有符合这一条件的图书都可显示出来。这个怎么弄?
图书管理系统
任务内容
任务概述:设计一个简单的图书管理程序,能新增、查询、显示、修改和删除图书信息。程序要求: 1)图书数据用文件来存储,按书名排序存放。查询显示时每屏不超过20个记录,超过时分屏显示。 2)记录修改:能选择修改图书的书名、作者和出版社,书号不可修改。在修改或删除之前需要用户进一步确认,确认无误后再进行操作。 3)支持模糊查询方式,在查询时只需输入书名的一部分,所有符合这一条件的图书都可显示出来。
#include<iostream>

#include<iomanip>

#include<string>

#include<fstream>

using namespace std;

class Book

{

public:

 void setall(int c,string n,string au,string pub,float p,string is);//对类中的私有数据进行设置

 void setcode(int t){code=t+1;}

 void setname(){cin>>bookname;}

 void setau(){cin>>author;}

 void setpub(){cin>>publish;}

 void setprice(){cin>>price;}

 void setis(){cin>>ISBN;}

 void display();

 int getcode(){return code;}

 string getname(){return bookname;}

 string getau(){return author;}

 string getpub(){return publish;}

 float getprice(){return price;}

 string getis(){return ISBN;}

private:

 string bookname;

 string author;

 string publish;

 float price;

 string ISBN;

 int  code;

};

void Book::setall(int c,string n,string au,string pub,float p,string is)

{

 code=c;

 bookname=n;

 author=au;

 publish=pub;

 price=p;

 ISBN=is;

}

void Book::display()

{  

  cout<<setw(2)<<code<<setw(13)<<bookname<<setw(10)<<author<<setw(20)<<publish<<setw(10)<<price<<setw(20)<<ISBN<<endl;

}

Book b[1000];

int i=0,temp=0;

int m=0;

class Person//管理员的类

{

public:

 void setnews(string a1,string a2)

 {

  account=a1;

  password=a2;

 }

 void setperson()

 {

  cout<<"\t\t\t请建立新账号:";

  cin>>account;

  cout<<endl;

  cout<<"\t\t\t请输入您想要的密码:";

  cin>>password;

  cout<<endl;

 }

 string getaccount(){return account;}

 string getpassword(){return password;}

private:

 string account;

 string password;

};

Person p[100];

void save_person()//保存管理员的信息

{

   ofstream outfile("administrator",ios::out);

   for(int j=0;j<m;j++)

    outfile<<p[j].getaccount()<<setw(20)<<p[j].getpassword()<<endl;

   outfile.close();

}

void get_person()//读取管理员的信息

{  

 string a1=" ",a2,c=" ";

 ifstream infile("administrator",ios::in);

 for(int j=0;j<100;j++)

    {

  infile>>a1>>a2;

  if(a1==c){m=j;break;}

  p[j].setnews(a1,a2);

  c=a1;

 }

}

void enter()//增加图书的函数

{  

 cout<<"--------------------------------------------------------------------------------"<<endl;

 cout<<"                   书名      作者   出版社         售价   ISBN编号"<<endl;

 cout<<"请依次输入"<<"<"<<i+1<<">"<<"号书:";

 b[i].setcode(i);

 b[i].setname();

 b[i].setau();

 b[i].setpub();

 b[i].setprice();

 b[i].setis();

 temp=1;//查看退出前是否保存的中间变量

 for(int j=0;j<i;j++)

   if(b[i].getname()==b[j].getname())

   {

    cout<<"--------------------------------抱歉,此书已存在!--------------------------------"<<endl;

    i=i-1;

    temp=0;

    break;

   }

   if(temp==1)

   {

    cout<<"--------------------------------添加已完成-------------------------------------"<<endl;

   }

}

void modify()//修改信息的函数

{   

  string bn,s1,s2,s3,s4;

  int middle=0,flag=0;

  float p;

  cout<<"请输入要修改的书名或编号:";

     cin>>bn;

  for(int j=0;j<i;j++)

   if(bn==b[j].getname()||atoi(bn.c_str())==b[j].getcode())

   {  

    middle=1;//查看有无图书的中间变量

             s1=b[j].getname();

    s2=b[j].getau();

    s3=b[j].getpub();

    p=b[j].getprice();

    s4=b[j].getis();

    cout<<"________________________________________________________________________________\n";

    cout<<"编号      书名     作者             出版社     售价           ISBN编号"<<endl;

    b[j].display();

    cout<<"_____________________请依次从书名进行修改(编号不可修改)_________________________"<<endl;

    cout<<"请修改:  ";

    b[j].setname();

    b[j].setau();

    b[j].setpub();

    b[j].setprice();

    b[j].setis();

    for(int c=0;c<i;c++)//循环判断是否修改图书重名

          if(b[c].getname()==b[j].getname())flag++;

    if(flag==1)

    {

     cout<<"--------------------------------修改成功!---------------------------------------"<<endl;

     temp=1;

    }

    else if(flag==2)

    {

      b[j].setall(b[j].getcode(),s1,s2,s3,p,s4);

      cout<<"------------------------------已存在此书,修改失败-------------------------------"<<endl;

    }

        break;

   }

   if(middle==0){cout<<"--------------------------------无此图书!---------------------------------------"<<endl;}

}

void dele()//删除函数

{

   string bn;

   int middle=0;

  cout<<"请输入要删除的书名或编号:";

     cin>>bn;

  for(int j=0;j<i;j++)

   if(bn==b[j].getname()||atoi(bn.c_str())==b[j].getcode())//判断编号和书名是否存在

   {   int choice;

       cout<<"----------------------------此书已找到,确定要删除?------------------------------"<<endl<<"1:确定"<<"2:取消"<<endl;

             cin>>choice;

    if(choice==1)

    {

          for(int k=j;k<i;k++){b[k]=b[k+1];b[k].setcode((b[k].getcode()-2));}

          cout<<"*********************************此书已删除*************************************"<<endl;

    i=i-1;

                middle=1;

    temp=1;

    break;

    }

    else if(choice==2)middle=1;

   }

   if(middle==0){cout<<"--------------------------------无此图书!---------------------------------------"<<endl;}

}

void save()//保存到文件的函数

{

   char fname[20];

   cout<<"请输入您想要存储的文件名称(不带扩展名):";

   cin>>fname;

   ofstream outfile(fname,ios::out);

   outfile<<"编号      书名     作者             出版社     售价           ISBN编号"<<endl;

   for(int j=0;j<i;j++)

    outfile<<setw(2)<<b[j].getcode()<<setw(13)<<b[j].getname()<<setw(10)<<b[j].getau()<<setw(20)<<b[j].getpub()<<setw(10)<<b[j].getprice()<<setw(20)<<b[j].getis()<<endl;

   cout<<"----------------------------------文件已保存------------------------------------\n";

   outfile.close();

   temp=0;

}

void open()//从文本读取的函数

{  

 string s1,s2,s3,s4,s5,s6;

 int c=1000,a=1000;

 float p;

 char fname[20];

    cout<<"请输入您想要打开的文件名称(不带扩展名,默认文件名为file):";

    cin>>fname;

 ifstream infile(fname,ios::in);

 if(!infile)

    {

    cout<<"-------------------------------不存在此文件!------------------------------------"<<endl;

    }

 else if(infile)

    {

  infile>>s1>>s2>>s3>>s4>>s5>>s6;

     for(int j=0;j<1000;j++)

  {   

  infile>>c>>s1>>s2>>s3>>p>>s4;

  if(c==a){i=j;break;}//读取完成后给i一个值,以便后面操作

     b[j].setall(c,s1,s2,s3,p,s4);

  a=c;//判断是否读取完成

  }

      cout<<"-----------------------------文件已读取,请操作----------------------------------\n";

   infile.close();

 }

}

void out()//查询显示函数

{  

 int middle=0;

 string bn;

 int choice;

 if(i!=0)//有书存在

 {

     cout<<"----------------------1:对单个图书进行查询2:查询全部图书-----------------------"<<endl;

  cout<<"请选择:";

  cin>>choice;

  if(choice==1)

  {

   cout<<"请输入要查询的书名或编号:";

            cin>>bn;

         for(int j=0;j<i;j++)

         if(bn==b[j].getname()||atoi(bn.c_str())==b[j].getcode())//判断编号和书名是否存在,调用函数

      {  

        middle=1;

        cout<<"________________________________________________________________________________\n";

         cout<<"编号      书名     作者             出版社     售价           ISBN编号"<<endl;

        b[j].display();

     cout<<"________________________________________________________________________________\n";

     break;

      }

      if(middle==0){cout<<"--------------------------------无此图书!---------------------------------------"<<endl;}

  }

  else if(choice==2)

        {

   cout<<"------------------------------------------------------------------------------\n";

         cout<<"编号      书名     作者             出版社     售价           ISBN编号"<<endl;

         for(int j=0;j<i;j++)

               b[j].display();

      cout<<"------------------------------------------------------------------------------\n";

  }

 }

    else{cout<<"--------------------------------暂无图书!---------------------------------------";}

}

void main()

{  

 int flag=0;

 string ch;

 bool sign=false;

 string choice;//原因是因为害怕用户使用时误入其他的字符,导致程序崩溃

    cout<<"*******************************欢迎进入图书管理系统*****************************"<<endl;

    while(1)

{  cout<<"\t\t\t--------------------"<<endl;

 cout<<"\t\t\t1:登陆管理员账号"<<endl;

 cout<<"\t\t\t--------------------"<<endl;

 cout<<"\t\t\t2:申请管理员账号"<<endl;

 cout<<"\t\t\t--------------------"<<endl;

 cout<<"\t\t\t3:退出本系统"<<endl;

 cout<<"\t\t\t--------------------"<<endl;

    cout<<"请选择:";

 cin>>ch;

  if(atoi(ch.c_str())==1)//登陆信息

  {  

  get_person();

  string p1,p2;

  cout<<"\t\t\t**********************"<<endl;

  cout<<"\t\t\t请输入账号:";

  cin>>p1;

  cout<<"\t\t\t***********************"<<endl;

  cout<<"\t\t\t请输入密码:";

  cin>>p2;

  cout<<"\t\t\t***********************"<<endl;

  for(int a=0;a<m;a++)

   if(p1==p[a].getaccount()&&p2==p[a].getpassword())

   {sign=true;break;}

   if(sign==false)

   {cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!输入登录信息错误,请重新操作!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;}

  }

  else if(atoi(ch.c_str())==2)//申请账号

  {

   get_person();

   while(true)

   {

    p[m].setperson();

    for(int j=0;j<m;j++)

        if(p[m].getaccount()==p[j].getaccount())

     {cout<<"---------------------------此账号已经存在,请重新申请----------------------------"<<endl<<endl;flag=1;break;}

        else flag=0;

    if(flag==0)break;

         }

   m++;

   save_person();//调用储存函数

      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~申请完成,请重新登录!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;sign=false;

  }

  else if(atoi(ch.c_str())==3)

  {

   cout<<"*********************************谢谢使用本系统*********************************";

   break;

  }

  else cout<<"-------------------------!Warning! please input again:--------------------------";

  if(sign==true)break;

 }

if(sign==true)

{

 cout<<"--------------------------------成功登陆,请操作---------------------------------"<<endl;

 while(1)

 {

  cout<<"\t1:导入图书文件"<<endl;

  cout<<"\t2:--增添图书--"<<endl;

  cout<<"\t3:修改图书信息"<<endl;

  cout<<"\t4:--删除图书--"<<endl;

  cout<<"\t5:保存到文本文件"<<endl;

  cout<<"\t6:--查询图书--"<<endl;

  cout<<"\t7:退出"<<endl;

        cout<<"请选择:";

  cin>>choice;

  if(atoi(choice.c_str())==1)open();

  else if(atoi(choice.c_str())==2){enter();i++;}

  else if(atoi(choice.c_str())==3)modify();

  else if(atoi(choice.c_str())==4)dele();

  else if(atoi(choice.c_str())==5)save();

  else if(atoi(choice.c_str())==6)out();

  else if(atoi(choice.c_str())==7)

  {  

   int choice;

   if(temp==0)//判断是否用户在退出前对文件做了其他的操作,如操作则可选择其他两功能

   {

    cout<<"*********************************谢谢使用本系统*********************************";

    break;

   }

   else

   {

    cout<<"--------您还没有保存刚才的图书信息,您可选择--------1:退出并且保存2:直接退出"<<endl;

    cin>>choice;

    if(choice==1)

    {save();cout<<"*********************************谢谢使用本系统*********************************"<<endl;break;}

    else if(choice==2)

    {cout<<"*********************************谢谢使用本系统*********************************"<<endl;break;}

   }

  }

  else cout<<"-------------------------!Warning! please input again:--------------------------"<<endl;

 }

}

}
搜索更多相关主题的帖子: 图书 管理程序 查询方式 include 
2013-04-25 16:15
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:2 
这个有点麻烦啊!这两天看这么长的有点多了,你去网上查模糊算法,看看有没有什么启示

Maybe
2013-04-25 21:38
y3765258
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:172
注 册:2013-4-9
收藏
得分:2 
数据结构中得图吧。

有问题一起探讨,一起进步。
2013-04-26 09:24
快速回复:支持模糊查询方式,在查询时只需输入书名的一部分,所有符合这一条件的 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017435 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved