#include<iostream>
#include <iomanip>
#include "string"
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 10000
typedef struct
{
char no[20];
char name[50];
char author[20];
}Book;
typedef struct
{
Book* elem;
int length;
}SqList;
void Printf(SqList& L);
int InitList(SqList& L)
{
= new Book[MAXSIZE];
if (!) exit(OVERFLOW);
L.length = 0;
return OK;
}
int GetElem(SqList L, int i, Book& e)
{
if (i<1 || i>L.length) return ERROR;
e = [i - 1];
return OK;
}
void LocateElem(SqList L)
{
Book e;
int i;
char n = 0;
while (1)
{
cout << "Enter 1 search by book number, enter 2 search by book title, enter 3 search by author name, enter 4 search by sequence number, enter # return to the previous level:" << endl;
cin >> n;
if (n == '#')
break;
if (n == '1')
{
cout << "Please enter the book number you want to find:";
cin >> ;
for (i = 0; i < L.length; i++)
{
if (strcmp([i].no, e.no) == 0)
{
cout << [i].no << " " << [i].name << " " << [i].author << endl;
break;
}
}
if (i >= L.length)
cout << "No book is found! Please check if the input is correct" << endl;
}
if (n == '2')
{
cout << "Please enter the title of the book you are looking for:";
cin >> ;
for (i = 0; i < L.length; i++)
{
if (strcmp([i].name, ) == 0)
{
cout << [i].no << " " << [i].name << " " << [i].author << endl;
break;
}
}
if (i >= L.length)
cout << "No book is found! Please check if the input is correct" << endl;
}
if (n == '3')
{
cout << "Please enter the author you are looking for:";
cin >> ;
for (i = 0; i < L.length; i++)
{
if (strcmp([i].author, ) == 0)
{
cout << [i].no << " " << [i].name << " " << [i].author << endl;
break;
}
}
if (i >= L.length)
cout << "No book is found! Please check if the input is correct" << endl;
}
if (n == '4')
{
cout << "Please enter the serial number you want to find:";
cin >> i;
if (i <= L.length)
{
cout << [i - 1].no << " " << [i - 1].name << " " << [i - 1].author << endl;
}
if (i > L.length)
cout << "No book is found! Please check if the input is correct" << endl;
}
}
}
int ListInsert(SqList& L, int i)
{
int j = 0;
if ((i < 1) || (i > L.length + 1)) return ERROR;
if (L.length == MAXSIZE) return ERROR;
for (j = L.length - 1; j >= i - 1; j--)
[j + 1] = [j];
cout << "Please enter the book number, title, author" << endl;
cin >> [i].no >> [i].name >> [i].author;
++L.length;
Printf(L);
return OK;
}
void ListDelete(SqList& L)
{
int i;
int j;
char s = 0;
cout << "Enter the serial number of the book you want to delete:" << endl;
cin >> i;
if ((i < 1) || (i > L.length)) cout << "The input serial number is incorrect" << endl;
else
{
cout << "Whether to delete the book? Enter 1 to confirm, enter 0 to negative:";
cin >> s;
if (s == '1')
{
for (j = i; j <= L.length - 1; j++)
[j - 1] = [j];
--L.length;
Printf(L);
}
}
}
void Update(SqList& L)
{
Book e;
int i;
char n;
while (1)
{
cout << "Please enter the serial number of the book you want to modify, enter 0 to return to the previous level:";
cin >> i;
if (i == 0) break;
else if ((i < 1) || (i > L.length)) cout << "The input serial number is incorrect" << endl;
else
{
cout << [i - 1].no << " " << [i - 1].name << " " << [i - 1].author << endl;
cout << "Please select the object to be modified, 1: book number, 2: book title, 3: author, enter # to return to previous level" << endl;
cin >> n;
if (n == '#')
break;
switch (n)
{
case '1':cout << "Modify it to:";
cin >> [i - 1].no;
cout << [i - 1].no << " " << [i - 1].name << " " << [i - 1].author << endl; break;
case '2':cout << "Modify it to:";
cin >> [i - 1].name;
cout << [i - 1].no << " " << [i - 1].name << " " << [i - 1].author << endl; break;
case '3':cout << "Modify it to:";
cin >> [i - 1].author;
cout << [i - 1].no << " " << [i - 1].name << " " << [i - 1].author << endl; break;
default:break;
}
}
}
}
void Create(SqList& L, int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Please enter the number of the number" << i + 1 << "The book number, title, author of this book" << endl;
cin >> [i].no >> [i].name >> [i].author;
L.length++;
}
}
void Printf(SqList& L)
{
int i;
cout << "/----------------------------------------------------------------------------------------------------------------------------- << endl;
cout << " " << setw(10) << left << "Serial Number" << setw(10) << left << "Book Number" << setw(30) << left << "Book Title" << setw(10) << left << "author" << endl << endl;
for (i = 0; i < L.length; i++)
{
cout << " " << setw(10) << left << i + 1 << setw(10) << left << [i].no << setw(30) << left << [i].name << setw(10) << left << [i].author << endl;
}
cout << "/-----------------------------------------------------------------------/" << endl;
}
void main()
{
SqList L;
Book B;
char n[20];
int m = 0;
char s = 0;
InitList(L);
cout << "/------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- << endl;
cout << "Create Book Information" << endl;
cout << "Please enter the number of books:";
cin >> m;
Create(L, m);
Printf(L);
while (1)
{
cout << "Please select the action to perform: 1: Find 2: Insert 3: Delete 4: Modify 0: Show: :" << endl;
cin >> s;
switch (s)
{
case '0': Printf(L); break;
case '1': LocateElem(L); break;
case '2': ListInsert(L, L.length); break;
case '3': ListDelete(L); break;
case '4': Update(L); break;
}
}
system("pause");
}