数据结构实验报告

时间:2024-08-25 04:40:54编辑:奇事君

数据结构课程设计(基于C) 图书管理系统

头文件:
============
#ifndef _DATA_STRUCT_H_
#define _DATA_STRUCT_H_

/*图书结构*/
struct Book
{
unsigned long BookID;/*图书编号*/
char BookName[512];/*书名*/
char Writer[512];/*作者*/
int CurrentNumber;/*现存量*/

Book *pNext;/*下一个图书信息*/
};

/*图书索引结构*/
struct Index
{
unsigned long BookID;/*图书编号*/
Index *pNext;/*下一个索引指针*/
};

/*借阅信息结构*/
struct Borrow
{
unsigned long BookID;/*借阅图书编号*/
char BookName[512];/*书名*/
unsigned long StuID;/*图书证号*/
char ReturnTime[512];/*归还日期*/

Borrow *pNext;/*下一个借阅信息*/
};

#endif/*_DATA_STRUCT_H_*/


实现文件:
===============
// BookManage.cpp : 定义控制台应用程序的入口点。
//

#include
#include
#include
#include
#include "DataStruct.h"

/*
数据结构课程设计(基于C) 图书管理系统
悬赏分:100 - 离问题结束还有 14 天 23 小时
【问题描述】
图书管理基本业务活动包括,对一本书的采编入库、清楚库存、借阅和归还等等。试设计一个图书管理系统,将上述业务活动借助于计算机系统完成。
【基本要求】
(1) 每种书的登记至少包括书号书名作者现存量总库存量5项
(2)对书号建立索引表(线性表)以提高查找效率
(3) 系统要实现的操作及其功能定义如下:
1 采编入库:新购入一种书,经分类和确定数好之后登记到图书馆账目中去。如果这种书在账户中已有,则只将总库存量增加。
2借阅:如果一本书的现存量大于零,则借出一本,登出借阅者的图书证号和归还日期
3归还:注销对借阅者的登记,改变该书的现存量
*/

/*获取输入字符串*/
void GetInput(char *pInOutStr,int StrLen,bool OnlyGetNumber = false);
/*显示主菜单 并返回选择*/
int ShowMainMenu();
/*图书入库操作,传入参数为图书列表头指针的指针和索引信息的头指针的指针,因为在此函数内部可能会改变头指针的值*/
void PutBookInLib(Book **pHead,Index **pIndex);
/*查询图书馆库存,查询不改变数据,故传入指针即可*/
void QueryBookLib(Book *pHead);
/*借阅图书*/
void BorrowBook(Borrow **pBorrow,Book *pHead);
/*查询借阅*/
void QueryBorrow(Borrow *pBorrow);
/*归还图书*/
void ReturnBook(Borrow **pBorrow,Book *pHead);
/*释放图书链表及索引链表及借阅信息链表*/
void DeleteBookList(Book **pHead);
void DeleteIndexList(Index **pIndex);
void DeleteBorrowList(Borrow **pBorrow);

int main()
{
/*图书列表头指针*/
Book *pBookHead = NULL;
/*图书索引头指针*/
Index *pIndexHead = NULL;
/*借阅信息头指针*/
Borrow *pBorrowHead = NULL;
/*用来保存当前用户的输入*/
int ChoosedNum = -1;

while ( true )
{
ChoosedNum = ShowMainMenu();
switch(ChoosedNum)
{
case 0 :/*退出*/
{
return 0;
break;
}
case 1:/*图书入库*/
{
PutBookInLib(&pBookHead,&pIndexHead);
break;
}
case 2:/*查询库存*/
{
QueryBookLib(pBookHead);
printf("按回车键继续...");
fflush(stdin);
getchar();
system("cls");
break;
}
case 3:/*借阅图书*/
{
QueryBookLib(pBookHead);
BorrowBook(&pBorrowHead,pBookHead);
break;
}
case 4:/*查询借阅*/
{
QueryBorrow(pBorrowHead);
printf("按回车键继续...");
fflush(stdin);
getchar();
system("cls");
break;
}
case 5:/*归还图书*/
{
QueryBorrow(pBorrowHead);
ReturnBook(&pBorrowHead,pBookHead);
break;
}
}
}

DeleteBookList(&pBookHead);
DeleteIndexList(&pIndexHead);
DeleteBorrowList(&pBorrowHead);

fflush(stdin);
getchar();
return 0;
}

