#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string s_id;
string s_name;
string s_sex;
int s_age;
public:
Student(const string& id, const string& name, const string& sex, const int age)
:s_id(id), s_name(name), s_sex(sex), s_age(age)
{
}
~Student(){}
//获取学号
string& Id() { return s_id; }//既可以获取属性的值,又可以修改属性的值
const string& Id() const { return s_id; }//增强通用性,常对象和普通对象都可以调用
//获取姓名
string& Name() { return s_name; }
const string& Name() const { return s_name; }
//获取性别
string& Sex() { return s_sex; }
const string& Sex() const { return s_sex; }
//获取年龄
int& Age() { return s_age; }
const int& Age() const { return s_age; }
};
class StudManage
{
private:
vector<Student> studata;
typename vector<Student>::iterator cur;
//typename的作用是告诉编译器iterator是一个类型而不是静态的成员变量
public:
StudManage()
{}
~StudManage()
{}
//添加学生
void Add_Student(const Student& s)
{
studata.push_back(s);
}
//打印所有学生的信息
void Print() const
{
cout << "请输出学生信息:" << endl;
for (auto const & x : studata)
{
cout << x.Id() << " " << x.Name() << " " << x.Sex() << " " << x.Age() << endl;
}
cout << endl;
}
//按照学号查询学生信息
bool Find_Id(const string& id)
{
bool res = false;
cur = studata.begin();
for (; cur != studata.end(); cur++)
{
if (cur->Id() == id)
{
res = true;
break;
}
}
return res;
}
//按照学号删除学生信息
bool Del_Id(const string& id)
{
bool res = Find_Id(id);
if (res)
{
studata.erase(cur);
cur = studata.end();
}
return res;
}
};
//增
void Add_Student(StudManage& stuMan)
{
string id, name, sex;
int age;
char ch;
do
{
cout << "请输入学生信息:" << endl;
cin >> id >> name >> sex >> age;
Student s1(id, name, sex, age);
stuMan.Add_Student(s1);
cout << "是否继续(y/n)" << endl;
cin >> ch;
} while (ch == 'y' || ch == 'Y');
cout << "添加成功!" << endl;
}
//查
bool Find_Student(StudManage& stuMan)
{
string id;
cout << "请输入要查询学生的学号:" << endl;
cin >> id;
return stuMan.Find_Id(id);
}
//删
bool Del_Student(StudManage& stuMan)
{
string id;
cout << "请输入要删除学生的学号:" << endl;
cin >> id;
return stuMan.Del_Id(id);
}
int MenuStudent()
{
int pos;
cout << "******************************************" << endl;
cout << " 1 添加学生" << endl;
cout << " 2 删除学生" << endl;
cout << " 3 查询学生" << endl;
cout << " 4 打印学生信息" << endl;
cout << " 0 退出" << endl;
cout << "******************************************" << endl;
cout << "请选择需要的操作:" << endl;
cin >> pos;
return pos;
}
int main()
{
StudManage studMan;
int pos = 0;
do
{
pos = MenuStudent();
switch (pos)
{
case 0:cout << "退出" << endl;
case 1:Add_Student(studMan);
break;
case 2:
if (Del_Student(studMan))
{
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败!!" << endl;
}
break;
case 3:
if (Find_Student(studMan))
{
cout << "查询成功!" << endl;
}
else
{
cout << "查询失败!" << endl;
}
break;
case 4:studMan.Print();
break;
default:cout << "选择错误!" << endl;
}
}while (pos != 0);
return 0;
}
c++实现简单的学生管理系统
最新推荐文章于 2024-08-12 00:13:37 发布