程序设计任务-简单的学生信息管理系统

因为时间有限所以只简单的写了一下,若有不对的地方,敬请各位指正.

头文件 Stu.h

#ifndef CODEDEFINE_H
#define CODEDEFINE_H
#include<iostream>
using namespace std;
class student{
	private:
		char name[20];
		char id[20];
		char sex;
		double graphics;
		double english;
		double math;
		double computer;
		double cLanguage;
		double totGrade;
	public:
		student* next;
		student();
		student(char *,char *,char ,double ,double ,double ,double ,double);
		student(student &s);
		//获取元素 
		char const* getName() const;
		char const* getId() const;
		char  getSex() const;
		double getTotGrades() const;
		double getGraphics()const;
		double getMath() const;
		double getEnglish() const;
		double getComputer() const;
		double getCLanguage() const;
		//改变元素
		void setName();
		void setId();
		void setSex();
		void setGrades();
		void changeStuInfor();
		//显示信息 
		void showInfor();
};
bool operator< (const student& a,const student& b);
class mainSystem{
	//用链表来代替容器 
	private:
		student* head;
		student* tail;
	public: 
		int size;
		
		mainSystem();
		
		void keepInfor();


		//show menu
		void menu();


		//show student's imformation
 		void showStuInfor();


		//show student's imformation with 
		void showAllStuGradeInfor();


		//change student's imformation
		void changeStuInfor();


		//delete student imformation
		void deleteStuInfor();


		//add student imfortion
		void addStuInfor();
		
		//
		void exit1();
		
		void searchStuInfor();
		//
		void searchStuWithId();


		//
		void searchStuWithName();
};
#endif



made.cpp

#include"Stu.h"
#include<conio.h>
#include<windows.h>
#include<iomanip>
#include<cstring>
#include<fstream>
#include<algorithm>
using namespace std; 


bool operator <  (const student& a,const student& b){
	if (strcmp(a.getId(),b.getId()) < 0) return true;
	return false;
}
bool operator == (const student& a,const student& b){
	if(strcmp(a.getId(),b.getId()) == 0) return true;
	return false;
}


student::student(){}
student::student(char *n,char *id1,char s,double g,double e,double m,double com,double cL){
	for(int i=0;i<20;i++){
		name[i] = n[i];
		id[i] = id1[i];
	}
	sex = s;
	graphics = g;
	english = e;
	math = m;
	computer = com;
	cLanguage = cL;
	totGrade = g+e+m+com+cL;
	next = NULL;
}
//拷贝构造 
student::student(student &s){
	for(int i=0;i<20;i++){
		name[i] = *(s.getName()+i);
		id[i] = *(s.getId()+i);
	}
	sex = s.getSex();
	graphics = s.getGraphics();
	english = s.getEnglish();
	math = s.getMath();
	computer = s.getComputer();
	cLanguage = s.getCLanguage();
	totGrade = s.getTotGrades();
}
char const* student::getName() const{
	return name;
}
char const* student::getId() const{
	return id;
}
char student::getSex() const{
	return sex;
}
double student::getGraphics() const{
	return graphics;
}
double student::getMath() const{
	return math;
}
double student::getEnglish() const{
	return english;
}
double student::getComputer() const{
	return computer;
}
double student::getCLanguage() const{
	return cLanguage;
}
double student::getTotGrades() const{
	return totGrade;
}
//change student imformation
void student::setName(){
	char newName[20];
	cout<<"\t\t请输入新的姓名:";
	cin>>newName;
	strcpy(name,newName);
}
void student::setSex(){
	char newSex;
	cout<<"\t\t请输入新的性别:";
	cin>>newSex;
	sex = newSex;
}
void student::setGrades(){
	double g,m,e,c,cl,com;
	cout<<"\t\t请输入新的成绩:"<<endl;
	cout<<"\t\t物理:";
	cin>>graphics;
	cout<<"\t\t英语:";
	cin>>english;
	cout<<"\t\t数学:";
	cin>>math;
	cout<<"\t\t计导:";
	cin>>computer;
	cout<<"\t\tC语言:";
	cin>>cLanguage;
	totGrade = graphics+english+math+computer+cLanguage;
}
void student::changeStuInfor(){
	cout<<"\t\t学生信息:"<<endl;
	cout<<"\t\t姓名:";              cin>>name;
	cout<<"\t\t学号:";              cin>>id;
	cout<<"\t\t性别(男:1,女:2):";   cin>>sex;
	cout<<"\t\t物理:";              cin>>graphics;
	cout<<"\t\t英语:";     			cin>>english;
	cout<<"\t\t数学:";        		cin>>math;
	cout<<"\t\t计算机导论:";     	cin>>computer;
	cout<<"\t\tC语言:";    			cin>>cLanguage;
}
void student::showInfor(){
	cout<<setiosflags(ios::left)<<setw(8)<<name<<setw(8)<<id<<setw(8)<<(sex=='1'?"男":"女")<<setw(8)<<setprecision(4)
	<<setw(8)<<graphics<<setw(8)<<english<<setw(8)<<math<<setw(8)<<computer<<setw(8)<<cLanguage<<setw(8)<<totGrade<<endl;
}
//构造函数,负责创建文件以及获取文件的内容 
mainSystem::mainSystem(){
	ifstream f;
	f.open("user.txt");
	if(!f){
		cout<<"cant's open File"<<endl;
		return;
	}
	size = 0;
	head = new student();
	tail = head;
	
	student* temp= new student();
	while( f.read((char *)temp,sizeof(student)) ){
		tail->next = temp;
		tail = tail->next;
		temp = new student();
		size++;
	}
	f.close();
}


