-
大小: 10KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-05-27
- 語言: C/C++
- 標(biāo)簽: 數(shù)據(jù)結(jié)構(gòu)??
資源簡介
實(shí)現(xiàn)一個(gè)學(xué)生管理系統(tǒng),即定義一個(gè)包含學(xué)生信息(學(xué)號,姓名,成績)的順序表,可以不考慮重名的情況,系統(tǒng)包含以下功能:
(1) 根據(jù)指定學(xué)生個(gè)數(shù),逐個(gè)輸入學(xué)生信息;
(2) 逐個(gè)顯示學(xué)生表中所有學(xué)生的相關(guān)信息;
(3) 給定一個(gè)學(xué)生信息,插入到表中指定的位置;
(4) 刪除指定位置的學(xué)生記錄;
(5) 統(tǒng)計(jì)表中學(xué)生個(gè)數(shù);
(6) 利用直接插入排序或者折半插入排序按照姓名進(jìn)行排序;
(7) 利用快速排序按照學(xué)號進(jìn)行排序;
(8) 根據(jù)姓名進(jìn)行折半查找,要求使用遞歸算法實(shí)現(xiàn),成功返回此學(xué)生的學(xué)號和成績;
(9) 根據(jù)學(xué)號進(jìn)行折半查找,要求使用非遞歸算法實(shí)現(xiàn),成功返回此學(xué)生的姓名和成績。
代碼片段和文件信息
#include
#include
#include
#define?MAXSIZE?21?????//能容納的最大學(xué)生數(shù)
typedef?struct{
int?no;????//學(xué)號
char?name[30]; //姓名
double?score; //成績
}Student;
typedef??struct?{
??Student??*stu;?????//指向數(shù)據(jù)元素的基地址
??int??length;???????//線性表的當(dāng)前長度???????????????????????????????????????????????????????????
?}SqList;
//錄入學(xué)生信息
void?input(SqList?&L){
cout?<“請輸入學(xué)生個(gè)數(shù)(學(xué)生個(gè)數(shù)<=20):“;
int?n;
cin?>>?n;
while(n?>?20?||?n?1){
cout?<“對不起,輸入有誤,請重新輸入“?< cout?<“請輸入學(xué)生個(gè)數(shù)(學(xué)生個(gè)數(shù)<=20):“;
cin?>>?n;
}
L.stu?=?new?Student[MAXSIZE];
L.length?=?n;
for(int?i?=?1?;?i?<=?n?;?i++){
cout?< cout?<“請依次輸入第“?< cin?>>?L.stu[i].name;
cout?<“請依次輸入第“?< cin?>>?L.stu[i].no;
cout?<“請依次輸入第“?< cin?>>?L.stu[i].score;
}
cout?< cout?<“輸入完畢,共輸入了“?<}
void?output(SqList?L){
cout?<“總共有“?< cout?<“學(xué)號“?<“\t“?<“姓名“?<“\t“?<“成績“?< for(int?i?=?1?;?i?<=?L.length?;?i++){
cout?< cout?< }
cout?<}
//判斷學(xué)生學(xué)號是否相同
int?judge(int?n??SqList?L){
for(int?i?=?1?;?i?<=?L.length?;?i++){
if(n?==?L.stu[i].no)
return?1;
}
return?0;
}
//插入學(xué)生
void?insert(SqList?&L){
int?num?=?L.length;
if(num?==?MAXSIZE){
cout?<“順序表已滿,無法繼續(xù)插入學(xué)生信息!“?< exit(1);
}
Student?s;
int?n;
cout?<“請輸入要插入學(xué)生的位置:“;
cin?>>?n;
while(n?1?||?n?>?(num?+?1)){
cout?<<“對不起,輸入的位置有誤,請重新輸入!“< cout?<“請輸入要插入學(xué)生的位置:“;
cin?>>?n;
}
cout?<“請輸入該生的學(xué)號:“;
cin?>>?s.no;
int?flag1?=?judge(s.no??L);
while(flag1){
cout?<“對不起,該學(xué)號已存在,請重新輸入!“?< cout?<“請輸入該生的學(xué)號:“;
cin?>>?s.no;
flag1?=?judge(s.no??L);
}
cout?<“請輸入該生的姓名:“;
cin?>>?s.name;
cout?<“請輸入該生的成績:“;
cin?>>?s.score;
for(int?i?=?num?;?i?>=?n?;?i--)
L.stu[i?+?1]?=?L.stu[i];
L.stu[n]?=?s;
L.length++;
}
//刪除學(xué)生
void?deletestu(SqList?&L){
int?num?=?L.length;
if(num?==?0){
cout?<“順序表以為空!“?< exit(1);
}
int?n;
cout?<“請輸入要刪除學(xué)生的位置:“;
cin?>>?n;
while(n?1?||?n?>?num){
cout?<<“對不起,刪除的位置有誤,請重新輸入!“< cout?<“請輸入要刪除學(xué)生的位置:“;
cin?>>?n;
}
char?ch;
cout?<“要刪除的學(xué)生學(xué)號為“?< cout?<“是否刪除?(Y/N)“?< cin?>>?ch;
while(ch?!=?‘Y‘?&&?ch?!=?‘y‘?&&?ch?!=?‘N‘?&&?ch?!=?‘n‘){
cout?<“對不起,輸入有誤,?請重新輸入!“?< cout?<“是否繼續(xù)查找?(Y/N):“;
cin?>>?ch;
}
if(ch?==?‘Y‘?||?ch?==?‘y‘){
for(int?i?=?n;?i?<=?num;?i++)
L.stu[i]?=?L.stu[i?+?1];
L.length--;
cout?<“刪除完畢!“?< }
else
cout?<“取消刪除學(xué)生操作!“?<}
//統(tǒng)計(jì)表中學(xué)生的個(gè)數(shù)
void?Length(SqList?L){
cout?< cout?<“目前共有學(xué)生“?<}
//利用折半插入排序按照姓名進(jìn)行排序
void?Sort_Name(SqList?&L){
int?len?=?L.length;
for(int?i?=?2?;?i?<=?len?;?i+
評論
共有 條評論