基础项目实战——学生管理系统(c++)

目录

前言

这一期我们来一起学习我们在大学做过的课程设计------学生管理系统,这是一个非常简单且非常值得像我这样的新手独立完成的一个基础项目,用到基础数据结构里的链表来实现,所以指针和链表不太理解的同学先去理解,这样做这个项目会比较顺畅。

一、功能菜单界面

进入到该系统我们就可以选择我们想要的操作,为了方便我们能准确的知道哪个数字是实现什么操作,我们将其一直显示在运行窗口。

cpp 复制代码
int menu() {
	cout << "***********************************************" << endl;
	cout << "*       Welcome to Student Management System  *" << endl;
	cout << "***********************************************" << endl;
	cout << "            Please select an option:         " << 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 << "            0.退出系统                       " << endl;
	cout << "select an option(0-8):";
	int option;
	cin >> option;
	return option;
}

调试的结果

然后就是我们输入想要操作的数字就执行相应的操作,我们可以用enum来表示不同操作的数字。我们每次进行不同操作时为了让我们的运行界面看起来简洁美观,我们可以调用这个函数system("cls"),它表示清空运行界面的所有内容,但是为了不让每次操作完之后就马上清空,所以我们在这个函数之前加入一个暂停的函数system("pause");

cpp 复制代码
enum MenuOption {
	Entry=1,
	Delete=2,
	Update=3,
	Find=4,
	Read=5,
	Save=6,
	Display=7,
	Statistics=8,
	Exit=0
};
cpp 复制代码
int main() {
	List list;
	bool isRunning=true;
	while (isRunning) {
		switch (menu()) {
			case Entry:
				list.addStudent();
				break;
			case Delete:
				list.deleteStudent();
				break;
			case Update:
				list.updateStudent();
				break;
			case Find:
				list.find();
				break;
			case Read:
				list.read();
				break;
			case Save:
				list.save();
				break;
			case Display:
				list.print();
				break;
			case Statistics:
				list.statistics();
				break;
			case Exit:
				isRunning=false;
				break;
		}

		system("pause");
		system("cls");
	}
	
	return 0;
}

二、类与结构体的实现

如果你学了链表这个是非常容易理解的,我就不过多解释了

cpp 复制代码
struct Student {
	unsigned long long number;
	string name;
	int age;
	float score;
};

struct node {
	Student data;
	node* next;
};

class List {
private:
	node* head;
	int size;
public:
	List() :head(NULL), size(0) {}
	~List() {}
	void addStudent();
	void deleteStudent();
	void updateStudent();
	void find();
	void read();
	void save();
	void print();
	void statistics();
};

三、录入学生信息

cpp 复制代码
void List::addStudent() {
	node* newNode = new node;
	cout << "请输入学号:";
	cin >> newNode->data.number;
	cout << "请输入姓名:";
	cin >> newNode->data.name;
	cout << "请输入年龄:";
	cin >> newNode->data.age;
	cout << "请输入成绩:";
	cin >> newNode->data.score;
	newNode->next = head;
	head = newNode;
	size++;
}

四、删除学生信息

cpp 复制代码
void List::deleteStudent() {
	cout << "请输入要删除的学生学号:";
	int number;
	cin >> number;
	node* curr = head;
	node* prev = NULL;
	while (curr) {
		if (curr->data.number == number) {
			if (prev) {
				prev->next = curr->next;
			}
			else {
				head = curr->next;
			}
			delete curr;
			size--;
			return;
		}
		prev = curr;
		curr = curr->next;
	}
	cout << "学生信息不存在!" << endl;
}

五、更改学生信息

cpp 复制代码
void List::updateStudent() {
	cout << "请输入要修改的学生学号:";
	int number;
	cin >> number;
	node* curr = head;
	while (curr) {
		if (curr->data.number == number) {
			cout << "请输入新的姓名:";
			cin >> curr->data.name;
			cout << "请输入新的年龄:";
			cin >> curr->data.age;
			cout << "请输入新的成绩:";
			cin >> curr->data.score;
			return;
		}
		curr = curr->next;
	}
	cout << "学生信息不存在!" << endl;
}

