学生管理系统_vector实现

@学生管理系统

石家庄铁道大学小学期作业

记录

直接上代码:

//main.cpp:
#include "Stu_manage_stl.h"
using namespace std;
int main()
{
		int choice;
		Stu_manage_stl st;
		

		while (true)
		{
			st.show_meau();
			cout << "请输入你的选择" << endl;
			cin >> choice;
			switch (choice)
			{
			case 1: st.creat_txt();  break;
				//(1)创建功能:创建存储学生信息的文件,文件路径和文件名可以输入。
			case 2: st.export_(); break;
				//(2)文件导入:输入文件路径和文件名读取文件内全部学生信息,需要判断文件是否存在。
			case 3: st.show_(); break;
				//(3)显示功能:全部学生记录的显示
			case 4: st.add_num(); break;
				//(4)输入功能:一.次输入n位同学的基本信息,n可以输入。
			case 5: st.Del_stu(); break;
				//(5)删除功能:按学号删除一位学生记录。
			case 6: st.change_stu(); break;
				//(6)修改功能:按学号修改-位学生记录。
			case 7:st.find(); break;
				//(7)查找功能:按学号查找学生记录。
			case 8:st.sum_aver(); break;
				//(8)汇总功能:计算学生的平均分和总分。
			case 9:st.sort_(); break;
				//(9)排序功能:按学号或总分进行升序或降序排序。
			case 10: st.import_();		break;						//需要先进行 操作1.创建文件
				//(10)导出功能:将学生信息输出到文件中。
			case 11:  st.insert_(); break;
				//(11)插入功能:可根据需要添加n位学生信息到指定位置,n可以输入。
			case 12:st.exit_System(); break;
				// 退出
				break;

			default:
				break;
			}
		}
		
		return 0;

}
//class Stu_manage_stl.h

#pragma once
#include<vector>
#include<fstream>
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;
typedef struct StuInfo
{
	string stu_name;
	int stu_num;
	float Chinese, Math, Eng, aver, sum_;
	StuInfo(int num, string name, float m, float e, float c, float a = 0, float s = 0)
	{
		stu_num = num, stu_name = name;
		Math = m;	Eng = e;	Chinese = c;
		aver = a;	sum_ = s;
	}
	StuInfo() {};
}StuInfo;

class Stu_manage_stl
{
	
	vector<Stu_manage_stl> stu_vector;
	StuInfo stu_info;
	int now_num;// mark now the number of students
public:
	Stu_manage_stl(StuInfo );
	Stu_manage_stl();
	~Stu_manage_stl(); 
	void creat_txt();
	void show_meau();
	void  add_num();
	void show_info_all();
	void show_info_id(int x);
	void show_();
	int get_id() {
		return this->stu_info.stu_num;
	}
	void import_();
	void export_();
	int get_file_lines(string path);
	void Del_stu();
	void change_stu();
	void find();
	void sum_aver();
	void sort_();
	friend bool cmp1(Stu_manage_stl x1, Stu_manage_stl x2);
	friend bool cmp2(Stu_manage_stl x1, Stu_manage_stl x2);
	friend bool cmp3(Stu_manage_stl x1, Stu_manage_stl x2);
	friend bool cmp4(Stu_manage_stl x1, Stu_manage_stl x2);
	void insert_();
	void exit_System()
	{
		cout << "欢迎下次使用" << endl;
		system("pause");
		exit(0);
	}
};
//class Stu_manage_stl.cpp
#include "Stu_manage_stl.h"
Stu_manage_stl::Stu_manage_stl() {};
bool cmp1(Stu_manage_stl x1, Stu_manage_stl x2);
bool cmp2(Stu_manage_stl x1, Stu_manage_stl x2);
 bool cmp3(Stu_manage_stl x1, Stu_manage_stl x2);
 bool cmp4(Stu_manage_stl x1, Stu_manage_stl x2);
