c++图书管理

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Book
{
private:
    string name;
    string author;
    string publish;
    float price;
    string ISBN;
    int  code;
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>>name;
    }
    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 name;
    }
    string getau()
    {
        return author;
    }
    string getpub()
    {
        return publish;
    }
    float getprice()
    {
        return price;
    }
    string getis()
    {
        return ISBN;
    }
};
void Book::setall(int c,string n,string au,string pub,float p,string is)
{
    code=c;
   name=n;
    author=au;
    publish=pub;
    price=p;
    ISBN=is;
}
void Book::display()
{
    cout<<setw(2)<<code<<setw(13)<<name<<setw(10)<<author<<setw(20)<<publish<<setw(10)<<price<<setw(20)<<ISBN<<endl;
}
Book b[1000];
int i=0,temp=0;
int m=0;
void enter()//增加图书的函数
{
    system("cls");
    system("color 4");
    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()//修改信息的函数
{
    system("cls");
     system("color 5");
    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())//atoi()是直接可以把2字符串转为整型
        {
            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()//删除函数
 {
     system("cls");
     system("color e");
    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()//保存到文件的函数
{
    system("cls");
     system("color d");
    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()//从文本读取的函数
{
    system("cls");
     system("color c");
    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;    //读取完成后给i一个值,以便后面操作
                break;
            }
            b[j].setall(c,s1,s2,s3,p,s4);
            a=c;//判断是否读取完成
        }
        cout<<"-----------------------------文件已读取,请操作----------------------------------\n";
        infile.close();
    }
}
void out()//查询显示函数
{
    system("cls");
     system("color b");
    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<<"--------------------------------暂无图书!---------------------------------------";
    }
}
int main()
{
    system("cls");
    system("color a");
    int flag=0;
    string ch;
    bool sign=false;
    string choice;//原因是因为害怕用户使用时误入其他的字符,导致程序崩溃
    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;
        }
    }

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值