C++ Study Notes (7) - Student Information Management System

foreword

General Functional Overview

Specific code implementation

Login to Student System Module

Create a student information module

Show Student Information Module

Save Student Information Module

Read Student Information Module

Add student information module

Delete Student Information Module

Find Student Information Module

Modify Student Information Module

Insert student information module

Student Ranking Module


foreword

I have learned the classes and objects of C++ before, and have a preliminary understanding of C++. In order to deepen its application, I will learn to write a student information management system with everyone.

General Functional Overview

Functions of the student information management system:

  • void menu();//main menu
  • void CreateStuInfo();//Create student information
  • void GetStuFile();//Write student information into the file
  • void ShowStu();//Display all student information
  • void PutStuFile();//Display student information on the terminal
  • void AddStu();//Add student information
  • void DeleteStu();//Delete student information
  • void FindStu();//Find student information
  • void ModifyStu();//Modify student information
  • void InsertStu();//Insert student information
  • //void CountStu();//Statistics of student information
  • void SortStu();//Sort by student grades       

Singly linked list node definition 

struct Student_Node {
	int num;      //学号
	int age;      //年龄
	char name[20];//姓名
	char sex[10]; //性别
	char major[10];//专业
	char born[10]; //出生日期
	char address[20];//家庭住址
	int Math;       //数学成绩
	struct Student_Node* next;//指针域
};

student class

class Student
{
public:
	Student()
	{
		head = new Student_Node;
		head->next = NULL;
	}
	void menu();//主菜单
	void CreatStuInfo();//创建学生信息
	void GetStuFile();//将学生信息写入文件
	void ShowStu();//显示全部学生信息
	void PutStuFile();//将学生信息显示在终端上
	void AddStu();//增加学生信息
	void DeleteStu();//删除学生信息
	void FindStu();//查找学生信息
	void ModifyStu();//修改学生信息
	void InsertStu();//插入学生信息
	//void CountStu();//统计学生信息
	void SortStu();//按照学生成绩排序
private:
	struct Student_Node* head;
};

Specific code implementation

Login to Student System Module

First look at the running results: the login system here is a bit sloppy, you can limit the number of logins or existing accounts, and do not allow re-registration and other constraints.

Code example: 

//首页
void start_page()
{
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t|------欢迎使用学生信息管理系统--------|" << endl;
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t|------------------------------------|" << endl;
	cout << "\t\t\t\t请按Enter进行下一步!!!" << endl << "\t\t\t\t";
	getchar();//停顿便于观察
	system("cls");//刷新界面
}

//登录注册界面
void login()
{
	char username[20], password[10];
	cout << "\t\t\t\t欢迎您进入学生基本信息管理软件的注册界面!" << endl << endl;
	cout << "\t\t\t\t【0】我已经注册过了!" << endl << endl;
	cout << "\t\t\t\t【1】未注册,现在注册!" << endl << endl;
	cout << "\t\t\t\t请按Entet键继续......";
	getchar();
	int i;
	cout << "\t\t\t\t" << "请输入......";
	cin >> i;
	if (i == 1)
	{
		cout << "\t\t\t\t" << "欢迎新用户注册!" << endl << endl;
		cout << "\t\t\t\t" << "请输入您的用户名:";
		cin >> username;
		cout << "\t\t\t\t" << "请输入您的密码:";
		cin >> password;
		system("pause");
		system("cls");
		ofstream regist(".login.txt");
		regist << username;
		regist << password;
		regist.close();
		cout << "注册成功!" << endl;
		getchar();
		system("cls");
	}
	cout << "\t\t\t\t" << "请输入您的用户名:";
	cin >> username;
	cout << "\t\t\t\t" << "请输入您的密码:";
	cin >> password;
	char str[20];
	FILE* fp = fopen(".login.txt", "r");
	if (!fp)
	{
		cout << "打开失败" << endl;
		return;
	}
	while (fgets(str, 20, fp))
	{
	}
	fclose(fp);
	if (strcmp(strcat(username, password), str) == 0)
	{
		cout << "登录成功";//登录成功后,在这里添加主菜单
		//main_function();
	}
	else {
		cout << "\t\t\t\t账号或密码错误,请输入正确的账号和密码!!!" << endl;
	}
	getchar();
	//system("cls");
}

