生活随笔
收集整理的這篇文章主要介紹了
文件+树,图书目录管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文件+樹,圖書目錄管理系統
【主要內容】
開發一個圖書目錄管理系統,作為圖書館系統的子系統。目錄管理系統管理的對象是目錄,要求是實現以下基本功能:
(1)對目錄的增刪查改功能
(2)對文件中保存的目錄文件順序輸出
(3)輸出樹形目錄
【數據結構】
樹的存儲結構,用.dat文件當作數據庫。
關于文件的操作詳見我的另一篇博文:
新手使用fwrite和fread一點小知識
在實現階段,由于我讀題不認真(西八),沒看清楚需求。在存儲和讀取時樹和數組混用了,造成了不少代碼冗余,而我又懶得改。。各位大佬將就著看看吧。
代碼:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstdlib>
using namespace std
;
#define MAX_TREE_SIZE 100typedef struct Catalog { char Key
[20]; char Caption
[80]; char Parent
[20];
}Catalog
;typedef struct CatalogTree { Catalog Node
[MAX_TREE_SIZE
];int root
; int num
;
}CatalogTree
;void InitTree(CatalogTree
&T
)
{for (int i
= 0; i
< MAX_TREE_SIZE
; i
++){*T
.Node
[i
].Caption
= -1;*T
.Node
[i
].Key
= -1;*T
.Node
[i
].Parent
= -1;}T
.root
= 0;T
.num
= 0;
}
void Initdata()
{Catalog s
[5] = {{"0","中國圖書分類","-1"},{"A","馬列主義","0"},{"B","哲學、宗教","0"},{"C","社會科學理論","0"},{"A1","論十大關系","A"} };FILE
* fp
= fopen("data.dat", "w+");if (fp
== NULL){exit(-1);}fwrite((void*)s
, sizeof(s
), 1, fp
);fclose(fp
);
}
void LoadTree(CatalogTree
&T
) {FILE
* fp
= fopen("catalog-目錄管理數據源.dat", "rb");if (fp
== NULL){cout
<< "False" << endl
;exit(-1);}for (int i
= 0; i
<MAX_TREE_SIZE
; i
++){fseek(fp
, i
* sizeof(struct Catalog), 0);fread(&T
.Node
[i
], sizeof(struct Catalog), 1, fp
);if (feof(fp
)){T
.num
= i
;break;}}fclose(fp
);
}
void Save(CatalogTree
&T
)
{FILE
* fp
= fopen("catalog-目錄管理數據源.dat", "wb");for (int i
= 0; i
< T
.num
; i
++){fseek(fp
, i
* sizeof(struct Catalog), 0);fwrite(&T
.Node
[i
], sizeof(struct Catalog), 1, fp
);}fclose(fp
);
}
void addCatalog(CatalogTree
&T
)
{Catalog
*tmp
= new Catalog
;cout
<< "Key:";cin
>> tmp
->Key
;getchar();cout
<< "Caption:";cin
>> tmp
->Caption
;getchar();cout
<< "Parent:";cin
>> tmp
->Parent
;getchar();T
.Node
[T
.num
++] = *tmp
;Save(T
);
}
int Search(CatalogTree
& T
)
{int b
=0;Catalog
* tmp
= new Catalog
;cout
<< "輸入要查找的目錄的編號:" << "\n";cout
<< "Key:";cin
>> tmp
->Key
;getchar();for (int i
= 0; i
< T
.num
; i
++){if (T
.Node
[i
].Key
==tmp
->Key
){b
= i
;break;}}if (b
== 0)cout
<< "要找的目錄不存在" << endl
;return b
;
}void DeleteCatalog(CatalogTree
& T
)
{int i
= Search(T
);for (i
; i
< T
.num
; i
++){T
.Node
[i
] = T
.Node
[i
+ 1];}T
.num
--;Save(T
);
}
void AlterCatalog(CatalogTree
& T
)
{int i
= Search(T
);cout
<< "輸入要修改的內容:" << endl
;cout
<< "Key:";cin
>> T
.Node
[i
].Key
;getchar();cout
<< "Caption";cin
>> T
.Node
[i
].Caption
;getchar();cout
<< "Parent:";cin
>> T
.Node
[i
].Parent
;getchar();Save(T
);
}void PrintfCatalog(CatalogTree
&T
)
{for (int i
= 0; i
< T
.num
; i
++){cout
<< T
.Node
[i
].Key
<< T
.Node
[i
].Caption
<< T
.Node
[i
].Parent
<< endl
;}cout
<< T
.root
<< "\n" << T
.num
<< "\n";
}
void CreatCatalogTree()
{}
void PreOrder(CatalogTree
& T
, int k
, int level
)
{int m
= 0;for (int i
= 0; i
< level
; i
++)cout
<< " ";cout
<< "|--" << T
.Node
[k
].Caption
<< "\t" << T
.Node
[k
].Key
<<"\n";for (int j
= k
+ 1; j
< T
.num
; j
++){if (!strcmp(T
.Node
[j
].Parent
, T
.Node
[k
].Key
)){m
= 1;PreOrder(T
, j
, level
+ m
);}}
}
void PrintCatalogTree(CatalogTree
&T
)
{int i
= 0;int level
= 1;PreOrder(T
, i
, level
);
}void menu(CatalogTree
&T
)
{cout
<< " ************************************************* " << "\n";cout
<< " 分類管理菜單 " << "\n";cout
<< " ************************************************* " << "\n";cout
<< "* 1.增加分類 *" << "\n";cout
<< "* 2.輸出分類 *" << "\n";cout
<< "* 3.輸出樹形分類樹 *" << "\n";cout
<< "* 4.修改分類 *" << "\n";cout
<< "* 5.刪除分類 *" << "\n";cout
<< "* 0.返回上級 *" << "\n";cout
<< " ************************************************* " << "\n";cout
<< "請輸入你的選擇!0-5:" << endl
;int n
;cin
>> n
;switch (n
) {case 1:addCatalog(T
);system("pause");break;case 2:PrintfCatalog(T
);system("pause");break;case 3:PrintCatalogTree(T
);system("pause");break;case 4:AlterCatalog(T
);system("pause");break;case 5:DeleteCatalog(T
);system("pause");break;case 0:system("pause");break;default:cout
<< "輸入錯誤\n";system("pause");break;}
}
int main()
{CatalogTree T
;InitTree(T
);LoadTree(T
);while (1) {system("cls");menu(T
);LoadTree(T
);}return 0;
}
運行結果
總結
以上是生活随笔為你收集整理的文件+树,图书目录管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。