本人C++大一小白,最近有闲着的时间,就写了个学生信息管理系统,全文件操作,有些地方复杂度太大,还有很大的修改空间。
后期关于成绩修改和信息修改实现没写,思想跟已经实现的修改密码一样,你若感兴趣可以补上此功能!
#pragma once
#include
#include
#include
#define Max 1024
using namespace std;
class student_login
{
protected:
string account;//账号
string password;//密码
public:
student_login(string account = "###", string password = "###");//初始化账号密码
bool student_judge();//登录端口
void modify_password();//修改密码端口
};
#include"student login.h"
student_login::student_login(string account, string password)
{
this->account = account;
this->password = password;
}
bool student_login::student_judge()
{
string user_account, user_password;
fstream user_file("学生账号密码.txt", ios::in | ios::app);//读取原数据
if (!user_file.is_open())//判断是否打开
{
return false;
}
while (user_file >> user_account >> user_password)//读取文件数据
{
if (user_account == this->account && user_password == this->password)//登录成功
{
user_file.close();
return true;
}
}
return false;
}
void student_login::modify_password()
{
fstream file("学生账号密码.txt", ios::in);//读取文件原数据
if (!file.is_open())
{
cerr << "the file is not be opened!" << endl;
return;
}
//暂存账号密码
string user_account[Max];
string user_password[Max];
for (int i = 0; i < Max; i++)
{
user_account[i] = "###";
user_password[i] = "###";
}
string Account, Password;
int i = 0, j = 0;
while (file >> Account >> Password,!file.eof())
{
user_account[i++] = Account;
user_password[j++] = Password;
}
file.close();
fstream File("学生账号密码.txt", ios::out);
string modify_password;
cout << "请重置您的密码!" << endl;
cin >> modify_password;
i = 0, j = 0;
bool modify_flag = false;
for (int k = 0; user_account[k] != "###"; k++)//重置文件数据
{
if (user_account[k] == this->account)
{
File << user_account[k] << " " << modify_password << endl;
modify_flag = true;
}
else File << user_account[k] << " " << user_password[k] << endl;
}
File.close();
if (!modify_flag)
{
cerr << "密码修改失败!" << endl;
return;
}
else cout << "密码修改成功!" << endl;
}
#pragma once
#include"student login.h"
class student_information:public student_login
{
protected:
string user_name;//学生姓名
string user_number;//学生学号
string user_address;//学生家庭住址
double user_score;//学生高考分数
string major;//学生专业
string classroom;//学生班级
public:
student_information(string account="###",string name = "#", string number = "000000", string address = "无家庭住址", double score = 480, string Major = "无专业", string Classroom = "无班级");//480为最低录取线
void save_information();//查询学生基本学籍信息
};
class student_final_grades:public student_login
{
protected:
double higher_mathematics;//高等数学
double discrete;//离散数学
double english;//英语
double c;//高程语言
double physics;//大学物理
public:
student_final_grades(string account="###",double math = 0, double Distrete = 0, double English = 0, double C = 0, double Physics = 0);
void save_grades();//查询学生期末成绩
};
#include"student information.h"
student_information::student_information(string account,string name,string number, string address, double score, string Major, string Classroom)
{
this->account = account;
user_name = name;
user_number = number;
user_address = address;
user_score = score;
major = Major;
classroom = Classroom;
}
student_final_grades::student_final_grades(string account,double math, double Discrete, double English, double C, double Physics)
{
this->account = account;
higher_mathematics = math;
discrete = Discrete;
english = English;
c = C;
physics = Physics;
}
void student_information::save_information()
{
fstream StudentInformation("学生基本信息.txt", ios::in);//打开学生信息数据文件
if(!StudentInformation.is_open())//打开失败
{
cerr << "the file is not be opened!" << endl;
return;
}
string information;//存放文件学生信息数据
bool flag = false;//查询状态
string user_account;
while (StudentInformation>>user_account)
{
if (this->account==user_account)//查询成功
{
cout << "姓名" << " " << "学号" << " " << "家庭住址" << " " << "高考分数" << " " << "所在专业" << " " << "班级" << endl;
getline(StudentInformation, information);
cout << information << endl;
flag = true;
StudentInformation.close();
break;
}
}
if (!flag)
{
cerr << "学生信息查找失败!" << endl;
return;
}
}
void student_final_grades::save_grades()
{
fstream StudentGrades("学生期末成绩.txt", ios::in);
if (!StudentGrades.is_open())
{
cerr << "the StudentGrades file is not be opened" << endl;
return;
}
string grades;//存放学生成绩
string user_account;
bool flag = false;//查询状态
while (StudentGrades >> user_account)
{
if (this->account == user_account)//查找成功
{
cout << "姓名" << " " << "高数" << " " << "离散数学" << " " << "英语" << " " << "C++" << " " << "大学物理" << endl;
getline(StudentGrades, grades);
cout << grades << endl;
flag = true;
StudentGrades.close();
break;
}
}
if (!flag)
{
cerr << "学生成绩查询失败!" << endl;
return;
}
}
#pragma once
#include"student information.h"
class student :public student_information,public student_final_grades
{
public:
void set_login();//登录接口
void set_information();//基本学籍信息接口
void set_grades();//成绩查询接口
void set_modify();//密码修改接口
void student_choice();//学生端操作
void student_menu();//学生端操作菜单
};
static string LoginAccount, LoginPassword;//登录账号和密码变元
#include"student user.h"
bool flag = false;
void student::set_login()
{
cout << "请输入您的账号!" << endl;
cin >> LoginAccount;
cout << "请输入您的密码!" << endl;
cin >> LoginPassword;
student_login s1(LoginAccount, LoginPassword);//进入学生登录端
if (s1.student_judge())
{
cout << "登录成功!" << endl;
flag = true;
}
else cerr << "账号或密码错误!" << endl;
}
void student::set_information()
{
student_information s2(LoginAccount);//进入学生信息端
s2.save_information();
}
void student::set_grades()
{
student_final_grades s3(LoginAccount);//进入学生成绩端
s3.save_grades();
}
void student::set_modify()
{
student_login s4(LoginAccount, LoginPassword);
s4.modify_password();
}
void student::student_menu()
{
cout << "\t\t\t\t\t\t\t1.登录账号" << endl;
cout << "\t\t\t\t\t\t\t2.查询个人信息" << endl;
cout << "\t\t\t\t\t\t\t3.查询期末成绩" << endl;
cout << "\t\t\t\t\t\t\t4.修改个人密码" << endl;
}
void student::student_choice()
{
int choice;
cout << "\t\t\t\t\t\t您已经成功进入学生端,祝您使用愉快!" << endl;
student_menu();
cout << "请选择你要进行的操作" << endl;
cin >> choice;
switch (choice)
{
case 1: {set_login(), system("pause"), system("cls"), student_choice(); break; }
case 2:
{
if (!flag)
{
cerr << "请您先登录账号!" << endl;
system("pause");
system("cls");
student_choice();
break;
}
else { set_information(), system("pause"), system("cls"), student_choice(); break; }
}
case 3:
{
if (!flag)
{
cerr << "请您先登录账号!" << endl;
system("pause");
system("cls");
student_choice();
break;
}
else { set_grades(), system("pause"), system("cls"), student_choice(); break; }
}
case 4:
{
if (!flag)
{
cerr << "请您先登录账号!" << endl;
system("pause");
system("cls");
student_choice();
break;
}
else { set_modify(),system("pause"),system("cls"), student_choice(); break; }
}
default:
system("cls"), student_choice(); break;
}
}
#pragma once
#include
#include
#include
using namespace std;
class administrator_login
{
private:
string account;//管理员账号
string password;//管理员密码
public:
administrator_login(string account = "###", string password = "###");//构造函数
bool administrator_judge();//管理员登录端口
};
#include"adimistrator login.h"
administrator_login::administrator_login(string account,string password)
{
this->account = account;
this->password = password;
}
bool administrator_login::administrator_judge()
{
fstream administrator_file("管理员账号密码.txt", ios::in);
if (!administrator_file.is_open())
{
cerr << "文件打开失败!" << endl;
return false;
}
string administrator_account, administrator_password;
while (administrator_file >> administrator_account >> administrator_password)
{
if (this->account == administrator_account && this->password ==administrator_password)//登录成功
{
administrator_file.close();
return true;
}
}
return false;
}
#pragma once
#include"student user.h"
#include"adimistrator login.h"
#define Max 1024
class ManageStudent:public administrator_login,public student
{
public:
void AddStudent();//增加学生信息
void ReduceStudent();//删除学生信息
void InquireStudentMessage();//查询学生信息
void InquireStudentGrades();//查询学生期末成绩
};
#include"adiministrator manage student.h"
void ManageStudent::AddStudent()
{
fstream Add_account("学生账号密码.txt", ios::out | ios::app);
if (!Add_account.is_open())
{
cerr << "the file is not be opened!" << endl;
return;
}
cout << "正在为您录入学生信息!请为其设置学生端账号密码(初始密码为账号)" << endl;
string account;
cout << "请设置账号!" << endl;
cin >> account;
Add_account << account << " " << account << endl;
cout << "学生账号创建成功!" << endl;
Add_account.close();
fstream Add_message("学生基本信息.txt", ios::out | ios::app);
if (!Add_message.is_open())
{
cerr << "the file is not be opened!" << endl;
return;
}
cout << "正在为您录入学生信息!请输入学生学籍信息" << endl;
string name, number, address, score, major, classroom;
cout << "请输入学生姓名!" << endl;
cin >> name;
cout << "请输入学生学号!" << endl;
cin >> number;
cout << "请输入学生住址!" << endl;
cin >> address;
cout << "请输入学生高考分数!" << endl;
cin >> score;
cout << "请输入学生录取专业!" << endl;
cin >> major;
cout << "请输入学生所在班级!" << endl;
cin >> classroom;
Add_message << account << " " << name << " " << number << " " << address << " " << score << " " << major << " " << classroom << endl;
cout << "学生基本学籍信息录入完毕!" << endl;
Add_message.close();
cout << "正在为您录入学生信息!请输入学生期末成绩" << endl;
fstream Add_grades("学生期末成绩.txt", ios::out | ios::app);
if (!Add_grades.is_open())
{
cerr << "the file is not be opened!" << endl;
return;
}
cout << "请输入学生高等数学成绩!" << endl;
cin >> number;
cout << "请输入学生离散数学成绩!" << endl;
cin >> address;
cout << "请输入学生C++成绩!" << endl;
cin >> score;
cout << "请输入学生英语成绩!" << endl;
cin >> major;
cout << "请输入学生大学物理成绩!" << endl;
cin >> classroom;
Add_grades << account <<" "<< name << " " << number << " " << address << " " << score << " " << major <<" "<<classroom<< endl;
cout << "学生期末成绩录入完毕!" << endl;
Add_grades.close();
}
void ManageStudent::ReduceStudent()//直接注销学生账号,但信息保存,限制学生查看信息
{
string number;
cout << "正在为您注销学生账号!请输入您要删除学生的学号" << endl;
cin >> number;
fstream Reduce_account("学生账号密码.txt", ios::in);
if (!Reduce_account.is_open())
{
cerr << "the file is not be opened!" << endl;
return;
}
string buffer_account[Max], buffer_password[Max];
for (int i = 0; i < Max; i++)
{
buffer_account[i] = "#";
buffer_password[i] = "#";
}
string tempory_account, tempory_password;
int i = 0,j = 0;
while (Reduce_account >> tempory_account >> tempory_password,!Reduce_account.eof())
{
buffer_account[i++] = tempory_account;
buffer_password[j++] = tempory_password;
}
Reduce_account.close();
fstream Modify_account("学生账号密码.txt", ios::out);
bool flag1 = false;//账户存在性
for (int i = 0; buffer_account[i] != "#";i++)
if (buffer_account[i] == number) flag1 = true;
if (flag1)
{
for (int k = 0; buffer_account[k] != "#"; k++)
{
if (buffer_account[k] != number) Modify_account << buffer_account[k] << " " << buffer_password[k] << endl;
else continue;
}
cout << "账号注销成功!" << endl;
}
else
cout << "该学生不存在!" << endl;
}
void ManageStudent::InquireStudentMessage()
{
cout << "正在为您查询学生学籍信息!请输入学生学号" << endl;
string account;
cin >> account;
student_information s1(account);
s1.save_information();
}
void ManageStudent::InquireStudentGrades()
{
cout << "正在为您查询学生期末成绩!请输入学生学号" << endl;
string account;
cin >> account;
student_final_grades s1(account);
s1.save_grades();
}
#pragma once
#include"adiministrator manage student.h"
class administrator:public administrator_login,public ManageStudent
{
public:
void setAdministrator();//管理员登录
void administrator_menu();//管理员端操作菜单
void administrator_choice();//管理员端操作
};
static string AdministratorAccount, AdministratorPassword;//管理员账号密码变元
#include"administrator user.h"
bool Loginflag = false;
void administrator::administrator_menu()
{
cout << "\t\t\t\t\t1.管理员登录" << endl;
cout << "\t\t\t\t\t2.查询学生信息" << endl;
cout << "\t\t\t\t\t3.查询学生期末成绩" << endl;
cout << "\t\t\t\t\t4.录入新生学籍信息" << endl;
cout << "\t\t\t\t\t5.删除学生学籍信息" << endl;
}
void administrator::administrator_choice()
{
cout << "\t\t\t\t您已经成功进入管理员端,祝您使用愉快!" << endl;
administrator_menu();
int choice;
cout << "请选择您要进行的操作" << endl;
cin >> choice;
switch (choice)
{
case 1:
{
setAdministrator();
Loginflag = true;
system("pause");
system("cls");
administrator_choice();
break;
}
case 2:
{
if (!Loginflag)
{
cout << "请您先登录管理员账号" << endl;
system("pause");
system("cls");
administrator_choice();
}
InquireStudentMessage();
system("pause");
system("cls");
administrator_choice();
break;
}
case 3:
{
if (!Loginflag)
{
cout << "请您先登录管理员账号" << endl;
system("pause");
system("cls");
administrator_choice();
}
InquireStudentGrades();
system("pause");
system("cls");
administrator_choice();
break;
}
case 4:
{
if (!Loginflag)
{
cout << "请您先登录管理员账号" << endl;
system("pause");
system("cls");
administrator_choice();
}
AddStudent();
system("pause");
system("cls");
administrator_choice();
break;
}
case 5:
{
if (!Loginflag)
{
cout << "请您先登录管理员账号" << endl;
system("pause");
system("cls");
administrator_choice();
}
ReduceStudent();
system("pause");
system("cls");
administrator_choice();
break;
}
default:
system("cls"), administrator_choice();
}
}
void administrator::setAdministrator()
{
cout << "请输入您的管理员账号!" << endl;
cin >> AdministratorAccount;
cout << "请输入您的管理员密码!" << endl;
cin >> AdministratorPassword;
administrator_login A(AdministratorAccount, AdministratorPassword);
if (A.administrator_judge())
{
cout << "登录成功!" << endl;
Loginflag = true;
}
else cerr << "管理员账号或密码错误!" << endl;
}
#include"student user.h"
#include"administrator user.h"
void menu();
void user_choice();
int main()
{
user_choice();
return 0;
}
void menu()
{
cout << "\t\t\t\t欢迎使用学生信息管理系统!(系统创造者——皮皮晗)" << endl;
cout << "\t\t\t\t\t\t1.进入学生端" << endl;
cout << "\t\t\t\t\t\t2.进入管理员端" << endl;
cout << "请选择进入端口!" << endl;
}
void user_choice()
{
menu();
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
system("cls");
student s1;
s1.student_choice();
break;
}
case 2:
{
system("cls");
administrator a1;
a1.administrator_choice();
break;
}
default:
system("cls"), user_choice();
}
}
这是我写完简单贪吃蛇后的第二个小项目,总感觉不太满意,虽然有几个地方达到了预期效果,但离我的目标还很远!希望自己能在编程这条路上走得更远。遇到困难不要放弃,办法总比困难多,我们都可以!距离期末考还有三周,相信自己,我可以!