//根据学号顺序显示学生信息 
void mainSystem::showStuInfor(){
	system("cls");
	student* temp = head->next;
	cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)
	<<"物理"<<setw(8)<<"英语:"<<setw(8)<<"数学:"<<setw(8)<<"计导:"<<setw(8)<<"C语言:"<<setw(8)<<"总分:"<<endl;
	while(temp){
		temp->showInfor();
		temp = temp->next;
	}
	cout<<"\n"<<endl;
	system("pause");
}


//show all student's some imformation was counted. like grades , the average grades, the total grades,
// the lowest, this highest student's grades and the ranking
/*
this question stop thinking tempory
*/


//学生成绩统计
//首先按照总成绩进行排名然后显示出总分最高的以及各科分数最高的同学的信息 
void mainSystem::showAllStuGradeInfor(){
	//关于各科成绩好说先遍历一遍记录最高分然后输出就行
	//但是对于打印成绩排名...
	system("cls");
	double grade[1000];
	double p[6] = {0};
	student* temp = head;
	int i=0;
	while(temp != tail){
		p[0] = ( p[0] > temp->next->getGraphics()  ) ? p[0] : temp->next->getGraphics();
		p[1] = ( p[1] > temp->next->getEnglish()   ) ? p[1] : temp->next->getEnglish();
		p[2] = ( p[2] > temp->next->getMath()      ) ? p[2] : temp->next->getMath();
		p[3] = ( p[3] > temp->next->getComputer()  ) ? p[3] : temp->next->getComputer();
		p[4] = ( p[4] > temp->next->getCLanguage() ) ? p[4] : temp->next->getCLanguage();
		p[5] = ( p[5] > temp->next->getTotGrades() ) ? p[5] : temp->next->getTotGrades();
		grade[i++]  = temp->next->getTotGrades();
		temp = temp->next;
	}
	double r;//用于去重,如果分数重复的话可以去掉 
	int rank = 1;//排名计数器 
	cout<<"排名统计:"<<endl;
	cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)<<"总分:"<<setw(8)<<"排名:"<<endl;
	sort(grade,grade+i);//先将总分进行排序 
	r = -1;
	while(--i >= 0){
		if(r == grade[i]) continue;
		temp = head;
		while(temp != tail){
			if(temp->next->getTotGrades() == grade[i]) {
				cout<<setiosflags(ios::left)<<setw(8)<<temp->next->getName()<<setw(8)<<temp->next->getId()
				<<setw(8)<<temp->next->getSex()<<setprecision(4)<<setw(8)<<grade[i]<<setw(8)<<rank++<<endl;
			}
			temp = temp->next;
		}
		r = grade[i];
	}
	//显示物理最高分 
	temp = head;
	cout<<"\n物理最高分:"<<endl; 
	cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)<<"物理:"<<endl;
	while(temp != tail){
		if( temp->next->getGraphics() == p[0]) 
			cout<<setiosflags(ios::left)<<setw(8)<<temp->next->getName()<<setw(8)<<temp->next->getId()
			<<setw(8)<<temp->next->getSex()<<setprecision(4)<<setw(8)<<p[0]<<endl;
		temp = temp->next;
	}
	//显示英语最高分 
	temp = head;
	cout<<"\n英语最高分:"<<endl; 
	cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)<<"英语:"<<endl;
	while(temp != tail){
		if( temp->next->getEnglish() == p[1]) 
			cout<<setiosflags(ios::left)<<setw(8)<<temp->next->getName()<<setw(8)<<temp->next->getId()
			<<setw(8)<<temp->next->getSex()<<setprecision(4)<<setw(8)<<p[1]<<endl;
		temp = temp->next;
	}
	//显示数学最高分 
	temp = head;
	cout<<"\n数学最高分:"<<endl; 
	cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)<<"数学:"<<endl;
	while(temp != tail){
		if( temp->next->getMath() == p[2]) 
			cout<<setiosflags(ios::left)<<setw(8)<<temp->next->getName()<<setw(8)<<temp->next->getId()
			<<setw(8)<<temp->next->getSex()<<setprecision(4)<<setw(8)<<p[2]<<endl;
		temp = temp->next;
	}
	//显示计导最高分 
	temp = head;
	cout<<"\n计导最高分:"<<endl; 
	cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)<<"计导:"<<endl;
	while(temp != tail){
		if( temp->next->getComputer() == p[3]) 
			cout<<setiosflags(ios::left)<<setw(8)<<temp->next->getName()<<setw(8)<<temp->next->getId()
			<<setw(8)<<temp->next->getSex()<<setprecision(4)<<setw(8)<<p[3]<<endl;
		temp = temp->next;
	}
	//显示C语言最高分 
	temp = head;
	cout<<"\nC语言最高分:"<<endl; 
	cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)<<"C语言:"<<endl;
	while(temp != tail){
		if( temp->next->getCLanguage() == p[4]) 
			cout<<setiosflags(ios::left)<<setw(8)<<temp->next->getName()<<setw(8)<<temp->next->getId()
			<<setw(8)<<temp->next->getSex()<<setprecision(4)<<setw(8)<<p[4]<<endl;
		temp = temp->next;
	}
	system("pause");
}
//保存所有学生信息 
void mainSystem::keepInfor(){
//	cout<<33<<endl;
	ofstream f("user.txt");
	if(!f){
		cout<<"\t\tFailed Open File"<<endl;
		return ;
	}
	student* temp = head->next;
	while(temp){
		f.write((char *)temp,sizeof(student));
		temp = temp->next;
	}
}
//删除学生信息 
void mainSystem::deleteStuInfor(){
	char id[20];
	student* temp = head;
	cout<<"\t\t请输入学生的学号:\n\t\t";
	cin>>id;
	while(temp != tail){
		if(strcmp(temp->next->getId(),id) == 0) {
			cout<<"\t\t确认删除?(y/n) :";
			char a;
			cin>>a;
			if(a == 'y'||a == 'Y'){
				student* del = temp->next;
				if(temp->next == tail) {
					tail = temp;
					temp->next = NULL;
					size--;
					delete del;
					return;
				}
				temp->next = temp->next->next;
				delete del;
				keepInfor();
				cout<<"\t\t删除成功\n";
				size--;
				return;
			}
			
		}
		temp = temp ->next;
	}
	cout<<"\t\t未找到该学生信息"<<endl; 
}
//添加学生信息 
void mainSystem::addStuInfor(){
	char name[20];
	char id[20];
	char sex;
	double graphics,math,english,computer,CLanguage;
	cout<<"\t\t学生信息:"<<endl;
	cout<<"\t\t姓名:";              cin>>name;
	cout<<"\t\t学号:";              cin>>id;
	cout<<"\t\t性别(男:1,女:2):";   cin>>sex;
	cout<<"\t\t物理:";              cin>>graphics;
	cout<<"\t\t英语:";     			cin>>english;
	cout<<"\t\t数学:";        		cin>>math;
	cout<<"\t\t计算机导论:";     	cin>>computer;
	cout<<"\t\tC语言:";    			cin>>CLanguage;
	
	student* p = new student(name,id,sex,graphics,english,math,computer,CLanguage);
	p->showInfor(); 
	cout<<"\t\t确认保存?(y/n)";
	char a;
	cin>>a;
	
	if(a == 'y'||a =='Y') {
		student* temp = head;
		//没有小于但可能是最后一个
		
		while( temp != tail && *(temp->next) < *p){
			temp = temp->next;
		}
		
		if(temp == tail) {
			temp->next = p;
			tail = p;
			keepInfor();
			cout<<"\t\t添加成功";
			size++;
			return;
		} 
		p->next = temp->next;
		temp->next = p;
		keepInfor();
		cout<<"\t\t添加成功";
		size++;
	}
}
void mainSystem::searchStuInfor(){
	cout<<"\t1根据学生学号查找:\t2:根据学生姓名查找:"<<endl;
	cout<<"\t\t输入:";
	char a;
	cin>>a;
	switch(a){
		case '1': searchStuWithId();break;
		case '2': searchStuWithName();break;
		default:break;
	}
}


