图书管理系统1

来源:互联网 发布:java多分支语句有哪些 编辑:程序博客网 时间:2024/05/24 07:18

不要问我为什么会有图书管理系统1.。。。

那是因为。。。

肯定会有图书管理系统2啊。。。

嘻嘻~~

首先,在book.h里面定义一个book类

当然类中是有成员函数和成员变量的咯。。。。

#include<iostream>using namespace std;//定义一个图书类,其中图书含有的信息是:书的序号、书名(要求在40个字符以内)、书的作者(40个字符以内)。//同时书含有删除标志,含有getdetail、display、print函数,用来输出关于图书的相关内容。class book             //定义一个图书类{  public:    int booknumber;    //书序号    char bookname[40]; //书名    char author[40];   //作者    bool flagg=0;      //删除标志    int getdetail()    //getdetail函数用来输入图书的相关信息    {        cout<<"|>>>>>>>>>>>>>BOOKNUMBER:        |";        cin>>booknumber;        cout<<"|>>>>>>>>>>>>>BOOKNAME:          |";        cin>>bookname;        cout<<">>>>>>>>>>>>>>AUTHOR:            |";        cin>>author;    }    void display()    //用于输出所要输出的图书信息的提示语    {        cout<<"|BOOKNUMBER   BOOKNAME   AUTHOR   |"<<endl;    }    void print()      //输出图书的相关信息    {        cout<<"   "<<booknumber<<"   "<<bookname<<"   "<<author<<endl;    }    int getbooknumber() //得到书的序号信息    {        return booknumber;    }    char *getbookname() //得到书名信息    {        return bookname;    }    char *getauthor()  //得到书作者信息    {        return author;    }};

然后。。。

就是有那么一个含有比较长。。。代码的cpp文件,来实现具体的功能。。。

然后,不出你所料,我就要。。。。

。。。

。。。

放代码啦~~

