C++图书信息管理系统

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++图书信息管理系统

ōu yǐng   2022-05-28 我要评论

1.题目:

类型有:编号:ISBN
书名:name
价格:price

完成如下的功能:

①录入:从键盘输入(或从文件读入)图书(或学生)信息表的各个数据元素;
②查找:可按不同属性查找所有等于给定值的数据元素,找到并返回它们在表中的位序;
③插入:在表中第i(1=<i<=N+1)个位置插入一个新元素;
④删除:可删除表中第i(1=<i<=N)个位置上的元素;
⑤输出:依次打印表中的各个元素的值;
⑥排序:可按某属性对表中的元素进行排序。(可选)

2.实现方式:单链表(带头节点)

3.代码实现:

#include <iostream>
#include <string>
using namespace std;

struct Node
{
    int ISBN;//编号
    string name;//书名
    float price;//定价
    Node *next;
};

//操作类
class Link
{
private:
    int number;//图书数量
    Node *head;
public:
    Link(int a):number(a){}
    ~Link(){delete head;}
    void create_node();//创建
    void select();//功能选择
    int find_node(int i);//按照编号查找
    int find_node(string n);//按照书名查找
    int find_node(float p);//按照价格查找
    int insert_node(int pos);//插入
    int delete_node(int d);//删除
    int mod_node(int d);//修改
    void sort_node_ISBN();//按照编号排序
    void sort_node_price();//按照价格排序
    int get_node();//计数总数
    void print();//打印操作
};

void Link::create_node()
{
    Node *pnew;
    head = new Node;
    //cout<<"请按顺序输入图书的ISBN,书名,定价";
    head->ISBN = 0;
    head->name = 'n';
    head->price = 0;
    head->next = NULL;
    Node *ptemp = head;
    for(int i=0;i<number;i++)
    {
        pnew = new Node;
        cout<<endl;
        cout<<"请按顺序输入图书的ISBN,书名,定价:";
        cin>>pnew->ISBN>>pnew->name>>pnew->price;
        pnew->next = NULL;
        ptemp->next = pnew;
        ptemp = pnew;
    }
}

//按编号查找
int Link::find_node(int i)
{
    Node *ptemp = head->next;
    for(int count = 0;count<number;count++)
        {
            if(ptemp->ISBN == i)//按编号查找图书
            {
                cout<<"图书的编号为:"<<ptemp->ISBN<<"     书名为:"<<ptemp->name<<"       定价为:"<<ptemp->price<<endl;
                return 1;
            }
            ptemp = ptemp->next;
        }
        return 0;
}

//按照书名查找
int Link::find_node(string n)
{
    Node *ptemp = head->next;
    for(int count=0;count<number;count++)
        {
            if(ptemp->name == n)
            {
                cout<<"图书的编号为:"<<ptemp->ISBN<<"     书名为:"<<ptemp->name<<"       定价为:"<<ptemp->price<<endl;
                return 1;
            }
            ptemp = ptemp->next;
        }
        return 0;
}

//按照价格查找
int Link::find_node(float p)
{
    Node *ptemp = head->next;
    for(int count=0;count<number;count++)
        {
            if(ptemp->price == p)
            {
                cout<<"图书的编号为:"<<ptemp->ISBN<<"     书名为:"<<ptemp->name<<"       定价为:"<<ptemp->price<<endl;
                return 1;
            }
            ptemp = ptemp->next;
        }
    return 0;
}

//插入
int Link::insert_node(int pos)
{
    if((pos > number)||(pos<0))
    {
        cout<<"插入位置错误!"<<endl;
        return 0;
    }
    else
    {
        Node *ptemp = head,*pnew;
        for(int i=0;i<pos-1;i++)
        {
            ptemp = ptemp->next;
        }
        pnew = new Node;
        cout<<"请按顺序输入图书的ISBN,书名,价格:";
        cin>>pnew->ISBN>>pnew->name>>pnew->price;
        pnew->next = ptemp->next;
        ptemp->next = pnew;
        number += 1;
    }
    return 1;

}

//删除
int Link::delete_node(int d)
{
    if((d > number)||(d<0))
    {
        cout<<"删除位置错误!"<<endl;
        return 0;
    }
    else
    {
        Node *ptemp = head,*pdelete;
        for(int i=0;i<d-1;i++)
        {
            ptemp = ptemp->next;
        }
        pdelete = ptemp->next;
        ptemp->next = pdelete->next;
        delete pdelete;
        number -= 1;
    }
    return 1;
}

