c++对象数组模学生成绩管理系统Demo


一、设计要求

  • 写一个学生成绩管理系统,包含学号姓名成绩等学生信息。
  • 将数据存入二进制文件.dat中。
  • 实现对学生信息的显示查询修改等功能。

二、设计说明

创建了一个Student类和一个StudentMS类。Student类用来存储学生个人信息,StudentMS类操作这些信息.。Num和number作为两个全局变量,分别代表数组角标和当前存储的人数。(之所以要这样定义是为了从1开始使用对象数组,使数组角标与当前存储人数相等。)
每次录入一个学生Num和number都要加1。而每次修改则在update()中调用Student类的set()方法。

三、实现效果

在这里插入图片描述
在这里插入图片描述

四、给出完整代码

#include<iostream>
#include<string>
#include<fstream> 

using namespace std;
/*
	设计要求:
		使用c++模拟学生成绩管理系统 
		1.包含学号,姓名,成绩.√
		2.将数据存储在.dat二进制文件 √
		3.实现对信息的显示√ 
		4.实现对信息的查询 √
		5.实现对信息的修改 √ 
	@Hudie.
*/

static int Num = 1;//充当数组角标
static int number = 0; //学生个数 
//学生类 
class Student
{
private:
	string Id; 
	string Name; 
	int Chinese;
	int Math;
	int English;
	int Sum;
public:	
	string getId(); 
	string getName(); 
	int setChinese(int chinese);
	int getChinese();
	int setMath(int math); 
	int getMath(); 
	int setEnglish(int english);
	int getEnglish(); 
	int getSum();//学生总分 
	int getAverage();//学生平均分 
	//add()函数录入学生信息--->通过构造函数赋值
	void add(string id,string name,int chinese,int math,int english);
	//sum()函数获取总分 
	void sum(int Chinese,int Math,int English);
	
	//对单科成绩进行update后的总分 
	void upCsum(int temp);
	void upMsum(int temp);
	void upEsum(int temp);
	

};

//-----------------------Student学生类方法实现------------↓ 
string Student::getId()
{
	return Id;
}

string Student::getName()
{
	return Name;
}

int Student::setChinese(int chinese)
{
	Chinese = chinese;
}
int Student::getChinese()
{
	return Chinese;
}

int Student::setMath(int math)
{
	Math = math;
}

int Student::getMath()
{
	return Math;
}

int Student::setEnglish(int english)
{
	English = english;
}
int Student::getEnglish()
{
	return English;
}

int Student::getSum()
{
	return Sum;
}

int Student::getAverage()
{
	return (Chinese+Math+English)/3;
} 


void Student::add(string id,string name,int chinese,int math,int english)
{//将参数传递给学生对象 
	Id = id;
	Name = name;
	Chinese = chinese;
	Math = math;
	English = english;
} 

void Student::sum(int Chinese,int Math,int English)
{
	Sum = Chinese+Math+English;
}

void Student::upCsum(int temp)
{
	Sum = temp + Math + English;
}

void Student::upMsum(int temp)
{
	Sum = Chinese + temp + English;
}

void Student::upEsum(int temp)
{
	Sum = Chinese + Math + temp;
}

//-----------------------------------------------------------↑


//学生成绩信息系统类 
class StudentMS{
private:
	Student str[10001];//学生对象数组,人为规定使用[1-10000]共1000个大小
	int num;
public:
	void add();//录入学生信息 
	void show();//显示学生信息 
	void update();//数据更新 
	void select();//查找学生信息 
};

//----------------------------StudentMS学生成绩系统类方法实现--------------↓ 
void StudentMS::add()
{//add()方法作用:录入学生信息(直接录入二进制文件)

	//学生信息输入 
	string Id,Name;
	int Chinese,Math,English;
	cout<<"请输入Id:";
	cin>>Id;
	cout<<"请输入姓名:";
	cin>>Name;
	cout<<"请输入Chinese成绩:"; 
	cin>>Chinese;
	cout<<"请输入 Math  成绩:";
	cin>>Math;
	cout<<"请输入English成绩:";
	cin>>English;	
	
	//向add()和sum()函数传递参数 
	str[Num].add(Id,Name,Chinese,Math,English);
	str[Num].sum(Chinese,Math,English);
 
 	//对学生信息进行存储 
	//外存调用内存,使用ofstream 
	ofstream ofs("StudentMS.dat",ios::out |ios_base::binary);
	if(!ofs){
		cerr<<"存储失败!"<<endl; 
	}
	
	for(int i = 1;i<=number;i++){
		ofs.write(reinterpret_cast<char*>(&str[i]),sizeof(str[i]));
		ofs.flush(); 
	}
	ofs.close();
	
	number++;//学生人数+1 
	Num++;//数组角标+1
}

void StudentMS::show()
{//show()方法作用:学生成绩分析(读取二进制文件)

	//内存调用外存,使用ifstream 

	ifstream ifs("StudentMS.dat",ios::in | ios_base::binary);
	
	if(ifs){
		cout<<"Id"<<"\t"<<"Name"<<"\t"<<"Chinese"<<"\t\t"<<"Math"<<"\t"<<"English"<<"\t\t"<<"平均分"<<"\t"<<"总分"<<endl; 
		for(int i=1;i<=number;i++){
			ifs.read(reinterpret_cast<char*>(&str),sizeof(str[i]));
			cout<<str[i].getId()<<"\t"<<str[i].getName()<<"\t"<<str[i].getChinese()<<"\t\t"<<str[i].getMath()<<"\t"<<str[i].getEnglish()<<"\t\t"<<str[i].getAverage()<<"\t"<<str[i].getSum()<<"\t"<<endl;
		}
	}else{
		cout<<"读取失败"<<endl;	
	} 
	ifs.close();
} 

