c 语言链表图书馆,c++ 类链表之图书管理系统代码

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

using namespace std;

class book

{

public:

int num;

float price;

book *next;

public:

void bookMenu();

void startMenu(string str); //用来执行一个图书的子系统命令

book* create(); //用来创建链表

bool check(string str); //用来检查用户输入的是否非法字符

void addBook(book *head, int num,int price);//用来添加一本书到目录

void showBook(book *head); //用来显示仓库中的图书

void deleteBook(book *head, int num);

void allBook(book*head); //用来检测有多少个图书;

};

book* head = NULL; //用来创建一个全局头节点

void book::deleteBook(book *head,int num)

{

book *l = NULL;

if (head == NULL)

{

cout << "不好意思,图书馆仓库里面没有书了,麻烦你添加在删除好吗?" << endl;

return;

}

if (head->num == num )

{

l = head;

head = head->next;

::head = head;

delete l;

cout << num << "头节点删除成功!" << endl;

}

while (head)

{

if (head->next == NULL)

{

cout << "找不到要删除的编号:" << num << endl;

return;

}

if (head->next->num == num)

{

l = head->next;

head->next = l->next;

::head = head;

delete l;

cout << num << "编号删除成功!" << endl;

}

head = head->next;

}

}

bool book::check(string str)

{

int num = 0;

for (int i = 0; i < str.length(); i++)

{

if (str[i] == *.*)

{

num++;

}

if (num > 1)

{

cout << "你输入了一个非法字符,请重新输入。" << endl;

return false;

}

}

if (str[0] > *9* || str[0] < *0* || str[0] == *.*)

{

cout << "你输入了一个非法字符,请重新输入。" << endl;

return false;

}

return true;

}

book* book::create()

{

book *p1, *p2;

p1 = new book;

head = p1;

p2 = p1;

cout << "请输入图书的编号:" << endl;

string str;

cin >> str;

while (!check(str))

{

cin >> str;

}

p1->num = atoi(str.c_str());

if (p1->num != 0)

{

cout << "请输入图书的价格:" << endl;

cin >> str;

while (!check(str))

{

cin >> str;

}

p1->price = atof(str.c_str());

}

else

{

delete p1; p2 = NULL; head = NULL; return head;

}

while (p1->num != 0)

{

p2 = p1;

p1 = new book;

cout << "请输入图书的编号:" << endl;

cin >> str;

while (!check(str))

{

cin >> str;

}

p1->num = atoi(str.c_str());

if (p1->num != 0)

{

cout << "请输入图书的价格:" << endl;

cin >> str;

while (!check(str))

{

cin >> str;

}

p1->price = atof(str.c_str());

p2->next = p1;

}

}

delete p1;

p2->next = NULL;

return head;

}

void book::bookMenu() //用来显示图书系统目录

{

cout << "1、重建图书 2、添加图书 3、删除图书 4、显示图书 5、图书总数 Q、退出程序\n\n";

cout << "<< 提示: 所有输入按0、或者回车键退回主程序,或者返回主程序! >>\n ";

}

void book::addBook(book*head, int num,int price)

{

book *list = new book;

book *l = NULL;

while (head)

{

l = head;

head = head->next;

}

l->next = list;

list->num = num;

list->price = price;

list->next = NULL;

cout << "你的图书已录入到图书馆系统里面了! \n" << endl;

cout << "<< 提示: 所有输入按0、或者回车键退回主程序,或者返回主程序! >>\n ";

cin.get();

cin.get();

}

void book::showBook(book *head)

{

cout << "仓库所有的图书信息如下表所示:" << endl;

while (head)

{

cout << "图书编号:" << head->num << "\t";

cout << "图书价格:" << head->price << endl;

head = head->next;

}

}

void book::allBook(book *head)

{

int num = 0;

while (head)

{

num++;

head = head->next;

}

cout << "你的图书馆里面现在一共有" << num << "本书!" << endl;

cin.get();

cin.get();

}

int main()

{

book *books = NULL;

books->bookMenu();

string str;

begin:

cin >> str;

if (str[0] == *q* || str[0] == *Q*)

{

cout << "退出程序,再见!!!" << endl;

cin.get(); cin.get();

return 0;

}

else

{

switch (atoi(str.c_str()))

{

case 1:

books->create();

system("cls");

books->bookMenu();

goto begin;

case 2:

{

cout << "请输入你的图书编号:" << endl;

int num, price;

cin >> str;

while (!(books->check(str)))

{

cin >> str;

}

num = atoi(str.c_str());

if (num != 0)

{

cout << "请输入图书的价格:" << endl;

cin >> str;

while (!(books->check(str)))

{

cin >> str;

}

price = atof(str.c_str());

}

books->addBook(::head, num, price);

system("cls");

books->bookMenu();

goto begin;

}

case 3:

{

cout << "请输入你要删除的图书编号:" << endl;

int num;

cin >> str;

while (!(books->check(str)))

{

cin >> str;

}

num = atoi(str.c_str());

books->deleteBook(::head, num);

cin.get(); cin.get();

system("cls");

books->bookMenu();

goto begin;

}

case 4:

{

books->showBook(::head);

cin.get(); cin.get();

system("cls");

books->bookMenu();

goto begin;

break;

}

case 5:

system("cls");

books->allBook(::head);

system("cls");

books->bookMenu();

goto begin;

default:

system("cls");

books->bookMenu();

goto begin;

break;

}

}

getchar(); getchar();

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的C++图书管理系统链表的示例代码。以下是一个基本的实现: ```cpp #include <iostream> #include <string> using namespace std; // 图书 class Book { private: string title; string author; int year; public: Book(string title, string author, int year) { this->title = title; this->author = author; this->year = year; } string getTitle() { return title; } string getAuthor() { return author; } int getYear() { return year; } }; // 链表节点 class Node { public: Book* book; Node* next; Node(Book* book) { this->book = book; next = nullptr; } }; // 链表 class LinkedList { private: Node* head; public: LinkedList() { head = nullptr; } // 添加图书到链表 void addBook(Book* book) { Node* newNode = new Node(book); if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } // 显示链表中的所有图书 void displayBooks() { Node* current = head; if (current == nullptr) { cout << "链表为空" << endl; return; } cout << "图书列表:" << endl; while (current != nullptr) { cout << "书名:" << current->book->getTitle() << endl; cout << "作者:" << current->book->getAuthor() << endl; cout << "出版年份:" << current->book->getYear() << endl; cout << "-------------------------" << endl; current = current->next; } } }; int main() { // 创建图书对象 Book book1("C++ Primer", "Stanley B. Lippman", 2012); Book book2("Effective C++", "Scott Meyers", 2005); Book book3("The C++ Programming Language", "Bjarne Stroustrup", 2013); // 创建链表对象 LinkedList linkedList; // 添加图书到链表 linkedList.addBook(&book1); linkedList.addBook(&book2); linkedList.addBook(&book3); // 显示链表中的所有图书 linkedList.displayBooks(); return 0; } ``` 这是一个简单的图书管理系统,使用链表来存储图书对象。您可以根据需要进行修改和扩展。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值