链表实现C/C++简单学生学籍管理系统。
实验结果图: 剩下的操作有兴趣的话复制粘贴去试试:
#include"ManagerStu.h"
int Student
::AGE
[] = { 0 };
int Student
::CLNUM
[] = { 0 };
string Student
::DEPART
[] = { " " };
int Student
::departTypes
= 0;
int Student
::sizes
= 0;
int main()
{
ManagerStu p
;
p
.start();
return 0;
}
Student类:
#pragma once
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std
;
class Student {
public:
Student();
void setBasic();
void changeStu();
Student
* delMessage(int nums
);
Student
* searchStu(int nums
);
Student
* searchStu(string names
);
Student
* searchStuSex(string sex
);
Student
* searchStuAge(int age
);
Student
* searchStuClass(int classNum
);
Student
* searchStuDepart(string Depart
);
void searchAll();
bool judgeNums(int num
);
bool judgeClass(int num
);
bool judgeSex(string u
);
bool judgeName(string
& inStr
);
bool judgeAge(int num
);
void judge1(int num
);
void judge2(int num
);
void judge3(string u
);
void judge4(string
& inStr
);
void judge5(int num
);
void statisticsSex();
void statisticsClass();
void statisticsAge();
void statisticsDepart();
void statisticsAll();
void showMess();
void showBasic();
void setBasicFile();
void writeF(ofstream
& ofs
);
static int AGE
[31];
static int CLNUM
[201];
static string DEPART
[15];
static int departTypes
;
static int sizes
;
Student
* next
= NULL;
private:
int nums
;
string name
;
string sex
;
int age
;
string department
;
string majoy
;
int classNum
;
};
#include"Student.h"
Student
::Student() {
nums
= 0;
name
= " ";
sex
= " ";
age
= 0;
classNum
= 0;
majoy
= " ";
department
= " ";
}
void Student
::setBasic() {
cout
<< "依次输入学生的学号、姓名、性别、年龄、系、专业、班号:\n";
cin
>> nums
;
judge1(nums
);
cin
>> name
;
judge4(name
);
cin
>> sex
;
judge3(sex
);
cin
>> age
;
judge5(age
);
cin
>> department
>> majoy
;
cin
>> classNum
;
judge2(classNum
);
sizes
++;
CLNUM
[classNum
]++;
AGE
[age
]++;
int i
= 0;
while (i
< departTypes
) {
if (DEPART
[i
] == department
) break;
i
++;
}
if (i
== departTypes
) {
DEPART
[i
] = department
;
departTypes
++;
}
}
Student
* Student
::delMessage(int nums
) {
Student
* p
= this;
Student
* pLast
= NULL;
while (p
!= NULL) {
if (p
->nums
!= nums
) {
pLast
= p
;
p
= p
->next
;
}
else break;
}
if (p
== NULL) {
cout
<< "查无此人!无法进行删除。\n";
return NULL;
}
else {
if (p
->nums
== this->nums
) return p
->next
;
else {
pLast
->next
= p
->next
;
AGE
[p
->age
]--;
CLNUM
[p
->classNum
]--;
delete p
;
return this;
}
}
}
void Student
::changeStu() {
cout
<< "输入相应学生的学号,进行修改信息:\n";
int u
; cin
>> u
;
int times
= 3;
while (times
-- && !judgeNums(u
))
cin
>> u
;
Student
* p
= this;
while (p
!= NULL) {
if (p
->nums
!= u
) p
= p
->next
;
else break;
}
cout
<< "输入对应操作数进行修改:\n";
cout
<< "1.学号 2.姓名 3.性别 4.年龄 5.系别 6.专业 7.班号\n";
int oper
;
cin
>> oper
;
switch (oper
) {
case 1:
cout
<< "输入学号:";
cin
>> p
->nums
;
p
->judge1(p
->nums
);
break;
case 2:
cout
<< "输入姓名:";
cin
>> p
->name
;
p
->judge4(p
->name
);
break;
case 3:
cout
<< "输入性别:";
cin
>> p
->sex
;
p
->judge3(p
->sex
);
break;
case 4:
cout
<< "输入年龄:";
cin
>> p
->age
;
p
->judge5(p
->age
);
break;
case 5:
cout
<< "输入系别:";
cin
>> p
->department
;
break;
case 6:
cout
<< "输入专业:";
cin
>> p
->majoy
;
break;
case 7:
cout
<< "输入班号:";
cin
>> p
->classNum
;
p
->judge2(p
->classNum
);
break;
default:
break;
}
}
Student
* Student
::searchStu(int nums
) {
Student
* p
= this;
Student
* u
= NULL;
while (p
!= NULL) {
if (p
->nums
!= nums
) p
= p
->next
;
else {
u
= new Student
;
*u
= *p
;
u
->next
= NULL;
return u
;
}
}
return NULL;
}
Student
* Student
::searchStu(string names
) {
Student
* p
= this;
Student
* u
= NULL;
while (p
!= NULL) {
if (p
->nums
!= nums
) p
= p
->next
;
else {
u
= new Student
;
*u
= *p
;
u
->next
= NULL;
return u
;
}
}
return NULL;
}
Student
* Student
::searchStuSex(string sex
) {
Student
* p
= this;
Student
* temp
= NULL, * head
= NULL;
Student
* tNext
= NULL;
while (p
!= NULL) {
if (p
->sex
== sex
) {
tNext
= new Student
;
*tNext
= *p
;
tNext
->next
= NULL;
if (head
== NULL) {
temp
= tNext
;
head
= temp
;
}
else {
temp
->next
= tNext
;
temp
= temp
->next
;
}
if(tNext
!=NULL)
tNext
= tNext
->next
;
}
p
= p
->next
;
}
if (head
!= NULL)
return head
;
else return NULL;
}
Student
* Student
::searchStuAge(int age
) {
Student
* head
= NULL;
Student
* p
= this;
Student
* temp
= NULL;
Student
* tNext
= NULL;
while (p
!= NULL) {
if (p
->age
== age
) {
tNext
= new Student
;
*tNext
= *p
;
tNext
->next
= NULL;
if (head
== NULL) {
temp
= tNext
;
head
= temp
;
}
else {
temp
->next
= tNext
;
temp
= temp
->next
;
}
if (tNext
!= NULL)
tNext
= tNext
->next
;
}
p
= p
->next
;
}
if (head
!= NULL)
return head
;
else return NULL;
}
Student
* Student
::searchStuClass(int classNum
) {
Student
* p
= this;
Student
* temp
= NULL, * head
= NULL;
Student
* tNext
= NULL;
while (p
!= NULL) {
if (p
->classNum
== classNum
) {
tNext
= new Student
;
*tNext
= *p
;
tNext
->next
= NULL;
if (head
== NULL) {
temp
= tNext
;
head
= temp
;
}
else {
temp
->next
= tNext
;
temp
= temp
->next
;
}
if (tNext
!= NULL)
tNext
= tNext
->next
;
}
p
= p
->next
;
}
if (head
!= NULL)
return head
;
else return NULL;
}
Student
* Student
::searchStuDepart(string Depart
) {
Student
* p
= this;
Student
* temp
= NULL, * head
= NULL;
Student
* tNext
= NULL;
while (p
!= NULL) {
if (p
->department
== Depart
) {
tNext
= new Student
;
*tNext
= *p
;
tNext
->next
= NULL;
if (head
== NULL) {
temp
= tNext
;
head
= temp
;
}
else {
temp
->next
= tNext
;
temp
= temp
->next
;
}
if (tNext
!= NULL)
tNext
= tNext
->next
;
}
p
= p
->next
;
}
if (head
!= NULL)
return head
;
else return NULL;
}
void Student
::searchAll() {
cout
<< "1.按学号查询单个学生:\n";
cout
<< "2.按姓名查询单个学生\n";
cout
<< "3.按性别查询学生信息\n";
cout
<< "4.按班号查询学生信息\n";
cout
<< "请输入相应操作数:";
int u
; cin
>> u
;
int numS
;
string nameS
;
string sexX
;
int numC
;
Student
* p
= NULL;
switch (u
) {
case 1:
cout
<< "输入学号:";
cin
>> numS
;
judge1(numS
);
p
= searchStu(numS
);
if (p
!= NULL)p
->showMess();
else cout
<< "查无此人!请重新操作\n";
break;
case 2:
cout
<< "输入性名:";
cin
>> nameS
;
judge4(nameS
);
p
= searchStu(nameS
);
if (p
!= NULL)p
->showMess();
else cout
<< "查无此人!请重新操作\n";
break;
case 3:
cout
<< "输入性别:";
cin
>> sexX
;
judge3(sexX
);
p
= searchStuSex(sexX
);
if (p
!= NULL)p
->showMess();
else cout
<< "查无此人!请重新操作\n";
break;
case 4:
cout
<< "输入班号:";
cin
>> numC
;
judge2(numC
);
p
= searchStuClass(numC
);
if (p
!= NULL) p
->showMess();
else cout
<< "查无此人!请重新操作\n";
break;
default:
break;
}
}
bool Student
::judgeNums(int num
) {
for (int i
= 1; i
<= 9; i
++)
num
/= 10;
if (num
== 0 || num
> 10) return false;
else return true;
}
bool Student
::judgeClass(int num
) {
if (num
>= 1 && num
<= 200) return true;
else return false;
}
bool Student
::judgeSex(string u
) {
if (u
== "男" || u
== "女")return true;
else return false;
}
bool Student
::judgeName(string
& inStr
) {
unsigned char* str
= (unsigned char*)inStr
.c_str();
size_t length
= inStr
.length();
bool t
= true;
for (size_t i
= 0; i
< length
; ++i
)
{
if (str
[i
] > 160)
t
= true;
if (inStr
[i
]>=0 && inStr
[i
]<=127) {
t
= false;
break;
}
}
if (t
) return true;
else
return false;
}
bool Student
::judgeAge(int num
) {
if (num
<= 30 && num
>= 10) return true;
else return false;
}
void Student
::judge1(int num
) {
int times
= 4;
while (!judgeNums(nums
) && times
> 1) {
cout
<< "学号格式错误," << "还剩" << (times
- 1) << "次输入机会:"; cin
>> nums
;
times
--;
}
if (times
== 1) { cout
<< "输入学号错误!\n"; system("pause"); exit(0); }
else return;
}
void Student
::judge2(int num
) {
int times
= 4;
while (!judgeClass(classNum
) && times
>1) {
cout
<< "班号格式错误," << "还剩" << (times
- 1) << "次输入机会:"; cin
>> classNum
;
times
--;
}
if (times
== 1) { cout
<< "输入班号错误!\n"; system("pause"); exit(0); }
}
void Student
::judge3(string u
) {
int times
= 4;
while (!judgeSex(sex
) && times
>1) {
cout
<< "性别格式错误," << "还剩" << (times
- 1) << "次输入机会:"; cin
>> sex
;
times
--;
}
if (times
== 1) { cout
<< "输入性别错误!\n"; system("pause"); exit(0); }
}
void Student
::judge4(string
& inStr
) {
int times
= 4;
while (!judgeName(name
) && times
> 1) {
cout
<< "姓名格式错误," << "还剩" << (times
- 1) << "次输入机会:"; cin
>> name
;
times
--;
}
if (times
== 1) { cout
<< "输入姓名错误!\n"; system("pause"); exit(0); }
}
void Student
::judge5(int num
) {
int times
= 4;
while (!judgeAge(age
) && times
> 1) {
cout
<< "年龄输入错误," << "还剩" << (times
- 1) << "次输入机会:"; cin
>> age
;
times
--;
}
if (times
== 1) { cout
<< "输入年龄错误!\n"; system("pause"); exit(0); }
}
void Student
::statisticsSex() {
cout
<< "按性别统计学生人数:" << '\n';
Student
* p
= searchStuSex("男");
int numS
=0;
while (p
!= NULL) {
p
= p
->next
;
numS
++;
}
cout
<< "男生人数:" << numS
<< '\n';
cout
<< "女生人数:" << sizes
-numS
<< '\n';
}
void Student
::statisticsClass() {
int numC
=0;
Student
* p
= NULL;
cout
<< "按班号统计学生人数:" << '\n';
for (int i
= 1; i
<= 200; i
++) {
numC
= 0;
if (CLNUM
[i
] != 0) {
p
= searchStuClass(i
);
while (p
!= NULL) {
p
= p
->next
;
numC
++;
}
cout
<< "班号:" << i
<< '\t' << "人数:" << numC
<< '\n';
}
else continue;
}
}
void Student
::statisticsAge() {
int numA
= 0;
Student
* p
= NULL;
cout
<< "按年龄统计学生人数:" << '\n';
for (int i
= 1; i
<= 30; i
++) {
numA
= 0;
if (AGE
[i
] != 0) {
Student
* p
= searchStuAge(i
);
while (p
!= NULL) {
p
= p
->next
;
numA
++;
}
cout
<< "年龄:" << i
<< " \t" << "人数:" << numA
<< '\n';
}
else continue;
}
}
void Student
::statisticsDepart() {
int numD
= 0;
cout
<< "按系别统计学生人数:" << '\n';
for (int i
= 0; i
< departTypes
; i
++) {
numD
= 0;
Student
* p
= searchStuDepart(DEPART
[i
]);
while (p
!= NULL) {
p
= p
->next
;
numD
++;
}
cout
<< "系别:" << DEPART
[i
] << '\t' << "人数:" << numD
<< '\n';
}
}
void Student
::statisticsAll() {
cout
<< "1.根据性别统计人数:\n";
cout
<< "2.根据班号统计人数\n";
cout
<< "3.根据年龄统计人数\n";
cout
<< "4.根据系别统计人数\n";
cout
<< "请输入相应操作数:";
int u
; cin
>> u
;
switch (u
) {
case 1:
statisticsSex();
break;
case 2:
statisticsClass();
break;
case 3:
statisticsAge();
break;
case 4:
statisticsDepart();
break;
default:
break;
}
}
void Student
::showMess() {
Student
* p
= this;
cout
<< setiosflags(ios
::left
) << "学号:" << "\t\t" << "姓名:" << "\t\t" <<
"性别:" << "\t\t" << "年龄:" << "\t\t" << "系别:" << "\t\t" << "专业:" << "\t\t" << "班号:" << '\n';
while (p
!= NULL) {
p
->showBasic();
p
= p
->next
;
}
}
void Student
::showBasic() {
cout
<< setw(16) << nums
;
cout
<< setw(16) << name
;
cout
<< setw(16) << sex
;
cout
<< setw(16) << age
;
cout
<< setw(16) << department
;
cout
<< setw(16) << majoy
;
cout
<< setw(16) << classNum
;
cout
<< '\n';
}
void Student
::setBasicFile() {
Student
* p
= this;
Student
* pLast
= NULL;
ifstream
instuf("学生信息.txt", ios
::in
);
instuf
.seekg(0, ios
::beg
);
instuf
.peek();
if (instuf
.eof()) cout
<< "文件为空!\n";
else {
while (!instuf
.eof()) {
instuf
>> p
->nums
;
if (!judgeNums(p
->nums
))break;
instuf
>> p
->name
;
instuf
>> p
->sex
;
instuf
>> p
->age
>> p
->department
>> p
->majoy
;
instuf
>> p
->classNum
;
sizes
++;
CLNUM
[p
->classNum
]++;
AGE
[p
->age
]++;
int i
= 0;
while (i
< departTypes
) {
if (DEPART
[i
] == p
->department
) break;
i
++;
}
if (i
== departTypes
) {
DEPART
[i
] = p
->department
;
departTypes
++;
}
p
->next
= new Student
;
pLast
= p
;
p
= p
->next
;
}
}
if (pLast
!= NULL)
pLast
->next
= NULL;
}
void Student
::writeF(ofstream
& ofs
) {
Student
* p
= this;
if (p
== NULL) { cout
<< "没有数据! 请重新操作\n"; return; }
ofs
<< nums
<< ' ' << name
<< ' ' << sex
<< ' ' << age
<< ' ' << department
<< ' ' << majoy
<< ' ' << classNum
<< '\n';
}
ManagerStu类:
#pragma once
#include"Student.h"
class ManagerStu {
public:
void start();
void outScreen();
void out();
void oper();
void writeFile();
Student
* readFile();
private:
Student
* head
= NULL;
};
#include"ManagerStu.h"
void ManagerStu
::start() {
this->outScreen();
this->oper();
}
void ManagerStu
::outScreen() {
cout
<< "---------------------------------------------------------\n";
cout
<< "\n\n";
cout
<< " 学校学籍管理系统\n";
cout
<< " 实现功能:\n\n";
cout
<< " 1.录入 2.批量录入\n";
cout
<< " 3.删除 4.修改 5.查询 \n";
cout
<< " 6.所有信息分类统计 \n";
cout
<< " 7.输出所有学生信息\n";
cout
<< " 8.保存在文件中 \n\n";
cout
<< " 0.退出 \n";
cout
<< "\n\n";
cout
<< "---------------------------------------------------------\n";
}
void ManagerStu
::out() {
exit(0);
}
void ManagerStu
::oper() {
cout
<< "输入数字做相应操作:";
char t
;
int u
;
Student
* p
=NULL;
Student
* pNext
= NULL;
head
= p
;
Student
* temp
= head
;
while (cin
>>t
) {
if(!(t
<= '9' && t
>= '0')){
cout
<< "无此操作数!请重新输入(退出输入0):\n";
cin
>> t
;
if (t
== '0') out();
}
else {
switch (t
) {
case '1':
pNext
= new Student
;
pNext
->setBasic();
if (p
== NULL) {
p
= pNext
;
head
= p
;
}
else {
p
->next
= pNext
;
p
= p
->next
;
}
pNext
= pNext
->next
;
break;
case '2':
p
= readFile();
temp
= head
;
while (temp
!= NULL && temp
->next
!= NULL) temp
= temp
->next
;
if (temp
== NULL) head
= p
;
else
temp
->next
= p
;
cout
<< "信息录入成功!\n";
while (p
->next
!= NULL)p
= p
->next
;
break;
case '3':
head
->showMess();
cout
<< "输入要删除成员的学号:";
cin
>> u
;
head
=head
->delMessage(u
);
head
->sizes
--;
cout
<< "删除后:\n";
head
->showMess();
break;
case '4':
head
->showMess();
head
->changeStu();
cout
<< "修改后:\n";
head
->showMess();
break;
case '5':
head
->searchAll();
break;
case '6':
head
->statisticsAll();
break;
case '7':
head
->showMess();
break;
case '8':
writeFile();
cout
<< "信息保存成功!\n";
free(head
);
head
->sizes
= 0;
head
= NULL;
break;
case '0':
exit(0);
break;
default:
break;
}
}
system("pause");
system("cls");
outScreen();
cout
<< "输入数字做相应操作,输入0则退出\n";
}
}
void ManagerStu
::writeFile() {
ofstream ofs
;
Student
* p
= head
;
ofs
.open("学生信息.txt", ios
::trunc
);
while (p
!= NULL) {
p
->writeF(ofs
);
p
= p
->next
;
}
ofs
.close();
}
Student
* ManagerStu
::readFile() {
Student
* p
= new Student
;
p
->setBasicFile();
return p
;
}
在做这玩意时,碰到最大的坑是,在按学号查找学生时原链表丢失了,导致花了好长时间找和改。
转载请注明原文地址:https://ipadbbs.8miu.com/read-61130.html