基于C/C++实现的简单学生信息管理系统

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41151210/article/details/84637700
                           基于C/C++实现的简单学生信息管理系统

这是本人在学习了C/C++后,自己尝试写的一个小项目,该程序实现了对学生的学号姓名,年龄和学科成绩进行管理的一个小程序,具体功能实现了包括(1:添加学生信息 2:查看学生信息(分为按学号姓名以及查看全部学生信息)3:删除学生信息 4:修改学生信息),具体代码实现如下:
头文件studentInformationSystem.h
#ifndef STUDENTINFORMATIONSYSTEM_H
#define STUDENTINFORMATIONSYSTEM_H
#include
#include
#include<windows.h>
#include
#include<string.h>
#include <process.h>
#include<conio.h>
#include<time.h>
#define SUPERUSER “zhonggaoke”
#define PASSWARD “zhonggaoke0410”
using namespace std;
typedef struct Score {
float math;
float english;
float chinese;
}score;
typedef struct Student {
char studentNumber[20];
char studentName[20];
char studentSex[20];
int studentAge;
score studentScore;
}stu;
#endif
源文件studentInformation.cpp
#include"studentInformationSystem.h"

class studentInformation {
public:
stu thisStudent;
public:
studentInformation();//构造函数
~studentInformation();//析构函数
int systemMenu();//系统菜单
int checkUser(int input);//检查那种用户执行
int addStudentInformation();//添加学生信息(super)
int checkStudentInformation();//查看学生信息
int nameAndNumberCheckStudentInformation();//按姓名学号查看信息
int allStudentInformation();//全部学生信息
int deleteStudentInformation();//删除学生信息(super)
int modificationStudentInformation();//修改学生信息(super)
int trueFunction();//实现各个操作
};

studentInformation::studentInformation()
{
system(“title student information system.”);
system(“color 5f”);
system(“mode con cols=60 lines=24”);
}
studentInformation::~studentInformation(){}

/菜单/

int studentInformation::systemMenu()
{
system(“cls”);
int input;
for (int i = 0; i < 6; i++) { cout << endl; }
cout << " )-------->"; cout << “1:添加学生信息.” << endl;
cout << " )-------->"; cout << “2:删除学生信息.” << endl;
cout << " )-------->"; cout << “3:修改学生信息.” << endl;
cout << " )-------->"; cout << “4:查看学生信息.” << endl<<endl;
cout << " ->"; cout << “请选择:”;
cin >> input;
while (input < 0 || input>10)
{
system(“cls”);
systemMenu();
}
return input;
}

/判断用户/

int studentInformation::checkUser(int input)
{
system(“cls”);
string account;
string passward;
if (input == 1 || input == 2 || input == 3)
{
cout << “该功能仅限于管理者登陆.” << endl;
cout << “SUPER USER:”;
cin >> account;
while (account != SUPERUSER)
{
cout << “账户错误(重新输入):”;
cin >> account;
}
cout << “PASSWARD:”;
cin >> passward;
while (passward != PASSWARD)
{
cout << “密码错误(重新输入):”;
cin >> passward;
}
}
system(“cls”);
cout << “登陆成功.” << endl;
return 0;
}

/添加学生信息/

int studentInformation::addStudentInformation()
{
system(“cls”);
int checkValue;
system(“title addstudentinformation”);
cout << “请输入学号:” << endl;
cin >> thisStudent.studentNumber;
cout << “请输入姓名:” << endl;
cin >> thisStudent.studentName;
cout << “请输入年龄:” << endl;
cin >> thisStudent.studentAge;
cout << “请输入性别:” << endl;
cin >> thisStudent.studentSex;
cout << “请输入数学成绩:” << endl;
cin >> thisStudent.studentScore.math;
cout << “请输入英语成绩:” << endl;
cin >> thisStudent.studentScore.english;
cout << “请输入语文成绩:” << endl;
cin >> thisStudent.studentScore.chinese;
FILE *thisFile;
thisFile = fopen(“E:\vs2017\studentinformationsystem\studentInformation.txt”, “a”);
fprintf(thisFile, “%-20s”,thisStudent.studentNumber);
fprintf(thisFile, “%-20s”,thisStudent.studentName);
fprintf(thisFile, “%-20s”, thisStudent.studentSex);
fprintf(thisFile, “%-4d”, thisStudent.studentAge);
fprintf(thisFile, “语文:%-8.1f”,thisStudent.studentScore.chinese);
fprintf(thisFile, “数学:%-8.1f”,thisStudent.studentScore.math);
fprintf(thisFile, “英语:%-8.1f”,thisStudent.studentScore.english);
fprintf(thisFile, “\n”);
fclose(thisFile);
cout << “1:继续添加 2:返回上一层” << endl;
cout << “请选择:”;
cin >> checkValue;
if (checkValue == 1)
{
addStudentInformation();
}
if (checkValue == 2)
{
system(“cls”);
cout << “—>3秒后将返回菜单…”;
Sleep(3000);
trueFunction();
}
return 0;
}

