#include "Student.h"
#include<iostream>
using namespace std;
string filename("untitled");
void show(student *p);
Student::Student()
{
head = new student;
head->stu_id = 0;
head->stu_name = "No name";
head->stu_next = NULL;
}
Student::~Student()
{
student *p = head, *q;
while (p)
{
q = p;
p = q->stu_next;
delete q;
}
}
//注册函数
void Student::logon()
{
student tmp;
cout << "学号 (0 结束输入 ): ";
cin >> tmp.stu_id;
while (tmp.stu_id)
{
if (findById(tmp.stu_id) == NULL)
{
cout << "姓 名: ";
cin >> tmp.stu_name;
cout << "年 龄: ";
cin >> tmp.stu_age;
cout << "性 别: ";
cin >> tmp.stu_sex;
cout << "籍 贯: ";
cin >> tmp.stu_birth;
cout << "系 别:";
cin >> tmp.stu_department;
cout << "专 业:";
cin >> tmp.stu_major;
cout << "班 级:";
cin >> tmp.stu_class;
cout << "手 机: ";
cin >> tmp.stu_tel;
cout << "电子邮箱: ";
cin >> tmp.stu_email;
insert(tmp);
}
else cout << "重复的学号: " << tmp.stu_id << endl;
cout << "学号 (0 结束输入): ";
cin >> tmp.stu_id;
}
}
//查找函数
void Student::Query() const {
int select;
unsigned id;
string name;
string sex;
string email;
student *p;
cout << "1、按学号查询\n2、按姓名查询\n3、按性别查询\n4、按Email查询\n0、返回\n";
cin >> select;
switch (select)
{
case 1: cout << "请输入学号: "; cin >> id;
if ((p = findById(id))) show(p->stu_next);
break;
case 2: cout << "请输入姓名: "; cin >> name;
if ((p = findByName(name))) show(p->stu_next);
break;
case 3:cout << "请输入性别:"; cin >> sex;
if ((p = findBySex(sex))) show(p->stu_next);
break;
case 4:cout << "请输入Email:"; cin >> email;
if ((p = findByEmail(email))) show(p->stu_next);
break;
case 0: return;
default: cout << "选择错误. \n";
}
}
student *Student::findByEmail(const string &email) const {
student *p;
for (p = head; p->stu_next; p = p->stu_next)
if (p->stu_next->stu_email == email) return p;
cout << "没有找到输入的Email" << endl;
return NULL;
}
student *Student::findBySex(const string &sex) const {
student *p;
for (p = head; p->stu_next; p = p->stu_next)
if (p->stu_next->stu_sex == sex) return p;
cout << "没有找到输入的性别的用户" << endl;
return NULL;
}
student *Student::findByName(const string &name) const {
student *p;
for (p = head; p->stu_next; p = p->stu_next)
if (p->stu_next->stu_name == name) return p;
cout << "没有找到输入的姓名" << endl;
return NULL;
}
student *Student::findById(unsigned id) const
{
student *p;
for (p = head; p->stu_next; p = p->stu_next)
if (p->stu_next->stu_id == id) return p;
return NULL;
}
//删除函数
void Student::deleteBy() {
int select;
unsigned id;
string name;
cout << "1、按学号删除\n2、按姓名删除\n0、返回\n";
cin >> select;
switch (select)
{
case 1: cout << "请输入学号: "; cin >> id;
deleteById(id);
break;
case 2: cout << "请输入姓名: "; cin >> name;
deleteByName(name);
break;
case 0: return;
default: cout << "选择错误. \n";
}
}
bool Student::deleteById(unsigned &id) {
student *q, *p;
p = findById(id);
if (p == NULL)
{
cout << "没有找到学号是 \"" << id << "\" 的学生,删除失败! \n";
return false;
}
q = p->stu_next;
p->stu_next = q->stu_next;
delete q;
cout << "成功删除 " << id << " 的信息. \n";
return true;
}
bool Student::deleteByName(string &name) {
student *q, *p;
p = findByName(name);
if (p == NULL) {
cout << "没有找到姓名是 \"" << name << "\" 的学生, 删除失败! \n";
return false;
}
q = p->stu_next;
p->stu_next = q->stu_next;
delete q;
cout << "成功删除 " << name << " 的信息. \n";
return true;
}
//修改函数
void Student::modify() {
int select;
cout << "1、按学号修改\n2、按姓名修改\n0、返回\n";
cin >> select;
switch (select) {
case 1: if (modifyById()) cout << "修改成功. \n"; break;
case 2: if (modifyByName()) cout << "修改成功. \n"; break;
case 0: return;
default: cout << "选择错误. \n";
}
}
bool Student::modifyById()
{
student *p;
unsigned id;
cout << "输入要修改的学号: ";
cin >> id;
p = findById(id);
if (p == NULL)
{
cout << "没有找到学号是 \"" << id << "\" 的学生, 修改失败! \n";
return false;
}
createByStdin(1,*(p->stu_next));
return true;
}
bool Student::modifyByName()
{
student *p;
string name;
cout << "输入要修改人的姓名: ";
cin >> name;
p = findByName(name);
if (p == NULL) {
cout << "没有找到姓名是 \"" << name << "\" 的学生, 修改失败! \n";
return false;
}
createByStdin(2, *(p->stu_next));
return true;
}
student *Student::createByStdin(int model,student &tmp)
{
if (model != 1) { cout << "学 号: "; cin >> tmp.stu_id; }
if (model != 2) { cout << "姓 名: "; cin >> tmp.stu_name; }
cout << "年龄:";
cin >> tmp.stu_age;
cout << "性 别: ";
cin >> tmp.stu_sex;
cout << "籍贯: ";
cin >> tmp.stu_birth;
cout << "系别:";
cin >> tmp.stu_department;
cout << "专业:";
cin >> tmp.stu_major;
cout << "班级:";
cin >> tmp.stu_class;
cout << "手 机: ";
cin >> tmp.stu_tel;
cout << "电子邮箱: ";
cin >> tmp.stu_email;
return &tmp;
}
//文件操作函数
void Student::saveToFile()
{
string new_file;
cout << "请输入欲保存的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::out);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = headCount();
file << len << endl;
student *pHead = head->stu_next;
while (pHead != NULL)
{
file << pHead->stu_id << endl;
file << pHead->stu_name << endl;
file << pHead->stu_sex << endl;
file << pHead->stu_age << endl;
file << pHead->stu_birth << endl;
file << pHead->stu_department << endl;
file << pHead->stu_major << endl;
file << pHead->stu_class << endl;
file << pHead->stu_email << endl;
file << pHead->stu_tel << endl;
pHead = pHead->stu_next;
}
file.close();
cout << "保存成功. " << endl;
filename = new_file;
return;
}
void Student::readFile()
{
string new_file;
cout << "请输入欲读取的文件名: ";
cin >> new_file;
fstream file;
file.open(new_file.c_str(), ios::in);
if (file.bad())
{
cout << "文件打开失败. " << endl;
return;
}
int len = 0;
file >> len;
if (!len)
{
cout << "文件数据异常. " << endl;
return;
}
student pHead;
file.get();
while (len--)
{
file >> pHead.stu_id;
file >> pHead.stu_name;
file >> pHead.stu_sex;
file >> pHead.stu_age;
file >> pHead.stu_birth;
file >> pHead.stu_department;
file >> pHead.stu_major;
file >> pHead.stu_class;
file >> pHead.stu_email;
file >> pHead.stu_tel;
insert(pHead);
}
file.close();
cout << "文件读入成功. " << endl;
filename = new_file;
return;
}
bool Student::insert(const student &astu)
{
student *newnode, *p = head;
if (p->stu_next == NULL) //当没有下一个堆,则新建一个堆来储存修改数据
{
p->stu_next = new student(astu); //用传入的已修改结构初始化
p->stu_next->stu_next = NULL; //NULL处理再下一个堆
return true;
}
while (p->stu_next) //当存在下一个堆
{
if (p->stu_next->stu_id == astu.stu_id) //判重
{
cout << "重复的学号, 插入失败! \n";
return false;
}
if (p->stu_next->stu_id > astu.stu_id) //如果下一个堆的id大于传入id
{
newnode = new student(astu); //newnode指向初始修改结构
newnode->stu_next = p->stu_next;
p->stu_next = newnode; // 移位
return true;
}
p = p->stu_next; //既不大于也不重复就后移添加
}
p->stu_next = new student(astu);
p->stu_next->stu_next = NULL;
return true;
}
//显示函数
void Student::Show() const {
student *p;
cout << "----------------------------------------------------------\n";
人工智能-项目实践-信息管理系统-c++课程设计 学生信息管理系统
版权申诉
18 浏览量
2024-03-10
10:30:22
上传
评论
收藏 7KB ZIP 举报
博士僧小星
- 粉丝: 1923
- 资源: 5884
最新资源
- (大赛作品)STM32实现的F072RB NUCLEO智能家居控制.zip
- STM32实现的数字示波器源码+数字信号处理教程、配套实例.zip
- 【cookie续续】【cookie续续】【cookie续续】
- 低功耗STM32实现的F411开发板(原理图+PCB源文件+官方例程+驱动等).zip
- 基于stm32实现的 nucleo-L476的智能灯(操作说明+源码).zip
- 基于STM32实现的 NUCLEO板设计彩色LED照明灯(纯cubeMX开发).zip
- 基于STM32实现的 的联合调试侦听设备解决方案(原理图、PCB源文件、调试工具、视频).zip
- 基于STM32实现的 人群定位、调速智能风扇设计(程序、设计报告、视频演示).zip
- 哔哩下载姬(DownKyi)
- 千纬认字app再次更新,增加部首、笔画动画功能
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