#include<iostream>
#include<fstream>
#include<algorithm>
#define FILENAME "student.txt"
using namespace std;
struct Student {
int m_id;
string m_name;
string m_major;
string m_sex;
int m_age;
Student *next;
};
void menu() {
cout << "学生信息管理系统" << endl;
cout << " 1.录入" << endl;
cout << " 2.插入" << endl;
cout << " 3.删除" << endl;
cout << " 4.输出" << endl;
cout << " 5.查找" << endl;
cout << " 6.清空" << endl;
cout << " 7.排序" << endl;
cout << " 8.退出" << endl;
cout << endl;
}
void clear() {
system("pause");
system("cls");
}
void create() {
ofstream ofs;
bool flag = true;
int id;
string name;
string major;
string sex;
int age;
while (flag) {
cout << "请输入学生的学号;" << endl;
cin >> id;
cout << "请输入学生的姓名:" << endl;
cin >> name;
cout << "请输入学生的专业:" << endl;
cin >> major;
cout << "请输入学生的性别:" << endl;
cin >> sex;
cout << "请输入学生的的年龄" << endl;
cin >> age;
ofs.open(FILENAME, ios::app | ios::binary);
ofs << id << endl << name << endl << major << endl << sex << endl << age << endl;
ofs.close();
cout << "是否继续录入学生信息:(0.取消 1.确定)" << endl;
cin >> flag;
}
cout << "录入完成!" << endl;
clear();
}
void save(Student *head) {
ofstream ofs;
Student *p = head;
ofs.open(FILENAME, ios::trunc | ios::binary);
while (p->next != nullptr) {
p = p->next;
ofs << p->m_id << endl << p->m_name << endl << p->m_major << endl << p->m_sex << endl << p->m_age << endl;
}
}
Student* makeLinkList(Student *head) {
ifstream ifs;
ifs.open(FILENAME, ios::in | ios::binary);
if (!ifs.is_open()) {
cout << "文件打开失败" << endl;
exit(0);
}
int id;
string name;
string major;
string sex;
int age;
Student *p = head;
while (ifs >> id && ifs >> name && ifs >> major && ifs >> sex && ifs >> age) {
Student *newNode = new Student;
newNode->m_id = id;
newNode->m_name = name;
newNode->m_major = major;
newNode->m_sex = sex;
newNode->m_age = age;
p->next = newNode;
p = newNode;
}
p->next = nullptr;
ifs.close();
return head;
}
void output(Student *head) {
head = makeLinkList(head);
Student *p = head;
while (p->next != nullptr) {
p = p->next;
cout << "学生的学号:" << p->m_id << " "
<< "学生的姓名:" << p->m_name << " "
<< "学生的专业:" << p->m_major << " "
<< "学生的性别;" << p->m_sex << " "
<< "学生的年龄:" << p->m_age << " "
<< endl;
}
}
void insert(Student* head) {
output(head);
head = makeLinkList(head);
Student *p = head;
int id;
string name;
string major;
string sex;
int age;
int pos, i = 0, j = 0;
cout << "请输入要插入的位置在第几个后:" << endl;
cin >> pos;
while (p->next != nullptr) {
p = p->next;
j++;
}
p = head;
if (pos > j || pos < 1) {
cout << "您的输入不合法!" << endl;
clear();
return;
}
cout << "请输入学生的学号;" << endl;
cin >> id;
cout << "请输入学生的姓名:" << endl;
cin >> name;
cout << "请输入学生的专业:" << endl;
cin >> major;
cout << "请输入学生的性别:" << endl;
cin >> sex;
cout << "请输入学生的的年龄" << endl;
cin >> age;
while (p->next != nullptr) {
p = p->next;
i++;
if (i == pos) {
Student* newNode = new Student;
newNode->next = p->next;
p->next = newNode;
newNode->m_id = id;
newNode->m_name = name;
newNode->m_major = major;
newNode->m_sex = sex;
newNode->m_age = age;
}
}
p->next = nullptr;
save(head);
cout << "插入完成!" << endl;
clear();
}
void _delete(Student *head) {
output(head);
head = makeLinkList(head);
int pos, i = 0, j = 0;
cout << "请输入要删除第几个学生:" << endl;
cin >> pos;
Student *p = head;
while (p->next != nullptr) {
p = p->next;
j++;
}
if (pos > j || pos < 1) {
cout << "您的输入不合法!" << endl;
clear();
return;
}
p = head;
while (p->next != nullptr) {
if (i == pos) {
p->next = p->next->next;
break;
}
p = p->next;
}
save(head);
cout << "删除完成!" << endl;
clear();
}
void find(Student *head) {
head = makeLinkList(head);
cout << "请输入要查询的学号:" << endl;
int pos, flag = -1;
cin >> pos;
Student *p = head;
while (p->next != nullptr) {
p = p->next;
if (p->m_id == pos) {
cout << "已经查到了学号为:" << pos << " 的学生" << endl
<< "学生的姓名:" << p->m_name << " "
<< "学生的专业:" << p->m_major << " "
<< "学生的性别;" << p->m_sex << " "
<< "学生的年龄:" << p->m_age << " "
<< endl;
flag = 0;
break;
}
}
if (flag == -1)
cout << "没有查到该学号的学生!" << endl;
cout << "查询完成!" << endl;
clear();
}
bool cmp(Student x, Student y) {
return x.m_id < y.m_id;
}
void _sort(Student *head) {
head = makeLinkList(head);
vector<Student> tmpVector;
Student *p = head;
while (p->next != nullptr) {
p = p->next;
tmpVector.push_back(*p);
}
sort(tmpVector.begin(), tmpVector.end(), cmp);
Student *newhead = new Student;
Student *np = newhead;
while (!tmpVector.empty()) {
Student * newNode = new Student;
np->next = newNode;
np = newNode;
Student tmp = *tmpVector.begin();
tmpVector.erase(tmpVector.begin());
newNode->m_age = tmp.m_age;
newNode->m_id = tmp.m_id;
newNode->m_major = tmp.m_major;
newNode->m_name = tmp.m_major;
newNode->m_sex = tmp.m_sex;
}
np->next = nullptr;
save(newhead);
cout << "排序完成!" << endl;
clear();
}
void clean() {
ofstream ofs;
ofs.open(FILENAME, ios::trunc | ios::binary);
cout << "清空完成!" << endl;
clear();
}
int main() {
int choose;
Student *head = new Student;
while (1) {
menu();
cout << "请输入选项:" << endl;
cin >> choose;
switch (choose) {
case 1:
create();
break;
case 2:
insert(head);
break;
case 3:
_delete(head);
break;
case 4:
output(head);
clear();
break;
case 5:
find(head);
break;
case 6:
clean();
break;
case 7:
_sort(head);
break;
case 8:
exit(0);
break;
default:
system("cls");
cout << "您的输入有误,请重新输入" << endl;
system("pause");
break;
}
}
return 0;
}
学生管理系统
于 2023-10-13 18:24:40 首次发布