/*获取输入字符串*/
void GetInput(char *pInOutStr,int StrLen,bool OnlyGetNumber)
{
memset(pInOutStr,0x0,StrLen);

fflush(stdin);
int Count = 0;
while ( true )
{
char TmpC = 0;
fread(&TmpC,1,1,stdin);

if ( 10 == TmpC )
{
break;
}

if ( OnlyGetNumber && TmpC >= '0' && TmpC <= '9' )
{
pInOutStr[Count] = TmpC;
Count++;
}
else
{
pInOutStr[Count] = TmpC;
Count++;
}
}
}

/*显示主菜单 并返回选择*/
int ShowMainMenu()
{
char InPutStr[1024];

NO_INPUT:

printf("\n\n\n");
printf("******************************************************************\n");
printf("* 图书管理系统 *\n");
printf("******************************************************************\n");
printf("\n");
printf("请选择:\n");
printf("\t1.图书入库\n");
printf("\t2.查询库存\n");
printf("\t3.借阅图书\n");
printf("\t4.查询借阅\n");
printf("\t5.归还图书\n");
printf("\t0.退出\n");
printf("\n");
printf("\t输入 : ");

GetInput(InPutStr,1024,true);

if ( strlen(InPutStr) < 1 )
{
system("cls");
goto NO_INPUT;
}

return atoi(InPutStr);
}

/*图书入库操作*/
void PutBookInLib(Book **pHead,Index **pIndex)
{
Book *pWork = *pHead;
unsigned long BookID = 1;
char InPutStr[1024];

/*先计算处当前入库图书的ID号*/
if ( NULL == pWork )
{
BookID = 1;
}
else
{
while ( pWork->pNext != NULL )
{
pWork = pWork->pNext;
}
BookID = pWork->BookID + 1;
}

Book *pTmpNewBook = (Book *)(malloc(sizeof(Book)));
memset(pTmpNewBook,0x0,sizeof(Book));
Index *pTmpIndex = (Index *)(malloc(sizeof(Index)));
memset(pTmpIndex,0x0,sizeof(Index));

pTmpNewBook->BookID = BookID;
pTmpIndex->BookID = BookID;

system("cls");
printf("输入图书名称 : ");
GetInput(InPutStr,1024);
strcpy(pTmpNewBook->BookName,InPutStr);
system("cls");
printf(">\n",pTmpNewBook->BookName);
printf("图书作者 : ");
GetInput(InPutStr,1024);
strcpy(pTmpNewBook->Writer,InPutStr);
system("cls");
printf(">\n",pTmpNewBook->BookName);
printf("作者:%s\n",pTmpNewBook->Writer);
printf("入库数量 : ");
GetInput(InPutStr,1024,true);
pTmpNewBook->CurrentNumber = atoi(InPutStr);

if ( pTmpNewBook->CurrentNumber < 1 )
{
system("cls");
printf("入库数量错误!(按回车键继续...)");
fflush(stdin);
getchar();
system("cls");
return;
}

if ( NULL == *pHead )
{
*pHead = pTmpNewBook;
*pIndex = pTmpIndex;
}
else
{
Book *pWork = *pHead;
while ( pWork != NULL )
{
if ( strcmp(pWork->BookName,pTmpNewBook->BookName) == 0 && strcmp(pWork->Writer,pTmpNewBook->Writer) == 0 )
{
pWork->CurrentNumber += pTmpNewBook->CurrentNumber;
return;
}
pWork = pWork->pNext;
}

pWork = *pHead;
while ( pWork->pNext != NULL )
{
pWork = pWork->pNext;
}
pWork->pNext = pTmpNewBook;
Index *pWork2 = *pIndex;
while ( pWork2->pNext != NULL )
{
pWork2 = pWork2->pNext;
}
pWork2->pNext = pTmpIndex;
}

system("cls");
printf("图书:>\n作者:%s\n入库数:%d\n\n入库操作成功!(按回车键继续...)",pTmpNewBook->BookName,pTmpNewBook->Writer,pTmpNewBook->CurrentNumber);
fflush(stdin);
getchar();
system("cls");
}

/*释放图书链表及索引链表*/
void DeleteBookList(Book **pHead)
{
Book *pWork = *pHead;
Book *pNext = pWork;
while ( NULL != pWork )
{
pNext = pWork->pNext;
free(pWork);
pWork = pNext;
}
}

void DeleteIndexList(Index **pIndex)
{
Index *pWork = *pIndex;
Index *pNext = pWork;
while ( NULL != pWork )
{
pNext = pWork->pNext;
free(pWork);
pWork = pNext;
}
}

void DeleteBorrowList(Borrow **pBorrow)
{
Borrow *pWork = *pBorrow;
Borrow *pNext = pWork;
while ( NULL != pWork )
{
pNext = pWork->pNext;
free(pWork);
pWork = pNext;
}
}

