基于顺序表实现图书信息管理系统C++

#include<iostream>
#include<string>
#define MAXSIZE 1000
#define ElemType Book
#define Status bool
#define OK true
#define ERROR false

using namespace std;



//图书。信息为:书号、名称、价格
typedef struct
{
    int no;
    string name;
    float price;
}Book;
//定义顺序表
typedef struct
{
    ElemType *elem;
    int length;
}SqList;

//初始化顺序表
Status InitList(SqList &L)
{
    L.elem = new ElemType[MAXSIZE];
	//if(!L.elem) exit(OVERFLOW);
	L.length = 0;
	return OK;
}

//查询指定图书
void LocateElem(SqList &L,ElemType e)
{
    int i;
    for(i = 0;i < L.length;i++)
    {
        if(L.elem[i].no == e.no)
        {
            cout << "书号" <<  "\t书名\t" <<  "价格" << endl;
            cout <<  L.elem[i].no << "\t" << L.elem[i].name << "\t" << L.elem[i].price << endl;
            cout << endl;
            break;
        }
    }
    if(i == L.length)
    {
        cout << "该书号不存在,查询失败!" << endl;
        cout << endl;
    }
}

Status CompareElem(SqList &L,ElemType e)
{
    for(int i = 0;i < L.length;i++){
        if(L.elem[i].no == e.no)
            return OK;
    }
    return ERROR;
}

//初始插入书籍
Status ListInitInsert(SqList &L, int i, ElemType e){
	if((i < 1) || (i > L.length+1)) return ERROR;
	if(L.length == MAXSIZE) return ERROR;
	for(int j = L.length-1; j >= i-1; j--)
		L.elem[j+1] = L.elem[j];
	L.elem[i-1] = e;
	++L.length;
	return OK;
}

//插入一本书
Status ListInsert(SqList &L,ElemType e)
{
    if(CompareElem(L,e)) {
        cout << "该书号已存在,插入失败" << endl;
        cout << endl;
        return ERROR;
    }
    if(L.length >= MAXSIZE)
    {
        cout << "发生溢出,插入失败" << endl;
        cout << endl;
        return ERROR;
    }
    for(int j = L.length;j >= e.no;j--)
    {
        L.elem[j] = L.elem[j-1];
    }
    L.elem[e.no-1] = e;
    ++L.length;
    return OK;
}

//删除一本书
Status ListDelete(SqList &L,ElemType e)
{
    for(int i = 0;i < L.length;i++) {
        if(L.elem[i].no == e.no) {
            for(int j = i;j < L.length -1;j++) {
                L.elem[j].no = L.elem[j+1].no;
                L.elem[j].name = L.elem[j+1].name;
                L.elem[j].price = L.elem[j+1].price;
            }
            break;
        }
    }
    --L.length;
    return OK;
}

//修改图书信息
Status Listmodify(SqList &L,ElemType e)
{
    for(int i = 0;i < L.length;i++)
    {
        if(e.no == L.elem[i].no){
            L.elem[i].name = e.name;
            L.elem[i].price = e.price;
            return OK;
        }
    }
    return ERROR;
}

//显示全部图书信息
void showList(SqList &L)
{
    int temp;
    string temp1;
    float temp2;
    for(int k = 0;k < L.length - 1;k++) {
        for(int j = k;j < L.length - 1 - k;j++){
            if(L.elem[j].no > L.elem[j+1].no){
                temp = L.elem[j].no;
                L.elem[j].no = L.elem[j+1].no;
                L.elem[j + 1].no = temp;

                temp1 = L.elem[j].name;
                L.elem[j].name = L.elem[j+1].name;
                L.elem[j + 1].name = temp1;

                temp2 = L.elem[j].price;
                L.elem[j].price = L.elem[j+1].price;
                L.elem[j + 1].price = temp2;
            }
        }
    }
    cout << "书号" <<  "\t书名\t" <<  "价格" << endl;
    for(int i = 0;i < L.length;i++)
    {
        cout <<  L.elem[i].no << "\t" << L.elem[i].name << "\t" << L.elem[i].price << endl;
    }
}


int main()
{
    cout << "           图书信息管理系统" << endl;
    int num;
    SqList L;
    InitList(L);
    cout << "图书总数: ";
    cin >> num;
    for(int i = 0;i < num;i++)
    {
        cout << "请输入第" << i+1 << "书的信息" << endl;
        Book book;
        cin >> book.no >> book.name >> book.price;
        if(ListInitInsert(L,i+1,book))
            cout << "插入成功!" << endl;
        else
            cout << "插入失败。" << endl;
    }
    cout << "输入信息完毕" << endl;
    cout << endl;
    showList(L);
    cout << endl;
    bool flag = true;
    int op;
    while(flag)
    {
        cout << "请选择需要的操作." << endl;
        cout << "0 : 结束程序" << endl;
        cout << "1 : 查询指定图书" << endl;
        cout << "2 : 显示全部图书信息" << endl;
        cout << "3 : 添加一本书" << endl;
        cout << "4 : 删除一本书" << endl;
        cout << "5 : 修改书的信息" << endl;
        cin >> op;
        if(op == 0)
        {
            flag = false;
            cout << "退出成功!" << endl;
        }else if(op == 1) {
            cout << "请输入待查询图书书号:";
            Book b;
            cin >> b.no;
            cout << endl;
            LocateElem(L,b);
        }else if(op == 2) {
            cout << endl;
            showList(L);
            cout << endl;
        }else if(op == 3) {
            cout << "请输入这本书的信息: " << endl;
            Book book;
            cin >> book.no >> book.name >> book.price;
            if(ListInsert(L,book)){
                cout << "插入成功!" << endl;
            } else {
                cout << "插入失败!" << endl;
            }
        }else if(op == 4) {
            cout << "请输入要删除图书的书号: ";
            Book b;
            cin >> b.no;
            if(ListDelete(L,b)) {
                cout << "删除成功!" << endl;
                cout << endl;
            }else {
                cout << "删除失败!" << endl;
                cout << endl;
            }
        }else if(op == 5) {
            Book b;
            cout << "请输入要修改的图书信息: " << endl;
            cin >> b.no >> b.name >> b.price;
            if(Listmodify(L,b)){
                cout << "修改成功!" << endl;
                cout << endl;
            } else {
                cout << "修改失败!" << endl;
                cout << endl;
            }
        }else{
            cout << "输入错误,请重新选择!" << endl;
        }
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值