/查看学生信息/

int studentInformation::checkStudentInformation()
{
system(“cls”);
int checkChioce;
cout << “1:输入姓名/学号查看学生信息.” << endl;
cout << “2:查看全部学生信息.” << endl;
cout << “->请选择:”;
cin >> checkChioce;
while (checkChioce != 1 && checkChioce!=2)
{
cout << “!!!不合法的输入.” << endl;
cout << “->请选择:”;
cin >> checkChioce;
}
if (checkChioce == 1)
{
nameAndNumberCheckStudentInformation();
}
if (checkChioce == 2)
{
allStudentInformation();
}
return 0;
}
/按姓名学号查看学生信息/

int studentInformation::nameAndNumberCheckStudentInformation()
{
string thisNameOrNumber;
string getNameNumber;
int choice;
system(“cls”);
cout << “->按学生姓名学号查看学生信息:” << endl;
cout << “请输入学生姓名或者学号:”;
cin >> thisNameOrNumber;
ifstream file(“E:\vs2017\studentinformationsystem\studentInformation.txt”);
if (file)
{
while (getline(file, getNameNumber))
{
string::size_type idx;
idx = getNameNumber.find(thisNameOrNumber);
if (idx != string::npos)
{
//value = 1;
cout << getNameNumber << endl;
}
}
}
else
{
cout << “no file.”<<endl;
}
cout << “1:继续查找.2:返回菜单.” << endl;
cout << “请选择:”;
cin >> choice;
while (choice != 1 && choice != 2)
{
cout << “错误!!!” << endl;
cout << “请重新输入:”;
cin >> choice;
}
if (choice == 1)
{
nameAndNumberCheckStudentInformation();
}
if (choice == 2)
{
system(“cls”);
cout << “—>3秒后将返回菜单…”;
Sleep(3000);
trueFunction();
}
return 0;
}

/全部学生信息查看/

int studentInformation::allStudentInformation()
{
system(“cls”);
FILE *thisFile;
thisFile = fopen(“E:\vs2017\studentinformationsystem\studentInformation.txt”, “r”);
char date;
char checkValue;
//date = fgetc(thisFile);
while ((date = fgetc(thisFile)) != EOF)
{
cout << date;
}
cout << “E返回菜单.” << endl;
cout << “请输入:”;
cin >> checkValue;
while (checkValue != ‘e’ && checkValue != ‘E’)
{
cout << “错误的输入!!!” << endl;
cout << “请重新输入:”;
cin >> checkValue;
}
system(“cls”);
cout << “—>3秒后将返回菜单…”;
Sleep(3000);
trueFunction();
return 0;
}

/删除学生信息/

int studentInformation::deleteStudentInformation() //判断无该学生信息
{
system(“cls”);
char choice;
int n = 0;
int input;
string inputNameOrNumber;
string getStudentInformation;
string studentInformation[100];
string midValue;
ifstream thisFile(“E:\vs2017\studentinformationsystem\studentInformation.txt”);
cout << “<------删除学生信息------->” << endl;
cout << “请输入要删除的学生信息(姓名或者学号):”;
cin >> inputNameOrNumber;
if (thisFile)
{
while (getline(thisFile, getStudentInformation))
{
string::size_type idx;
idx = getStudentInformation.find(inputNameOrNumber);
studentInformation[n] = getStudentInformation;
if (idx!=string::npos)
{
cout << “要删除的学生信息是:” << endl;
cout<< getStudentInformation << endl;
midValue = getStudentInformation;
}
n = n++;
}
}
else
{
cout << “no file.” << endl;
}
cout << “是否确定删除该学生信息(Y/N)?” << endl;
cout << “请选择:”;
cin >> choice;
if (choice == ‘y’ || choice == ‘Y’)
{
/删除/
fstream file(“E:\vs2017\studentinformationsystem\studentInformation.txt”, ios::out);
ofstream fout(“E:\vs2017\studentinformationsystem\studentInformation.txt”);
if (!fout)
{
cout << “删除失败!!!” << endl;
}
else
{
for (int i = 0; i < n; i++)
{
if (studentInformation[i] != midValue)
{
fout << studentInformation[i] << endl;
}
}
cout << “删除成功!!!” << endl;
}
cout << “1:继续删除. 2:返回菜单.” << endl;
cout << “请选择:”;
cin >> input;
if (input == 1)
{
deleteStudentInformation();
}
else if (input == 2)
{
trueFunction();
}
}
else if (choice == ‘n’ || choice == ‘N’)
{
system(“cls”);
cout << “—>3秒后将返回菜单…”;
Sleep(3000);
trueFunction();
}
return 0;
}

