c++做一个学生信息管理系统(vector)

用c++ STL中的vector容器来做一个学生信息管理系统


  • 这个是一个比较综合的项目,用到许多c++的知识点
  1. 用到面向对象的思想,类的使用(权限,构造函数等...)
  2. 用到友元函数的运用场景
  3. c++运算符重载
  4. 输入输出流的操作
  5. 文件流操作
  6. 仿函数的使用
  7. Lambad表达式使用
  8. ...
#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;
}

运行结果:

有课程设计需要的可以参考参考

有需要修改或者添加功能的可以联系我喔

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的学生信息管理系统程序的C++代码: ```c++ #include <iostream> #include <vector> using namespace std; // 学生类 class Student { public: string name; // 姓名 int age; // 年龄 string gender; // 性别 Student(string name, int age, string gender) { this->name = name; this->age = age; this->gender = gender; } }; // 学生信息管理系统类 class StudentManagementSystem { public: vector<Student> students; // 存储学生信息的vector // 添加学生信息 void addStudent(string name, int age, string gender) { Student student(name, age, gender); students.push_back(student); } // 查找学生信息 Student* findStudent(string name) { for (int i = 0; i < students.size(); i++) { if (students[i].name == name) { return &students[i]; } } return NULL; } // 删除学生信息 void deleteStudent(string name) { for (int i = 0; i < students.size(); i++) { if (students[i].name == name) { students.erase(students.begin() + i); break; } } } }; int main() { StudentManagementSystem system; system.addStudent("张三", 18, "男"); system.addStudent("李四", 19, "女"); system.addStudent("王五", 20, "男"); Student* student = system.findStudent("李四"); if (student != NULL) { cout << "姓名:" << student->name << ", 年龄:" << student->age << ", 性别:" << student->gender << endl; } system.deleteStudent("李四"); return 0; } ``` 这个程序使用了C++的面向对象特性,定义了两个类:学生类和学生信息管理系统类。学生信息管理系统类使用了vector来存储学生信息,并提供了添加、查找和删除学生信息的功能。在主函数中,我们创建了一个学生信息管理系统对象,添加了三个学生信息,查找了一个学生信息并输出了其信息,然后删除了一个学生信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值