C++實(shí)現(xiàn)學(xué)生管理系統(tǒng)示例解析
此項目為一個小型學(xué)生管理系統(tǒng),僅供初學(xué)者學(xué)習(xí)交流使用,使用者可以在此項目中用已學(xué)的C++基礎(chǔ)知識進(jìn)行實(shí)戰(zhàn)演練,加深對所學(xué)知識的了解。
可以從github直接克隆項目(項目地址)
1. 項目要點(diǎn)
1、在每次進(jìn)入最初登陸界面時,由于要再次加載文件內(nèi)容,因此需先將list underst 和 list ad 中的內(nèi)容使用clear()函數(shù)清空后再讀入。
2、在讀取文件時,由于使用!infile.eof()函數(shù)會導(dǎo)致最后一行讀取兩次。因此,在讀文件循環(huán)內(nèi)加入infile.get(),目的是在讀完一行后立即換行。
2. 功能介紹

從上圖可以看出,此項目主要有三個功能模塊:開通管理員賬戶、管理員功能界面(管理員身份登錄)、本科生功能界面(本科生省份登錄)。第一次運(yùn)行本項目時,首先要開通管理員賬戶才能進(jìn)行其他的操作。
2.1 管理員功能界面

從上圖可以看到管理員共有五項功能。
- 查看所有學(xué)生信息
- 按姓名查看學(xué)生信息
- 按學(xué)號查看學(xué)生信息錄
- 入學(xué)生信息按學(xué)號
- 刪除學(xué)生信息
2.2 學(xué)生功能界面