void StudentMS::update()
{
	string id;
	int n,temp;
	cout<<"------------------------------------------"<<endl; 
	cout<<"请输入需要修改成绩学生的Id:";
	cin>>id;
	for(int i=1;i<=number;i++){
		if(id.compare(str[i].getId())==0){
			cout<<"------------------------------------------"<<endl; 
			cout<<"请输入需要修改的成绩序号(1.Chinese,2.Math,3.English):";
			cin>>n;	
			switch(n){
				case 1: //修改Chinese 
					cout<<"------------------------------------------"<<endl; 
					cout<<"请输入修改后的中文成绩:";
					cin>>temp;
					str[i].setChinese(temp);
					str[i].upCsum(temp);
					break;
				case 2://修改Math 
					cout<<"------------------------------------------"<<endl; 
					cout<<"请输入修改后的高数成绩:";
					cin>>temp;
					str[i].setMath(temp);
					str[i].upMsum(temp);
					break;
				case 3://修改English
					cout<<"------------------------------------------"<<endl; 
					cout<<"请输入修改后的英语成绩:";
					cin>>temp;
					str[i].setEnglish(temp);
					str[i].upEsum(temp);
					break;
				default:
					cout<<"输入不合法!"; 
		}
			break;
		}
	}


}

void StudentMS::select()
{//查找 
	cout<<"------------------------------------------"<<endl; 
	cout<<"请输入需要查找的学生Id:";
	string id="";
	cin>>id;
 
	for(int i=1;i<=number;i++){
		if(id.compare(str[i].getId())==0){
			cout<<"------------------------------------------"<<endl; 
			cout<<"查找成功!"<<endl; 
			cout<<"Id"<<"\t"<<"Name"<<"\t"<<"Chinese"<<"\t\t"<<"Math"<<"\t"<<"English"<<"\t\t"<<"平均分"<<"\t"<<"总分"<<endl;
			cout<<str[i].getId()<<"\t"<<str[i].getName()<<"\t"<<str[i].getChinese()<<"\t\t"<<str[i].getMath()<<"\t"<<str[i].getEnglish()<<"\t\t"<<str[i].getAverage()<<"\t"<<str[i].getSum()<<"\t"<<endl;
			break;
		}
	}
}

//----------------------------------------------------↑ 
StudentMS ss;
void view();//视图层 

int main(){ 
	Student stu[1001];
	view();
	
	while(true){
		int n = -1;
		cout<<"------------------------------------------"<<endl; 
		cout<<"请输入序号进行操作:"; 
		cin>>n;
		switch(n){
			case 1:
				cout<<"------------------------------------------"<<endl; 
				cout<<"您选择了[1.录入学生信息],下面请按照提示完成操作!"<<endl;
				ss.add();
				cout<<"信息录入成功!(已存入StudentMS.dat二进制文件)"<<endl;
				break;
			case 2:
				cout<<"------------------------------------------"<<endl; 
				cout<<"您选择了[2.修改学生信息],下面请按照提示完成操作!"<<endl;
				ss.update();
				cout<<"信息修改成功.(已存入StudentMS.dat二进制文件中)"<<endl;
				break;
			case 3:
				cout<<"------------------------------------------"<<endl; 
				cout<<"您选择了[3.查询学生信息],下面请按照提示完成操作!"<<endl;
				ss.select(); 
				break; 
			case 4:
				cout<<"------------------------------------------"<<endl; 
				cout<<"您选择了[4.学生成绩分析],下面展示StudentMS.bat二进制文件:"<<endl;
				ss.show();
			 	break;
			case 0:
				cout<<"------------------------------------------"<<endl; 
				cout<<"您选择了[0.退出],您已退出本产品,谢谢使用!"<<endl;
				exit(0);
				break;
			default:
				cout<<"------------------------------------------"<<endl; 
				cout<<"输入不合法,请重新输入:"; 
		}
		
	}
	return 0; 
}

void view()//视图层 
{
	cout<<"\t\t**********************************************************"<<endl;
	cout<<"\t\t**\t\t欢迎进入学生成绩管理系统1.0"<<"\t\t"<<"**"<<endl;
	cout<<"\t\t**\t\t1.录入学生信息"<<"\t\t\t\t"<<"**"<<endl;
	cout<<"\t\t**\t\t2.修改学生成绩"<<"\t\t\t\t"<<"**"<<endl;
	cout<<"\t\t**\t\t3.查询学生成绩"<<"\t\t\t\t"<<"**"<<endl;	
	cout<<"\t\t**\t\t4.学生成绩分析"<<"\t\t\t\t"<<"**"<<endl;	
	cout<<"\t\t**\t\t0.退出系统"<<"\t\t\t\t"<<"**"<<endl;
	cout<<"\t\t**********************************************************"<<endl<<endl;
}

发布了328 篇原创文章 · 获赞 798 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/weixin_43691058/article/details/103662562
今日推荐