六、查找学生信息

cpp 复制代码
void List::find(){
	cout << "请输入要查询的学生学号:";
	int number;
	cin >> number;
	node* curr = head;
	while (curr) {
		if (curr->data.number == number) {
			cout << "学号:" << curr->data.number << endl;
			cout << "姓名:" << curr->data.name << endl;
			cout << "年龄:" << curr->data.age << endl;
			cout << "成绩:" << curr->data.score << endl;
			return;
		}
		curr = curr->next;
	}
	cout << "学生信息不存在!" << endl;
}

七、统计学生人数

cpp 复制代码
void List::statistics() {
	int cnt = 0;
	node* curr = head;
	while (curr) {
		cnt++;
		curr = curr->next;
	}
	cout << "学生数量:" << cnt << endl;
}

八、保存学生信息

将所以学生的信息全部保存在新创建的文件内如student.txt文件

cpp 复制代码
void List::save() {
	ofstream fout;
	fout.open("student.txt");
	node* curr = head;
	while(curr) {
		fout << curr->data.number << " " << curr->data.name << " " << curr->data.age << " " << curr->data.score << endl;
		curr = curr->next;
	}
	fout.close();
}

九、读取学生信息

cpp 复制代码
void List::read() {
	ifstream fin;
	fin.open("student.txt");
	string line;
	while (getline(fin, line)) {
		cout<<line<<endl;
	}
	fin.close();
}

十、打印所有学生信息

cpp 复制代码
void List::print(){
	node* curr = head;
	cout << "***********************************************" << endl;
	cout << "*       Student Information                   *" << endl;
	cout << "*   学号   *   姓名   *   年龄   *   成绩   *" << endl;
	while (curr) {
		cout << curr->data.number << " " << curr->data.name << " " << curr->data.age << " " << curr->data.score << endl;
		curr = curr->next;
	}
	delete curr;
}

十一、退出系统

我们定义一个布尔变量isRunning,如果它为true系统就继续运行,否则退出系统。

十二、文件拆分

新创建studenManager.cpp和studenManager.h两个文件。

studenManager.cpp文件内容如下:

cpp 复制代码
#include "StudentManager.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int menu() {
	cout << "***********************************************" << endl;
	cout << "*       Welcome to Student Management System  *" << endl;
	cout << "***********************************************" << endl;
	cout << "            Please select an option:         " << 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 << "            0.退出系统                       " << endl;
	cout << "select an option(0-8):";
	int option;
	cin >> option;
	return option;
}

void List::addStudent() {
	node* newNode = new node;
	cout << "请输入学号:";
	cin >> newNode->data.number;
	cout << "请输入姓名:";
	cin >> newNode->data.name;
	cout << "请输入年龄:";
	cin >> newNode->data.age;
	cout << "请输入成绩:";
	cin >> newNode->data.score;
	newNode->next = head;
	head = newNode;
	size++;
}

void List::deleteStudent() {
	cout << "请输入要删除的学生学号:";
	int number;
	cin >> number;
	node* curr = head;
	node* prev = NULL;
	while (curr) {
		if (curr->data.number == number) {
			if (prev) {
				prev->next = curr->next;
			}
			else {
				head = curr->next;
			}
			delete curr;
			size--;
			return;
		}
		prev = curr;
		curr = curr->next;
	}
	cout << "学生信息不存在!" << endl;
}

void List::updateStudent() {
	cout << "请输入要修改的学生学号:";
	int number;
	cin >> number;
	node* curr = head;
	while (curr) {
		if (curr->data.number == number) {
			cout << "请输入新的姓名:";
			cin >> curr->data.name;
			cout << "请输入新的年龄:";
			cin >> curr->data.age;
			cout << "请输入新的成绩:";
			cin >> curr->data.score;
			return;
		}
		curr = curr->next;
	}
	cout << "学生信息不存在!" << endl;
}