Stu_manage_stl::Stu_manage_stl(StuInfo x )
{
	this->now_num = 0;
	this->stu_info = x;
}
Stu_manage_stl::~Stu_manage_stl() {}
void Stu_manage_stl::creat_txt()
{
	cout << "in put the file's path:";
	string file_path;
	cin >> file_path;
	ifstream ifs(file_path, ios::in);
	if (ifs.is_open()) { cout << "success to creat!" << endl; ifs.close(); }
	else { cout << "fail to creat" << endl; ifs.close(); }
}
void Stu_manage_stl::show_meau()
{
	cout << "***学生管理系统***" << endl;
	cout << "***1.创建***" << endl;
	cout << "***2.导入***" << endl;
	cout << "***3.显示***" << endl;
	cout << "***4.输入***" << endl;
	cout << "***5.删除***" << endl;
	cout << "***6.修改***" << endl;
	cout << "***7.查询***" << endl;
	cout << "***8.汇总***" << endl;
	cout << "***9.排序***" << endl;
	cout << "***10.导出***" << endl;
	cout << "***11.插入***" << endl;
	cout << "***12.退出***" << endl;
	system("pause");
}
void  Stu_manage_stl::add_num()
{
	int added_num = 0;
	cout << "请输入想要添加学生的数量" << endl;
	cin >> added_num;
	if (added_num > 0)
	{
		int id;
		float math, chinese, eng;
		string name;
		for (int i = 0; i < added_num; i++)
		{
			StuInfo tmp;
			cout << "学生姓名";	cin >> tmp.stu_name;
			cout << "学生学号";	cin >> tmp.stu_num;
			cout << "语文成绩";	cin >> tmp.Chinese;
			cout << "英语成绩";	cin >> tmp.Eng;
			cout << "数学成绩";	cin >> tmp.Math;
			this->stu_vector.push_back(tmp);
			cout << "--------------------------------------------------" << endl;
		}
		cout << "学生添加成功!" << endl;
	}
	else
	{
		cout << "输入数据有误" << endl;
	}
	this->now_num += added_num;
	system("pause");
	system("cls");
}
void Stu_manage_stl::show_info_all()
{
	cout << "当前共有 " << now_num << " 学生" << endl;
	for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
		this->show_info_id(i->get_id());
	system("pause");
	system("cls");
}
void Stu_manage_stl::show_info_id(int idx)
{
	vector<Stu_manage_stl>	::iterator itor= stu_vector.begin();
	for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
	{
		if (i->get_id() == idx)
		{
			itor = i;		break;
		}
	}

	cout << "name : " << itor->stu_info.stu_name << "\t id : " << itor->stu_info.stu_num << "\tmath : " << itor->stu_info.Math
		<< "\tchinese : " << itor->stu_info.Chinese << "\teng : " << itor->stu_info.Eng << endl;
}
void Stu_manage_stl::show_()
{
	cout << " 1.show all stu 2.input the id you want to show:";
	int choice;
	cin >> choice;
	if (choice == 1)
	{
		this->show_info_all();
	}
	else
	{
		cout << "in put the id:";
		int idx;	cin >> idx;
		this->show_info_id(idx);
	}

}
void Stu_manage_stl::import_()
{
	cout << " input the file''s path or directly the name(when cin the name The default is in the current file)" << endl;
	cout << "path or name: ";
	string path_or_name;
	cin >> path_or_name;
	ofstream ofs(path_or_name, ios::out);
	if (ofs.is_open())
	{

		for (auto itor = this->stu_vector.begin(); itor != this->stu_vector.end(); itor++)
			ofs << "name: " << itor->stu_info.stu_name << "\t id: " << itor->stu_info.stu_num << "\tmath: " << itor->stu_info.Math
			<< "\tchinese: " << itor->stu_info.Chinese << "\teng: " << itor->stu_info.Eng << endl;


		cout << "success to import to " << path_or_name << endl;
		ofs.close();
	}
	else { cout << " fail to open!" << endl; ofs.close(); }
	system("paues");
	system("cls");
	
}
void Stu_manage_stl::export_()
{
	cout << "cin the file's name or path you want to  export:";
	string name_or_path;
	
	cin >> name_or_path;

	int lines = this->get_file_lines(name_or_path);
	ifstream ifs(name_or_path, ios::in);
	cout << "the file has" << lines << " students" << endl;
	for (int i = 0; i < lines; i++)
	{
		
		StuInfo tmpp;
		string absorb;// used to absorb the such as "name"  "id"
		ifs >>absorb >>tmpp.stu_name >> absorb>>tmpp.stu_num >>absorb>> tmpp.Math >>absorb>> tmpp.Chinese >>absorb >>tmpp.Eng;
		Stu_manage_stl tmp(tmpp);
		this->stu_vector.push_back(tmp);
		
	}
	this->now_num += lines;
	system("pause");
	system("cls");
}
int Stu_manage_stl::get_file_lines(string path)
{
	int lines=0;
	ifstream ifs(path, ios::in);
	if (ifs.is_open())
	{
		string tmp;
		while (getline(ifs, tmp, '\n'))
		{
			lines++;
		}
		ifs.close();
	}
	else { ifs.close(); return -1; }

	return lines;
}
void Stu_manage_stl::Del_stu()
{
	cout << "cin the stu's id you want to delete: ";
	int id = 0;
	cin >> id;  
	bool flag = false;
	for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
	{
		if (i->get_id() == id) {
			flag = true;
			break;
		}
	}
	if (flag == true)
		for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
		{
			if (i->get_id() == id) {
				this->stu_vector.erase(i);
				break;
			}
		}
	else cout << "flil to find this student" << endl;
	system("pause");
	system("cls");

}
void Stu_manage_stl:: change_stu()
{
	cout << "cin the  stu's id you want to change:";
	int idx;
	cin >> idx;
	bool flag = false;
	for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
	{
		if (i->get_id() == idx) {
			flag = true;
			break;
		}
	}
	if (flag == true)
		for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
		{
			if (i->get_id() == idx) {
				
				cout << "学生姓名";	cin >> i->stu_info.stu_name;
				cout << "学生学号";	cin >> i->stu_info.stu_num;
				cout << "语文成绩";	cin >> i->stu_info.Chinese;
				cout << "英语成绩";	cin >> i->stu_info.Eng;
				cout << "数学成绩";	cin >> i->stu_info.Math;
				break;
			}
		}
	else cout << "flil to find this student" << endl;
	system("pause");
	system("cls");
}
void Stu_manage_stl::find()
{
	cout << "cin the  stu's id you want to find:";
	int idx;
	cin >> idx;
	bool flag = false;
	for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
	{
		if (i->get_id() == idx) {
			flag = true;
			break;
		}
	}
	if (flag == true)
		for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
		{
			if (i->get_id() == idx) {
				this->show_info_id(idx);
				break;
			}
		}
	else cout << "flil to find this student" << endl;
	system("pause");
	system("cls");
}
void Stu_manage_stl::sum_aver()
{
	for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
	{
		
		i->stu_info.sum_ = i->stu_info.Chinese + i->stu_info.Eng + i->stu_info.Math;
		i->stu_info.aver = (i->stu_info.sum_) / 3;
		
		cout <<"ID: " <<i->stu_info.stu_num<<"\tsum:" << i->stu_info.sum_ << "\taver: " << i->stu_info.aver << endl;
	}
	system("pause");
	system("cls");

}
void Stu_manage_stl::sort_()
{
	cout << "1 . sort by id  \n2.sort by sum marks:";
	cout << "your choice:";
	int choice;
	cin >> choice;
	if (choice == 1)
	{
		cout << "1.Ascending\t2.Descending :";
		int A_D;
		cin >> A_D;
		if (A_D == 1)
		{
			auto itor_b = this->stu_vector.begin();
			auto itor_e = this->stu_vector.end();
			sort(itor_b, itor_e, cmp1);
		}
		else
		{
			auto itor_b = this->stu_vector.begin();
			auto itor_e = this->stu_vector.end();
			sort(itor_b, itor_e, cmp2);
		}
	}
	else if (choice == 2)
	{
		cout << "1.Ascending\t2.Descending :";
		int A_D;
		cin >> A_D;
		if (A_D == 1)
		{
			auto itor_b = this->stu_vector.begin();
			auto itor_e = this->stu_vector.end();
			sort(itor_b, itor_e, cmp3);
		}
		else
		{
			auto itor_b = this->stu_vector.begin();
			auto itor_e = this->stu_vector.end();
			sort(itor_b, itor_e, cmp4);
		}
	}
	system("pause");
	system("cls");
}
bool cmp1(Stu_manage_stl x1, Stu_manage_stl x2)
{
	return x1.stu_info.stu_num < x2.stu_info.stu_num;
}
bool cmp2(Stu_manage_stl x1, Stu_manage_stl x2)
{
	return x1.stu_info.stu_num > x2.stu_info.stu_num;
}
bool cmp3(Stu_manage_stl x1, Stu_manage_stl x2)
{
	return x1.stu_info.sum_ < x2.stu_info.sum_;
}
bool cmp4(Stu_manage_stl x1, Stu_manage_stl x2)
{
	return x1.stu_info.sum_ > x2.stu_info.sum_;
}
void Stu_manage_stl::insert_()
{
	int idx;
	cout << " insert by (id) cin>>id:";
	cin >> idx;
	for (auto i = this->stu_vector.begin(); i != this->stu_vector.end(); i++)
	{
		if (i->stu_info.stu_num == idx)
		{
			StuInfo tmp;
			cout << "学生姓名";	cin >> tmp.stu_name;
			cout << "学生学号";	cin >> tmp.stu_num;
			cout << "语文成绩";	cin >> tmp.Chinese;
			cout << "英语成绩";	cin >> tmp.Eng;
			cout << "数学成绩";	cin >> tmp.Math;
			Stu_manage_stl tmpp(tmp);
			this->stu_vector.insert(i, tmpp);
			break;
		}
	}
}

怎么说呢对一个大一的来说,写个三四百行的程序,还是听难得,我是FW hh


版权声明:本文为qq_46704655原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。