1. 程式人生 > >程式設計任務-簡單的學生資訊管理系統

程式設計任務-簡單的學生資訊管理系統

因為時間有限所以只簡單的寫了一下,若有不對的地方,敬請各位指正.

標頭檔案 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