/修改学生信息/

int studentInformation::modificationStudentInformation()
{
system(“cls”);
char choice;
int n = 0;
int input;
string inputNameOrNumber;
string getStudentInformation;
string studentInformation[100];
string midValue;
ifstream thisFile(“E:\vs2017\studentinformationsystem\studentInformation.txt”);
cout << “<------修改学生信息------->” << endl;
cout << “请输入要修改的学生信息(姓名或者学号):”;
cin >> inputNameOrNumber;
if (thisFile)
{
while (getline(thisFile, getStudentInformation))
{
string::size_type idx;
idx = getStudentInformation.find(inputNameOrNumber);
studentInformation[n] = getStudentInformation;
if (idx != string::npos)
{
cout << “要修改的学生信息是:” << endl;
cout << getStudentInformation << endl;
midValue = getStudentInformation;
}
n = n++;
}
}
else
{
cout << “no file.” << endl;
}
cout << “是否确定修改该学生信息(Y/N)?” << endl;
cout << “请选择:”;
cin >> choice;
if (choice = ‘y’ || choice == ‘Y’)
{
fstream file(“E:\vs2017\studentinformationsystem\studentInformation.txt”, ios::out);
ofstream fout(“E:\vs2017\studentinformationsystem\studentInformation.txt”);
if (!fout)
{
cout << “修改失败!!!” << endl;
}
else
{
for (int i = 0; i < n; i++)
{
if (studentInformation[i] != midValue)
{
fout << studentInformation[i] << endl;
}
else if (studentInformation[i] == midValue)
{
cout << “请重新输入学生信息:” << endl;
cout << “请输入学号:” << endl;
cin >> thisStudent.studentNumber;
cout << “请输入姓名:” << endl;
cin >> thisStudent.studentName;
cout << “请输入年龄:” << endl;
cin >> thisStudent.studentAge;
cout << “请输入性别:” << endl;
cin >> thisStudent.studentSex;
cout << “请输入数学成绩:” << endl;
cin >> thisStudent.studentScore.math;
cout << “请输入英语成绩:” << endl;
cin >> thisStudent.studentScore.english;
cout << “请输入语文成绩:” << endl;
cin >> thisStudent.studentScore.chinese;
FILE *thisFile;
string midInformation;
thisFile = fopen(“E:\vs2017\studentinformationsystem\midFile.txt”, “a”);
fprintf(thisFile, “%-20s”, thisStudent.studentNumber);
fprintf(thisFile, “%-20s”, thisStudent.studentName);
fprintf(thisFile, “%-20s”, thisStudent.studentSex);
fprintf(thisFile, “%-4d”, thisStudent.studentAge);
fprintf(thisFile, “语文:%-8.1f”, thisStudent.studentScore.chinese);
fprintf(thisFile, “数学:%-8.1f”, thisStudent.studentScore.math);
fprintf(thisFile, “英语:%-8.1f”, thisStudent.studentScore.english);
//fprintf(thisFile, “\n”);
fclose(thisFile);
ifstream midFile(“E:\vs2017\studentinformationsystem\midFile.txt”);
getline(midFile, midInformation);
fstream _file(“E:\vs2017\studentinformationsystem\midFile.txt”, ios::out);//清空midFile文件
fout << midInformation << endl;
}
}
cout << “修改成功!!!” << endl;
}
cout << “1:继续修改 2:返回菜单” << endl;
cout << “请选择:”;
int _input;
cin >> _input;
while (_input != 1 && _input != 2)
{
cout << “输入错误!!!” << endl;
cout << “请重新输入:”;
cin >> _input;
}
if (_input == 1)
{
modificationStudentInformation();
}
else if (_input == 2)
{
trueFunction();
}
}
else if (choice == ‘n’ || choice == ‘N’)
{
trueFunction();
}
return 0;
}

/实现/

int studentInformation::trueFunction()
{
int key;
key = systemMenu();
switch (key) {
case 1:checkUser(key); addStudentInformation(); break;
case 2:checkUser(key); deleteStudentInformation(); break;
case 3:checkUser(key); modificationStudentInformation(); break;
case 4:checkStudentInformation(); break;
}
return 0;
}
int main()
{
studentInformation melody;
melody.trueFunction();
system(“pause”);
return 0;
}
其中修改学生信息处理方法为创建两哥文件夹分别为studentInformation.txt和midFile.tex利用getline获取一个学生的全部信息(getStudentInformation)再利用getStudentInformation.find(name/number)找到需要修改的学生信息将其储存再MidFile.txt文件下,再将读取的每个学生信息存入清空的studentInformation.txt文件中当遇到需要修改的学生再重新输入信息。

展开阅读全文

没有更多推荐了,返回首页