Create a student information module

Example of running results: It is obvious that the age and date of birth do not match with the actual situation. You can add some necessary constraints according to your needs.

code show as below:

//创建学生信息
void Student::CreatStuInfo()
{
	cout << "\t\t\t\t请输入您要创建的学生的数量:" << endl << "\t\t\t\t";
	int i;
	cin >> i;
	struct Student_Node* p1 = head;
	struct Student_Node* p2;
	for (int j = 1; j < i + 1; j++)
	{
		p2 = new Student_Node;
		cout << "\t\t\t\t请输入第" << j << "个学生的学号:" << endl << "\t\t\t\t";
		cin >> p2->num;
		cout << "\t\t\t\t请输入第" << j << "个学生的年龄:" << endl << "\t\t\t\t";
		cin >> p2->age;
		cout << "\t\t\t\t请输入第" << j << "个学生的姓名:" << endl << "\t\t\t\t";
		cin >> p2->name;
		cout << "\t\t\t\t请输入第" << j << "个学生的性别:" << endl << "\t\t\t\t";
		cin >> p2->sex;
		cout << "\t\t\t\t请输入第" << j << "个学生的专业:" << endl << "\t\t\t\t";
		cin >> p2->major;
		cout << "\t\t\t\t请输入第" << j << "个学生的出生日期:" << endl << "\t\t\t\t";
		cin >> p2->born;
		cout << "\t\t\t\t请输入第" << j << "个学生的家庭地址:" << endl << "\t\t\t\t";
		cin >> p2->address;
		cout << "\t\t\t\t请输入第" << j << "个学生的数学成绩:" << endl << "\t\t\t\t";
		cin >> p2->Math;
		p2->next = NULL;
		p1->next = p2;
		p1 = p2;
	}
	getchar();
}

//主菜单
void Student::menu()
{
	cout << "\t\t\t\t------------------------------------" << endl;
	cout << "\t\t\t\t|            主菜单                 |" << endl;
	cout << "\t\t\t\t| 【1】创建学生的信息               |" << endl;
	cout << "\t\t\t\t| 【2】显示学生的信息               |" << endl;
	cout << "\t\t\t\t| 【3】将录入的学生信息进行保存     |" << endl;
	cout << "\t\t\t\t| 【4】将所保存的学生的信息进行读取 |" << endl;
	cout << "\t\t\t\t| 【5】增添学生的信息               |" << endl;
	cout << "\t\t\t\t| 【6】删除学生的信息               |" << endl;
	cout << "\t\t\t\t| 【7】查找学生的信息               |" << endl;
	cout << "\t\t\t\t| 【8】修改学生的信息               |" << endl;
	cout << "\t\t\t\t| 【9】插入学生的信息               |" << endl;
	cout << "\t\t\t\t| 【10】统计学生的信息              |" << endl;
	cout << "\t\t\t\t| 【11】退出学生基本信息管理软件    |" << endl;
	cout << "\t\t\t\t| 【12】 按照学生的数学成绩进行排序 |" << endl;
	cout << "\t\t\t\t------------------------------------|" << endl;
	cout << "\t\t\t\t请输入您的选择:" << endl << "\t\t\t\t";
}

Show Student Information Module

Example of running results

 code show as below:

