1. 程式人生 > >資料結構 結構體 利用順序表寫的圖書管理系統

資料結構 結構體 利用順序表寫的圖書管理系統

圖書管理系統 ,可以增刪查改,VS2013編譯

#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)
{
L.elem = new Book[MAXSIZE];
if(!L.elem)  exit(OVERFLOW);
L.length = 0;
return OK;
}
int GetElem(SqList L,int i,Book &e)      //取出
{
if(i<1 || i>L.length) return ERROR;
e = L.elem[i-1];
return OK;
}
void LocateElem(SqList L)   //查詢
{
Book e;
int i;
char n = 0;
while (1)
{
cout << "輸入1按書號查詢,輸入2按書名查詢,輸入3按作者名查詢,輸入4按序號查詢,輸入#返回上一級:" << endl;
cin >> n;
if (n == '#')
break;
if (n == '1')
{
cout << "請輸入要查詢的書號:";
cin >> e.no;
for (i = 0; i < L.length; i++)
{
if (strcmp(L.elem[i].no , e.no) == 0)
{
cout << L.elem[i].no << "    " << L.elem[i].name << "   " << L.elem[i].author << endl;
break;
}

}
if (i >= L.length)
cout << "查無此書!請檢視輸入是否正確" << endl;


}
if (n == '2')
{
cout << "請輸入要查詢的書名:";
cin >> e.name;
for (i = 0; i < L.length; i++)
{
if (strcmp(L.elem[i].name, e.name) == 0)
{
cout << L.elem[i].no << "    " << L.elem[i].name << "   " << L.elem[i].author << endl;
break;
}


}
if (i >= L.length)
cout << "查無此書!請檢視輸入是否正確" << endl;
}
if (n == '3')
{
cout << "請輸入要查詢的作者:";
cin >> e.author;
for (i = 0; i < L.length; i++)
{
if (strcmp(L.elem[i].author, e.author) == 0)
{
cout << L.elem[i].no << "    " << L.elem[i].name << "   " << L.elem[i].author << endl;
break;
}


}
if (i >= L.length)
cout << "查無此書!請檢視輸入是否正確" << endl;
}
if (n == '4')
{
cout << "請輸入要查詢的序號:";
cin >> i;
if( i <= L.length)
{
cout << L.elem[i-1].no << "    " << L.elem[i-1].name << "   " << L.elem[i-1].author << endl;


}
if (i > L.length)
cout << "查無此書!請檢視輸入是否正確" << 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--)
L.elem[j+1]=L.elem[j];

cout<<"請輸入書號、書名、作者"<<endl;
cin >> L.elem[i].no >> L.elem[i].name >> L.elem[i].author;
++L.length;
Printf(L);

return OK;
}


void ListDelete(SqList &L)               //刪除
{
int i;
int j;
char s = 0;
cout << "輸入要刪除書本的序號:" << endl;
cin >> i;
if ((i<1) || (i>L.length)) cout << "輸入的序號不正確" << endl;
else
{
cout << "是否刪除該書  輸入1確定,輸入0否定:";
cin >> s;
if (s == '1')
{
for(j = i; j<= L.length-1;j++)
L.elem[j-1] = L.elem[j];
--L.length;
Printf(L);
}
}




}


void Update(SqList &L)
{
Book e;
int i;
char  n;
while (1)
{
cout << "請輸入要修改的書的序號 ,輸入0返回上一級:";
cin >> i;
if (i == 0) break;
else if ((i<1) || (i>L.length)) cout << "輸入的序號不正確" << endl;
else
{
cout << L.elem[i - 1].no << "    " << L.elem[i - 1].name << "   " << L.elem[i - 1].author << endl;
cout << "請選擇要修改的物件,1:書號,2:書名,3:作者 ,輸入#返回上一級" << endl;
cin >> n;
if (n == '#')
break;
switch (n)
{
case '1':cout << "把其修改為:";
cin >> L.elem[i - 1].no;
cout << L.elem[i - 1].no << "    " << L.elem[i - 1].name << "   " << L.elem[i - 1].author << endl; break;
case '2':cout << "把其修改為:";
cin >> L.elem[i - 1].name;
cout << L.elem[i - 1].no << "    " << L.elem[i - 1].name << "   " << L.elem[i - 1].author << endl; break;
case '3':cout << "把其修改為:";
cin >> L.elem[i - 1].author;
cout << L.elem[i - 1].no << "    " << L.elem[i - 1].name << "   " << L.elem[i - 1].author << endl; break;
default:break;
}




}
}

}


void Create(SqList &L,int n)            //建立圖書資訊
{
int i;

for(i = 0;i < n; i++)
{
cout<<"請分別輸入第"<<i+1<<"本書的書號、書名、作者"<<endl;
cin >> L.elem[i].no >> L.elem[i].name >> L.elem[i].author;

L.length++;
}
}
void Printf(SqList &L)
{
int i;
cout << "/---------------------  存在以下圖書  ----------------------------------/" << endl;
cout << "       " << setw(10) << left << "序號" << setw(10) << left << "書號" << setw(30) << left << "書名" << setw(10) << left << "作者" << endl << endl;
for(i=0;i<L.length;i++)
{
cout << "        " << setw(10) << left << i+1 << setw(10) << left << L.elem[i].no << setw(30) << left << L.elem[i].name << setw(10) << left << L.elem[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 << "建立圖書資訊" << endl;
cout <<"請輸入書本數目:";
cin>>m;
Create(L, m);
Printf(L);
while (1)
{
cout << "請選擇要進行的操作 : 1:查詢     2: 插入     3: 刪除     4:修改    0: 顯示:   :" << 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");
}