用c++ STL中的vector容器来做一个学生信息管理系统
- 这个是一个比较综合的项目,用到许多c++的知识点
- 用到面向对象的思想,类的使用(权限,构造函数等...)
- 用到友元函数的运用场景
- c++运算符重载
- 输入输出流的操作
- 文件流操作
- 仿函数的使用
- Lambad表达式使用
- ...
#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
#include<fstream>
using namespace std;
class Student
{
public:
Student(string id="", string name="", int age=0, string address="") :id(id), name(name), age(age), address(address)
{
}
friend fstream& operator<<(fstream& out, Student& stu)
{
out << stu.id << "\t" << stu.name << "\t" << stu.age << "\t" << stu.address << endl;
return out;
}
friend fstream& operator>>(fstream& in, Student& stu)
{
in >> stu.id >> stu.name >> stu.age >> stu.address;
return in;
}
friend ostream& operator<<(ostream& out, const Student& stu)
{
out << stu.id << "\t" << stu.name << "\t" << stu.age << "\t" << stu.address << endl;
return out;
}
friend istream& operator>>(istream& in, Student& stu)
{
in >> stu.id >> stu.name >> stu.age >> stu.address;
return in;
}
int getAge() const
{
return age;
}
string getName() const
{
return name;
}
string getId() const
{
return id;
}
private:
string id;
string name;
int age;
string address;
};
class StudentSystem
{
public:
void menu();
void keyDown();
void insertStudent();
void eraseStudent();
void showStudent();
void searchStudent();
void Sort();
void readFile(string url);
void writeFile(string url);
private:
vector<Student> stu;
};
void StudentSystem::menu()
{
cout << "----------------------" << endl;
cout << "1.录入系统" << endl;
cout << "2.浏览信息" << endl;
cout << "3.查找信息" << endl;
cout << "4.删除信息" << endl;
cout << "5.排序信息" << endl;
cout << "----------------------" << endl;
cout << "请输入你的选择" << endl;
}
void StudentSystem::keyDown()
{
int userKey = 0;
cin >> userKey;
switch (userKey)
{
case 1:
insertStudent();
writeFile("student.txt");
break;
case 2:
showStudent();
break;
case 3:
searchStudent();
break;
case 4:
eraseStudent();
writeFile("student.txt");
break;
case 5:
Sort();
writeFile("student.txt");
break;
case 6:
cout << "退出成功" << endl;
exit(0);
break;
}
}
class SortByAge
{
public:
bool operator()(const Student& one, const Student& two)
{
return one.getAge() > two.getAge();
}
};
void StudentSystem::insertStudent()
{
cout << "请输入学生的学号 姓名 年龄 家庭地址" << endl;
Student temp;
cin >> temp;
stu.push_back(temp);
}
void StudentSystem::eraseStudent()
{
string name;
cout << "请输入要删除的姓名:" << endl;
cin >> name;
auto result = find_if(stu.begin(), stu.end(), [=](const Student& object) {return object.getName() == name; });
if (result != stu.end())
{
stu.erase(result);
}
else
{
cout << "没有找到要删除的名字" << endl;
}
}
void StudentSystem::showStudent()
{
cout << "学号" << "\t" << "姓名" <<"\t" << "年龄" << "\t" << "家庭地址" << endl;
for (auto v : stu)
{
cout << v << endl;
}
}
void StudentSystem::searchStudent()
{
cout << "------------" << endl;
cout << "1.按姓名查找" << endl;
cout << "2.按年龄查找" << endl;
cout << "3.按学号查找" << endl;
cout << "------------" << endl;
int userKey = 0;
string name;
int age;
string num;
cin >> userKey;
vector<Student>::iterator result;
switch (userKey)
{
case 1:
cout << "请输入要查找的姓名" << endl;
cin >> name;
result = find_if(stu.begin(), stu.end(), [=](const Student& object) {return object.getName() == name; });
if (result != stu.end())
{
cout << "查找成功" << endl;
cout << *result << endl;
}
else
{
cout << "没有该名字的学生" << endl;
}
break;
case 2:
cout << "请输入要查找的年龄:" << endl;
cin >> age;
result = find_if(stu.begin(), stu.end(), [=](const Student& object) {return object.getAge() == age; });
if (result != stu.end())
{
cout << "查找成功" << endl;
cout << *result << endl;
}
else
{
cout << "没有该年龄的学生" << endl;
}
break;
case 3:
cout << "请输入要查找的学号:" << endl;
cin >> num;
result = find_if(stu.begin(), stu.end(), [=](const Student& object) {return object.getId() == num; });
if (result != stu.end())
{
cout << "查找成功" << endl;
cout << *result << endl;
}
else
{
cout << "没有该学号的学生" << endl;
}
break;
}
}
void StudentSystem::Sort()
{
sort(stu.begin(), stu.end(),SortByAge());
}
void StudentSystem::readFile(string url)
{
fstream file(url, ios::in|ios::trunc);
if (!file)
{
// cout << "文件打开失败" << endl;
return;
}
Student stuOne;
while (true)
{
file >> stuOne;
if (file.eof())
{
break;
}
stu.push_back(stuOne);
}
file.close();
}
void StudentSystem::writeFile(string url)
{
fstream file(url, ios::out);
for (auto v : stu)
{
file << v;
}
file.close();
}
int main()
{
StudentSystem* pStudent = new StudentSystem();
pStudent->readFile("student.txt");
while (true)
{
pStudent->menu();
pStudent->keyDown();
}
return 0;
}
运行结果:
有课程设计需要的可以参考参考
有需要修改或者添加功能的可以联系我喔