//显示全部学生成绩
void Student::ShowStu()
{
	struct Student_Node* p1 = head->next;
	int i = 1;
	if (p1 == nullptr)
	{
		i = 0;
	}
	if (i == 0)
	{
		cout << "\t\t\t\t没有找到学生的信息!!!" << endl;
	}
	if (i == 1)
	{
		while (p1)
		{
			cout << endl;
			cout << "学生的学号:\t\t\t" << p1->num << endl;
			cout << "学生的年龄:\t\t\t" << p1->age << endl;
			cout << "学生的姓名:\t\t\t" << p1->name << endl;
			cout << "学生的性别:\t\t\t" << p1->sex << endl;
			cout << "学生的专业:\t\t\t" << p1->major << endl;
			cout << "学生的出生日期:\t\t\t" << p1->born << endl;
			cout << "学生的家庭住址:\t\t\t" << p1->address << endl;
			cout << "学生的英语成绩:\t\t\t" << p1->Math;
			p1 = p1->next;
		}
	}
	getchar();
}

Save Student Information Module

Example of running results:

 Under the corresponding path, you can find the created Notepad

 code show as below:

//将学生信息写入文件中
void Student::GetStuFile()
{
	ofstream outfile(".studentlist.txt");
	if (!outfile)
	{
		cout << "打开文件失败!\n" << endl;
	}
	struct Student_Node* p1 = head->next;
	while (p1)
	{
		outfile << "学号:" << p1->num << endl;
		outfile << "年龄:" << p1->age << endl;
		outfile << "姓名:" << p1->name << endl;
		outfile << "性别:" << p1->sex << endl;
		outfile << "专业:" << p1->major << endl;
		outfile << "出生日期:" << p1->born << endl;
		outfile << "家庭地址:" << p1->address << endl;
		outfile << "英语成绩:" << p1->Math << endl;
		p1 = p1->next;
	}
	outfile.close();
	cout << "保存成功!" << endl;
	getchar();
}

Read Student Information Module

Example of running results: student information can be read directly from the file

code show as below:

//将学生信息写到终端
void Student::PutStuFile()
{
	int count = 0;
	char str[1024];
	FILE* fp = fopen(".studentlist.txt", "r");
	if (!fp)
	{
		return;
	}
	while (fgets(str, 1024, fp))
	{
		if (count % 8 == 0)
		{
			cout << endl;
		}
		cout << str;
		count++;
	}
	fclose(fp);
	system("pause");
}

Add student information module

//增加学生信息
void Student::AddStu()
{
	struct Student_Node* p1 = head->next;
	struct Student_Node* p2;
	int a = 1, b = 1;
	int c; //学生的学号                
	while (p1->next != NULL)
	{
		p1 = p1->next;
		a++;
	}
	cout << "\t\t\t\t请输入学生的学号:" << endl << "\t\t\t\t";
	cin >> c;
	p1 = head->next;
	for (int i = 1; i <= a; i++)//判断学生的学号是否重复 
	{
		if (p1->num == c)
		{
			cout << "\t\t\t\t出错:学号重复!!!" << endl;
			b = 0;
			break;
		}
		p1 = p1->next;
	}
	if (b != 0)//判断 
	{
		p1 = head;
		while (p1->next != NULL)
		{
			p1 = p1->next;
		}
		p2 = new Student_Node;
		p2->num = c;
		cout << "\t\t\t\t请输入学生的年龄:" << endl << "\t\t\t\t"; cin >> p2->age;
		cout << "\t\t\t\t请输入学生的姓名:" << endl << "\t\t\t\t"; cin >> p2->name;
		cout << "\t\t\t\t请输入学生的性别:" << endl << "\t\t\t\t"; cin >> p2->sex;
		cout << "\t\t\t\t请输入学生的专业:" << endl << "\t\t\t\t"; cin >> p2->major;
		cout << "\t\t\t\t请输入学生的出生日期:" << endl << "\t\t\t\t"; cin >> p2->born;
		cout << "\t\t\t\t请输入学生的家庭住址:" << endl << "\t\t\t\t"; cin >> p2->address;
		cout << "\t\t\t\t请输入学生的数学成绩:" << endl << "\t\t\t\t"; cin >> p2->Math;
		p2->next = NULL;
		p1->next = p2;
		p1 = p2;
	}
	getchar();
}