/*查询图书馆库存,查询不改变数据,故传入指针即可*/
void QueryBookLib(Book *pHead)
{
Book *pWork = pHead;
int TotalCount = 0;
while ( NULL != pWork )
{
TotalCount++;
pWork = pWork->pNext;
}
system("cls");
printf("当前库存共%d种图书,列表如下:\n=====================================================\n",TotalCount);
pWork = pHead;
while ( NULL != pWork )
{
printf("编号:[%d] 书名:> 作者:%s 当前库存:%d\n",pWork->BookID,pWork->BookName,pWork->Writer,pWork->CurrentNumber);
pWork = pWork->pNext;
}
printf("=====================================================\n",TotalCount);
}

/*借阅图书*/
void BorrowBook(Borrow **pBorrow,Book *pHead)
{
char InPutStr[1024];
bool HadTheBook = false;
Borrow *pTmpBorrow = (Borrow *)(malloc(sizeof(Borrow)));
memset(pTmpBorrow,0x0,sizeof(Borrow));

printf("请输入你想借书的编号 : ");
GetInput(InPutStr,1024,true);
pTmpBorrow->BookID = atoi(InPutStr);

Book *pWorkBook = pHead;
while ( pWorkBook != NULL )
{
if ( pWorkBook->BookID == pTmpBorrow->BookID )
{
HadTheBook = true;
break;
}
pWorkBook = pWorkBook->pNext;
}

if ( HadTheBook )
{
if ( pWorkBook->CurrentNumber < 1 )
{
system("cls");
printf("图书>当前无库存,无法借阅!\n(按回车键继续...)",pWorkBook->BookName);
fflush(stdin);
getchar();
system("cls");
free(pTmpBorrow);
return;
}
}
else
{
system("cls");
printf("图书编号%d不存在!\n(按回车键继续...)",pTmpBorrow->BookID);
fflush(stdin);
getchar();
system("cls");
free(pTmpBorrow);
return;
}

strcpy(pTmpBorrow->BookName,pWorkBook->BookName);
printf("请输入借书证号 : ");
GetInput(InPutStr,1024,true);
pTmpBorrow->StuID = atoi(InPutStr);

if ( 0 == pTmpBorrow->StuID )
{
system("cls");
printf("借书证号不存在(借书证号输入整数)!\n(按回车键继续...)");
fflush(stdin);
getchar();
system("cls");
free(pTmpBorrow);
return;
}

time_t ltime;
struct tm *today;
time(<ime);
ltime += 60 * 60 * 24 * 30;
today = localtime(<ime);
sprintf(pTmpBorrow->ReturnTime,"%d年%d月%d日", today->tm_year+1900,
today->tm_mon+1,
today->tm_mday);

if ( *pBorrow == NULL )
{
*pBorrow = pTmpBorrow;
}
else
{
Borrow *pWork = *pBorrow;
while ( NULL != pWork->pNext )
{
pWork = pWork->pNext;
}
pWork->pNext = pTmpBorrow;
}
pWorkBook->CurrentNumber--;

system("cls");
printf("借书证号:%d\n借阅图书:>\n\n操作成功!(按回车键继续...)",pTmpBorrow->StuID,pTmpBorrow->BookName);
fflush(stdin);
getchar();
system("cls");
}

/*查询借阅*/
void QueryBorrow(Borrow *pBorrow)
{
Borrow *pWork = pBorrow;
int TotalCount = 0;
while ( NULL != pWork )
{
TotalCount++;
pWork = pWork->pNext;
}
system("cls");
printf("当前借阅信息共%d条,列表如下:\n=====================================================\n",TotalCount);
pWork = pBorrow;
while ( NULL != pWork )
{
printf("借数证号:[%d] 借书名:> 图书编号:%d 归还日期:%s\n",pWork->StuID,pWork->BookName,pWork->BookID,pWork->ReturnTime);
pWork = pWork->pNext;
}
printf("=====================================================\n",TotalCount);
}

