基础项目实战——学生管理系统(c++)
目录
- 前言
- 一、功能菜单界面
- 二、类与结构体的实现
- 三、录入学生信息
- 四、删除学生信息
- 五、更改学生信息
- 六、查找学生信息
- 七、统计学生人数
- 八、保存学生信息
- 九、读取学生信息
- 十、打印所有学生信息
- 十一、退出系统
- 十二、文件拆分
- 结语
前言
这一期我们来一起学习我们在大学做过的课程设计——学生管理系统,这是一个非常简单且非常值得像我这样的新手独立完成的一个基础项目,用到基础数据结构里的链表来实现,所以指针和链表不太理解的同学先去理解,这样做这个项目会比较顺畅。
一、功能菜单界面
进入到该系统我们就可以选择我们想要的操作,为了方便我们能准确的知道哪个数字是实现什么操作,我们将其一直显示在运行窗口。
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”);
enum MenuOption {
Entry=1,
Delete=2,
Update=3,
Find=4,
Read=5,
Save=6,
Display=7,
Statistics=8,
Exit=0
};
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;
}
二、类与结构体的实现
如果你学了链表这个是非常容易理解的,我就不过多解释了
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();
};
三、录入学生信息
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;
}
八、保存学生信息
将所以学生的信息全部保存在新创建的文件内如student.txt文件
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;
}
十一、退出系统
我们定义一个布尔变量isRunning,如果它为true系统就继续运行,否则退出系统。
十二、文件拆分
新创建studenManager.cpp和studenManager.h两个文件。
studenManager.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内容如下:
#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;
}
结语
大家看完之后一定动手做一做,这个项目很容易的做的快的话其实半个几十分钟可以搞定的。
想看更多内容可以关注我,看我作品,关注我让我们一起学习编程,希望大家能点赞关注支持一下,让我有持续更新的动力,谢谢大家。