從上圖可以看到學(xué)生共有兩項功能。
查看個人信息修改密碼
需要注意的是,在登錄學(xué)生賬戶之前首先要登進(jìn)管理員系統(tǒng)創(chuàng)建學(xué)生信息之后才可以使用該賬戶登錄學(xué)生界面。
3. code(以下代碼均在VS2017上編譯運(yùn)行通過)
/*Administer.h 此文件為Administer類的頭文件*/
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Administer
{
private:
string name;
string ID;
string password;
public:
Administer() {}
Administer(string na, string id, string passw);
string get_id(){return ID;}
string get_name(){return name;}
string get_password(){ return password; }
void display();
};
/*Administer.cpp 此文件為Administer類的實(shí)現(xiàn)*/
#include"Administer.h"
Administer::Administer(string na, string id, string passw) :name(na), ID(id), password(passw)
{}
void Administer::display()
{
cout << endl << "******************" << endl;
cout << endl << "* 姓名:" << name;
cout << endl << "* 賬號:" << ID;
cout << endl << "******************" << endl;
}
/*UnderStudent.h 此文件為UnderStuent類的頭文件*/
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Understudent
{
private:
string name;
string ID;
string password;
float grade;
string sex;
public:
Understudent() {}
Understudent(string na, string id, string passw, float gra, string s);
string get_name(){return name;}
string get_id(){return ID;}
string get_password(){return password;}
float get_grade() { return grade; }
string get_sex() { return sex; }
void display();
bool operator == (const Understudent &u)const //注意此處參數(shù)必須為const類型,才能與STL中l(wèi)ist的內(nèi)置函數(shù)匹配
{
return ID == u.ID;
}
void set_password(string passw)
{
password = passw;
}
};
/*UnderStudent.cpp 此文件為UnderStudent類的實(shí)現(xiàn)*/
#include"UnderStudent.h"
Understudent::Understudent(string na, string id, string passw, float gra, string s):name(na),ID(id),password(passw),grade(gra), sex(s)
{}
void Understudent::display()
{
cout << "******************"<< endl;
cout << "* 姓名:" << name << endl;
cout << "* 學(xué)號:" << ID <<endl ;
cout << "* 性別:" << sex <<endl ;
cout << "* 績點(diǎn):" << grade << endl;
cout << "******************"<< endl;
}
/*System.h 此文件為System的頭文件*/
#pragma once
#include<list>
#include<fstream>
#include"UnderStudent.h"
#include"Administer.h"
class System
{
private:
list<Understudent> underst;
list<Administer> ad;
static int underst_count;
static int ad_count;
public:
virtual void load_interface(); //登陸界面
void exit_system(); //退出系統(tǒng)
void understudent_functionshow(); //學(xué)生用戶功能界面
void administer_functionshow(); //管理員功能界面
void set_ad_account(); //設(shè)置管理員賬戶
void enter_ad_account(); //管理員身份登陸
void enter_underst_account(); //本科生身份登陸
void save_undst(); //保存本科生數(shù)據(jù)
void save_ad(); //保存管理員數(shù)據(jù)
void load_undst(); //讀取本科生數(shù)據(jù)
void load_ad(); //讀取管理員數(shù)據(jù)
/*管理員功能*/
void input_underst_info(); //錄入本科生信息
void look_all_underst(); //查看所有本科生信息
void look_underst_by_name(string name); //根據(jù)姓名查看本科生信息
void look_underst_by_id(string id); //根據(jù)ID查看本科生信息
void delete_underst_by_id(string id); //根據(jù)ID刪除本科生信息
/*本科生功能*/
void change_password(string id); //修改密碼
};
/*System.cpp 此文件為System類的實(shí)現(xiàn)*/
#include"System.h"
int System::underst_count = 0;
int System::ad_count = 0;
//登陸界面
void System::load_interface()
{
int i;
do
{
system("cls");
load_ad();
load_undst();
cout << "********************" << endl;
cout << "1)開通管理員賬戶!" << endl;
cout << "2)管理員身份登陸!" << endl;
cout << "3)本科生身份登陸!" << endl;
cout << "4)退出系統(tǒng)!" << endl;
cout << "********************" << endl;
cout << "請輸入操作:";
cin >> i;
while (i < 1 || i>4)
{
cout << "請輸入正確的序號!" << endl;
cout << "請重新輸入:";
cin >> i;
}
switch (i)
{
case 1:
set_ad_account();
break;
case 2:
enter_ad_account();
break;
case 3:
enter_underst_account();
break;
case 4:
exit_system();
break;
default:
break;
}
//cin.get();
} while (true);
}
//退出系統(tǒng)
void System::exit_system()
{
cout << "****************感謝使用!******************" << endl;
exit(0);
}
//本科生功能界面
void System::understudent_functionshow()
{
cout << "***************************" << endl;
cout << "1)查看個人信息" << endl;
cout << "2)修改密碼" << endl;
cout << "3)返回上一級菜單!" << endl;
cout << "*****************************" << endl;
cout << "請選擇你要進(jìn)行的操作:";
}
//管理員功能界面
void System::administer_functionshow()
{
cout << "***************************" << endl;
cout << "1)查看所有學(xué)生信息!" << endl;
cout << "2)按姓名查找學(xué)生信息!" << endl;
cout << "3)按學(xué)號查找學(xué)生信息!" << endl;
cout << "4)錄入學(xué)生信息" << endl;
cout << "5)按學(xué)號刪除學(xué)生信息" << endl;
cout << "6)返回上一級菜單!" << endl;
cout << "*****************************" << endl;
cout << "請選擇你要進(jìn)行的操作:";
}
//設(shè)置管理員賬戶
void System::set_ad_account()
{
string name;
string id;
string password;
string password2;
cout << endl<<"請輸入姓名:";
cin >> name;
cout << endl << "請輸入ID:";
cin >> id;
cout << endl << "請輸入密碼:";
cin >> password;
cout << endl << "請再次輸入密碼:";
cin >> password2;
while (password != password2)
{
cout << "兩次密碼不一致,請再次確認(rèn):";
cin >> password2;
}
Administer adm(name, id, password);
ad.push_back(adm);
cout << "開戶成功!" << endl;
cin.get();
ad_count++;
save_ad();
}
//管理員身份登陸
void System::enter_ad_account()
{
string udst_name; //要查詢的學(xué)生的名字
string udst_id; //要查詢學(xué)生的ID
string id;
string passw;
list<Administer>::iterator iter;
cout << "請輸入賬戶:";
cin >> id;
int flag = 1;
for (iter = ad.begin(); iter != ad.end(); iter++)
{
if (id == iter->get_id())
{
flag = 0;
break;
}
}
if (flag)
{
cout << endl<<"賬戶不存在!" << endl;
return;
}
cout << endl << "請輸入密碼:";
cin >> passw;
while (passw != iter->get_password())
{
cout << endl<<"密碼錯誤,請重新輸入:";
cin >> passw;
}
cin.get();
int n;
do
{
system("cls");
administer_functionshow();
cin >> n;
while (n < 1 || n>6)
{
cout << "請輸入正確的選項:";
cin >> n;
}
switch (n)
{
case 1:
look_all_underst();
break;
case 2:
cout << "請輸入要查詢學(xué)生的名字:";
cin >> udst_name;
look_underst_by_name(udst_name);
break;
case 3:
cout << "請輸入要查詢學(xué)生的ID:";
cin >> udst_id;
look_underst_by_id(udst_id);
break;
case 4:
input_underst_info();
break;
case 5:
cout << "請輸入要刪除學(xué)生的ID:";
cin >> udst_id;
delete_underst_by_id(udst_id);
break;
case 6:
return;
break;
default:
break;
}
} while (1);
}
//本科生身份登陸
void System::enter_underst_account()
{
list<Understudent>::iterator iter;
string id;
string passw;
cout << "請輸入賬戶:";
cin >> id;
int flag = 1;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (id == iter->get_id())
{
flag = 0;
break;
}
}
if (flag)
{
cout << endl << "賬戶不存在!" << endl;
return;
}
cout << endl << "請輸入密碼:";
cin >> passw;
while (passw != iter->get_password())
{
cout << endl << "密碼錯誤,請重新輸入:";
cin >> passw;
}
int n;
do
{
system("cls");
understudent_functionshow();
cin >> n;
while (n < 1 || n>3)
{
cout << endl << "請輸入正確的操作:";
cin >> n;
}
system("cls");
switch (n)
{
case 1:
iter->display();
break;
case 2:
change_password(id);
break;
case 3:
return;
break;
default:
break;
}
system("pause");
} while (true);
}
//保存管理員數(shù)據(jù)
void System::save_ad()
{
ofstream outfile("administer.dat",ios::out);
list<Administer>::iterator iter;
outfile << ad_count << endl;
for (iter = ad.begin(); iter != ad.end(); iter++)
{
outfile << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_password() << endl;
}
outfile.close();
}
//保存本科生數(shù)據(jù)
void System::save_undst()
{
ofstream outfile("understudent.dat",ios::out);
list<Understudent>::iterator iter;
outfile << underst_count << endl;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
outfile << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_password() << "\t" << iter->get_grade() << "\t"
<< iter->get_sex() << endl;
}
outfile.close();
}
//讀取本科生數(shù)據(jù)
void System::load_undst()
{
ifstream infile("understudent.dat");
if (!infile)
{
cout << "無本科生資料!" << endl;
return;
}
string name;
string ID;
string password;
float grade;
string sex;
infile >> underst_count;//讀取本科生總?cè)藬?shù)
infile.get();
if (!underst.empty())
underst.clear();
while (!infile.eof() && infile.peek() != EOF)
{
infile >> name >> ID >> password >> grade >> sex;
Understudent undst(name, ID, password, grade, sex);
underst.push_back(undst);
infile.get();
}
infile.close();
cout << "讀取本科生資料正常。" << endl;
}
//讀取管理員數(shù)據(jù)
void System::load_ad()
{
ifstream infile("administer.dat");
if (!infile)
{
cout << "無管理員資料!" << endl;
return;
}
string name;
string ID;
string password;
infile >> ad_count;//讀取管理員總?cè)藬?shù)
infile.get();
if (!ad.empty())
ad.clear();
while (!infile.eof()||infile.peek()!=EOF)
{
infile >> name >> ID >> password;
Administer adm(name, ID, password);
ad.push_back(adm);
infile.get();
}
infile.close();
cout << "讀取管理員資料正常。" << endl;
}
/*
管理員權(quán)限:
*/
//1)查看所有本科生信息
void System::look_all_underst()
{
system("cls");
if (underst.empty())
{
cout << "無本科生數(shù)據(jù)!" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
cout << "姓名" << "\t" << "ID" << "\t" << "\t" <<"性別" << "\t" << "績點(diǎn)" << endl;
for (iter = underst.begin(); iter != underst.end(); iter++)
cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
cout << endl << "學(xué)生總?cè)藬?shù):" << underst_count << endl;
system("pause");
}
//2)按姓名查看本科生數(shù)據(jù)
void System::look_underst_by_name(string udst_name)
{
system("cls");
if (underst.empty())
{
cout << "無本科生數(shù)據(jù)!" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_name() == udst_name)
{
cout << "姓名" << "\t" << "ID" << "\t" << "\t" << "性別" << "\t" << "績點(diǎn)" << endl;
cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
//姓名可以重復(fù),因此遍歷所有
}
if (iter == --underst.end())
{
system("pause");
return;
}
}
cout << "無該生數(shù)據(jù)!" << endl;
system("pause");
return;
}
//3)按ID查看本科生數(shù)據(jù)
void System::look_underst_by_id(string udst_id)
{
system("cls");
if (underst.empty())
{
cout << "無本科生數(shù)據(jù)!" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_id()==udst_id)
{
cout << "姓名" << "\t" << "ID" << "\t" << "\t" << "性別" << "\t" << "績點(diǎn)" << endl;
cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
system("pause");
return; //ID不能有重復(fù)
}
}
cout << "無該生數(shù)據(jù)!" << endl;
system("pause");
return;
}
//4)錄入本科生信息
void System::input_underst_info()
{
string name;
string ID;
string password;
float grade;
string sex;
char s; //是否繼續(xù)錄入flag
do
{
system("cls");
cout << endl << "請輸入學(xué)生姓名:";
cin >> name;
cout << endl << "請輸入學(xué)生ID:";
cin >> ID;
cout << endl << "請輸入學(xué)生初始密碼:";
cin >> password;
cout << endl << "請輸入學(xué)生績點(diǎn):";
cin >> grade;
cout <<endl<< "請輸入學(xué)生性別:";
cin >> sex;
Understudent undst(name, ID, password, grade, sex);
underst.push_back(undst);
underst_count++;
cout << endl << "是否繼續(xù)錄入?(Y/N)";
cin >> s;
while (s != 'Y'&&s != 'N'&&s != 'y'&&s != 'n')
{
cout << endl << "請輸入正確操作(Y/N):";
cin >> s;
}
} while (s == 'Y'||s=='y');
save_undst();
}
//5)按ID刪除學(xué)生信息
void System::delete_underst_by_id(string udst_id)
{
system("cls");
if (underst.empty())
{
cout << "無本科生數(shù)據(jù)!" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
string name;
string ID;
string password;
float grade;
string sex;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_id() == udst_id)
{
name = iter->get_name();
ID = iter->get_id();
password = iter->get_password();
grade = iter->get_grade();
sex = iter->get_sex();
Understudent undst(name, ID, password, grade, sex);
underst.remove(undst);
underst_count--;
cout << "刪除成功!" << endl;
system("pause");
save_undst();
return; //ID不能有重復(fù)
}
}
cout << "無該生數(shù)據(jù)!" << endl;
system("pause");
return;
}
/*
本科生權(quán)限
*/
//2)修改密碼
void System::change_password(string id)
{
string password, passw;
cout << "請輸入新密碼:";
cin >> password;
cout <<endl<<"請再次輸入:";
cin >> passw;
while (password != passw)
{
cout << endl<<"兩次密碼不一致,請重新輸入:";
cin >> passw;
}
list<Understudent>::iterator iter;
for (iter = underst.begin(); iter != underst.end(); iter++)
{
if (iter->get_id() == id)
break;
}
iter->set_password(password);
cout << "修改密碼成功!" << endl;
save_undst();
}
/*mai.cpp 此文件為主函數(shù)所在文件*/
#include"System.h"
int main()
{
System s;
s.load_interface();
system("pause");
return 0;
}
代碼目錄結(jié)構(gòu)

