实验一代码

#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
//图书信息的定义
typedef struct {
    char no[8];     //8位书号
    char name[20];  //书名
    double price;   //价格
}Book;

//链表的定义
typedef struct LNode {
    Book data;            //数据域
    struct LNode* next;   //指针域
}LNode, * LinkList;
LNode* first;             //定义链表头结点

//显示所有图书
void print() {
    cout << "当前管理系统中全部图书信息如下:" << endl;
    LNode* p = first->next;
    while (p) {
        cout << p->data.no << ' ' << p->data.name << ' ';
        cout << setiosflags(ios::fixed) << setprecision(2) << p->data.price << endl;//保留两位小数
        p = p->next;
    }
}
//插入图书
void insert(int i, Book book) {
    LNode* p = first;
    int j = 0;
    //寻找插入位置的前一个位置
    while (p && j < i - 1) 
    { p = p->next; j++; }
    if (!p) cout << "位置出错" << endl;
    else {
        LNode* s = new LNode();
        s->data = book;
        s->next = p->next;
        p->next = s;
    }
}
//删除图书
void Delete(int i) {
    LNode* p = first;
    int j = 0;
    //寻找删除位置前一个位置
    while (p && j < i - 1) 
    { p = p->next; j++; }
    if (!p || !p->next)cout << "位置出错" << endl;
    else {
        LNode* q = p->next;
        Book book = q->data;
        p->next = q->next;
        cout << "删除的图书信息为:" << q->data.no << q->data.name << setiosflags(ios::fixed) << setprecision(2) << q->data.price << endl;
        delete q;
    }
}

//统计图书总数
int statis() {
    LNode* p = first->next;
    int count = 0;
    while (p) {
        count++;
        p = p->next;
    }
    return count;
}

//图书去重
void norepetition() {
    LNode* p = first->next, * r = p->next;
    while (p) {
        r = p->next;
        while (r) {
            if (strcmp(p->data.no, r->data.no) == 0) {
                p->next = r->next;
            }
            r = r->next;
        }
        p = p->next;
    }
}
//查找喜爱图书
void inquire(char name[]) {
    LNode* p = first->next;
    Book book[100];
    int i = 0;
    while (p) {
        if (strcmp(p->data.name, name) == 0) {   //比较图书名称
            book[i] = p->data;
            i++;
        }
        p = p->next;
    }
    if (i == 0) { cout << "抱歉,没有你的最爱!" << endl; }
    else {
        cout << "查询到的图书数量为:" << i << endl << "图书信息如下:" << endl;
        for (int j = 0; j < i; j++) {
            cout << book[j].no << ' ' << book[j].name << ' ' << book[j].price << endl;
        }
    }
}

//批量修改
void modific() {
    LNode* p = first->next;
    double average = 0;
    while (p) {
        average += p->data.price;  //依次求平均数
        p = p->next;
    }
    p = first->next;  //重置p指针
    while (p) {
        if (p->data.price < average) {
            p->data.price *= 1.2;
        }
        else { p->data.price *= 1.1; }
        p = p->next;
    }
}

//快速排序按照图书价格降序排序
void sort() {
    LNode* p = first->next;
    LNode* q = p->next;
    while (p != NULL) {
        q = p->next;
        while (q != NULL) {
            if (p->data.price < q->data.price) {
                Book x;
                x = p->data;
                p->data = q->data;
                q->data = x;
            }q = q->next;
        }p = p->next;
    }
}

//最贵图书的查找
void expensive() {
    LNode* p = first->next;
    double max = 0;
    while (p) {
        if (p->data.price > max) 
        {
        max = p->data.price; 
        }
        p = p->next;
    }
    cout << "最贵图书如下:" << endl;
    p = first->next;     //重置p指针
    while (p) {
        if (p->data.price == max) 
        { 
        cout << p->data.no << ' ' << p->data.name << ' ' << setiosflags(ios::fixed) << setprecision(2) << p->data.price << endl; 
        }
        p = p->next;
    }
}

void Menu() {
    cout << "欢迎使用图书信息管理系统" << endl;
    cout << "请输入序号对应的功能:"   << 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 << "9.查找最贵图书"           << endl;
    cout << "10.退出"                  << endl;
}

int main() {
    Book book[100];       //定义图书存储空间
    bool tag = true;
    int number = 0,n;       
    while (tag) {
        cout << "请输入需输入的书本数量:";
        cin >> n; 
        cout << "请输入书号,书名和价格(中间以空格符隔开)" << endl;
        for (int i = 0; i < n;i++) {
            cin >> book[number].no;
            cin >> book[number].name;
            cin >> book[number].price;
            number++;
        }
        tag = false;
    }
    //尾插法
    first = new LNode();
    first->next = NULL;       //得到尾指针
    LNode* p = first, * s;
    for (int i = 0; i < number; i++) { 
        s = new LNode();
        s->data = book[i];
        s->next = NULL;
        p->next = s;
        p = s;
    }
    p->next = NULL;
    int mode = 0;
    while (1) {
        Menu();
        cin >> mode;
        switch (mode) {
        case 1: {
            print();
            break;
        }
        case 2: {
            cout << "请输入要插入的位置以及图书信息(中间用空格隔开):" << endl;
            int i;//插入位置
            cin >> i >> book[number].no >> book[number].name >> book[number].price;
            insert(i, book[number]);
            break;
        }
        case 3: {
            int i;//删除位置
            cin >> i;
            Delete(i);
            break;
        }
        case 4: {
            cout << "图书管理系统中总共有:" << statis() << "本书。" << endl;
            break;
        }
        case 5: {
            norepetition();
            break;
        }
        case 6: {
            char name[40];
            cin >> name;
            inquire(name);
            break;
        }
        case 7: {
            modific();
            break;
        }
        case 8: {
            sort();
            break;
        }
        case 9: {
            expensive();
            break;
        }
        }
    }
}   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值