資源簡介
本演示程序利用B樹作為索引,實現了圖書管理功能,可以進行采編入庫、清除庫存及圖書借閱、圖書歸還、圖書預約、列出某著者全部著作名、列出某種書的狀態、每次插入或刪除一個關鍵字后以凹入表顯示B樹的狀態等操作。

代碼片段和文件信息
#?include?
#?include?
#?include?
#?include?
#?include?
#define?m?3
#define?OK?1
#define?ERROR?0
#define?MAX?20
typedef?int?Status;
typedef?int?keytype;
typedef struct ReaderNode
{
?unsigned?long?int cardnum;
?char ????name[MAX+1];
?struct?ReaderNode?*next;
?int????time;
}ReaderNode*ReaderType;
typedef?struct?BookNode{
???char?title[MAX+1];
???char?author[MAX+1];
???int??currenttotal;
???ReaderType reader;
???ReaderType bespeaker; ???
}BookNode*BookType;
typedef?struct?BTNode{
???int????keynum;
???struct?BTNode?*parent;
???int????key[m];
???BookNode?book[m];
???struct?BTNode?*ptr[m+1];
???int????leaf;
}BTNode*BTree;
int?x0;
int?y0;
short?x=8;
void?head()
{
?printf(“?*--------------------------------------------------------------------------*\n“);
?printf(“?|??編號??|????????書名????????|???????作者名???????|??總庫存量?|??現庫存量?|\n“);
?printf(“?*--------------------------------------------------------------------------*\n“);??
}
void?Visit(BTree?T)
{
?int?i=0;
?while(ikeynum)
?{
??printf(“?|%8d|%20s|%20s|????%4d???|????%4d???|\n“T->key[i]T->book[i].titleT->book[i].authorT->book[i].totalT->book[i].current);
??printf(“?*--------------------------------------------------------------------------*\n“);?
??i++;
?}
}
?
void?BTreeTraverse(BTree?T)//歷遍函數
{
?int?i;
?if(!T) return?;
?Visit(T);
?for(i=0;i<=T->keynum;?i++)
?BTreeTraverse(T->ptr[i]);
}
?
void?ShowBTree(BTree?Tshort?x)//以凹入表形式輸出B樹
{
?int i;
?if(!T) return;
?printf(“\n“);
?for(i=0;i<=x;i++)?putchar(‘?‘);
?for(i=0;ikeynum;i++)
?{
??printf(“%d“T->key[i]);
?}
?for(i=0?;i<=T->keynum;i++)
?ShowBTree(T->ptr[i]x+7);
}
void?gotoxy(int?xint?y){
?COORD?coord;
?coord.X=x;
?coord.Y=y;
?SetConsoleCursorPosition(?GetStdHandle(?STD_OUTPUT_HANDLE?)?coord?);
}
Status?EmptyNode(BTNode?*ptrint?i)//書結點初始化
{
??ptr->book[i].current=0;
??ptr->book[i].total=0;
??ptr->book[i].reader=NULL;
??ptr->book[i].bespeaker=NULL;
??ptr->book[i].title[0]=‘\0‘;
??ptr->book[i].author[0]=‘\0‘;
?return?OK;
}
Status?InputChar(char?*a)//輸入字符串,并檢驗合法性
{
?int?i=0;
?char?k;
?while(1)
?{
??k=getch();
??if(i>=MAX)
??{
???printf(“\n輸入數據超出允許范圍請重新輸入!“);
???i=0;
???continue;
??}
??if(k==13)/*輸入的是回車就結束*/
??{
???if(i==0)
???{
????printf(“\n輸入不能為空請重新輸入!\n“);
????continue;
???}
???a[i]=‘\0‘;
???break;
??}
??if(k==8)/*輸入的是回格就刪除上一個數*/
??{
???putchar(‘\b‘);
???putchar(32);
???putchar(‘\b‘);
???if(i>0)
???i--;
???continue;
??}
??putch(k);
??a[i]=k;
??i++;
?}
?return?OK;
}
Status?AddNum(BTNode?*ptrint?jint?k)//輸入整數,并檢驗合法性
{
?int?i;
?while(1)
?{
??scanf(“%d“&i);
??fflush(stdin);
??if(i)?break;
??else
??printf(“\n輸入數目不允許為空!請重新輸入:“);
??continue;?
?}
?if(k)
?{
??ptr->book[j].current+=i;
??ptr->book[j].total+=i;
?}
?else
?{
??ptr->book[j].current=i;
??ptr->book[j].total=i;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????192555??2010-07-09?00:15??圖書管理.exe
?????文件??????17132??2010-07-09?00:15??圖書管理.c
-----------?---------??----------?-----??----
???????????????209687????????????????????2
- 上一篇:圖像匹配、圖像配準、圖像校正
- 下一篇:基于51單片機的測量放大電路,簡單實用
評論
共有 條評論