⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ban.cpp

📁 学生管理类,很好的
💻 CPP
字号:
// Ban.cpp: implementation of the Ban class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <iostream.h>
#include <iomanip.h>
#include "ban.h"                               //严重注意!!  stu数组从零开始,学号从1开始
#include "student.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int Ban::totalClass=0;
int Ban::totalStu=0;

Ban::Ban()
{
    classnum=totalClass+1;
	cout<<"Input the student number of class "<<classnum<<endl;
	cin>>sum;
	creatStu();
}
Ban::Ban(int member)                //构造函数,参数为班级人数,建立类数组 stu[sum]
{
	classnum=totalClass+1;
	sum=member;
	creatStu();
}

Ban::~Ban()
{

}
void Ban::creatStu()					        //开辟内存空间,建立学生数组
{	
	int choice;
	totalClass++;	totalStu+=sum;
	stu= new Student[sum];											//分配内存
	for(int i=0;i<sum;i++) stu[i].setnum(i+1);						//初始化学号

	cout<<"Initial student's score of class "<<classnum<<" ?"<<endl		//询问是否初始化成绩
		<<"1)yes    2)no"<<endl
		<<"Please enter your choice:"<<endl;
	cin>>choice;

	if(choice==1) { for(int i=1;i<=sum;i++) modify(i); }			//依次初始化成绩
	else if(choice==2) ;											//不初始化成绩
	else cout<<"Your choice is error!"<<endl;
}

Ban::Ban()                                   //构造函数,询问班级人数,建立类数组 stu[sum]
{
	classnum=totalClass+1;
	cout<<"Input the student number of class "<<classnum<<endl;
	cin>>sum;
	creatStu();	
}


Ban::Ban(int member)                //构造函数,参数为班级人数,建立类数组 stu[sum]
{
	classnum=totalClass+1;
	sum=member;
	creatStu();
}


void Ban::show()							//可按学号,各门成绩排序输出
{
	int subject,arrange,i,j,error;
	cout<<"Print the score by which one?"<<endl
		<<"1)No.    2)Maths   3)Physics   4)Chemistry"<<endl
		<<"Please enter your choice:  "<<endl;
	cin>>subject;
	cout<<endl;
	cout<<"How to arrange the score?"<<endl
		<<"1)up       2)down"<<endl
		<<"Please enter your choice:  "<<endl;
	cin>>arrange;

	if(subject==1)													//按学号排列
	{
		for(i=0;i<sum;i++) 	{ 
			for(j=i;j<sum;j++) {
				if(stu[i].num>stu[j].num) 
				{stu[i].exchange(stu[i],stu[j]);}
			}
		}
	}
	else if(subject==2)												//按数学成绩排列
	{
		for(i=0;i<sum;i++) 	{ 
			for(j=i;j<sum;j++) {
				if(stu[i].math>stu[j].math) 
				{stu[i].exchange(stu[i],stu[j]);}
			}
		}
	}

	else if(subject==3)												//按物理成绩排列
	{
		for(i=0;i<sum;i++) 	{ 
			for(j=i;j<sum;j++) {
				if(stu[i].phy>stu[j].phy) {stu[i].exchange(stu[i],stu[j]);}
			}
		}
	}

	else if(subject==4)												//按化学成绩排列
	{
		for(i=0;i<sum;i++) 	{ 
			for(j=i;j<sum;j++) {
				if(stu[i].che>stu[j].che) {stu[i].exchange(stu[i],stu[j]);}
			}
		}
	}

	else{cout<<"Your subject choice is error!!"<<endl;  error=99;}

	if(arrange==1)  			//升序输出
	{ 
		cout<<endl<<"Student of Class "<<classnum<<":"<<endl
			<<setw(15)<<setiosflags(ios::left)<<"Num"<<setw(15)<<"Maths"<<setw(15)<<"chemistry"<<endl;
		for(int i=0;i<sum;i++)  stu[i].show();   
	}			
	else if(arrange==2)			//降序输出
	{ 
		cout<<endl<<"Student of Class "<<classnum<<":"<<endl
			<<setw(15)<<setiosflags(ios::left)<<"Num"<<setw(15)<<"Maths"<<setw(15)<<"chemistry"<<endl;
		for(int i=sum-1;i>=0;i--)  stu[i].show();   
	}	    
	else{cout<<"Your arrange choice is error!!"<<endl;  error=99;}
	
}
void Ban::modify(int i)							//修改学生成绩,通过提问分别输入各门成绩
{
	stu[i-1].setscore(i);
}
void Ban::modify(int i,float s1,float s2,float s3)  //重载修改学生成绩,直接修改数理化成绩
{
	stu[i-1].setscore(s1,s2,s3);
}
void Ban::getTotalClass()                               //返回输出班级总数
{
	cout<<"The Classes totaled : "<<totalClass<<endl;
}
void Ban::getTotalStu()                               //返回输出
{
	cout<<"The Students totaled : "<<totalStu<<endl;
}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -