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

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

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

從上圖可以看到學(xué)生共有兩項(xiàng)功能。
查看個(gè)人信息修改密碼
需要注意的是,在登錄學(xué)生賬戶之前首先要登進(jìn)管理員系統(tǒng)創(chuàng)建學(xué)生信息之后才可以使用該賬戶登錄學(xué)生界面。
3. code(以下代碼均在VS2017上編譯運(yùn)行通過(guò))
/*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 << "* 賬號(hào):" << 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é)號(hào):" << ID <<endl ;
cout << "* 性別:" << sex <<endl ;
cout << "* 績(jī)點(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 << "請(qǐng)輸入操作:";
cin >> i;
while (i < 1 || i>4)
{
cout << "請(qǐng)輸入正確的序號(hào)!" << endl;
cout << "請(qǐng)重新輸入:";
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)查看個(gè)人信息" << endl;
cout << "2)修改密碼" << endl;
cout << "3)返回上一級(jí)菜單!" << endl;
cout << "*****************************" << endl;
cout << "請(qǐng)選擇你要進(jìn)行的操作:";
}
//管理員功能界面
void System::administer_functionshow()
{
cout << "***************************" << endl;
cout << "1)查看所有學(xué)生信息!" << endl;
cout << "2)按姓名查找學(xué)生信息!" << endl;
cout << "3)按學(xué)號(hào)查找學(xué)生信息!" << endl;
cout << "4)錄入學(xué)生信息" << endl;
cout << "5)按學(xué)號(hào)刪除學(xué)生信息" << endl;
cout << "6)返回上一級(jí)菜單!" << endl;
cout << "*****************************" << endl;
cout << "請(qǐng)選擇你要進(jìn)行的操作:";
}
//設(shè)置管理員賬戶
void System::set_ad_account()
{
string name;
string id;
string password;
string password2;
cout << endl<<"請(qǐng)輸入姓名:";
cin >> name;
cout << endl << "請(qǐng)輸入ID:";
cin >> id;
cout << endl << "請(qǐng)輸入密碼:";
cin >> password;
cout << endl << "請(qǐng)?jiān)俅屋斎朊艽a:";
cin >> password2;
while (password != password2)
{
cout << "兩次密碼不一致,請(qǐng)?jiān)俅未_認(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 << "請(qǐng)輸入賬戶:";
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 << "請(qǐng)輸入密碼:";
cin >> passw;
while (passw != iter->get_password())
{
cout << endl<<"密碼錯(cuò)誤,請(qǐng)重新輸入:";
cin >> passw;
}
cin.get();
int n;
do
{
system("cls");
administer_functionshow();
cin >> n;
while (n < 1 || n>6)
{
cout << "請(qǐng)輸入正確的選項(xiàng):";
cin >> n;
}
switch (n)
{
case 1:
look_all_underst();
break;
case 2:
cout << "請(qǐng)輸入要查詢學(xué)生的名字:";
cin >> udst_name;
look_underst_by_name(udst_name);
break;
case 3:
cout << "請(qǐng)輸入要查詢學(xué)生的ID:";
cin >> udst_id;
look_underst_by_id(udst_id);
break;
case 4:
input_underst_info();
break;
case 5:
cout << "請(qǐng)輸入要?jiǎng)h除學(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 << "請(qǐng)輸入賬戶:";
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 << "請(qǐng)輸入密碼:";
cin >> passw;
while (passw != iter->get_password())
{
cout << endl << "密碼錯(cuò)誤,請(qǐng)重新輸入:";
cin >> passw;
}
int n;
do
{
system("cls");
understudent_functionshow();
cin >> n;
while (n < 1 || n>3)
{
cout << endl << "請(qǐng)輸入正確的操作:";
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 << "無(wú)本科生資料!" << 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 << "無(wú)管理員資料!" << 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 << "無(wú)本科生數(shù)據(jù)!" << endl;
system("pause");
return;
}
list<Understudent>::iterator iter;
cout << "姓名" << "\t" << "ID" << "\t" << "\t" <<"性別" << "\t" << "績(jī)點(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 << "無(wú)本科生數(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" << "績(jī)點(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 << "無(wú)該生數(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 << "無(wú)本科生數(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" << "績(jī)點(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 << "無(wú)該生數(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 << "請(qǐng)輸入學(xué)生姓名:";
cin >> name;
cout << endl << "請(qǐng)輸入學(xué)生ID:";
cin >> ID;
cout << endl << "請(qǐng)輸入學(xué)生初始密碼:";
cin >> password;
cout << endl << "請(qǐng)輸入學(xué)生績(jī)點(diǎn):";
cin >> grade;
cout <<endl<< "請(qǐng)輸入學(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 << "請(qǐng)輸入正確操作(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 << "無(wú)本科生數(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 << "無(wú)該生數(shù)據(jù)!" << endl;
system("pause");
return;
}
/*
本科生權(quán)限
*/
//2)修改密碼
void System::change_password(string id)
{
string password, passw;
cout << "請(qǐng)輸入新密碼:";
cin >> password;
cout <<endl<<"請(qǐng)?jiān)俅屋斎?";
cin >> passw;
while (password != passw)
{
cout << endl<<"兩次密碼不一致,請(qǐng)重新輸入:";
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)容請(qǐng)搜索腳本之家以前的文章或繼續(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)簡(jiǎn)單學(xué)生管理系統(tǒng)
- C++實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- C++基礎(chǔ)學(xué)生管理系統(tǒng)
- C++實(shí)現(xiàn)簡(jiǎn)單的學(xué)生管理系統(tǒng)
- C++實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理系統(tǒng)
相關(guān)文章
C++中關(guān)鍵字 override 的簡(jiǎn)析
這篇小文來(lái)聊聊 C++中的關(guān)鍵字 override,它的含義其實(shí)兩句話就說(shuō)完了,但為了敘述的完整性,讓我們從虛函數(shù)說(shuō)起。感興趣的小伙伴可以跟著小編一起學(xué)習(xí)下面文章內(nèi)容2021-09-09
VS2019配置opencv詳細(xì)圖文教程和測(cè)試代碼的實(shí)現(xiàn)
這篇文章主要介紹了VS2019配置opencv詳細(xì)圖文教程和測(cè)試代碼的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
C語(yǔ)言解決青蛙跳臺(tái)階問(wèn)題(升級(jí)版)
所謂的青蛙跳臺(tái)階問(wèn)題,就是指一只青蛙一次可以跳上1級(jí)臺(tái)階,也可以跳上2級(jí)。求該青蛙跳上一個(gè)n級(jí)的臺(tái)階總共有多少種跳法。本文將用C語(yǔ)言解決這一問(wèn)題,需要的可以參考一下2022-01-01
C++小游戲教程之猜數(shù)游戲的實(shí)現(xiàn)
這篇文章主要和大家詳細(xì)介紹如何利用C++做一個(gè)簡(jiǎn)易的猜數(shù)游戲,分為用戶猜數(shù)和系統(tǒng)猜數(shù)。文中的示例代碼講解詳細(xì) ,感興趣的小伙伴可以嘗試一下2022-11-11

