C++ 单链表实现 图书管理系统

C++ 单链表实现 图书管理系统

一、题目要求:

用链表实现图书管理系统,包括如下的功能:

查找:按不同属性查找所有等于给定值 的数据元素 ,找到并返回它们;

插入:在表中第 i (1=<i<=N+1)个位置插入一个新的数据元素;

删除:可删除表中第 i (1=<i<=N)个位置上的数据元素;

输出:依次打印表中的各个元素的值;

排序:可按某属性对表中的元素进行排序。

二、代码实现

1、ManageSystem.h文件

#pragma once

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

class Book  //书本类
{
   
    friend class ListNode;
    friend class ListChain;
protected:
    long long ISBN;  //书号
    string name;  //书名
    int price;  //价格
public:
    Book();  //无参构造函数,创建一个Book对象
    Book(long long, string, int);  //含参构造函数,创建一个Book对象
    Book(const Book&);  //拷贝构造函数
    ~Book();  //析构函数
    Book& operator=(const Book&);  //重载=
    friend istream& operator>>(istream&, Book&);  //重载输入>>
    friend ostream& operator<<(ostream&, Book&);  //重载输出<<
    long long GetISBN();  //返回书号
    string Getname();  //返回书名
    int Getprice();  //返回价格
    void ChangeISBN(long long);  //修改书号
    void Changename(string);  //修改书名
    void Changeprice(int);  //修改价格
};

class ListNode  //结点类
{
   
public:
    Book data;  //数据域
    ListNode *next;  //指针域
};

class ListChain  //链表类
{
   
protected:
    ListNode *head;  //头指针
    int length;  //长度
public:
    ListChain(); //无参构造函数,建立一个空链表(只包含头结点)
    void InputTail(const Book bl[], const int);  //尾插法将数据读入
    void ClearChain();  //删除链表
    void ShowListChain();  //遍历链表
    Book Find(long long);  //依据书号查找
    Book Find(string);  //依据书名查找
    bool Insert(const Book&, int);  //插入
    bool Delete(int);  //删除
    bool Sort(string);  //排序
};

2、ManageSystem.cpp文件

#include "ManageSystem.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

Book::Book() {
   };  //无参构造函数,创建一个Book对象

Book::Book(long long i, string n, int p):ISBN(i), name(n), price(p) {
   };  //构造函数,创建一个Book对象

Book::Book(const Book& temp):ISBN(temp.ISBN), name(temp.name), price(temp.price) {
   };  //拷贝构造函数

Book::~Book() {
   };  //析构函数

long long Book::GetISBN() {
   return ISBN;}  //返回书号

string Book::Getname() {
   return name;}  //返回书名