到此這篇關(guān)于C++實(shí)現(xiàn)學(xué)生管理系統(tǒng)示例解析的文章就介紹到這了,更多相關(guān)C++學(xué)生管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++使用文件實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
- C++實(shí)現(xiàn)學(xué)生考勤信息管理系統(tǒng)
- C++實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
- linux下C/C++學(xué)生信息管理系統(tǒng)
- C++學(xué)生信息管理系統(tǒng)
- C++實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)
- C++實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- C++基礎(chǔ)學(xué)生管理系統(tǒng)
- C++實(shí)現(xiàn)簡單的學(xué)生管理系統(tǒng)
- C++實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng)
相關(guān)文章
c++ signal實(shí)現(xiàn)發(fā)送信號
這篇文章主要為大家詳細(xì)介紹了c++ signal實(shí)現(xiàn)發(fā)送信號的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
C++ windows LOG4plus的使用小結(jié)
這篇文章主要介紹了C++ windows LOG4plus的使用小結(jié),本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
一文詳解如何實(shí)現(xiàn)QT的多語言切換(靜態(tài)+動態(tài))
這篇文章主要給大家介紹了關(guān)于如何實(shí)現(xiàn)QT的多語言切換(靜態(tài)+動態(tài))的相關(guān)資料,Qt是一款跨平臺的C++應(yīng)用程序開發(fā)框架,提供了一套豐富的工具和類庫來簡化應(yīng)用程序開發(fā),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
VS報錯C6011的問題:取消對NULL指針的引用(解決方法)