/*归还图书*/
void ReturnBook(Borrow **pBorrow,Book *pHead)
{
char InPutStr[1024];
Borrow TmpBorrow;
bool HasFindBorrwo = false;

memset(&TmpBorrow,0x0,sizeof(TmpBorrow));
printf("请输入借书证号 : ");
GetInput(InPutStr,1024,true);
TmpBorrow.StuID = atoi(InPutStr);
printf("请输入所还图书编号 : ");
GetInput(InPutStr,1024,true);
TmpBorrow.BookID = atoi(InPutStr);

Borrow *pWorkBorrow = *pBorrow;
Borrow *pDeletePre = NULL;
while ( NULL != pWorkBorrow )
{
if ( pWorkBorrow->BookID == TmpBorrow.BookID && pWorkBorrow->StuID == TmpBorrow.StuID )
{
HasFindBorrwo = true;
break;
}
pDeletePre = pWorkBorrow;
pWorkBorrow = pWorkBorrow->pNext;
}

if ( ! HasFindBorrwo )
{
system("cls");
printf("你输入的借书信息不存在!\n(按回车键继续...)");
fflush(stdin);
getchar();
system("cls");
return;
}

if ( NULL == pDeletePre )
{
*pBorrow = pWorkBorrow->pNext;
}
else
{
pDeletePre->pNext = pWorkBorrow->pNext;
}

Book *pWorkBook = pHead;
while ( NULL != pWorkBook )
{
if ( pWorkBook->BookID == pWorkBorrow->BookID )
{
pWorkBook->CurrentNumber ++;
}
pWorkBook = pWorkBook->pNext;
}

system("cls");
printf("借书证号:%d\n归还图书:>\n\n操作成功!(按回车键继续...)",pWorkBorrow->StuID,pWorkBorrow->BookName);
fflush(stdin);
getchar();
system("cls");
free(pWorkBorrow);
}


数据结构实验:线性表(1)

/*2.1创建空顺序表*/
PSeqList createNullList_seq(int m)
{
PSeqList palist =(PSeqList)malloc(sizeof(struct SeqList));
if (palist!=NULL)
{
palist->element=(DataType*)malloc(sizeof(DataType)*m);
if (palist->element)
{
palist->MAXNUM=m;
palist->n=0;
return palist;
}
else free (palist);
}
printf("Out of space!! \n");
return NULL;
}

/*2.2判断线性表是否为空*/
int isNullList_seq(PSeqList palist)
{
return(palist->n==0);
}

/*2.3顺序表删除*/
int deleteP_seq(PSeqList palist,int p)
{
int q;
if(ppalist->n-1)
{
printf("Not exist!\n");
return 0;
}
for (q=p;qn-1;q++)
palist->element[q]=palist->element [q+1];
palist->n=palist->n-1;
return 1;
}


/*2.4在顺序表中求某元素的下标*/
int locate_seq(PSeqList palist,DataType X)
{
int q;
for (q=0;qn;q++)
if(palist->element[q]==X)return q;
return -1;
}

/*2.5顺序表的前插入*/
int insertPre_seq(PSeqList palist,int p,DataType X)
{
int q;
if(palist->n >=palist->MAXNUM)
{
printf("Overflow! \n");
return 0;
}
if(ppalist->n)
{
printf("Not exist! \n");
return 0;
}
for(q=palist->n-1;q>=p;q--)
palist ->element[q+1]=palist ->element[q];
palist->element[p]=X;
palist->n=palist ->n+1;
return 1;
}





//2.6遍历palist所指所有元素的值
void print_seq(PSeqList palist)
{
int i;
if(palist != NULL)
for(i=0;in;i++)
printf("out of element:%d\n",palist->element[i]);
printf("\n\n");
}

//2.7线性表排序
void sort_seq(PSeqList palist)
{
int i,j;
DataType x;
for(i=1;in;i++)
{
x=palist->element[i];
for(j=i-1;j>=0;j--)
if(xelement[j])
palist->element[j+1]=palist->element[j];
else
break;
palist->element[j+1]=x;
}
}
/*2.8顺序表的后插*/
int insertPost_seq(PSeqList palist,int p,DataType X)
{
int q;
if(palist->n >=palist->MAXNUM)
{
printf("Overflow! \n");
return 0;
}
if(ppalist->n)
{
printf("Not exist! \n");
return 0;
}
for(q=palist->n-1;q>p;q--)
palist ->element[q+1]=palist ->element[q];
palist->element[p+1]=X;
palist->n=palist ->n+1;
return 1;
}
/*2.9尾插*/
int insertRear(PSeqList palist,DataType x)
{
if(palist->n==palist->MAXNUM)
{
printf("\n List overflow!");
return(0);
}
palist->element[palist->n]=x;
palist->n++;
return(1);
}
/*2.10头插*/
int insertHead(PSeqList palist,DataType x)
{
int q;
if(palist->n==palist->MAXNUM)
{
printf("\n List overflow!");
return(0);
}
for(q=palist->n-1;q>=0;q--)
palist ->element[q+1]=palist ->element[q];
palist->element[0]=x;
palist->n++;
return(1);
}
/*2.11头删*/
int deleteHead(PSeqList palist)
{
int q;
for (q=0;qn-1;q++)
palist->element[q]=palist->element [q+1];
palist->n=palist->n-1;
return 1;

}
/*2.12尾删*/
int deleteRear(PSeqList palist)
{
palist->n--;
return(1);
}


上一篇:培训会计

下一篇:一代宗师 影评