文章目录
—————————————————————————————————
零、写在前面
- 源代码也不知道同学在哪找的,优化和添加原来的一些功能,基本是三创了。小文我还是初学,很多地方还有很多纰漏,很多操作实际上也不熟(比如容器),有什么写的不好的地方还请大神指正。
不过拿去交作业是够了。 - 我用的是visual studio 2019,不同平台可能会有兼容问题,这 我就解决不了了,但是这些功能都用的是简单方法,应该问题不大。
一、简介
- 此学生管理系统为基于c++的一个简单的程序,涉及基本的输入输出、类的操作、system()函数、顺序容器的一些操作。作业要求是实现下列功能
- (一)实现帐号登陆界面。
- (二)实现任意添加学生的信息。
- (三)实现删除学生的信息。
- (四)实现查询学生的信息。
- (五)实现编辑学生的信息。
- (六)实现学生的信息保存功能。(未实现)
- (七)实现注销功能。(未实现)
由于小文太菜&实践有限后面两个功能没有研究出来
二、怎么用这些代码
- 如果有同学不嫌弃这些代码,要用这个去交作业,就得认真看看这部分了。
- 为了使代码调试起来容易我将类的各个成员函数的具体实现分到不同的CPP文件中。类的声明部分我放在了_MANAGE_.h中,登录界面的声明我扔到_LOGIN.h_里面。
- 简单来说你直接新建工程 然后按照下面图片分别在头文件区里面新建两个头文件,在源文件里面新建8个CPP文件然后把代码复制粘贴,理论上能运了。
下面就到代码环节
三、功能实现
(1)登录界面
LOGIN.h
#ifndef _LOGIN_
#define _LOGIN_
void login();
#endif
Login.cpp
想要更改密码和用户名的话修改if里面的“username==”和“password==”,注意修改的话要全部修改,这里的小小特色功能是可以在用户输错三次之后自动退出程序。
#include <iostream>
#include<string>
#include <sstream>
using namespace std;
void login()
{
int i;
string username;
string password;
bool success = false;
cout << "\t欢迎来到学生信息管理系统" << endl;
cout << "\t请输入你的用户名和密码" << endl;
do
{
cout <<"请勿输入空格以及直接回车,否则程序将无法进行下去"<< endl;
cout << "用户名: ";
cin >> username;
cout << endl;
cout << "密码: ";
cin >> password;
cout << endl;
if (username == "Marvin" && password == "Ashina@123")
{
cout << "\n————————欢迎使用学生管理系统——————————\n";
success = true;
}
else if (username != "Marvin" && password == "Ashina@123")
{
L1: cout << "\n———————用户名有错————————";
for (i = 3; i >=1 ; --i)
{
cout << "还有" << i << "次尝试机会" << endl;
cout << "用户名: ";
cin >> username;
if (username == "Marvin")
{
success = true;
break;
}
if(i==1)
{
cout << "错误次数过多,系统自动退出。" << endl;
exit(0);
}
}
}
else if (username == "Marvin" && password != "Ashina@123")
{
L2: cout << "\n————————密码有错————————";
for (i = 3; i >= 1; --i)
{
cout << "还有" << i << "次尝试机会" << endl;
cout << "密码: ";
cin >> password;
if (password == "Ashina@123")
{
success = true;
break;
}
if (i == 1)
{
cout << "错误次数过多,系统自动退出。" << endl;
exit(0);
}
}
}
else if (username != "Marvin" && password != "Ashina@123")
{
cout << "\n————————密码和用户名都错了————————";
for (i = 3; i >= 1; --i)
{
cout << "还有" << i << "次尝试机会" << endl;
cout << "用户名: ";
cin >> username;
cout << "密码: ";
cin >> password;
if (username == "Marvin" && password == "Ashina@123")
{
success = true;
break;
}
else if (username != "Marvin" && password == "Ashina@123")
goto L1;
else if (username == "Marvin" && password != "Ashina@123")
goto L2;
if (i == 1)
{
cout << "错误次数过多,系统自动退出。" << endl;
exit(0);
}
}
success = false;
}
} while (!success);
getchar();
system("cls");
}
(二)类的声明
MANAGE.h
#ifndef _MANAGE_
#define _MANAGE_
#include <iostream>
#include <list>
#include<string>
#include <sstream>
using namespace std;
// 永远不要再头文件定义变量!!!!
class Manage
{
private:
struct Student
{
string name;
string age;
string id;
string sex;
string birth;
string major;
string grade;
string cla;
string skill;
};
list<Student> studlist;
//list是一种序列式容器。
public:
void StartUp(); //登录界面
bool AddInform(); //添加学生信息
void Print(); //查看学生信息
void Search(); //查找学生信息
void Delete(); //删除学生信息
void Edit(); //编辑学生信息
};
#endif
(三)系统初始界面
StartUp.cpp
#include <iostream>
#include"_MANAGE_.h"
void Manage::StartUp() //面板
{
cout << "*********学生信息系统**********" << endl;
cout << "* *" << endl;
cout << "* 1 添加学生信息 *" << endl;
cout << "* 2 查看学生信息 *" << endl;
cout << "* 3 查找学生信息 *" << endl;
cout << "* 4 删除学生信息 *" << endl;
cout << "* 5 编辑学生信息 *" << endl;
cout << "* 6 退出信息系统 *" << endl;
cout << "* *" << endl;
cout << "***********************************" << endl;
}
(四)添加学生信息
AddInform.cpp
#include <iostream>
#include <list>
#include<string>
#include <sstream>
#include"_MANAGE_.h"
using namespace std;
bool validation(string str); //输入内容确认函数
bool Manage::AddInform() //添加学生信息
{
Student student;
cout << "输入学生信息:" << endl;
L1: //跳转实现错误检测
cout << "该学生学号为 ( 0 -- 退出 ): ";
cin >> student.id;
if (student.id == "0") return false;
//如果学号相同则输出提示并重新输入
for (list<Student>::iterator it = studlist.begin(); it != studlist.end(); it++)
if (it->id == student.id)
{
cout << endl << "学号已经存在:" << endl << endl;
goto L1; //L1
}
//输入部分
cout << "该学生姓名为: ";
cin >> student.name;
//如果什么都没输入的话
if (student.name.length() == 0 || student.name == " ")
{
cout << "请输入学生姓名:" << endl;
cin >> student.name;
}
//学生姓名
//返回false则停止添加学生信息
//输入部分
cout << "该学生年龄为: ";
cin >> student.age;
if (student.age.length() == 0 || student.age == " ")
{
cout << "请输入学生年龄: " << endl;
cin >> student.age;
}
if (!validation(student.age))
{
cout << "请输入数字: " << endl;
cin >> student.age;
}
//学生年龄
//返回false则停止添加学生信息
//输入部分
cout << "该学生生日为: ";
cin >> student.birth;
if (student.birth.length() == 0 || student.birth == " ")
{
cout << "请输入学生生日: " << endl;
cin >> student.birth;
}
if (!validation(student.birth))
{
cout << "请输入数字: " << endl;
cin >> student.birth;
}
//学生生日
//返回false则停止添加学生信息
//输入部分
cout << "该学生性别为: ";
cin >> student.sex;
if (student.sex.length() == 0 || student.sex == " ")
{
cout << "请输入学生性别: " << endl;
cin >> student.sex;
}
//学生性别
//返回false则停止添加学生信息
//输入部分
cout << "该学生专业为: ";
cin >> student.major;
if (student.major.length() == 0 || student.major == " ")
{
cout << "请输入学生专业: " << endl;
cin >> student.major;
}
//学生专业
//返回false则停止添加学生信息
//输入部分
cout << "该学生年级为: ";
cin >> student.grade;
if (student.grade.length() == 0 || student.grade == " ")
{
cout << "请输入学生年级: " << endl;
cin >> student.grade;
}
//学生年级
//返回false则停止添加学生信息
cout << "该学生班级为: ";
cin >> student.cla;
if (student.cla.length() == 0 || student.cla == " ")
{
cout << "请输入学生班级: " << endl;
cin >> student.cla;
}
//学生班级
//返回false则停止添加学生信息
cout << "该学生特长为: ";
cin >> student.skill;
if (student.skill.length() == 0 || student.skill == " ")
{
cout << "请输入学生特长: " << endl;
cin >> student.skill;
}
//学生特长
//返回false则停止添加学生信息
cout << endl;
while (1)
{
string yn;
cout << "确定输入吗? (Y -- 确定,N -- 放弃):" << endl;
cin >> yn;
if (yn.length() != 1) continue;
if (yn == "Y" || yn == "y")
{
cout << endl << "该生信息已经添加" << endl << endl;
break;
}
else if (yn == "N" || yn == "n")
{
return false;
}
}
//新学生信息将被添加到容器尾部
studlist.push_back(student);
return true;
}
bool validation(string str)
{
for (int i = 0; i < str.size(); i++)
{
int tmp = (int)str[i];
if (tmp >= 48 && tmp <= 57)
{
continue;
}
else
{
return false;
}
}
return true;
}
(五)查看学生信息
Print.cpp
#include <iostream>
#include"_MANAGE_.h"
using namespace std;
void Manage:: Print() //查看学生信息
{
cout << endl;
if (studlist.begin() == studlist.end())
{
cout << "尚未有记录任何学生信息。" << endl;
}
//遍历整个list进行
for (list<Student>::const_iterator it = studlist.begin(); it != studlist.end(); it++)
{
cout << "姓名: " << it->id << endl;
cout << "年龄: " << it->age << endl;
cout << "学号: " << it->name << endl;
cout << "生日: " << it->birth << endl;
cout << "性别: " << it->sex << endl;
cout << "专业: " << it->major << endl;
cout << "年级: " << it->grade << endl;
cout << "班级: " << it->cla << endl;
cout << "特长: " << it->skill << endl;
}
}
(六)查找学生信息
Search.cpp
#include <iostream>
#include"_MANAGE_.h"
using namespace std;
void Manage:: Search() //查找(姓名||Student ID)
{
bool sign = false;
char key;
cout << "输入1按照学号进行搜索,输入2将按照名字搜索" << endl;
cin >> key;
//按Student ID查找
if (key == '1')
{
string _id;
cout << "输入学号:";
cin >> _id;
//遍历整个list进行
for (list<Student>::iterator it = studlist.begin(); it != studlist.end(); it++)
if (it->id == _id)
{
cout << "姓名: " << it->id << endl;
cout << "学号: " << it->name << endl;
cout << "生日: " << it->birth << endl;
cout << "性别: " << it->sex << endl;
cout << "专业: " << it->major << endl;
cout << "年级: " << it->grade << endl;
cout << "班级: " << it->cla << endl;
cout << "特长: " << it->grade << endl;
sign = true;
}
if (!sign)
cout << "学号“ " << _id << " ”不存在" << endl;
}
//按姓名查找
else if(key == '2')
{
string _name;
cout << "Search by name:";
cin >> _name;
for (list<Student>::iterator it = studlist.begin(); it != studlist.end(); it++)
if (it->name == _name)
{
cout << "姓名: " << it->id << endl;
cout << "学号: " << it->name << endl;
cout << "生日: " << it->birth << endl;
cout << "性别: " << it->sex << endl;
cout << "专业: " << it->major << endl;
cout << "年级: " << it->grade << endl;
cout << "班级: " << it->cla << endl;
cout << "特长: " << it->grade << endl;
sign = true;
}
if (!sign)
cout <<"姓名"<< _name << "不存在" << endl;
}
}
(七)删除学生信息
Delete.cpp
#include <iostream>
#include"_MANAGE_.h"
using namespace std;
void Manage::Delete()
{
char key = '0';
cout << "若输入1则按照学号进行搜索,输入2将按照名字搜索" << endl;
cin >> key;
bool sign = false;
if (key == '1')
{
string _id;
cout << "输入学号:" << endl;
cin >> _id;
//遍历整个list进行搜索
for (list<Student>::iterator it = studlist.begin(); it != studlist.end(); it++)
if (it->id == _id)
{
cout << "学号为"<< it->id <<"的学生的信息已删除" << endl;
studlist.erase(it);
sign = true;
return;
}
if (!sign)
cout <<"学号"<< _id <<"不存在" << endl;
}
else if (key == '2')
{
string _name;
cout << "输入学生名字:" << endl;
cin >> _name;
for (list<Student>::iterator it = studlist.begin(); it != studlist.end(); it++)
if (it->name == _name)
{
cout << "姓名为" << it->name << "的学生的信息已删除" << endl;
studlist.erase(it);
sign = true;
return;
}
if (!sign)
cout << "学号" << _name << "不存在" << endl;
}
}
(八)编辑学生信息
Edit.cpp
#include <iostream>
#include"_MANAGE_.h"
using namespace std;
void Manage::Edit() //编辑学生信息
{
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 << "* 9 特长 *" << endl;
cout << "* *" << endl;
cout << "***********************************" << endl << endl;
;
cout << "输入1按照学号进行搜索,输入2将按照名字搜索" << endl;
char key, i;
cin >> key;
//按学号查找
if (key == '1')
{
string _id;
bool sign = false;
cout << "输入学号:";
cin >> _id;
//遍历整个list进行
for (list<Student>::iterator it = studlist.begin(); it != studlist.end(); it++)
if (it->id == _id)
{
cout << "请根据上表输入你要修改的项目" << endl;
cin >> i;
switch (i)
{
case '1': //修改学号
cout << "请输入修改后的姓名" << endl;
cin >> it->name;
cout << endl << endl;
cout << "该生姓名已经修改为" << it->name << endl << endl;
sign = true;
break;
case '2': //修改学号
cout << "请输入修改后的年龄" << endl;
cin >> it->age;
cout << endl << endl;
cout << "该生年龄已经修改为" << it->age << endl << endl;
sign = true;
break;
case '3': //修改姓名
cout << "请输入修改后的学号" << endl;
cin >> it->id;
cout << endl << endl;
cout << "该生学号已经修改为" << it->id << endl << endl;
sign = true;
break;
case '4': //修改生日
cout << "请输入修改后的性别" << endl;
cin >> it->sex;
cout << endl << endl;
cout << "该生性别已经修改为" << it->sex << endl << endl;
sign = true;
break;
case '5': //修改性别
cout << "请输入修改后的生日" << endl;
cin >> it->birth;
cout << endl << endl;
cout << "该生生日已经修改为" << it->birth << endl << endl;
sign = true;
break;
case '6': //修改专业
cout << "请输入修改后的专业" << endl;
cin >> it->major;
cout << endl << endl;
cout << "该生专业已经修改为" << it->major << endl << endl;
sign = true;
case '7': //修改年级
cout << "请输入修改后的年级" << endl;
cin >> it->grade;
cout << endl << endl;
cout << "该生年级已经修改为" << it->grade << endl << endl;
sign = true;
case '8': //修改班级
cout << "请输入修改后的班级" << endl;
cin >> it->cla;
cout << endl << endl;
cout << "该生班级已经修改为" << it->cla << endl << endl;
sign = true;
case '9': //修改特长
cout << "请输入修改后的特长" << endl;
cin >> it->skill;
cout << endl << endl;
cout << "该生特长已经修改为" << it->skill << endl << endl;
sign = true;
}
}
if (!sign)
cout << "学号“ " << _id << " ”不存在" << endl;
}
//按姓名查找
else
{
string _name;
cout << "输入姓名:";
cin >> _name;
bool sign = false;
for (list<Student>::iterator it = studlist.begin(); it != studlist.end(); it++)
if (it->id == _name)
{
cout << "请根据上表输入你要修改的项目" << endl;
cin >> i;
switch (i)
{
case '1': //修改学号
cout << "请输入修改后的姓名" << endl;
cin >> it->name;
cout << endl << endl;
cout << "该生姓名已经修改为" << it->name << endl << endl;
sign = true;
break;
case '2': //修改学号
cout << "请输入修改后的年龄" << endl;
cin >> it->age;
cout << endl << endl;
cout << "该生年龄已经修改为" << it->age << endl << endl;
sign = true;
break;
case '3': //修改姓名
cout << "请输入修改后的学号" << endl;
cin >> it->id;
cout << endl << endl;
cout << "该生学号已经修改为" << it->id << endl << endl;
sign = true;
break;
case '4': //修改生日
cout << "请输入修改后的性别" << endl;
cin >> it->sex;
cout << endl << endl;
cout << "该生性别已经修改为" << it->sex << endl << endl;
sign = true;
break;
case '5': //修改性别
cout << "请输入修改后的生日" << endl;
cin >> it->birth;
cout << endl << endl;
cout << "该生生日已经修改为" << it->birth << endl << endl;
sign = true;
break;
case '6': //修改专业
cout << "请输入修改后的专业" << endl;
cin >> it->major;
cout << endl << endl;
cout << "该生专业已经修改为" << it->major << endl << endl;
sign = true;
case '7': //修改年级
cout << "请输入修改后的年级" << endl;
cin >> it->grade;
cout << endl << endl;
cout << "该生年级已经修改为" << it->grade << endl << endl;
sign = true;
case '8': //修改班级
cout << "请输入修改后的班级" << endl;
cin >> it->cla;
cout << endl << endl;
cout << "该生班级已经修改为" << it->cla << endl << endl;
sign = true;
case '9': //修改特长
cout << "请输入修改后的特长" << endl;
cin >> it->skill;
cout << endl << endl;
cout << "该生特长已经修改为" << it->skill << endl << endl;
sign = true;
}
if (!sign)
cout << "姓名" << _name << "不存在S" << endl;
}
}
}
(九)main函数
Main.cpp
#include <iostream>
#include <list>
#include<string>
#include <sstream>
#include"_MANAGE_.h"
#include"_LOGIN_.h"
using namespace std;
void main()
{
login();//调试时可以注释掉省去登录的工作
Manage test;
test.StartUp();
while (true)
{
char key = '0';
cout << endl << "选择一个功能:" << endl;
L1: cin >> key;
cout << endl;
switch (key)
{
case '1': //添加
while (test.AddInform());
break;
case '2': //输出所有信息
test.Print();
break;
case '3': //查找
test.Search();
break;
break;
case '4': //删除
test.Delete();
break;
case '5': //编辑
test.Edit();
break;
case '6': //退出程序
cout << "感谢使用系统,再见" << endl;
}
system("pause");
system("cls");
test.StartUp();
//功能执行完毕,key再接收一个值
cout << endl << "请选择另一个功能" << endl;
goto L1;
cout << endl;
}
}
到此所有的代码就都在这里了
如果出现什么有关system的错误,可以试试把using namespace std; 这行删了再加上去,一般这个错误就消失了。
二点五、流程图
三、写在后面
希望这些代码能更好地到帮助你完成作业或者学习c++。
其实本来想学学结合MySQL和MFC怎么做个图形界面出来的,但是失败了。不过你要真想做话,大神冰做出来了,链接在此
https://blog.csdn.net/jiushiabingbing/article/details/111793345
从拿到源码到研究明白再到自己优化添加功能花了一天时间,期间你站、VS的官方文档以及谢站都帮了不少忙。拿过来的源码是全都塞在一个cpp文件中的,为了调试方便进行二创我就给他分开了,又学到了头文件怎么写,调试错误时又学会了去查官方文档,虽然因为高强度用眼导致第二天起来眼睛疼,但是总的来说过程是快乐的。
谢谢你的阅读。