-
大小: 6KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-06-04
- 語言: C/C++
- 標(biāo)簽: 數(shù)據(jù)結(jié)構(gòu)??順序表??
資源簡(jiǎn)介
基于順序存儲(chǔ)的學(xué)生名冊(cè)管理程序源代碼
學(xué)生名冊(cè)管理程序,名冊(cè)中的每條記錄包括學(xué)號(hào)和姓名。程序能夠在順序表,棧和隊(duì)列三種模式下自由切換,支持?jǐn)?shù)據(jù)的插入、刪除、查找、按學(xué)號(hào)排序和屏幕打印。具體要求的功能如下:
1) 按照順序表的方式,在指定位置進(jìn)行插入與刪除。
2) 以棧的方式進(jìn)行插入與刪除。
3) 以隊(duì)列的方式進(jìn)行插入與刪除。
4) 按姓名進(jìn)行順序查找,按學(xué)號(hào)進(jìn)行二分查找(查找學(xué)號(hào)時(shí)需要先排序)。
5) 打印所有學(xué)生的信息。
學(xué)生名冊(cè)管理程序,名冊(cè)中的每條記錄包括學(xué)號(hào)和姓名。程序能夠在順序表,棧和隊(duì)列三種模式下自由切換,支持?jǐn)?shù)據(jù)的插入、刪除、查找、按學(xué)號(hào)排序和屏幕打印。具體要求的功能如下:
1) 按照順序表的方式,在指定位置進(jìn)行插入與刪除。
2) 以棧的方式進(jìn)行插入與刪除。
3) 以隊(duì)列的方式進(jìn)行插入與刪除。
4) 按姓名進(jìn)行順序查找,按學(xué)號(hào)進(jìn)行二分查找(查找學(xué)號(hào)時(shí)需要先排序)。
5) 打印所有學(xué)生的信息。
代碼片段和文件信息
#include
#include
#include
#include
#include
#define?INIT_SIZE?5??/*線性表存儲(chǔ)空間的初始分配量*/
#define?EX_SIZE?5??/*線性表存儲(chǔ)空間的分配增量*/
using?namespace?std;
typedef?struct?{
char?name[20];
int?ID;
}stu;
typedef?stu?Datatype;
typedef?struct?{
stu*?student;????//存儲(chǔ)空間的基地址
int?length;?????????//當(dāng)前長(zhǎng)度
int?size;??? ?//當(dāng)前分配的存儲(chǔ)容量
}sq_List;
int?Initialize(sq_List*);//初始化順序表
void?lv1menu(sq_List*);//一級(jí)菜單
void?lv2menu(char?sq_List*);//二級(jí)菜單
int?ins_ele(char?sq_List*);//插入
int?del_ele(char?sq_List*);//刪除
int?prt_all(sq_List*);//打印
void?sort(sq_List);//排序
int?sea_ele(sq_List?int);//按學(xué)號(hào)二分查找
int?main()
{
sq_List?database;
if?(!Initialize(&database))?{
cout?<“Error!?Initialization?failed!“?< exit(0);
}//初始化失敗則結(jié)束程序
lv1menu(&database);//顯示一級(jí)菜單
return?0;
}
int?Initialize(sq_List*?List)
/*初始化順序表,成功返回1,失敗返回0*/
{
List->student?=?(Datatype*)malloc(INIT_SIZE?*?sizeof(Datatype));
if?(!List->student)
return?0;?????????????/*初始化失敗*/
List->length?=?1;
List->size?=?INIT_SIZE;
return?1;????????????????/*初始化成功*/
}
void?lv1menu(sq_List*?List)
{
char?_op1;
bool?flag?=?true;
//system(“cls“);
do?{
cout?<“?????學(xué)生信息管理系統(tǒng)“?< cout?<“---------------------------“?< cout?<“A.按照順序表方式管理“?< cout?<“B.按照棧方式管理“?< cout?<“C.按照隊(duì)列方式管理“?< cout?<“D.查找\tE.打印\nF.退出“?< cout?<“選擇操作:“;
cin?>>?_op1;
_op1?=?toupper(_op1);
if?(_op1?==?‘F‘)flag?=?false;
lv2menu(_op1?List);
cout?<“\n“;
}?while?(flag);
}
void?lv2menu(char?m?sq_List*?List)
{
char?optype?_op2;
int?p;
optype?=?m;
int?target;
if?(optype?==?‘A‘?||?optype?==?‘B‘?||?optype?==?‘C‘)?{
p?=?0;
cout?<“\na.插入\nb.刪除\nc.返回“?< cout?<“選擇操作:“;
cin?>>?_op2;
cout?<“\n“;
if?(_op2?==?‘c‘)
lv1menu(List);
if?(_op2?==?‘a(chǎn)‘)?{
do?{
ins_ele(optype?List);
cout?<“是否繼續(xù)輸入(1表示繼續(xù)插入,0表示終止插入)“;
cin?>>?p;
cout?<“\n“;
}?while?(p?==?1);
}
if?(_op2?==?‘b‘)?{
do?{
del_ele(optype?List);
cout?<“刪除后表中元素有:?“?< prt_all(List);
cout?<“是否繼續(xù)刪除(1表示繼續(xù)刪除,0表示終止刪除)“;
cin?>>?p;
cout?<“\n“;
}?while?(p?==?1);
}
}
if?(optype?==?‘D‘)?{
cout?<“輸入要查找的學(xué)號(hào):“;
cin?>>?target;
p?=?sea_ele(*List?target);
if?(p?>?0)
cout?<“查找目標(biāo)位于第“?< else
cout?<“目標(biāo)未找到?!?< cout?<“\n“;
system(“pause“);
}
if?(optype?==?‘E‘)?{
prt_all(List);
}
}
int?ins_ele(char?optype?sq_List*?List)
{
Datatype*?temp?=?NULL?buff;//temp暫存指針,buff暫存數(shù)據(jù)
int?j;
if?(List->length?>=?List->size?-?1)???????????? /*當(dāng)前存儲(chǔ)空間已滿,增加分配*/
{
temp?=?(Datatype*)realloc(List->student?(List->size?+?EX_SIZE)?
評(píng)論
共有 條評(píng)論