今天又研究了一会链表,完成了小学期的第二个项目,附代码:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct StudentInfo
{
	int itsnumber;
	string itsname;
	double itsMath;
	double itsChinese;
	double itsEnglish;
	double itsSum;
	double itsAverage;
};
class StudentManage
{
private:
	vector<StudentInfo>stu;
public:
	void Create();  //创建文件
	void Import();//导入
	void show();//显示
	void setdata(); //输入n名学生的信息
	void DeleteData(); //删除
	void alterData(); //修改
	void queryData(); //查询
	void Summary(); //汇总
	void sortData();   //排序
	void exportData(); //导出
	void addDate(); //插入数据
};
void StudentManage::Create()  //创建文件
{
	string filename;
	cout << "请输入文件名" << endl;
	cin >> filename;
	ofstream outfile;
	outfile.open(filename.c_str(), ios::out); //打开输入文件,达到的创建的作用
	if (!outfile) //如果未能打开则报错
	{
		cerr << "打开" << filename << "失败!!" << endl;
		exit(0);
	}
	else
	{
		cout << "创建成功!!" << endl;
	}
	outfile.close();
}
void StudentManage::Import() //导入
{
	int i = 0, j = 0, h = 0;
	string filename;
	cout << "请输入文件名:" << endl;
	cin >> filename;
	ifstream infile(filename.c_str()); //打开输入文件
	if (!infile) //如果未能打开则报错
	{
		cerr << "打开" << filename << "失败!!" << endl;
		//exit(0);   //少了该句则可以返回菜单,继续操作
	}
	else
	{
		cout << "打开成功!!" << endl;
	}
	while (infile)
	{
		char c = infile.get();
		if (infile) cout << c;
	}
	cout << "导入成功!!" << endl;
	infile.close();
}
void StudentManage::show() //显示
{
	for(vector<StudentInfo>::iterator it=stu.begin();it<stu.end();it++)
		cout << (*it).itsname << "  " << (*it).itsnumber << "  " << (*it).itsChinese << "  " << (*it).itsMath << "  " << (*it).itsEnglish << "  " << (*it).itsSum << "  " << (*it).itsAverage << endl;
}
void StudentManage::setdata() //输入n名学生的信息
{
	StudentInfo s;
		int n;
		cout << "请输入学生总数:";
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> s.itsname >> s.itsnumber >> s.itsChinese >>s.itsMath >> s.itsEnglish;
			stu.push_back(s); //在模板尾部插入数据
		}
}
void StudentManage::DeleteData() //删除
{
	int a;
	int flag = 0; //用于判断是否找到并删除改学生
	cout << "请输入所要删除的学生的学号:";
	cin >> a;
	for (int i = 0; i < stu.size(); i++)
	{
		if (stu[i].itsnumber == a)
		{
			flag = 1;
			stu.erase(stu.begin() + i); //删除该位置的数据
			cout << "删除成功" << endl;
			break;
		}
	}
	if (flag == 0) cout << "输入学号有误,请重新输入" << endl;
}
void StudentManage::alterData()//修改
{
	int a;
	int flag = 0, f = 1; //用于判断是否找到并修改改学生
	cout << "请输入所要修改的学生的学号:";
	cin >> a;
	for (int i = 0; i < stu.size(); i++)
	{
		if (stu[i].itsnumber == a)
		{
			flag = 1;
			while (f == 1)
			{
				int x;
				long a;
				double b, c, d;
				string e;
				cout << "---修改操作菜单---" << endl;
				cout << "1.学号" << endl;
				cout << "2.姓名" << endl;
				cout << "3.语文成绩" << endl;
				cout << "4.数学成绩" << endl;
				cout << "5.英语成绩" << endl;
				cout << "6.退出返回" << endl;
				cout << "请选择:";
				cin >> x;
				switch (x)
				{
				case 1:
					cout << "请输入修改后的学号:" << endl;
					cin >> a;
					stu[i].itsnumber = a;
					break;
				case 2:
					cout << "请输入修改后的姓名:" << endl;
					cin >> e;
					stu[i].itsname = e;
					break;
				case 3:
					cout << "请输入修改后的语文成绩:" << endl;
					cin >> b;
					stu[i].itsChinese = b;
					break;
				case 4:
					cout << "请输入修改后的数学成绩:" << endl;
					cin >> c;
					stu[i].itsMath = c;
					break;
				case 5:
					cout << "请输入修改后的英语成绩:" << endl;
					cin >> d;
					stu[i].itsEnglish = d;
					break;
				case 6:
					f = 0;
					break;
				default:cout << "选择错误,请重新选择!!" << endl;
				}
			}
		}
	}
	if (flag == 0) cout << "输入学号有误,请重新输入!" << endl;
	else
	{
		cout << "修改成功!!" << endl;
		cout << "重新汇总中......." << endl;
		Summary();//对修改的成绩重新汇总
	}
}
void StudentManage::queryData() //查询
{
	int a;
	int flag = 0; //用于判断是否找到该学生
	cout << "请输入所要查询的学生的学号:";
	cin >> a;
	for (int i = 0; i < stu.size(); i++)
	{
		if (stu[i].itsnumber == a)
		{
			flag = 1;
			cout << "姓名:" << stu[i].itsname << endl;
			cout << "学号:" << stu[i].itsnumber << endl;
			cout << "语文:" << stu[i].itsChinese << endl;
			cout << "数学:" << stu[i].itsMath << endl;
			cout << "英语:" << stu[i].itsEnglish << endl;
			cout << "总分:" << stu[i].itsSum << endl;
			cout << "平均分:" << stu[i].itsAverage << endl;
			break;
		}
	}
	if (flag == 0) cout << "输入学号有误,请重新输入" << endl;
}
void StudentManage::Summary() //汇总
{
	for (int i = 0; i < stu.size(); i++)
	{
		stu[i].itsSum = stu[i].itsChinese + stu[i].itsMath + stu[i].itsEnglish;
		stu[i].itsAverage = stu[i].itsSum / 3.0;
	}
	cout << "汇总完成!!!" << endl;
}
void StudentManage::sortData()  //排序
{
	int x;
	StudentInfo temp;
	cout << "----排序菜单----" << endl;
	cout << "1.按学号升序排序" << endl;
	cout << "2.按学号降序排序" << endl;
	cout << "3.按总分升序排序" << endl;
	cout << "4.按总分降序排序" << endl;
	cout << "请选择:";
	cin >> x;
	switch (x)
	{
		//按学号升序排序并输出
	case 1:
		for (int j = 0; j < stu.size() - 1; j++)
			for (int i = 0; i < stu.size() - 1 - j; i++)
				if (stu[i].itsnumber > stu[i + 1].itsnumber)
				{
					temp = stu[i];
					stu[i] = stu[i + 1];
					stu[i + 1] = temp;
				}
		cout << "排序结果为" << endl;
		show();
		break;
		//按学号降序排序并输出
	case 2:
		for (int j = 0; j < stu.size() - 1; j++)
			for (int i = 0; i < stu.size() - 1 - j; i++)
				if (stu[i].itsnumber < stu[i + 1].itsnumber)
				{
					temp = stu[i];
					stu[i] = stu[i + 1];
					stu[i + 1] = temp;
				}
		cout << "排序结果为" << endl;
		show();
		break;
		//按总分升序排序并输出
	case 3:
		for (int j = 0; j < stu.size() - 1; j++)
			for (int i = 0; i < stu.size() - 1 - j; i++)
				if (stu[i].itsSum > stu[i + 1].itsSum)
				{
					temp = stu[i];
					stu[i] = stu[i + 1];
					stu[i + 1] = temp;
				}
		cout << "排序结果为" << endl;
		show();
		break;
		//按总分降序排序并输出
	case 4:
		for (int j = 0; j < stu.size() - 1; j++)
			for (int i = 0; i < stu.size() - 1 - j; i++)
				if (stu[i].itsSum < stu[i + 1].itsSum)
				{
					temp = stu[i];
					stu[i] = stu[i + 1];
					stu[i + 1] = temp;
				}
		cout << "排序结果为" << endl;
		show();
		break;
	}
}
void StudentManage::exportData() //导出
{
	int i = 0;
	string filename;
	cout << "请输入文件名:" << endl;
	cin >> filename;
	ofstream outfile(filename.c_str()); //打开输入文件
	if (!outfile) //如果未能打开则报错
	{
		cerr << "打开" << filename << "失败!!" << endl;
		//exit(0);   //少了该句则可以返回菜单,继续操作
	}
	else
	{
		cout << "打开成功!!" << endl;
	}
	for (i = 0; i < stu.size(); i++)
	{
		outfile << stu[i].itsname << "  " << stu[i].itsnumber << "  " << stu[i].itsChinese << "  " << stu[i].itsMath << "  " << stu[i].itsEnglish << "  " << stu[i].itsSum << "  " << stu[i].itsAverage << endl;
	}
	cout << "导出到文件成功!!" << endl;
}
void StudentManage:: addDate() //插入数据
{
	int a;
	int b, flag = 0;
	cout << "请输入想要插入到的位置(某个学号前)" << endl;
	cin >> a;
	cout << "请输入添加的学生数目:";
	cin >> b;
	vector<StudentInfo>stu1;
	StudentInfo temp;
	for (int i = 0; i < stu.size(); i++)
	{
		if (stu[i].itsnumber == a)
		{
			flag = 1; //找到该学号的标志
			for (int j = 0; j < b; j++)
			{
				cin >> temp.itsname >> temp.itsnumber >>  temp.itsChinese >> temp.itsMath >> temp.itsEnglish;
				stu1.push_back(temp);
			}
			stu.insert(stu.begin() + b - 1, stu1.begin(), stu1.end());
			break;
		}
	}
	if (flag == 0)  cout << "学号输入错误,请重新输入!!" << endl;
	else
	{
		cout << "插入成功" << endl;
		cout << "重新汇总中......." << endl;
		Summary();
	}
}
void menu()  //菜单
{
	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;
	cout << "请选择:" << endl;
}
void main()
{
	int i;
	StudentManage S;
	do
	{
		menu();
		cin >> i;
		switch (i)
		{
		case 1:cout << "-----创建-----" << endl;
			S.Create();
			break;
		case 2:
			cout << "-----导入-----" << endl;
			S.Import();
			break;
		case 3:
			cout << "-----显示-----" << endl;
			S.show();
			break;
		case 4:
			cout << "-----输入-----" << endl;
			S.setdata();
			break;
		case 5:
			cout << "-----删除-----" << endl;
			S.DeleteData();
			break;
		case 6:
			cout << "-----修改-----" << endl;
			S.alterData();
			break;
		case 7:
			cout << "-----查询-----" << endl;
			S.queryData();
			break;
		case 8:
			cout << "-----汇总-----" << endl;
			S.Summary();
			break;
		case 9:
			cout << "-----排序-----" << endl;
			S.sortData();
			break;
		case 10:
			cout << "-----导出-----" << endl;
			S.exportData();
			break;
		case 11:
			cout << "-----插入-----" << endl;
			S.addDate();
			break;
		case 12:
			cout << "-----退出-----" << endl;
			exit(0);
			break;
		default:cout << "选择错误,请重新选择!!" << endl;
		}
	} while (i != 0);
}
 
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号