该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#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;
}