学生数据管理系统(开源代码)

这是一个使用C++实现的学生成绩管理系统,包含添加、查找、删除、排序和显示学生信息的功能。系统通过类`Student`和`DataBase`进行数据操作,支持学号和姓名的搜索,并能按学号升序或总成绩降序排序。
摘要由CSDN通过智能技术生成

码字不易,就不考虑给个赞吗?么么哒~

版本1:

        头文件Student.h:

#ifndef stu_h
#define stu_h
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<cctype>
#include<iomanip>
using namespace std;
const int MAX_SIZE = 60;
class Student
{
private:
	string name;
	string id;
	double math;
	double English;
	double computer;
public:
	Student() :name(""), id(""), math(100.0), English(100.0), computer(100.0) {}
	Student(string n) :name(n), id(""), math(100.0), English(100.0), computer(100.0) {}
	Student(string n, string i):name(n), id(i), math(100.0), English(100.0), computer(100.0) {}
	void SetName(string n)
	{
		name = n;
	}
	void SetId(string i)
	{
		id = i;
	}
	void SetMath(double m)
	{
		math = m;
	}
	void SetEnglish(double e)
	{
		English = e;
	}
	void SetComputer(double c)
	{
		computer = c;
	}
	string GetName()
	{
		return name;
	}
	string GetId()
	{
		return id;
	}
	double GetMath()
	{
		return math;
	}
	double GetEnglish()
	{
		return English;
	}
	double GetComputer()
	{
		return computer;
	}
	void SetAll(double m, double e, double c)
	{
		math = m;
		English = e;
		computer = c;
	}
	double GetSum()
	{
		return math + English + computer;
	}
	bool Match(string s, short flag)
	{
		if (flag == 1)
			return name == s;
		else
			return id == s;
	}
	void Print()
	{
		cout << name << '\t' << id << endl;
		cout << "数学:" << math << "\t英语:" << English << "\t计算机:" << computer << endl;
	}
};
bool cmp1(Student st1, Student st2)
{
	return st1.GetId() < st2.GetId();
}
bool cmp2(Student st1, Student st2)
{
	return st1.GetSum() > st2.GetSum();
}
class DataBase
{
private:
	Student stu[MAX_SIZE];
	short size;
public:
	DataBase() :size(0) {}
	bool Push(string n, string i, double m, double e, double c)
	{
		if (size == MAX_SIZE)
			return false;
		Student st(n, i);
		st.SetAll(m,e,c);
		stu[size++] = st;
		return true;
	}
	bool Push()
	{
		if (size == MAX_SIZE)
		{
			cout << "系统不能容纳更多学生。" << endl;
			system("pause");
			return false;
		}
		string n, i;
		cout << "请输入要添加的学生姓名:";
		cin >> n;
		int idx = 0;
		do
		{
			cout << "请输入该学生的学号:";
			cin >> i;
			for (idx = 0; idx < size; idx++)
			{
				if (stu[idx].Match(i, 2))
				{
					cout << "该学号已存在,不能重复输入!" << endl;
					break;
				}
			}
		} while (idx<size);
		Student stu_tmp(n, i);
		double m = 0.0, e = 0.0, c = 0.0;
		cout << "请输入该学生的数学成绩:";
		cin >> m;
		cout << "请输入该学生的英语成绩:";
		cin >> e;
		cout << "请输入该学生的计算机成绩:";
		cin >> c;
		stu_tmp.SetAll(m, e, c);
		stu[size++] = stu_tmp;
		cout << "添加成功!" << endl;
		system("pause");
		return true;
	}
	short AimedSearch(short start_id, string str, short flag)
	{
		for (short i = start_id; i < size; i++)
		{
			if (stu[i].Match(str, flag))
			{
				stu[i].Print();
				return i;
			}
		}
		return -1;
	}
	short Search()
	{
		short choice = 0;
		do
		{
			cout << "请问你要按什么条件搜索?		1.姓名		2.学号		";
			cin >> choice;
		} while (choice != 1 && choice != 2);
		string match;
		cout << "请输入你要找的" << (choice == 1 ? "姓名" : "学号") << ":";
		cin >> match;
		short result = 0;
		char nod = 0;
		while (true)
		{
			result = AimedSearch(result, match, choice);
			if (result == -1)
			{
				cout << "未找到匹配信息。" << endl;
				system("pause");
				return result;
			}
			cout << "这是你要找的人吗?(y/n)";
			cin >> nod;
			nod = tolower(nod);
			if (nod == 'y')
				return result;
			else
				result++;
		}
	}
	bool Delete()
	{
		short result = 0;
		char choice = 0;
		result = Search();
		if (result == -1)
			return false;
		cout << "是否要删除这条信息?(y/n)";
		cin >> choice;
		choice = tolower(choice);
		short idx = 0;
		if (choice == 'y')
		{
			for (idx = result; idx < size - 1; idx++)
			{
				stu[idx] = stu[idx + 1];
			}
			size--;
			cout << "删除成功!" << endl;
			system("pause");
			return true;
		}
		cout << "信息未删除!" << endl;
		system("pause");
		return false;
	}
	bool Alter()
	{
		short result = 0;
		string rename;
		double rescore = 0.0;
		result = Search();
		if (result == -1)
			return false;
		char choice = 0;
		cout << "是否要修改这条信息?(y/n)";
		cin >> choice;
		choice = tolower(choice);
		if (choice == 'y')
		{
			cout << "修改什么?	1.名字	2.数学成绩	3.英语成绩	4.计算机成绩" << endl;
			char subchoice = 0;
			cin >> subchoice;
			switch (subchoice)
			{
			case '1':
			{
				cout << "输入一个新名字:";
				cin >> rename;
				stu[result].SetName(rename);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case '2':
			{
				cout << "输入数学成绩:";
				cin >> rescore;
				stu[result].SetMath(rescore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case '3':
			{
				cout << "输入英语成绩:";
				cin >> rescore;
				stu[result].SetEnglish(rescore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case '4':
			{
				cout << "输入计算机成绩:";
				cin >> rescore;
				stu[result].SetComputer(rescore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			default:
			{
				cout << "修改失败!" << endl;
				return false;
			}
			}
			return true;
		}
		cout << "信息未修改。" << endl;
		system("pause");
		return false;
	}
	void Display()
	{
		cout << endl << setw(12) << setiosflags(ios::left) << "姓名" << setw(12) << "学号" << setw(8) << "数学" << setw(8) << "英语" << setw(8) << "计算机" << endl << endl;
		for (int i = 0; i < size; i++)
		{
			cout << setw(12) << stu[i].GetName()<<setw(12)<<stu[i].GetId();
			printf("%-8.1lf%-8.1lf%-8.1lf\n", stu[i].GetMath(), stu[i].GetEnglish(), stu[i].GetComputer());
		}
		cout << resetiosflags(ios::right);
		system("pause");
	}
	void Sort()
	{
		char choice = 0;
		do
		{
			cout << "请输入排序方式	1.学号升序	2.总成绩降序" << endl;
			cin >> choice;
		} while (choice != '1' && choice != '2');
		if (choice == '1')
		{
			sort(&stu[0], &stu[0] + size, cmp1);
		}
		else
		{
			sort(&stu[0], &stu[0] + size, cmp2);
		}
		cout << "重新排序完成。" << endl;
		system("pause");
	}
	char ShowMenu()
	{
		char choice = 0;
		do
		{
			system("cls");
			cout << "----------欢迎使用成绩管理系统----------" << endl << endl;
			cout << "		1.添加学生" << endl;
			cout << "		2.查找学生" << endl;
			cout << "		3.删除学生" << endl;
			cout << "		4.重新排序" << endl;
			cout << "		5.显示全部" << endl;
			cout << "		6.修改信息" << endl;
			cout << "		7.退出" << endl;
			cin >> choice;
		} while (choice < '1' || choice>'7');
		return choice;
	}
	bool Call(char choice)
	{
		switch (choice)
		{
		case '1':
		{
			Push();
			return true;
		}
		case '2':
		{
			Search();
			return true;
		}
		case '3':
		{
			Delete();
			return true;
		}
		case '4':
		{
			Sort();
			return true;
		}
		case '5':
		{
			Display();
			return true;
		}
		case '6':
		{
			Alter();
			return true;
		}
		case '7':
		{
			return false;
		}
		default:
			break;
		}
	}
};
#endif

        源文件main.cpp:

#include"Student.h"
int main()
{
	DataBase student1;
	bool quit = false;
	student1.Push("徐","00001",100.0,100.0,100.0);
	student1.Push("吴", "00002", 98.0, 85.0, 99.0);
	while (!quit)
	{
		if (!student1.Call(student1.ShowMenu()))
			quit = true;
	}
	return 0;
}

版本2:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<cctype>
#include<iomanip>
using namespace std;
const int MAX_SIZE = 60;
class Student
{
private:
	string name;
	string id;
	double math;
	double English;
	double computer;
public:
	Student() :name(""), id(""), math(100.0), English(100.0), computer(100.0) {}
	Student(string n) :name(n), id(""), math(100.0), English(100.0), computer(100.0) {}
	Student(string n, string i) :name(n), id(i), math(100.0), English(100.0), computer(100.0) {}
	void SetName(string n)
	{
		name = n;
	}
	void SetId(string i)
	{
		id = i;
	}
	void SetMath(double m)
	{
		math = m;
	}
	void SetEnglish(double e)
	{
		English = e;
	}
	void SetComputer(double c)
	{
		computer = c;
	}
	string GetName()
	{
		return name;
	}
	string GetId()
	{
		return id;
	}
	double GetMath()
	{
		return math;
	}
	double GetEnglish()
	{
		return English;
	}
	double GetComputer()
	{
		return computer;
	}
	void SetAll(double m, double e, double c)
	{
		math = m;
		English = e;
		computer = c;
	}
	double GetSum()
	{
		return math + English + computer;
	}
	bool Match(string s, short flag)
	{
		if (flag == 1)
			return name == s;
		else
			return id == s;
	}
	void Print()
	{
		cout << name << '\t' << id << endl;
		cout << "数学:" << math << "\t英语:" << English << "\t计算机:" << computer << endl;
	}
};
bool cmp1(Student st1, Student st2)
{
	return st1.GetId() < st2.GetId();
}
bool cmp2(Student st1, Student st2)
{
	return st1.GetSum() > st2.GetSum();
}
class DataBase
{
private:
	Student stu[MAX_SIZE];
	short size;
public:
	DataBase() :size(0) {}
	bool Push(string n, string i, double m, double e, double c)
	{
		if (size == MAX_SIZE)
			return false;
		Student st(n, i);
		st.SetAll(m, e, c);
		stu[size++] = st;
		return true;
	}
	bool Push()
	{
		if (size == MAX_SIZE)
		{
			cout << "系统不能容纳更多学生。" << endl;
			system("pause");
			return false;
		}
		string n, i;
		cout << "请输入要添加的学生姓名:";
		cin >> n;
		int idx = 0;
		do
		{
			cout << "请输入该学生的学号:";
			cin >> i;
			for (idx = 0; idx < size; idx++)
			{
				if (stu[idx].Match(i, 2))
				{
					cout << "该学号已存在,不能重复输入!" << endl;
					break;
				}
			}
		} while (idx < size);
		Student stu_tmp(n, i);
		double m = 0.0, e = 0.0, c = 0.0;
		cout << "请输入该学生的数学成绩:";
		cin >> m;
		cout << "请输入该学生的英语成绩:";
		cin >> e;
		cout << "请输入该学生的计算机成绩:";
		cin >> c;
		stu_tmp.SetAll(m, e, c);
		stu[size++] = stu_tmp;
		cout << "添加成功!" << endl;
		system("pause");
		return true;
	}
	short AimedSearch(short start_id, string str, short flag)
	{
		for (short i = start_id; i < size; i++)
		{
			if (stu[i].Match(str, flag))
			{
				stu[i].Print();
				return i;
			}
		}
		return -1;
	}
	short Search()
	{
		short choice = 0;
		do
		{
			cout << "请问你要按什么条件搜索?		1.姓名		2.学号		";
			cin >> choice;
		} while (choice != 1 && choice != 2);
		string match;
		cout << "请输入你要找的" << (choice == 1 ? "姓名" : "学号") << ":";
		cin >> match;
		short result = 0;
		char nod = 0;
		while (true)
		{
			result = AimedSearch(result, match, choice);
			if (result == -1)
			{
				cout << "未找到匹配信息。" << endl;
				system("pause");
				return result;
			}
			cout << "这是你要找的人吗?(y/n)";
			cin >> nod;
			nod = tolower(nod);
			if (nod == 'y')
				return result;
			else
				result++;
		}
	}
	bool Delete()
	{
		short result = 0;
		char choice = 0;
		result = Search();
		if (result == -1)
			return false;
		cout << "是否要删除这条信息?(y/n)";
		cin >> choice;
		choice = tolower(choice);
		short idx = 0;
		if (choice == 'y')
		{
			for (idx = result; idx < size - 1; idx++)
			{
				stu[idx] = stu[idx + 1];
			}
			size--;
			cout << "删除成功!" << endl;
			system("pause");
			return true;
		}
		cout << "信息未删除!" << endl;
		system("pause");
		return false;
	}
	bool Alter()
	{
		short result = 0;
		string rename;
		double rescore = 0.0;
		result = Search();
		if (result == -1)
			return false;
		char choice = 0;
		cout << "是否要修改这条信息?(y/n)";
		cin >> choice;
		choice = tolower(choice);
		if (choice == 'y')
		{
			cout << "修改什么?	1.名字	2.数学成绩	3.英语成绩	4.计算机成绩" << endl;
			char subchoice = 0;
			cin >> subchoice;
			switch (subchoice)
			{
			case '1':
			{
				cout << "输入一个新名字:";
				cin >> rename;
				stu[result].SetName(rename);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case '2':
			{
				cout << "输入数学成绩:";
				cin >> rescore;
				stu[result].SetMath(rescore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case '3':
			{
				cout << "输入英语成绩:";
				cin >> rescore;
				stu[result].SetEnglish(rescore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case '4':
			{
				cout << "输入计算机成绩:";
				cin >> rescore;
				stu[result].SetComputer(rescore);
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			default:
			{
				cout << "修改失败!" << endl;
				return false;
			}
			}
			return true;
		}
		cout << "信息未修改。" << endl;
		system("pause");
		return false;
	}
	void Display()
	{
		cout << endl << setw(12) << setiosflags(ios::left) << "姓名" << setw(12) << "学号" << setw(8) << "数学" << setw(8) << "英语" << setw(8) << "计算机" << endl << endl;
		for (int i = 0; i < size; i++)
		{
			cout << setw(12) << stu[i].GetName() << setw(12) << stu[i].GetId();
			printf("%-8.1lf%-8.1lf%-8.1lf\n", stu[i].GetMath(), stu[i].GetEnglish(), stu[i].GetComputer());
		}
		cout << resetiosflags(ios::right);
		system("pause");
	}
	void Sort()
	{
		char choice = 0;
		do
		{
			cout << "请输入排序方式	1.学号升序	2.总成绩降序" << endl;
			cin >> choice;
		} while (choice != '1' && choice != '2');
		if (choice == '1')
		{
			sort(&stu[0], &stu[0] + size, cmp1);
		}
		else
		{
			sort(&stu[0], &stu[0] + size, cmp2);
		}
		cout << "重新排序完成。" << endl;
		system("pause");
	}
	char ShowMenu()
	{
		char choice = 0;
		do
		{
			system("cls");
			cout << "----------欢迎使用成绩管理系统----------" << endl << endl;
			cout << "		1.添加学生" << endl;
			cout << "		2.查找学生" << endl;
			cout << "		3.删除学生" << endl;
			cout << "		4.重新排序" << endl;
			cout << "		5.显示全部" << endl;
			cout << "		6.修改信息" << endl;
			cout << "		7.退出" << endl;
			cin >> choice;
		} while (choice < '1' || choice>'7');
		return choice;
	}
	bool Call(char choice)
	{
		switch (choice)
		{
		case '1':
		{
			Push();
			return true;
		}
		case '2':
		{
			Search();
			return true;
		}
		case '3':
		{
			Delete();
			return true;
		}
		case '4':
		{
			Sort();
			return true;
		}
		case '5':
		{
			Display();
			return true;
		}
		case '6':
		{
			Alter();
			return true;
		}
		case '7':
		{
			return false;
		}
		default:
			break;
		}
	}
};
int main()
{
	DataBase student1;
	bool quit = false;
	student1.Push("徐","00001",100.0,100.0,100.0);
	student1.Push("吴", "00002", 98.0, 85.0, 99.0);
	while (!quit)
	{
		if (!student1.Call(student1.ShowMenu()))
			quit = true;
	}
	return 0;
}

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vs2015+数据库,需要建的数据表如下: 1. “考试成绩管理系统用户登录”功能 具体要求: (1) 按照图示排列相应的控件,控件名称自定义,其中,界面中的图片可以不加; (2) 当输入正确的用户名和密码时,登录到主系统,如图所示,并且用户名或密码输入不正确时系统有所提示;当单击【取消】按钮时,用户名和密码被清空; (3) 程序中用到的数据库名为SCOREINFO,数据表名为userinfo,数据表结构如下图所示: (4) 数据表中的用户名和密码如下图。 2. 点击主窗体的“密码修改”菜单,完成“密码修改”功能,程序运行如下图所示: 具体要求: (1)此题必须使用数据库连接完成,原始密码必须为数据表里原有的数据,不使用数据库完成的为0分。 (2)需要建立数据库SCOREINFO及数据表userinfo,表的结构及数据第一部分的内容: (3)要有“原始密码输入错误”、“原始密码不能为空”及“两次输入密码不一致”的错误提示; (4)当单击【保存】按钮,新密码被更新到数据表中,不能更新的为0分; (5)单击【关闭】按钮,窗口关闭。 (6)3个Label;3个TextBox;2个Button 3. 完成“成绩查询”功能,程序运行如下图所示: 具体要求: (1) 按照图示排列相应的控件,界面下方是DataGridView控件; (2)程序用到的数据库名为SCOREINFO,数据表名为score,表结构如下: (3)完成的MainForm_Load事件处理程序:当加载窗体时,直接在窗体的dataGridView1控件中显示数据表的所有记录; (4)可以设查询条件:首先在组合框comboBox1中选择查询条件,并在textBox1中输入条件值(可以模糊查询,如按照姓名查询时,输入“王”,可以查所有姓王的同学的成绩),单击查询将结果显示在dataGridView1控件中。 (5)所需控件及属性:1个GroupBox,1个Label,Text为选择查询条件;1个ComboBox(Items:学号、姓名);1个TextBox;1个Button,Text为查询;1个DataGridView 4. 完成“课程信息修改”功能,程序运行如下图所示: 具体要求: (1)按照图示排列相应的控件,控件名称自定义,其中,程序刚始运行时,“学分”和“课程编码”的文本框是只读的; (2)在数据库名为SCOREINFO中,创建数据表名为course,表结构如下: (3)当单击【查询】时,直接在窗体的dataGridView2控件中显示数据表的所有记录; (4)当选中DataGridView控件中的某一行记录时(DataGridView控件的Mouse_Click事件),“课程名字”、“学分”、“课程代码”文本框中分别显示该项对应的课程信息; (5)当选中某一行记录并单击【编辑】按钮时,【编辑】按钮变为【保存修改】,同时“学分”和“课程编码”的文本框恢复正常(ReadOnly属性为false);在文本框中修改相应的信息后单击【保存修改】,将修改后的数据更新到数据表中。 (6)所需控件及属性:1个GroupBox,3个Label;3个TextBox(textBox2属性ReadOnly为True,textBox3属性ReadOnly为True);2个Button;1个DataGridView 5. 完成“课程信息删除”功能,程序运行如下图所示: 具体要求: (1)按照图示排列相应的控件,控件名称自定义,其中,程序刚始运行时,“学分”和“课程编码”的文本框是只读的; (2)数据表名为course,表结构同第4部分: (3)当单击【查询】时,直接在窗体的dataGridView控件中显示数据表的所有记录; (4)当选中DataGridView控件中的某一行记录时,“课程名字”、“学分”、“课程代码”文本框中分别显示该项对应的课程信息; (5)当选中某一行记录并单击【删除】按钮时,则该行从数据表中删除。 (6)所需控件:3个Label;3个TextBox(textBox2属性ReadOnly为True,textBox3属性ReadOnly为True);2个Button;1个DataGridView 6. 完成“课程信息添加”功能,程序运行如下图所示: 具体要求: (1)按照图示排列相应的控件,控件名称自定义; (2)程序用到的数据库数据表名为course,表结构如下同第四部分: (3)当单击【查询】时,直接在窗体的dataGridView1控件中显示数据表的所有记 (4)当选中DataGridView控件中的某一行记录时,“课程名字”、“学分”、“课程代码”文本框中分别显示该项对应的课程信息; (5)当单击【添加】按钮时,在文本框中添加新的内容并将新内容添加到数据表中,并且在DataGridView控件中显示出新的课程信息 (6)所需控件:3个Label;3个TextBox;2个Button;1个DataGridView
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值