void List::find(){
	cout << "请输入要查询的学生学号:";
	int number;
	cin >> number;
	node* curr = head;
	while (curr) {
		if (curr->data.number == number) {
			cout << "学号:" << curr->data.number << endl;
			cout << "姓名:" << curr->data.name << endl;
			cout << "年龄:" << curr->data.age << endl;
			cout << "成绩:" << curr->data.score << endl;
			return;
		}
		curr = curr->next;
	}
	cout << "学生信息不存在!" << endl;
}

void List::statistics() {
	int cnt = 0;
	node* curr = head;
	while (curr) {
		cnt++;
		curr = curr->next;
	}
	cout << "学生数量:" << cnt << endl;
}

void List::save() {
	ofstream fout;
	fout.open("student.txt");
	node* curr = head;
	while(curr) {
		fout << curr->data.number << " " << curr->data.name << " " << curr->data.age << " " << curr->data.score << endl;
		curr = curr->next;
	}
	fout.close();
}

void List::read() {
	ifstream fin;
	fin.open("student.txt");
	string line;
	while (getline(fin, line)) {
		cout<<line<<endl;
	}
	fin.close();
}

void List::print(){
	node* curr = head;
	cout << "***********************************************" << endl;
	cout << "*       Student Information                   *" << endl;
	cout << "*   学号   *   姓名   *   年龄   *   成绩   *" << endl;
	while (curr) {
		cout << curr->data.number << " " << curr->data.name << " " << curr->data.age << " " << curr->data.score << endl;
		curr = curr->next;
	}
	delete curr;
}

studenManager.h文件内容如下:

```cpp
#pragma once
#include <iostream>
using namespace std;

struct Student {
	unsigned long long number;
	string name;
	int age;
	float score;
};

struct node {
	Student data;
	node* next;
};

class List {
private:
	node* head;
	int size;
public:
	List() :head(NULL), size(0) {}
	~List() {}
	void addStudent();
	void deleteStudent();
	void updateStudent();
	void find();
	void read();
	void save();
	void print();
	void statistics();
};

int menu();
enum MenuOption {
	Entry=1,
	Delete=2,
	Update=3,
	Find=4,
	Read=5,
	Save=6,
	Display=7,
	Statistics=8,
	Exit=0
};

main.cpp内容如下:

cpp 复制代码
#include<iostream>
#include"StudentManager.h"
using namespace std;

int main() {
	List list;
	bool isRunning=true;
	while (isRunning) {
		switch (menu()) {
			case Entry:
				list.addStudent();
				break;
			case Delete:
				list.deleteStudent();
				break;
			case Update:
				list.updateStudent();
				break;
			case Find:
				list.find();
				break;
			case Read:
				list.read();
				break;
			case Save:
				list.save();
				break;
			case Display:
				list.print();
				break;
			case Statistics:
				list.statistics();
				break;
			case Exit:
				isRunning=false;
				break;
		}

		system("pause");
		system("cls");
	}
	
	return 0;
}

结语

大家看完之后一定动手做一做,这个项目很容易的做的快的话其实半个几十分钟可以搞定的。

想看更多内容可以关注我,看我作品,关注我让我们一起学习编程,希望大家能点赞关注支持一下,让我有持续更新的动力,谢谢大家。

相关推荐
敲上瘾7 小时前
Qt 主窗口QMainWindow:菜单栏、工具栏、状态栏与浮动窗口等
开发语言·c++·qt·ui·qt6.3
BK贝壳12 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
吴名氏.13 小时前
全栈跃迁:Java 后端 27 讲【3】Vue3工程化脚手架
java·开发语言·springboot·全栈工程师
codeejun13 小时前
每日一Go·MySQL-24、死锁排查与解决方案
开发语言·mysql·golang
人间乄惊鸿客14 小时前
c++ 右值
c++·算法
小此方15 小时前
QT从入门到精通(一)Qt新手保姆级教程:从官网安装到项目实战,四步带你彻底搞懂Qt框架
开发语言·qt
ShineWinsu16 小时前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
瑶池酒剑仙16 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
郝学胜-神的一滴18 小时前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
不积跬步无以至千里mmbb2618 小时前
【软件工艺师 rust相关课程】
开发语言·后端·rust