这是我大一上学期做的一个项目,快做完的时候老师说要用c做,就放弃了,但这个程序基本已经做完了,要添加新的功能很容易,比如搜索学生等,需要做学生管理系统的可以借鉴一下。
需要c语言完整版的看我下一篇博客。
我的制作思路是先创建一个学生的类,里面没有成员函数,只有数据成员,包括姓名,学号,各种成绩。整个程序的稳定性还是很高的。
#pragma once
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
string name;//姓名
int id;//学号
int mathScore;//高数成绩
int politicsScore;//思政成绩
int englishScore;//英语成绩
int Score;//总成绩
};
然后我有创建了一个功能类,里面包含了各种功能的声明,(可以接着往里边添加其他功能)
#pragma once
using namespace std;
#include"Student.h"
#include"studentManager.h"
#include<fstream>
#define FILENAME "empFile.txt"
class studentManager {
public:
studentManager();
//显示菜单
void Show_Menu();
//退出界面
void exitSystem();
//添加学生
void Add_Stu();
//保存文件
void save();
//初始化学生
void init_Stu();
//显示学生
void Show_Stu();
//判断学生是否存在
int IsExist(int id);
//删除学生
void Del_Stu();
//得到学生个数
int get_StuNum();
//记录文件是否为空
bool m_FileIsEmpty;
//记录学生人数
int m_StuNum;
//创建该对象数组;储存每个对象的指针;
Student**m_StuArray;
//用于释放内存
~studentManager();
};
下面是一个源cpp是用来定义上边那个功能类中的函数的
#include"studentManager.h"
#include"Student.h"
using namespace std;
//构造函数用于初始化
studentManager::studentManager() {
//1、文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open()) {
this->m_StuNum = 0;
this->m_StuArray = NULL;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//当文件存在且有内容
int num = this->get_StuNum();
this->m_StuNum = num;
//开辟空间
this->m_StuArray = new Student*[this->m_StuNum];
//将文件的数据,存放到数组中;
this->init_Stu();
}
//退出函数
void studentManager::exitSystem() {
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
//显示菜单函数
void studentManager::Show_Menu() {
cout << "****************" << endl;
cout << "0、退出系统" << endl;
cout << "1、增加学生信息" << endl;
cout << "2、显示学生信息" << endl;
cout << "3、删除学生信息" << endl;
}
//添加学生函数
void studentManager::Add_Stu() {
cout << "请输入添加学生数量" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0) {
this->m_StuNum = 0;
int newSize = this->m_StuNum + addNum;//新空间大小
//开辟新空间
Student**newSpace = new Student*[newSize];
if (this->m_StuNum != NULL) {
for (int i = 0; i < this->m_StuNum; i++) {
newSpace[i] = this->m_StuArray[i];//将原有数组中的元素赋给新空间
}
}
for (int j = 0; j < addNum; j++) {
string name;//姓名
int id;//学号
int mathScore = 0;//高数成绩
int politicsScore = 0;//思政成绩
int englishScore = 0;//英语成绩
int Score;//总成绩
cout << "请输入第" << j + 1 << "个新学生的姓名:" << endl;
cin >> name;
cout << "请输入第" << j + 1 << "个新学生的id:" << endl;
cin >> id;
cout << "请输入第" << j + 1 << "个新学生的高数成绩" << endl;
cin >> mathScore;
cout << "请输入第" << j + 1 << "个新学生的思政成绩" << endl;
cin >> politicsScore;
cout << "请输入第" << j + 1 << "个新学生的英语成绩" << endl;
cin >> englishScore;
Score = mathScore + politicsScore + englishScore;
Student *student = new Student;
student->id = id;
student->name = name;
student->englishScore = englishScore;
student->mathScore = mathScore;
student->politicsScore = politicsScore;
student->Score = Score;
//将学生指针保存到数组中
newSpace[this->m_StuNum + j] = student;//将添加的元素赋给新空间
}
//释放原有的空间
delete[]this->m_StuArray;
this->m_StuArray = NULL;
//更改新空间的指向
this->m_StuArray = newSpace;
this->m_StuNum = newSize;
this->m_FileIsEmpty = false;
this->save();
cout << "成功添加" << addNum << "名新职工" << endl;
}
else
{
cout << "输入数据有误" << endl;
}
//按任意键后清平
system("pause");
system("cls");
}
//得到学生个数函数
int studentManager::get_StuNum() {
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id; string name;
int englishScore;
int mathScore;
int politicsScore;
int Score;
int num = 0;
while (ifs >> id && ifs >> name && ifs >>mathScore&&ifs>> politicsScore&&ifs>> englishScore&&ifs>>Score) {
num++;
}
ifs.close();
return num;
}
//将个人数据保存到文件里的保存函数
void studentManager::save() {
ofstream ofs;
ofs.open(FILENAME, ios::out);//用输出的方式写文件
//将每个人的数据写入到文件中
for (int i = 0; i < this->m_StuNum; i++) {
ofs << this->m_StuArray[i]->id << " "
<< this->m_StuArray[i]->name << " "
<< this->m_StuArray[i]->mathScore << " "
<< this->m_StuArray[i]->politicsScore << " "
<< this->m_StuArray[i]->englishScore << " "
<< this->m_StuArray[i]->Score << endl;
}
ofs.close();
}
//保存到数组中的函数
void studentManager::init_Stu() {
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int mathScore;
int politicsScore;
int englishScore;
int Score;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> mathScore && ifs >> politicsScore && ifs >> englishScore&&ifs>>Score) {
Student*student = new Student;
student->id = id;
student->name = name;
student->mathScore = mathScore;
student->politicsScore = politicsScore;
student->englishScore = englishScore;
student->Score = Score;
this->m_StuArray[index] = student;
index++;
}
ifs.close();
}
//显示学生信息函数
void studentManager::Show_Stu() {
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
system("pause");
}
else {
for (int i = 0; i < m_StuNum; i++) {
cout << "id为:" << this->m_StuArray[i]->id << " "
<< "姓名:" << this->m_StuArray[i]->name << " "
<< "高数成绩:" << this->m_StuArray[i]->mathScore << " "
<< "思政成绩" << this->m_StuArray[i]->politicsScore << " "
<< "英语成绩:" << this->m_StuArray[i]->englishScore << " "
<< "总成绩:" << this->m_StuArray[i]->Score << endl;
}
system("pause");
}
system("cls");
}
//判断学生是否存在函数
int studentManager::IsExist(int id) {
int index = -1;
for (int i = 0; i < this->m_StuNum; i++) {
if (this->m_StuArray[i]->id == id)
{
//找到职工
index = i;
break;
}
}
return index;
}
//删除学生函数
void studentManager::Del_Stu() {
if (this->m_FileIsEmpty) {
cout << "文件不存在或记录为空" << endl;
}
else {
cout << "请输入想要删除的学生编号" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id);
if (index != -1)//职工存在
{
for (int i = index; i < this->m_StuNum - 1; i++) {
this->m_StuArray[i] = this->m_StuArray[i + 1];
}
this->m_StuNum--;
//同步更新到文件中
this->save();
cout << "删除成功!" << endl;
}
else {
cout << "删除失败,未找到该学生" << endl;
}
}
system("pause");
system("cls");
}
//析构函数清除内存
studentManager::~studentManager() {
if (this->m_StuArray != NULL) {
delete[]this->m_StuArray;
this->m_StuArray = NULL;
}
}
最后是main主程序,用来调用类中的函数
#include"studentManager.h"
#include"Student.h"
int main() {
//实例化管理者对象
studentManager sm;
int choice = 0;
while (true) {
sm.Show_Menu();
cout << "请输入您的选择" << endl;
cin >> choice;
switch (choice) {
case 0://退出系统
sm.exitSystem();
break;
case 1://添加学生
sm.Add_Stu();
break;
case 2://显示学生信息
sm.Show_Stu();
break;
case 3://删除学生
sm.Del_Stu();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
此程序用到了标准输入流和输出流方面的知识,会在当前文件生成一个txt文件保存数据。