Delete Student Information Module

void Student::DeleteStu()
{
	struct Student_Node* p1;
	p1 = head;
	struct Student_Node* p2;
	int i = 1;
	if (p1->next == NULL)
	{
		cout << "\t\t\t\t删除失败!!!" << endl;
		cout << "\t\t\t\t无可用的数据!!!" << endl;
		i = 0;
	}
	if (i == 1)
	{
		int j;
		cout << "\t\t\t\t请输入您想要删除的学生的学号:" << endl << "\t\t\t\t";
		cin >> j;
		while (p1->next != NULL)
		{
			if (p1->next->num == j)
			{
				cout << "\t\t\t\t******删除成功********" << endl;
				getchar();
				p2 = p1->next;
				p1->next = p2->next;
				delete p2;
				return;

			}
			p1 = p1->next;
		}
		cout << "\t\t\t\t删除错误!!!无法找到此学生的信息!!!" << endl;
	}
	getchar();
}

Find Student Information Module

void Student::FindStu()
{
	int  i = 0;
	struct Student_Node* p1 = head->next;
	int j;
	cout << "\t\t\t\t|--------------------------|" << endl;
	cout << "\t\t\t\t|                          |" << endl;
	cout << "\t\t\t\t|【1】请输入学生的学号查询 |" << endl;
	cout << "\t\t\t\t|【2】请输入学生的姓名查询 |" << endl;
	cout << "\t\t\t\t|                          |" << endl;
	cout << "\t\t\t\t|--------------------------|" << endl;
	cout << "\t\t\t\t";
	cin >> j;
	switch (j)
	{
	case 1:
		int a;
		cout << "\t\t\t\t请输入学生的学号:" << endl << "\t\t\t\t";
		cin >> a;
		while (p1 != NULL)
		{
			if (p1->num == a)
			{
				cout << "\t\t\t\t该学生的学号:\t\t\t\t";
				cout << p1->num << endl;
				cout << "\t\t\t\t该学生的年龄:\t\t\t\t";
				cout << p1->age << endl;
				cout << "\t\t\t\t该学生的姓名:\t\t\t\t";
				cout << p1->name << endl;
				cout << "\t\t\t\t该学生的性别:\t\t\t\t";
				cout << p1->sex << endl;
				cout << "\t\t\t\t该学生的专业:\t\t\t\t";
				cout << p1->major << endl;
				cout << "\t\t\t\t该学生的出生日期:\t\t\t";
				cout << p1->born << endl;
				cout << "\t\t\t\t该学生的家庭住址:\t\t\t";
				cout << p1->address << endl;
				cout << "\t\t\t\t该学生的数学成绩:\t\t\t";
				cout << p1->Math << endl;
				i++;
			}
			p1 = p1->next;
		}
		if (i == 0)
		{
			cout << "\t\t\t\t查询错误!!!" << endl;
			cout << "\t\t\t\t没有此学生的数据!!!" << endl;
		}
		break;
	case 2:
		char xingming[20];
		cout << "\t\t\t\t请输入学生的姓名:" << endl;
		cout << "\t\t\t\t";
		cin >> xingming;
		while (p1 != NULL)
		{
			if (strcmp(p1->name, xingming) == 0)
			{

				cout << "\t\t\t\t该学生的学号:\t\t\t\t";
				cout << p1->num << endl;
				cout << "\t\t\t\t该学生的年龄:\t\t\t\t";
				cout << p1->age << endl;
				cout << "\t\t\t\t该学生的姓名:\t\t\t\t";
				cout << p1->name << endl;
				cout << "\t\t\t\t该学生的性别:\t\t\t\t";
				cout << p1->sex << endl;
				cout << "\t\t\t\t该学生的专业:\t\t\t\t";
				cout << p1->major << endl;
				cout << "\t\t\t\t该学生的出生日期:\t\t\t";
				cout << p1->born << endl;
				cout << "\t\t\t\t该学生的家庭住址:\t\t\t";
				cout << p1->address << endl;
				cout << "\t\t\t\t该学生的数学成绩:\t\t\t";
				cout << p1->Math;
				i++;
			}
			p1 = p1->next;
		}
		if (i == 0)
		{

			cout << "\t\t\t\t查询错误!!!" << endl;
			cout << "\t\t\t\t没有此学生的数据!!!" << endl;
		}
		break;
	default:
		cout << "\t\t\t\t信息错误!!!" << endl;
	}
	getchar();
	
}

