#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
string username = "admin";
string passWord = "123456";
//设计学生结构体
struct Student
{
//学号
string s_Sno;
//姓名
string s_Name;
//性别(1,男/2,女)
int s_Sex;
//年龄
int s_Age;
//电话
string s_Phone;
//住址
string s_Addr;
};
//设计学生信息结构体
struct Informations
{
//学生信息中保存的数组
struct Student stuArray[MAX];
//当前信息记录个数
int s_Size;
};
//管理员登录
int landOn()
{
int i = 5;
while (i > 0)
{
string name;
string pass;
cout << "用户名:" << "";
cin >> name;
cout << "密码:" << "";
cin >> pass;
if (name == username && pass==passWord)
{
username = name;
passWord = pass;
break;
}
else
{
cout << "用户名或密码错误!请重新输入" << endl;
i--;
if (i == 0)
{
exit(0);
}
cout << "还有" << i << "次机会!" << endl;
}
}
}
//1、添加学生信息
void addStudent(Informations* info)
{
//判断管理系统是否满
if (info->s_Size == MAX)
{
cout << "管理系统已满,无法添加!";
}
else
{
//添加具体学生信息
//学号信息
string sno;
cout << "请输入学号:" << endl;
cin >> sno;
info->stuArray[info->s_Size].s_Sno = sno;
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
info->stuArray[info->s_Size].s_Name = name;
//性别
int sex = 0;
cout << "请输入性别(1--男,2--女):" << endl;
while (true)
{
//输入1或2正确输入
cin >> sex;
if (sex == 1 || sex == 2)
{
info->stuArray[info->s_Size].s_Sex = sex;
break;
}
cout << "输入有误,请重新输入:" << endl;
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
while (true)
{
cin >> age;
if (age > 0 && age <= 150)
{
info->stuArray[info->s_Size].s_Age = age;
break;
}
cout << "请输入正确的年龄:" << endl;
}
//电话
string phone;
cout << "请输入电话:" << endl;
while (true)
{
cin >> phone;
if (phone.size() == 11)
{
info->stuArray[info->s_Size].s_Phone = phone;
break;
}
cout << "输入有误,请输入正确的电话:" << endl;
}
//地址
string addr;
cout << "请输入住址:" << endl;
cin >> addr;
info->stuArray[info->s_Size].s_Addr = addr;
//更新学生信息人数
info->s_Size++;
cout << "添加成功" << endl;
system("pause"); //按任意键继续
system("cls"); //清屏
}
}
//2、显示学生信息
void showStudent(Informations* info)
{
//判断学生信息中是否为空,如果为空,提示记录为空
//如果不为0,则显示
if (info->s_Size == 0)
{
cout << "无信息" << endl;
}
else
{
for (int i = 0; i < info->s_Size; i++)
{
cout << "学号:" << info->stuArray[i].s_Sno << "\t";
cout << "姓名:" << info->stuArray[i].s_Name << "\t";
cout << "性别:" << (info->stuArray[i].s_Sex == 1 ? "男":"女") << "\t";
cout << "年龄:" << info->stuArray[i].s_Age << "\t";
cout << "电话:" << info->stuArray[i].s_Phone << "\t";
cout << "住址:" << info->stuArray[i].s_Addr << endl;
}
}
system("pause");
system("cls");
}
//检测学生信息是否存在,如果存在,返回具体编号,不存在返回-1
int isExist(Informations* info, string sno)
{
for (int i = 0; i < info->s_Size; i++)
{
//找到用户输入的姓名
if (info->stuArray[i].s_Sno == sno)
{
//找到了,返回具体位置
return i;
}
}
//遍历结束没找到,返回-1
return -1;
}
//3、删除指定联系人
void deleteStudent(Informations* info)
{
cout << "请输入要删除的学生学号:" << endl;
string sno;
cin >> sno;
//ret=-1未查到
//ret!=-1查到了
int ret = isExist(info, sno);
if (ret != -1)
{
//查找到了,具体删除操作
for (int i = ret; i < info->s_Size; i++)
{
//数据前移
info->stuArray[i] = info->stuArray[i + 1];
}
//更新信息个数
info->s_Size--;
cout << "删除成功" << endl;
}
else
{
//未查到
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//4、查找学生信息
void findStudent(Informations* info)
{
cout << "请输入要查找的学生学号:" << endl;
string sno;
cin >> sno;
//判断指定学生是否存在
int ret = isExist(info, sno);
if (ret != -1)
{
cout << "学号:" << info->stuArray[ret].s_Sno << "\t";
cout << "姓名:" << info->stuArray[ret].s_Name << "\t";
cout << "性别:" << info->stuArray[ret].s_Sex << "\t";
cout << "年龄:" << info->stuArray[ret].s_Age << "\t";
cout << "电话:" << info->stuArray[ret].s_Phone << "\t";
cout << "住址:" << info->stuArray[ret].s_Addr << endl;
}
else
{
cout << "没有找到此人的信息" << endl;
}
system("pause");
system("cls");
}
//5、修改指定学生信息
void modifyStudent(Informations* info)
{
cout << "请输入要修改的学生学号:" << endl;
string sno;
cin >> sno;
int ret = isExist(info, sno);
if (ret != -1)
{
//学号
string sno;
cout << "请输入学号:" << endl;
cin >> sno;
info->stuArray[ret].s_Sno = sno;
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
info->stuArray[ret].s_Name = name;
//性别
cout << "请输入性别(1--男,2--女):" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
//输入正确,覆盖写入退出循环
info->stuArray[ret].s_Sex = sex;
break;
}
cout << "输入有误,请重新输入:" << endl;
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
while (true)
{
cin >> age;
if (age > 0 && age <= 150)
{
info->stuArray[ret].s_Age = age;
break;
}
cout << "请输入正确的年龄:" << endl;
}
//电话
string phone;
cout << "请输入电话:" << endl;
while (true)
{
cin >> phone;
if (phone.size() == 11)
{
info->stuArray[ret].s_Phone = phone;
break;
}
cout << "输入有误,请输入正确的电话:" << endl;
}
//地址
string addr;
cout << "请输入住址:" << endl;
cin >> addr;
info->stuArray[ret].s_Addr = addr;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//6、清空学生信息
void cleanStudent(Informations* info)
{
cout << "确认要清空吗?(0--确认清空)" << endl;
int select = -1;
cin >> select;
if (select == 0)
{
//将当前学生信息数量置为0,做逻辑清空操作
info->s_Size = 0;
cout << "学生信息已清空" << endl;
}
else
{
cout << "学生信息未清空" << endl;
}
system("pause");
system("cls");
}
//菜单界面
void showMenu() {
cout << "**********************************" << endl;
cout << "*\t学生信息管理系统 *" << endl;
cout << "**********************************" << endl;
cout << "* 1、添加\t 2、显示 *" << endl;
cout << "* 3、删除\t 4、查找 *" << endl;
cout << "* 5、修改\t 6、清空 *" << endl;
cout << "* 0、退出\t *" << endl;
cout << "**********************************" << endl;
}
int main() {
landOn();
//创建学生信息结构体变量
Informations info;
//初始化学生信息中当前人数
info.s_Size = 0;
//创建用户选择输入变量
int select = 0;
while (true)
{
//菜单的调用
showMenu();
cin >> select;
switch (select)
{
//添加功能
case 1:
addStudent(&info); //利用地址传递,可以修饰实参
break;
//显示功能
case 2:
showStudent(&info);
break;
//删除功能
case 3:
deleteStudent(&info);
break;
//查找功能
case 4:
findStudent(&info);
break;
//修改功能
case 5:
modifyStudent(&info);
break;
//清空功能
case 6:
cleanStudent(&info);
break;
//退出功能
case 0:
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}