int Book::Getprice(
  • 9
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 控制台图书管理系统是一种基于命令行界面运行的图书管理系统。相比于传统的图形界面,控制台界面更加轻量化且更加适合用于一些简单的管理操作。下面是一个简单的c程序用于实现图书管理系统。 首先,我们需要定义一个结构体用于存储每本书的信息: ```c typedef struct { char name[50]; // 书名 char author[30]; // 作者 int year; // 出版年份 int pages; // 页数 } Book; ``` 接着,我们需要定义一些函数来实现图书管理系统的基本操作,如添加图书、删除图书、查找图书等。 ```c Book library[100]; // 存储图书信息的数组 int bookCount = 0; // 记录图书数量 // 添加图书 void addBook() { Book book; printf("请输入书名:"); scanf("%s", book.name); printf("请输入作者:"); scanf("%s", book.author); printf("请输入出版年份:"); scanf("%d", &book.year); printf("请输入页数:"); scanf("%d", &book.pages); library[bookCount++] = book; } // 删除图书 void deleteBook() { char name[50]; printf("请输入要删除的书名:"); scanf("%s", name); for (int i = 0; i < bookCount; i++) { if (strcmp(library[i].name, name) == 0) { for (int j = i; j < bookCount - 1; j++) { library[j] = library[j+1]; } bookCount--; printf("删除成功!\n"); return; } } printf("未找到该书!\n"); } // 查找图书 void searchBook() { char name[50]; printf("请输入要查找的书名:"); scanf("%s", name); for (int i = 0; i < bookCount; i++) { if (strcmp(library[i].name, name) == 0) { printf("书名:%s\n", library[i].name); printf("作者:%s\n", library[i].author); printf("出版年份:%d\n", library[i].year); printf("页数:%d\n", library[i].pages); return; } } printf("未找到该书!\n"); } // 显示图书信息 void displayBooks() { for (int i = 0; i < bookCount; i++) { printf("书名:%s\n", library[i].name); printf("作者:%s\n", library[i].author); printf("出版年份:%d\n", library[i].year); printf("页数:%d\n", library[i].pages); } } ``` 最后,在 main 函数中,我们可以使用一个循环来接收用户输入的命令,并调用相应的函数来执行操作。下面是一个简单的示例: ```c int main() { while (1) { printf("\n"); printf("请输入命令:\n"); printf("1. 添加图书\n"); printf("2. 删除图书\n"); printf("3. 查找图书\n"); printf("4. 显示所有图书\n"); printf("5. 退出\n"); int command; scanf("%d", &command); switch (command) { case 1: addBook(); break; case 2: deleteBook(); break; case 3: searchBook(); break; case 4: displayBooks(); break; case 5: return 0; default: printf("未知命令!\n"); } } } ``` 上述代码实现了一个基本的控制台图书管理系统。用户可以通过命令行界面使用这个系统来添加、删除、查找和显示图书信息。但是,这个系统还有很多需要完善的地方,比如可以添加一些输入检查、错误处理和持久化存储等功能,以提升系统的可靠性和稳定性。 ### 回答2: 首先,我们需要明确图书管理系统所需要的功能。通常,这些功能包括添加书籍、删除书籍、修改书籍信息、查询书籍信息等。 接下来,我们可以使用C语言编写一个简单的图书管理系统。首先,我们需要创建一个结构体,用于储存书籍信息。 typedef struct book{ char name[50]; //书名 char author[50]; //作者 int ISBN; //ISBN号 float price; //价格 }BOOK; 接下来,我们可以创建一个数组,来储存图书馆中的所有书籍。 BOOK books[100]; 接下来,我们可以设计添加书籍、删除书籍、修改书籍信息等操作的函数。 void add_book(){ int num; printf("请输入要添加的书籍数量:"); scanf("%d",&num); for(int i=0;i<num;i++){ printf("请输入书名:"); scanf("%s",books[i].name); printf("请输入作者:"); scanf("%s",books[i].author); printf("请输入ISBN号:"); scanf("%d",&books[i].ISBN); printf("请输入价格:"); scanf("%f",&books[i].price); printf("\n"); } printf("添加成功!\n"); } void del_book(){ int ISBN; printf("请输入要删除的书籍的ISBN号:"); scanf("%d",&ISBN); for(int i=0;i<100;i++){ if(books[i].ISBN==ISBN){ for(int j=i;j<99;j++){ books[j]=books[j+1]; } printf("删除成功!\n"); return; } } printf("没有找到这本书!\n"); } void modify_book(){ int ISBN; printf("请输入要修改的书籍的ISBN号:"); scanf("%d",&ISBN); for(int i=0;i<100;i++){ if(books[i].ISBN==ISBN){ printf("请输入修改后的书名:"); scanf("%s",books[i].name); printf("请输入修改后的作者:"); scanf("%s",books[i].author); printf("请输入修改后的价格:"); scanf("%f",&books[i].price); printf("修改成功!\n"); return; } } printf("没有找到这本书!\n"); } void query_book(){ char name[50]; printf("请输入要查询的书籍的书名:"); scanf("%s",name); int flag=0; for(int i=0;i<100;i++){ if(strcmp(books[i].name,name)==0){ printf("书名:%s\n",books[i].name); printf("作者:%s\n",books[i].author); printf("ISBN号:%d\n",books[i].ISBN); printf("价格:%.2f\n\n",books[i].price); flag=1; } } if(flag==0) printf("没有找到这本书!\n"); } 以上就是一个简单的控制台图书管理系统实现代码。当然,我们还可以根据需要添加更多的功能,比如按照价格、作者等条件进行查询。同时,我们也可以将系统的界面进行美化,增加用户的交互性和易用性。 ### 回答3: 控制台是计算机中最基本的用户界面,也是学习计算机编程的必经之路。在这个掌握了基本编程知识的阶段,我们可以尝试实现一个简单的控制台图书管理系统,来练习我们的编程能力。 一个简单的控制台图书管理系统具有以下功能: 1. 添加图书:输入图书的名称、作者、ISBN号、出版社和价格等信息,保存到图书库中。 2. 查找图书:按照图书的名称、作者、ISBN号进行查询,并显示查询结果。 3. 修改图书:根据用户的选择,修改图书的名称、作者、ISBN号、出版社或价格等信息。 4. 删除图书:根据用户的选择,删除图书信息。 实现这个系统需要使用编程语言和数据库管理系统。我们可以使用C语言来实现,因为它是一种高效、稳定、跨平台的编程语言,同时也是学习编程的入门语言。我们需要安装C编译器和SQLite数据库。 在程序中,我们需要先定义一个图书结构体,用来保存图书的信息,如下所示: ```c typedef struct { int id; char name[50]; char author[20]; char isbn[20]; char press[20]; double price; } book; ``` 接下来,可以有以下编程步骤: 1. 连接SQLite数据库,并创建图书表。 ```c sqlite3 *db; sqlite3_open("books.db", &db); char *sql = "CREATE TABLE book(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,author TEXT,isbn TEXT,press TEXT,price DOUBLE);"; sqlite3_exec(db, sql, NULL, NULL, NULL); ``` 2. 实现添加图书功能。 ```c void add_book() { book b; printf("请输入图书名称:"); scanf("%s", b.name); printf("请输入作者:"); scanf("%s", b.author); printf("请输入ISBN号:"); scanf("%s", b.isbn); printf("请输入出版社:"); scanf("%s", b.press); printf("请输入价格:"); scanf("%lf", &b.price); char *sql = "INSERT INTO book(name,author,isbn,press,price) VALUES(?,?,?,?,?)"; sqlite3_stmt *stmt; int res = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, b.name, strlen(b.name), NULL); sqlite3_bind_text(stmt, 2, b.author, strlen(b.author), NULL); sqlite3_bind_text(stmt, 3, b.isbn, strlen(b.isbn), NULL); sqlite3_bind_text(stmt, 4, b.press, strlen(b.press), NULL); sqlite3_bind_double(stmt, 5, b.price); sqlite3_step(stmt); printf("添加成功!\n"); sqlite3_finalize(stmt); } ``` 3. 实现查找图书功能。 ```c void search_book() { printf("请输入要查询的条件(1-名称,2-作者,3-ISBN号):"); int type; scanf("%d", &type); printf("请输入查询关键字:"); char keyword[20]; scanf("%s", keyword); char *sql; if (type == 1) { sql = "SELECT * FROM book WHERE name LIKE ?"; } else if (type == 2) { sql = "SELECT * FROM book WHERE author LIKE ?"; } else { sql = "SELECT * FROM book WHERE isbn LIKE ?"; } sqlite3_stmt *stmt; int res = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, keyword, strlen(keyword), NULL); printf("查询结果如下:\n"); while (sqlite3_step(stmt) == SQLITE_ROW) { int id = sqlite3_column_int(stmt, 0); const unsigned char *name = sqlite3_column_text(stmt, 1); const unsigned char *author = sqlite3_column_text(stmt, 2); const unsigned char *isbn = sqlite3_column_text(stmt, 3); const unsigned char *press = sqlite3_column_text(stmt, 4); double price = sqlite3_column_double(stmt, 5); printf("%d\t%s\t%s\t%s\t%s\t%.2lf\n", id, name, author, isbn, press, price); } sqlite3_finalize(stmt); } ``` 4. 实现修改图书功能。 ```c void modify_book() { printf("请输入要修改的图书ID:"); int id; scanf("%d", &id); printf("请输入要修改的信息(1-名称,2-作者,3-ISBN号,4-出版社,5-价格):"); int type; scanf("%d", &type); char field[20]; if (type == 1) { strcpy(field, "name"); } else if (type == 2) { strcpy(field, "author"); } else if (type == 3) { strcpy(field, "isbn"); } else if (type == 4) { strcpy(field, "press"); } else { strcpy(field, "price"); } char value[20]; printf("请输入修改后的内容:"); scanf("%s", value); char sql[100]; sprintf(sql, "UPDATE book SET %s = ? WHERE id = ?", field); sqlite3_stmt *stmt; int res = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, value, strlen(value), NULL); sqlite3_bind_int(stmt, 2, id); sqlite3_step(stmt); printf("修改成功!\n"); sqlite3_finalize(stmt); } ``` 5. 实现删除图书功能。 ```c void delete_book() { printf("请输入要删除的图书ID:"); int id; scanf("%d", &id); char sql[100]; sprintf(sql, "DELETE FROM book WHERE id = ?"); sqlite3_stmt *stmt; int res = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, id); sqlite3_step(stmt); printf("删除成功!\n"); sqlite3_finalize(stmt); } ``` 6. 实现主函数和菜单。 ```c int main() { sqlite3 *db; int res = sqlite3_open("books.db", &db); char *sql = "CREATE TABLE book(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,author TEXT,isbn TEXT,press TEXT,price DOUBLE);"; sqlite3_exec(db, sql, NULL, NULL, NULL); while (1) { printf("==============================\n"); printf("1-添加图书\n2-查找图书\n3-修改图书\n4-删除图书\n5-退出\n"); printf("==============================\n"); printf("请输入操作序号:"); int choice; scanf("%d", &choice); switch (choice) { case 1: add_book(); break; case 2: search_book(); break; case 3: modify_book(); break; case 4: delete_book(); break; case 5: printf("谢谢使用!\n"); return 0; default: printf("输入错误,请重新输入!\n"); break; } } } ``` 通过以上步骤,我们就完成了一个简单的控制台图书管理系统。当然,我们还可以增加更多的功能,比如显示所有图书、排序等。这些方法可以通过学习SQLite和C语言得到更好的实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值