Modify Student Information Module

void Student::ModifyStu()
{
	struct Student_Node* p1 = head->next;
	struct Student_Node* p2 = head->next;
	cout << "\t\t\t\t请输入需要修改的学生的学号:" << endl;
	int i;
	int j = 0;
	int k = 1;
	cout << "\t\t\t\t";
	cin >> i;
	while (p1)
	{
		if (p1->num == i)//对是否有该生的信息进行判断 
		{
			j = 1;
		}
		p1 = p1->next;
	}
	if (j == 0)
	{
		cout << "\t\t\t\t错误!!!" << endl;
		cout << "\t\t\t\t没有该生的信息!!!" << endl;
	}
	if (j == 1)
	{
		p1 = head->next;
		while (p1)
		{
			if (p1->num == i)
			{
				cout << "\t\t\t\t-----------------------------" << endl;
				cout << "\t\t\t\t|---【1】修改学生的学号------" << endl;
				cout << "\t\t\t\t|---【2】修改学生的姓名------" << endl;
				cout << "\t\t\t\t|---【3】修改学生的性别------" << endl;
				cout << "\t\t\t\t|---【4】修改学生的专业------" << endl;
				cout << "\t\t\t\t|---【5】修改学生的出生日期--" << endl;
				cout << "\t\t\t\t|---【6】修改学生的家庭住址--" << endl;
				cout << "\t\t\t\t|---【7】修改学生的数学成绩--" << endl;
				cout << "\t\t\t\t|---【8】修改学生的年龄------" << endl;
				cout << "\t\t\t\t|----------------------------" << endl;
				int x;
				cout << "\t\t\t\t";
				cin >> x;
				switch (x)
				{
				case 1:
					cout << "\t\t\t\t请输入修改后的学号:" << endl;
					int y;
					cout << "\t\t\t\t";
					cin >> y;
					while (p2)
					{
						if (p2->num == y)
						{
							cout << "\t\t\t\t出错!!!" << endl;
							cout << "\t\t\t\t学号重复!!!" << endl;
							break;
							k = 0;
						}
						p2 = p2->next;
					}
					if (k != 0)
					{
						p1->num = y;
					}
					break;
				case 2:
					cout << "\t\t\t\t请输入姓名:" << endl << "\t\t\t\t";
					cin >> p1->name;
					break;
				case 3:
					cout << "\t\t\t\t请输入性别:" << endl << "\t\t\t\t";
					cin >> p1->sex;
					break;
				case 4:
					cout << "\t\t\t\t请输入专业:" << endl << "\t\t\t\t";
					cin >> p1->major;
					break;
				case 5:
					cout << "\t\t\t\t请输入出生日期:" << endl << "\t\t\t\t";
					cin >> p1->born;
					break;
				case 6:
					cout << "\t\t\t\t请输入家庭住址:" << endl << "\t\t\t\t";
					cin >> p1->address;
					break;
				case 7:
					cout << "\t\t\t\t请输入数学成绩:" << endl << "\t\t\t\t";
					cin >> p1->Math;
					break;
				case 8:
					cout << "\t\t\t\t请输入年龄:" << endl << "\t\t\t\t";
					cin >> p1->age;
					break;
				}
				break;
			}
		}
	}
	getchar();
	
}

Insert student information module

