C++ 簡單的信息管理系統

一次比較綜合性的大作業
主要使用的就是通過Vector容器來處理數據
關於vector容器的增,刪,改,查等等用法
也複習了很多關於C++的一些知識
比如 類,多態,指針之類的

#include<iostream>
#include<string>
#include<vector>  //容器
#include<fstream>
using namespace std;
/*summary
Student類:
	這個類代表的是每一個學生的信息,包括基本信息(姓名,年齡,性別,報名情況等)
	用一個vector容器來放學生得報名情況
	主要方法功能有學生課程得增刪改查
*/
class Student {
public:
	int age;
	string name;
	string sex;
	vector<string> lectures;
	string lectures_list;
public:
	Student() {

	}
	~Student() {

	}
	void input() {
		cout << "請輸入姓名: ";
		string name;
		cin >> name;
		this->name = name;
		cout << endl;
		cout << "請輸入性別: ";
		string sex;
		cin >> sex;
		this->sex = sex;
		cout << endl;
		cout << "請輸入年齡: ";
		int age;
		cin >> age;
		this->age = age;
	}
	void show() {
		cout << "名字是: " << this->name << endl;
		cout << "性別是: " << this->sex << endl;
		cout << "年齡是: " << this->age << endl;
	}
	void add_lecture(string lecture_name) {
		this->lectures.push_back(lecture_name);

	}
	void show_lecture() {
		for (int i = 0; i < this->lectures.size(); i++) {
			cout << this->lectures[i] << endl;
		}
	}
	void addLectureName() {
		for (int i = 0; i < this->lectures.size(); i++) {
			this->lectures_list += this->lectures[i] + " ";
		}
	}
	void delete_lecture(string value) {
		vector<string>::iterator it;
		for (it = this->lectures.begin(); it != this->lectures.end();) {
			if (*it == value) {
				it = this->lectures.erase(it);
			}
			else {
				++it;
			}
		}
	}
};
vector<Student> students; //學生總名單
//Summary
//Lecture類:
//	Lecture類是課程的總類,可以派生出藝術類和體育類
class Lecture {
public:
	string lecture_name;
	//虛函數
	virtual string get_lecturename(int judge) {
		return NULL;
	};
};
//summary
//Art類:
//	繼承自Lecture類
//	通過一個get_lecturename方法來獲取學生所報名的課程
class Art : public Lecture {  //文藝類
public:
	string get_lecturename(int judge) {
		if (judge == 0) {
			return "音樂";
		}
		else if (judge == 1) {
			return "設計";
		}
		else {
			return "編程";
		}
	}

};
//summary
//Sport類:
//	繼承自Lecture類
//	通過一個get_lecturename方法來獲取學生所報名的課程
class Sport : public Lecture {  //體育類
public:
	string get_lecturename(int judge) {
		if (judge == 0) {
			return "拳擊";
		}
		else {
			return "跆拳道";
		}
	}
};
//summary
//add_student()方法:
//	主要功能是添加學生
//	所使用的方法是先創建一個學生,然後通過輸入信息完善學生的報名情況。
//	最後再加入Student的vector容器中
void add_student() {  //添加學生
	Student stu = Student();
	Lecture* lecture_Type;
	Art art = Art();
	Sport sport = Sport();
	stu.input();
	while (1) {
		cout << "請選擇學生想要學習的類型" << endl;
		cout << "0、體育類" << endl << "1、文藝類" << endl;
		int chose;
		cin >> chose;
		if (chose == 0) {
			lecture_Type = &sport;
			cout << "0、拳擊" << endl << "1、跆拳道" << endl;
		}
		else {
			lecture_Type = &art;
			cout << "0、音樂" << endl << "1、設計" << endl << "2、編程" << endl;
		}
		int chose_lec;
		cin >> chose_lec;
		string lecture_name = lecture_Type->get_lecturename(chose_lec);
		stu.add_lecture(lecture_name);
		cout << "是否添加課程? y/n";
		char chose_continue;
		cin >> chose_continue;
		if (chose_continue == 'n') {
			students.push_back(stu);
			break;
		}
	}

}
//summary
//delete_student()方法:
//	主要功能是刪除某個學生
//	通過先輸入需要刪除學生的名字,然後判斷Student容器中是否存在該學生
//	存在就刪除,不存在就提示輸入有誤
void delete_student() {  //刪除某個學生
	cout << "請輸入需要刪除的學生姓名:" << endl;
	string name;
	cin >> name;
	vector<Student>::iterator it;
	bool is_find = false;
	for (it = students.begin(); it != students.end();) {
		if (it->name == name) {
			it = students.erase(it);
			is_find = true;
		}
		else {
			++it;
		}
	}
	if (is_find == true) {
		cout << "已經成功刪除 " << name << " 同學的信息。" << endl;
	}
	else {
		cout << "沒有找到,請重新確認輸入是否有誤" << endl;
	}
}
//summary
//recompose_student()方法:
//	主要功能修改學生信息
//	也是先通過輸入姓名查找學生,再創建一個Student指針指向找到的學生
//	再通過指針來修改學生的信息
void recompose_student() { //修改學生信息
	cout << "請輸入想要修改的學生姓名" << endl;
	bool is_find = false;
	Student* FindStu = NULL;
	string name;
	cin >> name;
	for (int i = 0; i < students.size(); i++) {
		if (students[i].name == name) {
			is_find = true;
			FindStu = &students[i];
		}
	}
	if (FindStu == NULL) {
		cout << "沒有找到該學生,請確認輸入是否有誤。" << endl;
	}
	else {
		cout << "查找的學生是" << FindStu->name << "同學." << endl;
		cout << "該學生的補課內容是:" << endl;
		FindStu->show_lecture();
		cout << "請輸入想要刪除的課程名字。" << endl;
		string name_lecture;
		cin >> name_lecture;
		vector<string>::iterator it;
		it = find(FindStu->lectures.begin(), FindStu->lectures.end(), name_lecture);
		bool is_Consist = false;
		if (it != FindStu->lectures.end()) {
			is_Consist = true;
		}
		else {
			is_Consist = false;
		}
		if (is_Consist == true) {
			FindStu->delete_lecture(name_lecture);
			cout << name_lecture << "課程已經刪除成功!" << endl;
		}
		else {
			cout << "沒有找到該課程,請重新操作!" << endl;
		}
	}
}
//summary
//see_student()方法:
//	主要實現功能是查詢學生的報名信息
//	通過輸入名字來查找
void see_student() {   //查詢特定學生
	cout << "您想要查詢哪個學生? 請輸入姓名:" << endl;;
	string name;
	cin >> name;
	bool isFind = false;
	for (int i = 0; i < students.size(); i++) {
		if (students[i].name == name) {
			cout << name << "的基本信息如下:" << endl;
			students[i].show();
			students[i].show_lecture();
			isFind = true;
			break;
		}

	}
	if (isFind == true) {}
	else {
		cout << name << "沒有找到!請確認輸入是否有誤!" << endl;
	}

}
//summary
//show_nameList()方法:
//	主要功能是展示已經報名的學生基本信息
//	通過遍歷Student容器得到
void show_nameList() {   //展示已報名的學生列表
	int count = 0;
	for (int i = 0; i < students.size(); i++) {
		cout << ++count << "-" << students[i].name << endl;
	}
}
//summary
//save()方法():
//	主要功能是將當前的信息保存到本地文件"data.txt"中去
//	通過fstream中的 open()方法和write來向文件中寫入信息
void save() {  //保存信息到本地文件
	int count = 0;
	fstream write;
	ofstream outfile("data.txt");
	write.open("data.txt");

	for (int i = 0; i < students.size(); i++) {
		students[i].addLectureName();
		write << ++count << " " << students[i].name << " " << students[i].sex << " " << students[i].age << " | " << students[i].lectures_list << endl;
		students[i].lectures_list = "";
	}
	write.close();
}

int main() {
	//主菜單
	
	cout << "歡迎使用學生選課 信息管理系統" << endl;
	while (1) {
		cout << "=====================================" << endl;
		cout << "1、添加學生" << endl;
		cout << "2、刪除學生" << endl;
		cout << "3、修改學生" << endl;
		cout << "4、查詢學生" << endl;
		cout << "5、列出現有的學生名單" << endl;
		cout << "6、將信息輸出到一個txt文件中" << endl;
		cout << "9、退出程序" << endl<<"======================================"<<endl;
		int input;
		cin >> input;
		if (input == 9) {
			break;
		}
		else if (input == 1) {
			add_student();
		}
		else if (input == 2) {
			delete_student();
		}
		else if (input == 3) {
			recompose_student();
		}
		else if (input == 4) {
			see_student();
		}
		else if (input == 5) {
			show_nameList();
		}
		else if (input == 6) {
			save();
		}
		else {
			break;
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章