//根据学生的学号寻找学生信息 
void mainSystem::searchStuWithId(){
	student* temp = head;
	char id[20];
	bool key = false;
	cout<<"\t\t请输入学生的学号:";
	cin>>id;
	while(temp != tail ){
		if(strcmp(id,temp->next->getId()) == 0){
			if(!key) cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)
			<<"物理"<<setw(8)<<"英语:"<<setw(8)<<"数学:"<<setw(8)<<"计导:"<<setw(8)<<"C语言:"<<setw(8)<<"总分:"<<endl;
			key = true;
			temp->next->showInfor();
		}
		temp = temp->next;
	}
	if(!key) cout<<"NOT FOUND!"<<endl;
	system("pause");
}
//根据学生的姓名寻找学生信息 
void mainSystem::searchStuWithName(){
	student* temp = head;
	char name[20];
	bool key = false;
	cout<<"\t\t请输入学生的姓名:";
	cin>>name;
	while(temp != tail){
		if(strcmp(name,temp->next->getName()) == 0){
			if(!key) cout<<setiosflags(ios::left)<<setw(8)<<"姓名:"<<setw(8)<<"学号:"<<setw(8)<<"性别:"<<setw(8)
			<<"物理"<<setw(8)<<"英语:"<<setw(8)<<"数学:"<<setw(8)<<"计导:"<<setw(8)<<"C语言:"<<setw(8)<<"总分:"<<endl;
			key = true;
			temp->next->showInfor();
		}
		temp = temp->next;
	}
	if(!key) cout<<"NOT FOUND!"<<endl;
	system("pause");
}
//修改学生信息 
void mainSystem::changeStuInfor(){
	student* temp = head;
	char id[20];
	bool key = false;
	cout<<"\t\t请输入学生的学号:";
	cin>>id;
	while(temp != tail ){
		if(strcmp(temp->next->getId(),id) == 0){
			//找到该学生先显示
			temp = temp->next;
			temp->showInfor();
			cout<<"\t1:修改姓名\t2:修改性别\n\t3:修改成绩\t4:全部修改"<<endl;
			cout<<"\t输入:";
			char choice;
			cin>>choice;
			switch(choice){
				case '1': { temp->setName();keepInfor();return; }
				case '2': { temp->setSex();keepInfor();return;  }
				case '3': { temp->setGrades();keepInfor();return; }
				case '4': { temp->changeStuInfor();keepInfor();return; }
				default: ;
			}
		}
		temp = temp->next;
	}
	cout<<"\t\tNOT FOUND\n\t\t";
	system("pause");
}
/*显示菜单
	输入可能需要放进主函数中...不然每次无用的输入可能会导致刷新 
*/
void mainSystem::menu(){
	cout<<endl<<endl<<endl<<endl;
	cout<<"\t"<<"***********学生信息管理系统**************"<<endl;
	cout<<"\t"<<"*                                       *"<<endl;
	cout<<"\t"<<"*  1:查看学生信息        2:学生成绩统计 *"<<endl;
	cout<<"\t"<<"*                                       *"<<endl;
	cout<<"\t"<<"*  3:查找学生信息        4:修改学生信息 *"<<endl;
	cout<<"\t"<<"*                                       *"<<endl;
	cout<<"\t"<<"*  5:删除学生信息        6:增加学生信息 *"<<endl;
	cout<<"\t"<<"*                                       *"<<endl;
	cout<<"\t"<<"*  0:退出程序 		                *"<<endl;
	cout<<"\t"<<"*                                       *"<<endl;
	cout<<"\t"<<"*****************************************"<<endl;
	cout<<"\n\n\n\n\n"<<endl;
	cout<<"\t\t输入:";
}
//保存退出 
void mainSystem::exit1(){
	keepInfor();
	exit(0);
}




main.cpp

#include"Stu.h"
int main(){
	mainSystem* m = new mainSystem();
	int choice;
	while(1){
		m->menu();
		cin>>choice;
		switch(choice){
			case 1: m->showStuInfor();break;
			case 2: m->showAllStuGradeInfor();break;
			case 3: m->searchStuInfor();break;
			case 4: m->changeStuInfor();break;
			case 5: m->deleteStuInfor();break;
			case 6: m->addStuInfor();break;
			case 0: m->exit1();break;
			default: ;
		}
		system("cls");
	}
	return 0;
}



// @author: LovelyTotoro

猜你喜欢

转载自blog.csdn.net/lala__lailai/article/details/79105436