zoukankan      html  css  js  c++  java
  • 学生信息管理系统 文件存储【C++】

    整合后的部分源码:

    #include <iostream>
    #include <list>
    #include <string.h>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    
    class Course{
    
    public:
    	Course(size_t id,char name[]);
    	~Course();
    	size_t getId();
    	char* getName();
    
    	void setId(size_t id);
    	void setName(char name[]);
    
    	string toString();
    
    private : 
    	size_t id;		//course id
    	char name[20];	//course name
    };
    
    
    class Grand{
    
    public:
    	Grand();
    	Grand(size_t id,char* name);
    	~Grand();
    	size_t getId();
    	size_t getSize();
    	char* getName();
    	void getCompulsoryCourse(size_t ccId[]);
    	void getOptionalCourse(size_t ocId[]);
    	
    	void setCompulsoryCourse(size_t ccId);
    	void setOptionalCourse(size_t ocId);
    	void setName(char name[]);
    
    
    private:
    	size_t id;
    	char name[20];
    
    	size_t ccId[20];	//CompulsoryCourse id
    	size_t ocId[20];	//OptionalCourse id
    
    };
    
    
    class Student{
    public:
    	Student(size_t id ,char passwd[], char name[], char sex, size_t age, size_t gId);
    	~Student();
    
    	string toString();
    	size_t getId();				
    	char* getPasswd();		    
    	char* getName();			
    	char getSex();			    
    	size_t getAge();			    
    	size_t getGId();	        
    	void getCourses(size_t courseId[2][20]);
    
    	void setId(size_t id);				
    	void setPasswd(char passwd[]);		    
    	void setName(char name[]);			
    	void setSex(char sex);			    
    	void setAge(size_t age);			    
    	void setGId(size_t gId);	        
    	void setCourses(size_t cid); 
    	void setGrade(size_t cid,size_t grade);
    
    private:
    	size_t id;				//student id
    	char passwd[20];		    //password
    	char name[20];			//name
    	char sex;			    //sex
    	size_t age;			    //age
    	size_t gId;			    //grand id
    	size_t courseId[2][20]; //A maximum of 20 courses can be selected, with 0 lines for course id and 1 line for course score
    
    };
    
    
    class Teacher{
    public:
    	Teacher(size_t id, char name[], char sex, size_t age);
    	~Teacher();
    
    	size_t getId();
    	char* getName();
    	char getSex();
    	size_t getAge();
    
    	void setId(size_t id);
    	void setName(char name[]);
    	void setSex(char sex);
    	void setAge(size_t age);
    
    	string toString();
    
    private:
    	size_t id;		//teacher id
    	char name[20];	//teacher name
    	char sex;		//teacher sex
    	size_t age;		//teacher age
    };
    
    class Admin{
    
    public:
    	Admin();
    	//Grand Manager Block
    	void showGrandAll();
    	Grand* selectGrand(size_t id);
    	void addGrand();
    	void modifyGrand(size_t id);
    	void deleteGrand(size_t id);
    
    	//Teacher Manager Block
    	void showTeacherAll();
    	Teacher* selectTeacher(size_t id);
    	void addTeacher();
    	void modifyTeacher(size_t id);
    	void deleteTeacher(size_t id);
    
    	//Course Manager Block
    	void showCourseAll();
    	Course* selectCourse(size_t id);
    	void addCourse();
    	void modifyCourse(size_t id);
    	void deleteCourse(size_t id);
    
    	//Students choose course ,  directly according to the class id
    	void chooseCourse(size_t id);
    
    	//Student Manager Block
    	void showStudentOfGrand(size_t id);
    	Student* selectStudent(size_t id);
    	void addStudent();
    	void modifyStudent(size_t id);
    	void deleteStudent(size_t id);
    
    	//Grade Manager Block  Modify student grades according to student id
    	void modifyGrade(size_t id);
    
    	//load
    	void load1();
    	void load2();
    	void load3();
    	void load4();
    
    	//save
    	void save1();
    	void save2();
    	void save3();
    	void save4();
    
    private:
    	list<Student> students;		//student list
    	list<Teacher> teachers;		//teacher list
    	list<Grand> grands;			//grand list
    	list<Course> courses;		//course list
    	
    };
    
    class User{
    
    public:
    	User();
    	~User();
    	int login();
    	Student* selectStudent(size_t id);
    
    	void modifyPasswd();//modify passwd
    	void showCourse();//Show current course
    	void scanCourse();//Elective browsing
    	void chooseCourse(size_t cid);//Personal course selection, parameters: course id
    	void showGrade();//Show personal grades
    	void personMessage();//Show Personal information
    
    	Course* selectCourse(size_t id);
    	Grand* selectGrand(size_t id);
    
    	Student* getStudent();
    	void setStudent(Student* student);
    
    	void load1();
    	void load2();
    	void load3();
    	void load4();
    
    	void save1();
    	void save2();
    	void save3();
    	void save4();
    
    private:
    	list<Student> students;
    	list<Teacher> teachers;
    	list<Grand> grands;
    	list<Course> courses;
    	Student* student;
    
    };
    
    
    
    
    
    Admin :: Admin(){
    }
    
    void Admin:: showGrandAll(){
    	list<Grand> :: iterator it = grands.begin();
    	while(it!=grands.end()){
    		size_t cc[20];
    		it->getCompulsoryCourse(cc);
    		cout<<"id: "<<it->getId()<<", "<<it->getName()<<endl;
    		cout<<"CompulsoryCourse:";
    		for(int i=0;i<20;i++){
    			if(cc[i]!=0&&selectCourse(cc[i])){	
    				cout<<cc[i]<<" : "<<selectCourse(cc[i])->getName()<<", ";
    			}
    		}
    		cout<<endl;
    		it++;
    	}
    }
    
    Grand* Admin:: selectGrand(size_t id){
    	list<Grand> :: iterator it = grands.begin();;
    	while(it!=grands.end()){
    		if(it->getId()==id){
    			return &*it;
    		}
    		it++;
    	}
    	return NULL;
    }
    
    void Admin:: addGrand(){  
    	size_t id=1501;
    	char name[20];
    	size_t size=0;
    	
    	if(!grands.empty()){
    		id=grands.back().getId()+1;
    	}
    	cout<<"Input grand name: ";
    	cin>>name;
    	
    	Grand* grand = new Grand( id,name);
    	grands.push_back(*grand);
    	return;
    }
    
    void Admin:: modifyGrand(size_t id){
    	list<Grand> :: iterator it = grands.begin();
    	char name[20];
    	while(it!=grands.end()){
    		if(it->getId()==id){
    			cout<<"Input new name: ";
    			cin>>name;
    			it->setName(name);
    			cout<<"success"<<endl;
    			return;
    		}
    		it++;
    	}
    	cout<<"Not find this grand. "<<endl;
    	return;
    }
    
    void Admin:: deleteGrand(size_t id){
    	list<Grand> :: iterator it = grands.begin();
    	string name;
    	while(it!=grands.end()){
    		if(it->getId()==id){
    			grands.erase(it++);
    			cout<<"Delete success";
    			getchar();
    			return;
    		}
    		it++;
    	}
    	cout<<"Not find this grand";
    	getchar();
    	return;
    }
    
    
    //teacher manager
    void Admin:: showTeacherAll(){
    	list<Teacher> :: iterator it = teachers.begin();
    	while(it!=teachers.end()){
    		cout<<it++->toString()<<endl;
    	}
    }
    
    Teacher* Admin:: selectTeacher(size_t id){
    	list<Teacher> :: iterator it = teachers.begin();;
    	while(it!=teachers.end()){
    		if(it->getId()==id){
    			return &*it;
    		}
    		it++;
    	}
    	return NULL;
    }
    
    void Admin:: addTeacher(){
    	size_t id=10150101;
    	char name[20];
    	char sex;
    	size_t age;
    	if(!teachers.empty()){
    		id=teachers.back().getId()+1;
    	}
    	cout<<"input teacher message:( name, sex, age )";
    	cin>>name>>sex>>age;
    	Teacher* teacher = new Teacher(id,name,sex,age);
    	teachers.push_back(*teacher);
    	cout<<"success"<<endl;
    }
    
    void Admin:: modifyTeacher(size_t id){
    	list<Teacher> :: iterator it = teachers.begin();
    	char name[20];
    	char sex;
    	size_t age;
    	while(it!=teachers.end()){
    		if(it->getId()==id){
    			cout<<"input new massage: (name, sex, age)";
    			cin>>name;
    			cin>>sex;
    			cin>>age;
    			it->setName(name);
    			it->setSex(sex);
    			it->setAge(age);
    			cout<<"modify success"<<endl;
    			return;
    		}
    		it++;
    	}
    	cout<<"Not find"<<endl;
    	return;
    }
    
    void Admin:: deleteTeacher(size_t id){
    	list<Teacher> :: iterator it=teachers.begin();
    	string name;
    	while(it!=teachers.end()){
    		if(it->getId()==id){
    			teachers.erase(it++);
    			cout<<"delete success"<<endl;
    			return;
    		}
    		it++;
    	}
    	cout<<"Not find"<<endl;
    	return;
    }
    
    //course manager
    void Admin:: showCourseAll(){
    	list<Course> :: iterator it = courses.begin();
    	while(it!=courses.end()){
    		cout<<"id: "+to_string(it->getId())+" name: "+it->getName()<<endl;
    		it++;
    	}
    	
    }
    
    Course* Admin:: selectCourse(size_t id){
    	list<Course> :: iterator it = courses.begin();
    	while(it!=courses.end()){
    		if(it->getId()==id){
    			return &*it;
    		}
    		it++;
    	}
    	return NULL;
    }
    
    void Admin:: addCourse(){
    	size_t id = 8001;
    	char name[20];
    	if(!courses.empty()){
    		id=courses.back().getId()+1;
    	}
    	cout<<"Input course message name:";
    	cin>>name;
    	Course* course = new Course(id,name);
    	cout<<course->getName()<<endl;
    	
    	courses.push_back(*course);
    	cout<<"Add success"<<endl;
    	
    }
    
    void Admin:: modifyCourse(size_t id){
    	list<Course> :: iterator it = courses.begin();
    	char name[20];
    
    	while(it!=courses.end()){
    		if(it->getId()==id){
    			cout<<"Input new name: ";
    			cin>>name;
    			it->setName(name);
    			cout<<"Modify success"<<endl;
    			return;
    		}
    		it++;
    	}
    	cout<<"Not find"<<endl;
    	return;
    }
    
    void Admin:: deleteCourse(size_t id){
    	list<Course> :: iterator it=courses.begin();
    	string name;
    	while(it!=courses.end()){
    		if(it->getId()==id){
    			courses.erase(it++);
    			cout<<"delete success"<<endl;
    			return;
    		}
    		it++;
    	}
    	cout<<"Not find"<<endl;
    	return;
    }
    
    
    //Choose course by id
    void Admin:: chooseCourse(size_t id){
    	Grand* grand = selectGrand(id);
    	if(!grand){
    		return;
    	}
    	size_t cid;
    	cout<<"course list : ";
    	showCourseAll();
    	cout<<"Choose Course:";
    	cin>>cid;
    	grand->setCompulsoryCourse(cid);
    }
    
    //Student Manager
    //Displays all students in the class by the class id
    void Admin:: showStudentOfGrand(size_t id){
    	
    	list<Student> :: iterator it = students.begin();
    	while(it!=students.end()){
    		if(it->getGId()==id){
    			cout<<it->toString()<<endl;
    		}
    		it++;
    	}
    }
    
    //Returns the student by the student id
    Student* Admin:: selectStudent(size_t id){
    	list<Student> :: iterator it = students.begin();
    
    	while(it!=students.end()){
    		if(it->getId()==id){
    			return &*it;
    		}
    		it++;
    	}
    	return NULL;
    }
    
    
    void Admin:: addStudent(){
    	size_t id = 150101;
    	char passwd[20];
    	char name[20];
    	char sex;
    	size_t age;
    	size_t gId=0;
    
    	if(!students.empty()){
    		id=students.back().getId()+1;
    	}
    	
    	cout<<"Input student message:(name,passwd,sex,age,grandId) "<<endl;
    	cin>>name>>passwd>>sex>>age>>gId;
    
    	if(!selectGrand(gId)){
    		cout<<"Not find grand"<<endl;
    		return;
    	}
    
    	Student* stud = new Student(id,passwd,name,sex,age,gId);
    
    	//Add Compulsory Course to the class
    	size_t ccid[20];
    	selectGrand(gId)->getCompulsoryCourse(ccid);
    	for(int i=0;i<20;i++){
    		if(ccid[i]!=0){
    			stud->setCourses(ccid[i]);
    			cout<<ccid[i]<<endl;
    		}
    	}
    
    	//Student push list
    	students.push_back(*stud);
    
    	cout<<"add success"<<endl;
    }
    
    //Modify the student information according to the student id
    void Admin:: modifyStudent(size_t id){
    	char passwd[20];
    	char name[20];
    	char sex;
    	size_t age;
    	size_t gId=0;
    	cout<<"input student message:(name,passwd,sex,age,grandId) "<<endl;
    	cin>>name>>passwd>>sex>>age>>gId;
    	Student* stud = selectStudent(id);
    
    	if(!stud||!selectGrand(gId)){
    		cout<<"not find Student or Grand"<<endl;
    		return;
    	}
    	stud->setName(name);
    	stud->setPasswd(passwd);
    	stud->setSex(sex);
    	stud->setAge(age);
    	stud->setGId(gId);
    	return;
    }
    
    void Admin :: deleteStudent(size_t id){
    	list<Student> :: iterator it = students.begin();
    	while(it!=students.end()){
    		if(it->getId()==id){
    			students.erase(it++);
    			cout<<"delete success";
    			return;
    		}
    		it++;
    	}
    	return;
    }
    
    
    //load
    void Admin:: load1(){
    
    	//load in file
    	//Grand
    	int i,size;
        ifstream infile,infile1; 
    	Grand* grand = (Grand*)malloc(sizeof(Grand));
    	infile1.open("GrandSize.dat",ios::in);
    	infile1>>size;	//read size
    	cout<<"grand siez: "<<size<<endl;
    	infile.open("Grand_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char*)grand,sizeof(Grand));
    		grands.push_back(*grand);
    	}
    
        infile.close();  
    	cout<<"Grand read success!"<<endl;
    
    }
    
    void Admin:: load2(){
    	//Student
    	int i,size;
        ifstream infile,infile1; 
    	Student* student = (Student*)malloc(sizeof(Student));
    	infile1.open("StudentSize.dat",ios::in);
    	infile1>>size;	//read size
    	infile.open("Student_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char*)student,sizeof(Student));
    		students.push_back(*student);
    	}
    	infile.close();  
    	cout<<"Student read success!"<<endl;
    }
    
    void Admin:: load3(){
    
    	//Teacher
    	int i,size;
        ifstream infile,infile1; 
    	Teacher* teacher = (Teacher*)malloc(sizeof(Teacher));
    	infile1.open("TeacherSize.dat",ios::in);
    	infile1>>size;	
    	infile.open("Teacher_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char*)teacher,sizeof(Teacher));
    		teachers.push_back(*teacher);
    	}
    	infile.close();  
    	cout<<"Teacher read success!"<<endl;
    }
    
    void Admin:: load4(){
    	//Course
    	int i,size;
        ifstream infile,infile1; 
    	Course* course = (Course*)malloc(sizeof(Course));
    	infile1.open("CourseSize.dat",ios::in);
    	infile1>>size;	
    	infile.open("Course_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char *)course,sizeof(Course));
    		courses.push_back(*course);
    	}
    
        infile.close();  
    	cout<<"Course read success!"<<endl;
    }
    
    //save
    void Admin::save1(){
    
    	//Grand
    	char* buffer;
    	ofstream outfile,outfile1;
    	outfile1.open("GrandSize.dat",ios::out);
    	outfile1<<grands.size();
    	outfile.open("Grand_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	buffer = new char [sizeof(Grand)];
    	list<Grand> :: iterator it = grands.begin();
    	while(it!=grands.end()){
    		buffer = (char *)&*it;
    		outfile.write(buffer,sizeof(Grand));
    		it++;
    	}
    
        outfile.close();   
    	cout<<"Grand save success!"<<endl;
    
    }
    
    void Admin::save2(){
    	//Student
    	ofstream outfile,outfile1;
    	outfile1.open("StudentSize.dat",ios::out);
    	outfile1<<students.size();
    	outfile.open("Student_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	list<Student> :: iterator it = students.begin();
    	while(it!=students.end()){
    		outfile.write((char *)&*it,sizeof(Student));
    		it++;
    	}
    	outfile.close();   
    	cout<<"Student save success!"<<endl;
    }
    
    void Admin::save3(){
    	//Teacher
    	ofstream outfile,outfile1;
    	outfile1.open("TeacherSize.dat",ios::out);
    	outfile1<<teachers.size();
    	outfile.open("Teacher_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	list<Teacher> :: iterator it = teachers.begin();
    	while(it!=teachers.end()){
    		outfile.write((char *)&*it,sizeof(Teacher));
    		it++;
    	}
    	outfile.close();   
    	cout<<"Teacher save success!"<<endl;
    }
    
    void Admin::save4(){
    	//Course
    	ofstream outfile,outfile1;
    	outfile1.open("CourseSize.dat",ios::out);
    	outfile1<<courses.size();
    	outfile.open("Course_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	list<Course> :: iterator it = courses.begin();
    	while(it!=courses.end()){
    		outfile.write((char *)&*it,sizeof(Course));
    		it++;
    	}
        outfile.close();   
    	cout<<"Course save success!"<<endl;
    }
    
    
    
    Course::Course(size_t id,char name[]){
    	this->id=id;
    	for(int i=0;i<20;i++){
    		this->name[i]=name[i];
    	}
    
    }
    
    Course::~Course(){
    
    }
    
    size_t Course:: getId(){
    	return this->id;
    }
    
    char* Course:: getName(){
    	return this->name;
    }
    
    
    void Course:: setId(size_t id){
    	this->id=id;
    }
    
    void Course:: setName(char name[]){
    	for(int i=0;i<20;i++)
    		this->name[i]=name[i];
    }
    
    string Course:: toString(){
    	return "id: "+to_string(this->id)
    		+"name: "+this->name;
    }
    
    
    Grand :: Grand(){
    	for(int i=0;i<20;i++){
    		this->ccId[i]=0;
    		this->ocId[i]=0;
    	}
    }
    
    Grand::Grand(size_t id,char name[]){
    	this->id=id;
    	for(int i=0;i<20;i++){
    		this->ccId[i]=0;
    		this->ocId[i]=0;
    		this->name[i]=name[i];
    	}
    }
    
    Grand::~Grand(){
    
    }
    
    
    size_t Grand:: getId(){
    	return this->id;
    }
    
    
    char* Grand:: getName(){
    	return this->name;
    }
    
    void Grand:: getCompulsoryCourse(size_t ccId[]){
    	for(int i=0;i<20;i++){
    		ccId[i]=this->ccId[i];
    	}
    }
    
    void Grand:: getOptionalCourse(size_t ocId[]){
    	for(int i=0;i<20;i++){
    		ocId[i]=this->ocId[i];
    	}
    }
    
    
    void Grand:: setCompulsoryCourse(size_t ccId){
    	for(int i=0;i<20;i++){
    		if(this->ccId[i]==0){
    			this->ccId[i]=ccId;
    			return;
    		}
    	}
    }
    
    void Grand:: setOptionalCourse(size_t ocId){
    	for(int i=0;i<20;i++){
    		if(this->ocId[i]==0){
    			this->ocId[i]=ocId;
    			return;
    		}
    	}
    }
    
    void Grand:: setName(char name[]){
    	for(int i=0;i<20;i++)
    		this->name[i]=name[i];
    }
    
    
    Student::Student(size_t id,char passwd[], char name[], char sex, size_t age, size_t gId){
    	this->id=id;
    	for(int i=0;i<20;i++){
    		this->passwd[i]=passwd[i];
    		this->name[i]=name[i];
    	}
    	this->sex=sex;
    	this->age=age;
    	this->gId=gId;
    	for(int i=0;i<2;i++){
    		for(int j=0;j<20;j++){
    			this->courseId[i][j]=0;
    		}
    	}
    }
    
    Student::~Student(){
    }
    
    string Student::toString(){
    	return "Studnet id:"+to_string(this->id)
    		+", Passwd:"+this->passwd
    		+", Name:"+this->name
    		+", Sex:"+this->sex
    		+", Age:"+to_string(this->age)
    		+", Grand Id:"+to_string(this->gId);
    }
    
    size_t Student:: getId(){
    	return this->id;
    }
    
    char* Student:: getPasswd(){
    	return this->passwd;
    }
    
    char* Student:: getName(){
    	return this->name;
    }
    
    char Student:: getSex(){
    	return this->sex;
    }
    
    size_t Student:: getAge(){
    	return this->age;
    }
    
    size_t Student:: getGId(){
    	return this->gId;
    }
    
    
    void Student:: getCourses(size_t courseId[2][20]){
    	for(int i=0;i<2;i++){
    		for(int j=0;j<20;j++){
    			courseId[i][j]=this->courseId[i][j];
    		}
    	}
    }
    
    
    void Student:: setId(size_t id){
    	this->id=id;
    }
    
    void Student:: setPasswd(char passwd[]){
    	for(int i=0;i<20;i++)
    		this->passwd[i]=passwd[i];
    }
    
    void Student:: setName(char name[]){
    	for(int i=0;i<20;i++)
    		this->name[i]=name[i];
    }
    			
    void Student:: setSex(char sex){
    	this->sex=sex;
    }
    			    
    void Student:: setAge(size_t age){
    	this->age=age;
    }
    			    
    void Student:: setGId(size_t gId){
    	this->gId=gId;
    }
    
    
    void Student:: setCourses(size_t cid){
    	for(int i=0;i<20;i++){
    		if(this->courseId[0][i]==0){
    			this->courseId[0][i]=cid;
    			return;
    		}
    	}
    
    }
    
    void Student:: setGrade(size_t cid,size_t grade){
    	this->courseId[1][cid]=grade;
    }
    
    Teacher::Teacher(size_t id, char name[], char sex, size_t age){
    	this->id=id;
    	for(int i=0;i<20;i++)
    		this->name[i]=name[i];
    	this->sex=sex;
    	this->age=age;
    }
    
    Teacher::~Teacher(){
    
    }
    
    
    size_t Teacher:: getId(){
    	return this->id;
    }
    
    char* Teacher:: getName(){
    	return this->name;
    }
    
    char Teacher:: getSex(){
    	return this->sex;
    }
    
    size_t Teacher:: getAge(){
    	return this->age;
    }
    
    
    void Teacher:: setId(size_t id){
    	this->id=id;
    }
    
    void Teacher:: setName(char name[]){
    	for(int i=0;i<20;i++)
    		this->name[i]=name[i];
    }
    
    void Teacher:: setSex(char sex){
    	this->sex=sex;
    }
    
    void Teacher:: setAge(size_t age){
    	this->age=age;
    }
    
    string Teacher:: toString(){
    	return "id: "+to_string(this->id)
    		+", name: "+this->name
    		+", sex: "+this->sex
    		+", age: "+to_string(this->age);
    }
    
    
    User :: User(){
    
    }
    
    User :: ~User(){
    
    }
    
    Student* User :: getStudent(){
    	return this->student;
    }
    
    void User :: setStudent(Student* student){
    	this->student=student;
    }
    
    //Returns the student by student id
    Student* User:: selectStudent(size_t id){
    	list<Student> :: iterator it = students.begin();
    
    	while(it!=students.end()){
    		if(it->getId()==id){
    			return &*it;
    		}
    		it++;
    	}
    	return NULL;
    }
    
    //User Login
    int User :: login(){
    	size_t id;
    	char passwd[20];
    	cout<<"Input id: ";
    	cin>>id;
    	cout<<"Input password:";
    	cin>>passwd;
    	Student* student = this->selectStudent(id);
    	if(!student){
    		cout<<"Not find student"<<endl;
    		return 0;
    	}else if(strcmp(student->getPasswd(),passwd)!=0){
    		cout<<"Password error"<<endl;
    		return 0;
    	}else{	
    		this->student = student;
    		return 1;
    	}
    }
    
    
    
    //Modify Password
    void User :: modifyPasswd(){
    	char oldPasswd[20];
    	char newPasswd[20];
    	char secondPasswd[20];
    	cout<<"Input old password:";
    	cin>>oldPasswd;
    	if(strcmp(oldPasswd,this->student->getPasswd())!=0){
    		cout<<"Old Password input error!"<<endl;
    		return;
    	}
    	cout<<"Input new Password:";
    	cin>>newPasswd;
    	cout<<"Input second Password:";
    	cin>>secondPasswd;
    	if(strcmp(newPasswd,secondPasswd)!=0){
    		cout<<"tow passwd not same"<<endl;
    		return;
    	}
    	this->student->setPasswd(newPasswd);
    }
    
    
    //show crrent Course
    void User :: showCourse(){
    	size_t coursesId[2][20];
    	this->student->getCourses(coursesId);
    	cout<<"now courses: ";
    	for(int i=0;i<20;i++){
    		Course* course = this->selectCourse(coursesId[0][i]);
    		if(course){
    			cout<<course->getName()<<", "; 
    		}
    	}
    	cout<<endl;
    }
    
    Course* User:: selectCourse(size_t id){
    	list<Course> :: iterator it = courses.begin();
    	while(it!=courses.end()){
    		if(it->getId()==id){
    			return &*it;
    		}
    		it++;
    	}
    	return NULL;
    }
    
    Grand* User:: selectGrand(size_t id){
    	list<Grand> :: iterator it = grands.begin();;
    	while(it!=grands.end()){
    		if(it->getId()==id){
    			return &*it;
    		}
    		it++;
    	}
    	return NULL;
    }
    
    //Show choose courses
    void User :: scanCourse(){
    	Grand* grand = this->selectGrand(this->student->getGId());
    	if(!grand){
    		cout<<"not find error!"<<endl;
    		return;
    	}
    	size_t ocId[20];
    	grand->getOptionalCourse(ocId);
    	cout<<"OptionalCourse List: ";
    	for(int i=0;i<20;i++){
    		Course* course = this->selectCourse(ocId[i]);
    		if(course){
    			cout<<course->getName()<<", "; 
    		}
    	}
    	cout<<endl;
    }
    
    //Choose Course by course id
    void User :: chooseCourse(size_t cid){
    	size_t coursesId[2][20];
    	this->student->getCourses(coursesId);
    	//Check if you have already taken a course
    	for(int i=0;i<20;i++){
    		if(cid==coursesId[0][i]){
    			cout<<"the course has selected!"<<endl;
    			return;
    		}
    	}
    	for(int i=0;i<20;i++){
    		if(0==coursesId[0][i]){      //The first place to set the course is 0
    			this->student->setCourses(cid);
    			cout<<"Choose Course seccess!"<<endl;
    			return;
    		}
    	}
    
    	//Course Full
    	cout<<"courses is full!"<<endl;
    	return;
    
    }
    
    //Show person grade
    void User :: showGrade(){
    	size_t coursesId[2][20];
    	this->student->getCourses(coursesId);
    	for(int i=0;i<20;i++){
    		Course* course = this->selectCourse(coursesId[0][i]);
    		if(course){
    			cout<<course->getName()<<" grade: "<<coursesId[1][i]<<endl; 
    		}
    	}
    }
    
    //Person Message
    void User :: personMessage(){
    	cout<<"Person Message:"<<endl;
    	cout<<"Id: "<<this->student->getId()<<endl;
    	cout<<"Name: "<<this->student->getName()<<endl;
    	cout<<"Sex: "<<this->student->getSex()<<endl;
    	cout<<"Age: "<<this->student->getAge()<<endl;
    	cout<<"Grand: "<<this->selectGrand(this->student->getGId())->getName()<<endl;
    	cout<<"Course List: ";
    	showCourse();
    	cout<<"Grade List: "<<endl;
    	showGrade();
    }
    
    
    //load
    void User:: load1(){
    
    	//Grand
    	int i,size;
        ifstream infile,infile1; 
    	Grand* grand = (Grand*)malloc(sizeof(Grand));
    	infile1.open("GrandSize.dat",ios::in);
    	infile1>>size;	
    	infile.open("Grand_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char *)grand,sizeof(Grand));
    		grands.push_back(*grand);
    	}
    
        infile.close();  
    	cout<<"Grand read success!"<<endl;
    
    }
    
    void User:: load2(){
    	//Student
    	int i,size;
        ifstream infile,infile1; 
    	Student* student = (Student*)malloc(sizeof(Student));
    	infile1.open("StudentSize.dat",ios::in);
    	infile1>>size;	
    	infile.open("Student_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char *)student,sizeof(Student));
    
    		students.push_back(*student);
    	}
    	infile.close();  
    	cout<<"Student read success!"<<endl;
    }
    
    void User:: load3(){
    
    	//Teacher
    	int i,size;
        ifstream infile,infile1; 
    	Teacher* teacher = (Teacher*)malloc(sizeof(Teacher));
    	infile1.open("TeacherSize.dat",ios::in);
    	infile1>>size;	
    	infile.open("Teacher_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char *)teacher,sizeof(Teacher));
    		teachers.push_back(*teacher);
    	}
    	infile.close();  
    	cout<<"Teacher read success!"<<endl;
    }
    
    void User:: load4(){
    	//Course
    	int i,size;
        ifstream infile,infile1; 
    	Course* course = (Course*)malloc(sizeof(Course));
    	infile1.open("CourseSize.dat",ios::in);
    	infile1>>size;	
    	infile.open("Course_data.dat",ios::binary);   
    	if(!infile){
    		cerr<<"open error!"<<endl;
    		return; 
    	}
    	for(i=0;i<size;i++){
    		infile.read((char *)course,sizeof(Course));
    		courses.push_back(*course);
    	}
    
        infile.close();  
    	cout<<"Course read success!"<<endl;
    }
    
    
    //save
    void User::save1(){
    
    	//Grand
    	ofstream outfile,outfile1;
    	outfile1.open("GrandSize.dat",ios::out);
    	outfile1<<grands.size();
    	outfile.open("Grand_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	list<Grand> :: iterator it = grands.begin();
    	while(it!=grands.end()){
    		outfile.write((char *)&*it,sizeof(Grand));
    		it++;
    	}
    
        outfile.close();   
    	cout<<"Grand save success!"<<endl;
    
    }
    
    void User::save2(){
    	//Student
    	ofstream outfile,outfile1;
    	outfile1.open("StudentSize.dat",ios::out);
    	outfile1<<students.size();
    	outfile.open("Student_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	list<Student> :: iterator it = students.begin();
    	while(it!=students.end()){
    		outfile.write((char *)&*it,sizeof(Student));
    		it++;
    	}
    	outfile.close();   
    	cout<<"Student save success!"<<endl;
    }
    
    void User::save3(){
    	//Teacher
    	ofstream outfile,outfile1;
    	outfile1.open("TeacherSize.dat",ios::out);
    	outfile1<<teachers.size();
    	outfile.open("Teacher_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	list<Teacher> :: iterator it = teachers.begin();
    	while(it!=teachers.end()){
    		outfile.write((char *)&*it,sizeof(Teacher));
    		it++;
    	}
    	outfile.close();   
    	cout<<"Teacher save success!"<<endl;
    }
    
    void User::save4(){
    	//Course
    	ofstream outfile,outfile1;
    	outfile1.open("CourseSize.dat",ios::out);
    	outfile1<<courses.size();
    	outfile.open("Course_data.dat",ios::binary);   
    	if(!outfile){
    		cerr<<"open error!"<<endl; 
    		return; 
    	}
    	list<Course> :: iterator it = courses.begin();
    	while(it!=courses.end()){
    		outfile.write((char *)&*it,sizeof(Course));
    		it++;
    	}
        outfile.close();   
    	cout<<"Course save success!"<<endl;
    }
    
    
    
    
    void login_menu();
    
    void admin_menu();
    void menuGrand();
    void menuTeacher();
    void menuCourse();
    void menuSelect();
    
    void menuStudnet();
    
    void user_menu();
    
    
    Admin* admin = new Admin();
    User* user = new User();
    
    int main(){
    	
    	
    	login_menu();
    	
    	getchar();
    
    	return 0;
    }
    
    void login_menu(){
    	int a;
    	cout<<"***************"<<endl;
    	cout<<"1.Admin login:"<<endl;
    	cout<<"2.Student login:"<<endl;
    	cout<<"Input: ";
    	cin>>a;
    	switch (a){
    	case 1:
    		admin_menu();
    		break;
    	case 2:
    		user_menu();
    		break;
    	default:
    		break;
    	}
    }
    
    void admin_menu(){
    	int a;
    	string id, passwd;
    	cout<<"Input Admin Id:";
    	cin>>id;
    	if(id.compare("admin")!=0){
    		cout<<"Admin Id input error!"<<endl;
    		getchar();
    		return;
    	}
    	cout<<"Input Admin Password:";
    	cin>>passwd;
    	if(passwd.compare("123456")!=0){
    		cout<<"Admin password input error!"<<endl;
    		getchar();
    		return;
    	}
    	admin->load1();
    	admin->load2();
    	admin->load3();
    	admin->load4();
    	while (true){
    
    		system("clear");
    		cout<<"***************"<<endl;
    		cout<<"1.Grand Management"<<endl;
    		cout<<"2.Teacher Management"<<endl;
    		cout<<"3.Course Management"<<endl;
    		cout<<"4.Student Choose Course"<<endl;
    		cout<<"5.Studnet Management"<<endl;
    		cout<<"6.Exit"<<endl;
    		cout<<"***************"<<endl;
    		cout<<"Input: ";
    		cin>>a;
    
    		switch (a){
    		case 1:
    			menuGrand();
    			break;
    		case 2:
    			menuTeacher();
    			break;
    		case 3:
    			menuCourse();
    			break;
    		case 4:
    			menuSelect();
    			break;
    		case 5:
    			menuStudnet();
    			break;
    		case 6:
    			admin->save1();
    			admin->save2();
    			admin->save3();
    			admin->save4();
    			return;
    		default:
    			break;
    		}
    	}
    }
    
    void menuGrand(){
    	int a;
    	while (true){
    		getchar();
    		getchar();
    		system("clear");
    		cout<<"***************"<<endl;
    		cout<<"1.Show Grand"<<endl; 
    		cout<<"2.Select Grand"<<endl;
    		cout<<"3.Add Grand"<<endl;
    		cout<<"4.Modify Grand"<<endl;
    		cout<<"5.Delete Grand"<<endl;
    		cout<<"6.Exit"<<endl;
    		cout<<"***************"<<endl;
    		cout<<"Input: ";
    		cin>>a;
    		switch (a){
    		case 1:
    			admin->showGrandAll();
    			break;
    		case 2:
    			cout<<"Input Grand Id:";
    			int gid;
    			cin>>gid;
    			if(admin->selectGrand(gid)){
    				cout<<admin->selectGrand(gid)->getName()<<endl;
    			}
    			break;
    		case 3:
    			admin->addGrand();
    			break;
    		case 4:
    			cout<<"Input Geand Id:";
    			cin>>gid;
    			admin->modifyGrand(gid);
    			break;
    		case 5:
    			cout<<"Input Geand Id:";
    			cin>>gid;
    			admin->deleteGrand(gid);
    			break;
    		case 6:
    			return;
    		default:
    			break;
    		}
    	}
    
    }
    
    void menuTeacher(){
    	int a;
    	while (true){
    		getchar();
    		getchar();
    		system("clear");
    		cout<<"***************"<<endl;
    		cout<<"1.Show Teacher"<<endl;
    		cout<<"2.Select Teacher"<<endl;
    		cout<<"3.Add Teacher"<<endl;
    		cout<<"4.Modify Teacher"<<endl;
    		cout<<"5.Delete Teacher"<<endl;
    		cout<<"6.Exit   "    <<endl;
    		cout<<"***************"<<endl;
    		cout<<"Input: ";
    		cin>>a;
    		switch (a){
    		case 1:
    			admin->showTeacherAll();
    			break;
    		case 2:
    			cout<<"Input Teacher Id:";
    			int tid;
    			cin>>tid;
    			if(admin->selectTeacher(tid)){
    				cout<<admin->selectTeacher(tid)->toString()<<endl;
    			}
    			break;
    		case 3:
    			admin->addTeacher();
    			break;
    		case 4:
    			cout<<"Input Teacher Id:";
    			cin>>tid;
    			admin->modifyTeacher(tid);
    			break;
    		case 5:
    			cout<<"Input Teacher Id:";
    			cin>>tid;
    			admin->deleteTeacher(tid);
    			break;
    		case 6:
    			return;
    		default:
    			break;
    		}
    	}
    }
    
    void menuCourse(){
    	int a;
    	while (true){
    		getchar();
    		getchar();
    		system("clear");
    		cout<<"***************"<<endl;
    		cout<<"1.Show Course"<<endl;
    		cout<<"2.Select Course"<<endl;
    		cout<<"3.Add Course"<<endl;
    		cout<<"4.Modify Course"<<endl;
    		cout<<"5.Delete Course"<<endl;
    		cout<<"6.Exit "<<endl;
    		cout<<"***************"<<endl;
    		cout<<"Input: ";
    		cin>>a;
    		switch (a){
    		case 1:
    			admin->showCourseAll();
    			break;
    		case 2:
    			cout<<"Input Course Id:";
    			int cid;
    			cin>>cid;
    			if(admin->selectCourse(cid)){
    				cout<<admin->selectCourse(cid)->toString()<<endl;
    			}
    			break;
    		case 3:
    			admin->addCourse();
    			break;
    		case 4:
    			cout<<"Input Course Id:";
    			cin>>cid;
    			admin->modifyCourse(cid);
    			break;
    		case 5:
    			cout<<"Input Course Id:";
    			cin>>cid;
    			admin->deleteCourse(cid);
    			break;
    		case 6:
    			return;
    		default:
    			break;
    		}
    	}
    }
    
    void menuSelect(){
    	int gid;
    	cout<<"Input Grand Id:";
    	cin>>gid;
    	admin->chooseCourse(gid);
    }
    
    
    
    void menuStudnet(){
    	int a;
    	while (true){
    		getchar();
    		getchar();
    		system("clear");
    		cout<<"***************"<<endl;
    		cout<<"1.Show Student"<<endl;
    		cout<<"2.Select Student"<<endl;
    		cout<<"3.Add Student"<<endl;
    		cout<<"4.Modify Student"<<endl;
    		cout<<"5.Delete Student"<<endl;
    		cout<<"6.Exit"<<endl;
    		cout<<"***************"<<endl;
    		cout<<"Input: ";
    		cin>>a;
    		switch (a){
    		case 1:
    			int gid;
    			cout<<"Input Grand Id:";
    			cin>>gid;
    			admin->showStudentOfGrand(gid);
    			break;
    		case 2:
    			cout<<"Input Student Id:";
    			size_t sid;
    			cin>>sid;
    			if(admin->selectStudent(sid)){
    				cout<<admin->selectStudent(sid)->toString()<<endl;
    			}
    			break;
    		case 3:
    			admin->addStudent();
    			break;
    		case 4:
    			cout<<"Input Studnet Id:";
    			cin>>sid;
    			admin->modifyStudent(sid);
    			break;
    		case 5:
    			cout<<"Input Studnet Id:";
    			cin>>sid;
    			admin->deleteStudent(sid);
    			break;
    		case 6:
    			return;
    		default:
    			break;
    		}
    	}
    }
    
    
    void user_menu(){
    	int a;
    	user->load1();
    	user->load2();
    	user->load3();
    	user->load4();
    
    	if(!user->login()){
    		cout<<"Login failure"<<endl;
    		getchar();
    		return;
    	}
    	while (true){
    		getchar();
    		getchar();
    		system("clear");
    		cout<<"***************"<<endl;
    		cout<<"1.Modify Password"<<endl;
    		cout<<"2.Course Meassage"<<endl;
    		cout<<"3.Show Choose Course List"<<endl;
    		cout<<"4.Choose Course"<<endl;
    		cout<<"5.Select Grade"<<endl;
    		cout<<"6.Personal Information"<<endl;
    		cout<<"7.Exit"<<endl;
    		cout<<"***************"<<endl;
    		cout<<"Input: ";
    		cin>>a;
    
    		switch (a){
    		case 1:
    			user->modifyPasswd();
    			break;
    		case 2:
    			user->showCourse();
    			break;
    		case 3:
    			user->scanCourse();
    			break;
    		case 4:
    			size_t cid;
    			cout<<"Input Course Id:";
    			cin>>cid;
    			user->chooseCourse(cid);
    			break;
    		case 5:
    			user->showGrade();
    			break;
    		case 6:
    			user->personMessage();
    			break;
    		case 7:
    			user->save1();
    			user->save2();
    			user->save3();
    			user->save4();
    
    			return;
    		default:
    			break;
    		}
    	}
    
    }
    

    使用方式:首先管理员登陆,账户:admin 密码:123456

    登陆成功进行操作,添加学生,默认开始学号为150101,设置密码后可在登陆界面进行学生登陆。

    学生登陆,账户:【学号】 密码:【管理员时设置的密码】

    以上是将多文件整合在一起的代码,建议使用多文件的完整源码。

    完整源码:https://download.csdn.net/download/qq_35402412/10914066

    Linux下使用make执行

  • 相关阅读:
    POJ 3140 Contestants Division (树dp)
    POJ 3107 Godfather (树重心)
    POJ 1655 Balancing Act (树的重心)
    HDU 3534 Tree (经典树形dp)
    HDU 1561 The more, The Better (树形dp)
    HDU 1011 Starship Troopers (树dp)
    Light oj 1085
    Light oj 1013
    Light oj 1134
    FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869
  • 原文地址:https://www.cnblogs.com/yongtaochang/p/13615328.html
Copyright © 2011-2022 走看看