#include"book.h"#include<iostream>#include<stdlib.h>#include<string.h>#include<fstream>using namespace std;book books[10000];     //定义一个数组int top=-1;void menu()            //menu函数,输出的主界面,包含有整个工程的所有功能模块{    cout<<"|============================================|"<<endl;    cout<<"|  Welcome to the Book Management System!    |"<<endl;    cout<<"|============================================|"<<endl;    cout<<"|                                            |"<<endl;    cout<<"| 图书管理:                                 |"<<endl;    cout<<"| <1> 新增图书                               |"<<endl;    cout<<"| <2> 浏览图书                               |"<<endl;    cout<<"| <3> 查找图书                               |"<<endl;    cout<<"| <4> 删除图书                               |"<<endl;    cout<<"| <5> 退出系统                               |"<<endl;    cout<<"|  请选择服务项目                            |"<<endl;    cout<<"|+------------------------------------------+|"<<endl;}void set()   //添加图书的程序,使用输入输出流来打开book。txt并添加图书信息{    system("cls");    cout<<"    欢迎添加图书!"<<endl<<endl;    ofstream outfile("book.txt",ios::app);    ifstream infile("book.txt",ios::app);    book b1;    b1.getdetail();    outfile.write((char*)&b1,sizeof(b1));    outfile.close();    cout<<"添加成功!"<<endl<<endl<<endl;}void dis()    //查询图书。运用文件输入输出流的相关知识将book。txt中所存储的所有的图书输出{    system("cls");    cout<<"书库中所有图书为:"<<endl;    ofstream outfile("book.txt",ios::app);    ifstream infile("book.txt",ios::app);    book b1;    b1.display();    while(infile.read((char*)&b1,sizeof(b1)))    {        if(b1.flagg==0)        b1.print();    }    infile.close();    outfile.close();}//查询图书功能,可以按照图书的编号、作业、书名来进行查询。倘若查询的到图书的信息就输出信息,查询不到就输出图书不存在。void chaxun(){    system("cls");    cout<<"     欢迎查询图书!"<<endl<<endl<<endl;    int number,x;    char author[40],name[40];    book b1;    cout<<"1.按图书编号进行查找"<<endl<<endl;    cout<<"2.按图书作者进行查找"<<endl<<endl;    cout<<"3.按图书名进行查找"<<endl<<endl;    cin>>x;    if(x==1)    {        cout<<"需要查找的图书的标号为:"<<endl;        ifstream infile("book.txt",ios::app);        while(cin>>number)        {        infile.read((char*)&b1,sizeof(b1));        if(number==b1.getbooknumber()&& b1.flagg==0)        {            b1.display();            b1.print();            ofstream outfile("book.txt",ios::app);            infile.close();            outfile.close();            break;        }        else        {            cout<<"没有找到这个标号的图书。。。"<<endl;            cout<<"是否继续查找?y.......or..........n?"<<endl;            char c;            cin>>c;            if(c=='n')                      {                          system("cls");                          menu();                      }            else chaxun();        }        }    }    if(x==2)    {        cout<<"需要查找的作者姓名为:"<<endl;            cin>>author;        while(true)        {            ifstream infile("book.txt",ios::app);            infile.read((char*)&b1,sizeof(b1));            if(strcmp(author,b1.getauthor())==0&& b1.flagg==0)            {               b1.display();                b1.print();               ofstream outfile("book.txt",ios::app);               infile.close();               outfile.close();               break;            }               else        {            cout<<"没有找到这个作者的图书。。。"<<endl;            cout<<"是否继续查找?y.......or..........n?"<<endl;            char c;            cin>>c;            if(c=='n')                      {                          system("cls");                          menu();                      }            else chaxun();        }        }    }        if(x==3)    {        cout<<"需要查找的书名为:"<<endl;            cin>>name;        while(true)        {            ifstream infile("book.txt",ios::app);            infile.read((char*)&b1,sizeof(b1));            if(strcmp(name,b1.getbookname())==0&& b1.flagg==0)            {               b1.display();                b1.print();               ofstream outfile("book.txt",ios::app);               infile.close();               outfile.close();               break;            }        else        {            cout<<"没有找到这个书名的图书。。。"<<endl;            cout<<"是否继续查找?y.......or..........n?"<<endl;            char c;            cin>>c;            if(c=='n')                      {                          system("cls");                          menu();                      }            else chaxun();        }        }    }}void shanchu()  //删除图书功能。按照图书的标号来进行删除。//查找到书后进行删除并输出图书的相关信息,倘若没有查询到图书,就输出图书不存在{    system("cls");    cout<<"现在进入的是删除图书项目!"<<endl<<endl;    book b1,b2;    top=-1;    int number;    bool flag = true;    cout<<"需要删除的图书的标号为:"<<endl;    ifstream infile("book.txt",ios::app);    while(cin>>number)    {        infile.read((char*)&b1,sizeof(b1));        if(number==b1.getbooknumber())        {            b1.display();            b1.print();            cout<<"删除成功!"<<endl<<endl;            flag=false;            infile.close();            fstream infile("book.txt",ios::app);            while (1)            {                infile.read((char*)&b2,sizeof(b2));                if (!infile)                    break;                top++;                books[top]=b2;            }            infile.close();            fstream file1("book.txt",ios::out);            for (int i=0; i<=top; i++)                if (number!=books[i].getbooknumber())                    file1.write((char *)&books[i],sizeof(books[i]));            file1.close();            break;        }        else        {            cout<<"对不起,你所要删除的书不存在!"<<endl;            cout<<"还要继续删除文件吗?y.............or.............n?"<<endl;            char a;            cin>>a;            if(a=='n')            {                break;            }            else shanchu();        }        if (flag==0)        {            ofstream outfile("book.txt", ios::out);            ifstream infile("temp.txt", ios::in);            if (!outfile|| !infile)            {                return;            }            outfile.close();            infile.close();        }    }}void tuichu()//退出功能,在退出之前询问是否确定退出,倘若确定,则退出。{    char a;    system("cls");    cout<<"确定退出吗?y......or.......n"<<endl;    while(cin>>a)    {        if(a=='y')        {            cout<<"按任意键关闭窗口"<<endl;            exit(0);        }    else    {        menu();        int b;        while(cin>>b)        {            switch(b)            {                case 1:set();break;                case 2:dis();break;                case 3:chaxun();break;                case 4:shanchu();break;                case 5:tuichu();break;            }        }    }    }}
其实我也不是故意直接放代码,不讲解的。。。

好吧。。。

那,,,

为了表示诚意,我可以来具体讲讲主函数文件。。。

//打开book.cpp和book.h,含有主函数。#include"book.cpp"#include<iostream>#include<stdlib.h>#include<string.h>#include<fstream>using namespace std;int main()    //主函数中调用menu函数,输出界面,然后根据用户键盘输入来选择相应的功能。{    cout<<"你好!!!<<<      user      >>>"<<endl<<endl<<endl;    cout<<"这里是图书管理系统~"<<endl<<endl<<endl;    int choice;    while(true)    {        menu();        cin>>choice;        switch(choice)        {            case 1:set();break;            case 2:dis();break;            case 3:chaxun();break;            case 4:shanchu();break;            case 5:tuichu();break;        }    }}

嗯。。。

这几行代码,,,

还是自己看吧。。。

原创粉丝点击