C++小項目:學生管理系統

此項目爲一個小型學生管理系統,僅供初學者學習交流使用,使用者可以在此項目中用已學的C++基礎知識進行實戰演練,加深對所學知識的瞭解。

  • 寫在前面:項目中的要點提煉。
    1、在每次進入最初登陸界面時,由於要再次加載文件內容,因此需先將list underst 和 list ad 中的內容使用clear()函數清空後再讀入。
    2、在讀取文件時,由於使用!infile.eof()函數會導致最後一行讀取兩次。因此,在讀文件循環內加入infile.get(),目的是在讀完一行後立即換行。
  • 代碼(一下代碼均在VS2017上編譯運行通過)
/*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類的實現*/
#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	//注意此處參數必須爲const類型,才能與STL中list的內置函數匹配
	{
		return ID == u.ID;
	}
	void set_password(string passw)
	{
		password = passw;
	}
};
/*UnderStudent.cpp 此文件爲UnderStudent類的實現*/
#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 << "* 學號:" << ID     <<endl ;
	cout << "* 性別:"  << sex   <<endl ;
	cout << "* 績點:"  << 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();					    //退出系統
	void understudent_functionshow();		//學生用戶功能界面
	void administer_functionshow();			//管理員功能界面
	void set_ad_account();					//設置管理員賬戶
	void enter_ad_account();				//管理員身份登陸
	void enter_underst_account();			//本科生身份登陸
	void save_undst();						//保存本科生數據
	void save_ad();							//保存管理員數據
	void load_undst();						//讀取本科生數據
	void load_ad();							//讀取管理員數據
	/*管理員功能*/
	void input_underst_info();				//錄入本科生信息
	void look_all_underst();				//查看所有本科生信息
	void look_underst_by_name(string name);			//根據姓名查看本科生信息
	void look_underst_by_id(string id);				//根據ID查看本科生信息
	void delete_underst_by_id(string id);			//根據ID刪除本科生信息
	/*本科生功能*/
	void change_password(string id);			//修改密碼 
};
/*System.cpp 此文件爲System類的實現*/
#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)退出系統!" << 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);
}

//退出系統
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 << "請選擇你要進行的操作:";
}


//管理員功能界面
void System::administer_functionshow()
{
	cout << "***************************" << endl;	
	cout << "1)查看所有學生信息!" << endl;
	cout << "2)按姓名查找學生信息!" << endl;
	cout << "3)按學號查找學生信息!" << endl;
	cout << "4)錄入學生信息" << endl;
	cout << "5)按學號刪除學生信息" << endl;
	cout << "6)返回上一級菜單!" << endl;
	cout << "*****************************" << endl;
	cout << "請選擇你要進行的操作:";
}


//設置管理員賬戶
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 << "兩次密碼不一致,請再次確認:";
		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;	//要查詢的學生的名字
	string udst_id;		//要查詢學生的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 << "請輸入要查詢學生的名字:";
			cin >> udst_name;
			look_underst_by_name(udst_name);
			break;
		case 3:
			cout << "請輸入要查詢學生的ID:";
			cin >> udst_id;
			look_underst_by_id(udst_id);
			break;
		case 4:
			input_underst_info();
			break;
		case 5:
			cout << "請輸入要刪除學生的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);
}

//保存管理員數據
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();
}

//保存本科生數據
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();
}

//讀取本科生數據
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;//讀取本科生總人數
	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;
	
}

//讀取管理員數據
void System::load_ad()
{
	ifstream infile("administer.dat");
	if (!infile)
	{
		cout << "無管理員資料!" << endl;
		return;
	}
	string name;
	string ID;
	string password;
	infile >> ad_count;//讀取管理員總人數
	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;
}

/*
管理員權限:
*/

//1)查看所有本科生信息
void System::look_all_underst()
{
	system("cls");
	if (underst.empty())
	{
		cout << "無本科生數據!" << endl;
		system("pause");
		return;
	}
	list<Understudent>::iterator iter;
	cout << "姓名" << "\t" << "ID" << "\t" << "\t" <<"性別"  << "\t" << "績點" << 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 << "學生總人數:" << underst_count << endl;
	system("pause");
}

//2)按姓名查看本科生數據
void System::look_underst_by_name(string udst_name)
{
	system("cls");
	if (underst.empty())
	{
		cout << "無本科生數據!" << 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" << "績點" << endl;
			cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
			//姓名可以重複,因此遍歷所有
		}
		if (iter == --underst.end())
		{
			system("pause");
			return;
		}
	}	
	cout << "無該生數據!" << endl;
	system("pause");
	return;
}

//3)按ID查看本科生數據
void System::look_underst_by_id(string udst_id)
{
	system("cls");
	if (underst.empty())
	{
		cout << "無本科生數據!" << 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" << "績點" << endl;
			cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
			system("pause");
			return;	//ID不能有重複
		}
	}
	cout << "無該生數據!" << endl;
	system("pause");
	return;
}

//4)錄入本科生信息
void System::input_underst_info()
{

	string name;
	string ID;
	string password;
	float grade;
	string sex;
	char s;	//是否繼續錄入flag
	do
	{
		system("cls");
		cout << endl << "請輸入學生姓名:";
		cin >> name;
		cout << endl << "請輸入學生ID:";
		cin >> ID;
		cout << endl << "請輸入學生初始密碼:";
		cin >> password;
		cout << endl << "請輸入學生績點:";
		cin >> grade;
		cout <<endl<< "請輸入學生性別:";
		cin >> sex;
		Understudent undst(name, ID, password, grade, sex);
		underst.push_back(undst);
		underst_count++;
		cout << endl << "是否繼續錄入?(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刪除學生信息
void System::delete_underst_by_id(string udst_id)
{
	system("cls");
	if (underst.empty())
	{
		cout << "無本科生數據!" << 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不能有重複
		}
	}
	cout << "無該生數據!" << endl;
	system("pause");
	return;
}


/*
本科生權限
*/
//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 此文件爲主函數所在文件*/
#include"System.h"
int main()
{
	System s;
	s.load_interface();
	system("pause");
	return 0;
}

代碼目錄結構
在這裏插入圖片描述
效果圖
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章