void Student::InsertStu()
{
	struct Student_Node* p1, * p2;
	struct Student_Node* p3;
	int i;
	cout << "\t\t\t\t请输入要在哪个学号之后插入学生信息:" << endl << "\t\t\t\t";
	cin >> i;
	p1 = new Student_Node;
	cout << "\t\t\t\t请输入待插入学生的学号:" << endl << "\t\t\t\t";
	cin >> p1->num;
	cout << "\t\t\t\t请输入待插入学生的年龄:" << endl << "\t\t\t\t";
	cin >> p1->age;
	cout << "\t\t\t\t请输入待插入学生的姓名:" << endl << "\t\t\t\t";
	cin >> p1->name;
	cout << "\t\t\t\t请输入待插入学生的性别:" << endl << "\t\t\t\t";
	cin >> p1->sex;
	cout << "\t\t\t\t请输入待插入学生的专业:" << endl << "\t\t\t\t";
	cin >> p1->major;
	cout << "\t\t\t\t请输入待插入学生的出生日期:" << endl << "\t\t\t\t";
	cin >> p1->born;
	cout << "\t\t\t\t请输入待插入学生的数学成绩:" << endl << "\t\t\t\t";
	cin >> p1->Math;
	cout << "\t\t\t\t请输入待插入学生的家庭住址:" << endl << "\t\t\t\t";
	cin >> p1->address;
	p2 = head->next;
	p3 = head;
	while (p2)
	{
		p3 = p2;
		if (p3->num == i)
		{
			break;
		}
		p2 = p2->next;
	}
	p1->next = p3->next;
	p3->next = p1;
	getchar();
}

Student Ranking Module

void Student::SortStu()
{
	//由高分到低分排列  
	{
		int i = 1;
		if (head->next == NULL)
		{
			i = 0;
		}
		if (i == 0)
		{
			cout << "\t\t\t\t出错!!!" << endl;
			cout << "\t\t\t\t没有找到学生的数据!!!" << endl;
		}
		if (i == 1)
		{
			struct Student_Node* p1, * p2;
			int num_1;
			int age_1;
			char name_1[20];
			char sex_1[10];
			char major_1[10];
			char born_1[10];
			char address_1[10];
			int Math_1;
			int num_2;
			int age_2;
			char name_2[20];
			char sex_2[10];
			char major_2[10];
			char born_2[10];
			char address_2[10];
			int Math_2;
			for (p1 = head->next; p1 != NULL; p1 = p1->next)
			{
				for (p2 = p1->next; p2 != NULL; p2 = p2->next)
				{
					if (p1->Math < p2->Math)
					{
						num_1 = p1->num;
						age_1 = p1->age;
						strcpy(name_1, p1->name);
						strcpy(sex_1, p1->sex);
						strcpy(major_1, p1->major);
						strcpy(born_1, p1->born);
						strcpy(address_1, p1->address);
						Math_1 = p1->Math;
						num_2 = p2->num;
						age_2 = p2->age;
						strcpy(name_2, p2->name);
						strcpy(sex_2, p2->sex);
						strcpy(major_2, p2->major);
						strcpy(born_2, p2->born);
						strcpy(address_2, p2->address);
						Math_2 = p2->Math;
						p1->num = num_2;
						p1->age = age_2;
						strcpy(p1->name, name_2);
						strcpy(p1->sex, sex_2);
						strcpy(p1->major, major_2);
						strcpy(p1->born, born_2);
						strcpy(p1->address, address_2);
						p1->Math = Math_2;
						p2->num = num_1;
						p2->age = age_1;
						strcpy(p2->name, name_1);
						strcpy(p2->sex, sex_1);
						strcpy(p2->major, major_1);
						strcpy(p2->born, born_1);
						strcpy(p2->address, address_1);
						p2->Math = Math_1;
					}
				}
			}
			cout << "\t\t\t\tWonderful*****排序成功*****" << endl;
		}getchar();
	}
}

Guess you like

Origin blog.csdn.net/m0_58367586/article/details/127653583