C++학생 관리 시스템 예시 분석 실현

이 프로젝트 는 작은 학생 관리 시스템 으로 초보 자 들 이 교 류 를 배 워 서 사용 할 수 있 고 사용 자 는 이 프로젝트 에서 이미 배 운 C+기초 지식 으로 실전 연습 을 하여 배 운 지식 에 대한 이 해 를 강화 할 수 있다.
github 에서 직접 복제 할 수 있 는 프로젝트(항목 주소)
1.프로젝트 요점
1.최초 로그 인 인터페이스 에 들 어 갈 때마다 파일 내용 을 다시 불 러 오 려 면 list unders 와 list ad 의 내용 을 clear()함수 로 비 운 다음 읽 어야 합 니 다.
2.파일 을 읽 을 때 사용 하기 때문에!infile.eof()함 수 는 마지막 줄 을 두 번 읽 게 합 니 다.따라서 파일 순환 에 infile.get()을 추가 하 는 것 은 한 줄 을 읽 은 후 바로 줄 을 바 꾸 는 것 이 목적 입 니 다.
2.기능 소개
在这里插入图片描述
위의 그림 에서 알 수 있 듯 이 이 프로젝트 는 주로 세 가지 기능 모듈 이 있다.관리자 계 정 개설,관리자 기능 인터페이스(관리자 신분 로그 인),학부생 기능 인터페이스(학부생 성 로그 인)이다.이 프로젝트 를 처음 실행 할 때 먼저 관리자 계 정 을 개설 해 야 다른 작업 을 할 수 있 습 니 다.
2.1 관리자 기능 인터페이스
在这里插入图片描述
위의 그림 에서 관리 자 는 모두 다섯 가지 기능 을 가지 고 있 음 을 볼 수 있다.
모든 학생 정보 보기
이름 으로 학생 정 보 를 조회 하 다.
학생 정보 록
입 학생 정 보 는 학 번 별로학생 정보 삭제
2.2 학생 기능 인터페이스
在这里插入图片描述
위의 그림 에서 학생 들 이 모두 두 가지 기능 을 가지 고 있 음 을 볼 수 있다.
개인 정보 보기 비밀번호 변경
주의해 야 할 것 은 학생 계 정 에 로그 인하 기 전에 먼저 관리자 시스템 에 등록 하여 학생 정 보 를 만 든 후에 야 이 계 정 으로 학생 인터페이스 에 로그 인 할 수 있다 는 것 이다.
3.code(아래 코드 는 모두 VS 2017 에서 컴 파일 실행 통과)

/*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;
}
코드 디 렉 터 리 구조
在这里插入图片描述
여기 서 C++학생 관리 시스템 의 예시 해석 을 실현 하 는 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 C++학생 관리 시스템 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기