//修改
int Link::mod_node(int d)
{
    int aa;
    string bb;
    float cc;
    if((d > number)||(d<0))
    {
        cout<<"要修改的位置错误!"<<endl;
        return 0;
    }
    else
    {
        Node *ptemp = head->next;
        for(int i=0;i<d-1;i++)
        {
            ptemp = ptemp->next;
        }
        cout<<"要修改编号请输入0,要修改书名请输入1,要修改价格请输入2:";
        int k;
        cin>>k;
        switch(k)
        {
        case 0:
            cout<<"请输入要修改的编号:";
            cin>>aa;
            ptemp->ISBN = aa;
            cout<<endl;
            break;
        case 1:
            cout<<"请输入要更改的书名:";
            cin>>bb;
            ptemp->name = bb;
            cout<<endl;
            break;
        case 2:
            cout<<"请输入要更改的价格:";
            cin>>cc;
            ptemp->price = cc;
            cout<<endl;
            break;
        }
    }
    return 1;
}

//按编号排序
void Link::sort_node_ISBN()
{
    Node *ptemp = head->next,*pre;
    Node *pr = ptemp->next;
    ptemp->next = NULL;
    ptemp = pr;
    while(ptemp != NULL)
    {
        pr = ptemp->next;
        pre = head;
        while(pre->next != NULL && pre->next->ISBN > ptemp->ISBN)
        {
            pre = pre->next;
        }
        ptemp->next = pre->next;
        pre->next = ptemp;
        ptemp = pr;
    }
    Link::print();
}

//按照价格排序
void Link::sort_node_price()
{
    Node *ptemp = head->next,*pre;
    Node *pr = ptemp->next;
    ptemp->next = NULL;
    ptemp = pr;
    while(ptemp != NULL)
    {
        pr = ptemp->next;
        pre = head;
        while(pre->next != NULL && pre->next->price > ptemp->price)
        {
            pre = pre->next;
        }
        ptemp->next = pre->next;
        pre->next = ptemp;
        ptemp = pr;
    }
     Link::print();
}

//获取长度
int Link::get_node()
{
    return number;
}

//打印输出
void Link::print()
{
    Node *ptemp = head->next;
    for(int k=0;k<number;k++)
    {
        cout<<"图书编号:"<<ptemp->ISBN<<"       书名为:"<<ptemp->name<<"       价格为:"<<ptemp->price<<endl;
        ptemp = ptemp->next;
    }
}

//功能函数
void Link::select()
{
    int a;//ISBN
    string b;//书名
    float c;//定价
    int d;//位置
    int p;//选择功能
    cin>>p;
    switch(p)
    {
    case 0:
        cout<<"请输入图书的编号";
        cin>>a;
        if(this->find_node(a))
        {
            cout<<endl;
        }
        else
            cout<<"该图书不存在!"<<endl;
        break;
    case 1:
        cout<<"请输入图书的名字:";
        cin>>b;
        if(this->find_node(b))
        {
            cout<<endl;
        }
        else
            cout<<"该图书不存在!"<<endl;
        break;
    case 2:
        cout<<"请输入图书的价格:";
        cin>>c;
        if(this->find_node(c))
        {
            cout<<endl;
        }
        else
            cout<<"该图书不存在!"<<endl;
        break;
    case 3:
        cout<<"请输入要插入的位置:";
        cin>>d;
        if(this->insert_node(d))
        {
            cout<<"插入操作的结果为:"<<endl;
            this->print();//打印插入结果
        }
        break;
    case 4:
        cout<<"请输入要删除的位置:";
        cin>>d;
        if(this->delete_node(d))
        {
            cout<<"删除操作的结果为:"<<endl;
            this->print();//打印插入结果
        }
        break;
    case 5:
        cout<<"请输入要修改的图书的位置:";
        cin>>d;
        if(this->mod_node(d))
        {
            cout<<"修改后的结果为:"<<endl;
            this->print();
        }
        break;
    case 6:
        cout<<"按照图书的编号进行排序的结果为:"<<endl;
        this->sort_node_ISBN();
        break;
    case 7:
        cout<<"按照图书的价格进行排序的结果为:"<<endl;
        this->sort_node_price();
        break;
    case 8:
        cout<<"当前馆内的图书数量为:";
        cout<<this->get_node();
        break;
    }
}


int main()
{
    int sele=1;//功能选择
    int i;//最开始的数量
    cout<<"请输入你要输入的图书的数量:";
    cin>>i;
    Link l(i);
    l.create_node();
    cout<<endl;
    cout<<"0---------------------为查找(按编号)"<<endl;
    cout<<"1---------------------为查找(按书名)"<<endl;
    cout<<"2---------------------为查找(按定价)"<<endl;
    cout<<"3---------------------为插入"<<endl;
    cout<<"4---------------------为删除"<<endl;
    cout<<"5---------------------为修改"<<endl;
    cout<<"6---------------------为按照图书编号排序"<<endl;
    cout<<"7---------------------为按照图书的价格排序"<<endl;
    cout<<"8---------------------为显示当前馆内的图书总数"<<endl;
    cout<<"请输入要选择的功能:";
    while(sele == 1)
    {
        l.select();
        cout<<"是否要退出管理系统?(输入0退出、输入1继续)";
        cin>>sele;
        cout<<"请输入要选择的功能:";
    }
    cout<<"-----------已退出图书管理系统------------";
    return 0;
}

4.效果

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们