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

📄 student.cpp

📁 学生管理系统
💻 CPP
字号:
#include <iostream>                                         //引用库函数         
#include <fstream>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;                                        //引用C++的标准名字空间

void sleep(clock_t wait);                                   //清理屏幕的函数
void showTable();

#define FILEPATH "students.txt"                             //"students.txt"覆盖FILEPATH
#define MAXCOUNT 100                                        //对MAXCOUNT进行处理替换为100
#define BYID 0                                              //同上
#define BYMARK 1                                            //同上                         

struct student                                              //定义结构
{
	string id;                                              //学号
	string name;                                            //姓名
	string chinese;                                         //语文成绩
	string maths;                                           //数学成绩
	string english;                                         //英语成绩
};

class Manager                                               //定义一个类 
{
	student students[MAXCOUNT];                             //类数组变量
	fstream fs;
	int count;

public:                                                     //声明共有成员
	Manager();                                              //构造函数

	 ~Manager();                                            //析构函数

	void InputAll(int allcount);                  

	void InputOne(string id,string name,string chinese,string maths,string english);

	void Write2File();

	void ReadFromFile();

	void ShowStudents(int mod);                   

	void ShowById(int id);                       

	void ShowByName(string name);                

	void ShowSum();                              

	void Clear();                                  

	int IsExistById(int id);	

	int IsExistByName(string name);		
private:                                                   //声明私有函数
	string* SplitString(string readstr);
	string ConvertToString(int value);
	void OrderStudent(int bytype);
};

Manager::Manager()                                         //创建文件
{
	fs.open(FILEPATH,ios::in | ios::out);
	if(!fs)
	{
		fs.clear();
		fs.open(FILEPATH,ios::in | ios::out | ios::trunc);
		if(!fs)
		{
			cout<<"                程序无法创建文件!"<<endl;
			exit(1);
		}
	}
	fs.close();
	ReadFromFile();
}

Manager::~Manager()
{
	fs.clear();
	fs.close();
}
 
void Manager::InputAll(int allcount)                        //学生信息重新录入
{
	string id,name,chinese,maths,english;
	for(int i=0;i<allcount;i++)
	{cout<<"                请依次输入学号,姓名,语文成绩,数学成绩,英语成绩:"<<endl;
     cout<<"                ";
		cin>>id>>name>>chinese>>maths>>english;
		count=i+1;
		InputOne(id,name,chinese,maths,english);
	}
	Write2File();
	cout<<"                数据录入成功!!"<<endl;
	sleep((clock_t)2*CLOCKS_PER_SEC);
}

void Manager::InputOne(string id,string name,string chinese,string maths,string english)
{
	students[count-1].id=id;
	students[count-1].name=name;
	students[count-1].chinese=chinese;
	students[count-1].maths=maths;
	students[count-1].english=english;
}

void Manager::Write2File()                                  //输出保存的文件
{
	fs.open(FILEPATH,ios::out);
	for(int i=0;i<count;i++)
	{
		fs<<students[i].id<<"|"<<students[i].name<<"|"<<students[i].chinese<<"|"<<students[i].maths<<"|"<<students[i].english<<endl;
	}
	fs.clear();
	fs.close();
}

void Manager::ReadFromFile()                                  //从文件读取数据
{
	string tmpall[MAXCOUNT];
	count=0;
	fs.open(FILEPATH,ios::in);
	while(!fs.eof())
	{
		getline(fs,tmpall[count]);
		count++;
	}
	if(tmpall[count]=="")
		count--;

	string* tmpstring;
	for(int i=0;i<count;i++)
	{
		tmpstring=SplitString(tmpall[i]);
		students[i].id=*tmpstring;
		students[i].name=*(tmpstring+1);
		students[i].chinese=*(tmpstring+2);
		students[i].maths=*(tmpstring+3);
		students[i].english=*(tmpstring+4);
	}
	fs.clear();
	fs.close();
}

void Manager::ShowStudents(int mod)                              //对学生信息进行排序显示的
{
	int i;
	switch(mod)
	{
	case BYID:
		cout<<"                按学号排列输出结果如下:"<<endl;  //按学号升序显示学生信息
		cout<<"学号\t姓名\t语文\t数学\t英语"<<endl;

		OrderStudent(0);
		for(i=0;i<count;i++)
		{
			cout<<students[i].id<<'\t'<<students[i].name<<'\t'<<students[i].chinese<<'\t'<<students[i].maths<<'\t'<<students[i].english<<endl;
		}
		break;
	case BYMARK:
		int marktype;
		
		do                              
		{
			cout<<"                按哪科成绩进行排列输出?([0]语文 [1]数学 [2]英语):";
			cin>>marktype;
		}while(marktype<0 || marktype>2);

		char* p;
		switch(marktype)
		{
		case 0:
			p="语文";break;
		case 1:
			p="数学";break;
		case 2:
			p="英语";break;
		}
		cout<<"                按"<<p<<"成绩排列输出结果如下:"<<endl;//根据某科成绩降序输出
		cout<<"学号\t姓名\t语文\t数学\t英语"<<endl;

		marktype+=2;
		OrderStudent(marktype);
		for(i=0;i<count;i++)
		{
			cout<<students[i].id<<'\t'<<students[i].name<<'\t'<<students[i].chinese<<'\t'<<students[i].maths<<'\t'<<students[i].english<<endl;
		}
		break;
	}
}

void Manager::ShowById(int id)                                       //根据学号查询学生信息
{
	int tmpid=IsExistById(id);
	if(-1==tmpid)
	{
		cout<<"                没有该学号的学生"<<endl;
		return;
	}
	else
	{
		cout<<"                查询结果如下:"<<endl;
		cout<<"学号\t姓名\t语文\t数学\t英语"<<endl;
		cout<<students[tmpid].id<<'\t'<<students[tmpid].name<<'\t'<<students[tmpid].chinese<<'\t'<<students[tmpid].maths<<'\t'<<students[tmpid].english<<endl;
	}
}

void Manager::ShowByName(string name)                               //根据学生名称查询学生信息
{
	int tmpid=IsExistByName(name);
	if(-1==tmpid)
	{
		cout<<"                没有该名称的学生"<<endl;
		return;
	}
	else
	{
		cout<<"                查询结果如下:"<<endl;
		cout<<"学号\t姓名\t语文\t数学\t英语"<<endl;
		cout<<students[tmpid].id<<'\t'<<students[tmpid].name<<'\t'<<students[tmpid].chinese<<'\t'<<students[tmpid].maths<<'\t'<<students[tmpid].english<<endl;
	}
}
 
void Manager::ShowSum()                                           //计算全班各科平均分和及格率
{
	double chinese_sum=0;                                         //数据初始化
	double maths_sum=0;
	double english_sum=0;
	double chinese_pass=0;
	double maths_pass=0;
	double english_pass=0;

	for(int i=0;i<count;i++)
	{
		chinese_sum+=atof(students[i].chinese.c_str());          //计算平均分
		maths_sum+=atof(students[i].maths.c_str());
		english_sum+=atof(students[i].english.c_str());
		if(atof(students[i].chinese.c_str())>=60)                //计算及格人数
			chinese_pass++;
		if(atof(students[i].maths.c_str())>=60)
			maths_pass++;
		if(atof(students[i].english.c_str())>=60)
			english_pass++;
	}

	cout<<"                科目\t语文\t数学\t英语"<<endl;
	cout<<"                平均分\t"<<chinese_sum/count<<'\t'<<maths_sum/count<<'\t'<<english_sum/count<<endl;
	cout<<"                及格率\t"<<chinese_pass/count*100<<"%\t"<<maths_pass/count*100<<"%\t"<<english_pass/count*100<<"%"<<endl;

}

void Manager::Clear()                                           //清除文本数据
{
	fs.open(FILEPATH,ios::out | ios::trunc);
	fs.close();
	ReadFromFile();
}

int Manager::IsExistById(int id)                                //根据学号查询学生信息中用到
{
	for(int i=0;i<count;i++)
	{
		if(students[i].id==ConvertToString(id))
		{
			return i;
		}
	}
	return -1;
}

int Manager::IsExistByName(string name)                        //根据姓名查询学生信息中用到
{
	for(int i=0;i<MAXCOUNT;i++)
	{
		if(students[i].name==name)
		{
			return i;
		}
	}
	return -1;
}


string* Manager::SplitString(string readstr)
{
   int place1=readstr.find("|");
   int place2=readstr.find("|",place1+1);
   int i;

   string* tmpstring=new string[5];
   *tmpstring=readstr.substr(0,place1);

   for(i=1;i<4;i++)
   {
		*(tmpstring+i)=readstr.substr(place1+1,place2-place1-1);
		place1=place2;
		place2=readstr.find("|",place1+1);
   }

    *(tmpstring+i)=readstr.substr(place1+1);

   return tmpstring;
}

string Manager::ConvertToString(int value)
{
	  stringstream ss;
	  ss << value;
	  return ss.str();
}

void Manager::OrderStudent(int bytype)                                //在排序中用到
{
	int i,j;
	bool canorder=false;
	student tmpstu;
	for(i=0;i<count;i++)
	{
		for(j=1;j<count;j++)
		{
			switch(bytype)
			{
			case 0:
				canorder=students[j-1].id>students[j].id;break;
			case 2:
				canorder=atof(students[j-1].chinese.c_str())<atof(students[j].chinese.c_str());break;
			case 3:
				canorder=atof(students[j-1].maths.c_str())<atof(students[j].maths.c_str());break;
			case 4:
				canorder=atof(students[j-1].english.c_str())<atof(students[j].english.c_str());break;
			}
			if(canorder)
			{
				tmpstu=students[j-1];
				students[j-1]=students[j];
				students[j]=tmpstu;
			}
			canorder=false;
		}
	}
}
/************************************副函数*****************************************/ 
void showTable()                                                   //显示系统操作平台的函数
{
	system("cls");                                                 //清理屏幕
	cout<<"\n";
	cout<<"\t ╭────────────────────────────╮\n";//菜单显示
	cout<<"\t ∣             -=欢迎使用学生成绩管理系统=-               ∣\n"; 
	cout<<"\t |          ----------------------------------            |\n";
	cout<<"\t |             学校:华南理工大学                          ∣\n"; 
	cout<<"\t |             软别:软件学院四班                          ∣\n";       
	cout<<"\t |             姓名:翟煦                                  ∣\n";        
	cout<<"\t  ╰────────────────────────────╯\n";
	cout<<"\t ∣    -=菜单功能选择=-                                    ∣\n"; 
	cout<<"\t |    1. 学生信息重新录入                                 ∣\n"; 
	cout<<"\t |    2. 按学号升序显示学生信息                           ∣\n";       
	cout<<"\t |    3. 根据某科成绩降序输出                             ∣\n"; 
	cout<<"\t |    4. 根据学号查询学生信息                             ∣\n"; 
	cout<<"\t |    5. 根据学生名称查询学生信息                         ∣\n"; 
	cout<<"\t |    6. 清空学生信息                                     ∣\n"; 
	cout<<"\t |    7. 计算全班各科平均分和及格率                       ∣\n"; 
	cout<<"\t |    0. 退出管理系统                                     ∣\n";         
	cout<<"\t  ╰────────────────────────────╯\n";
	return;
}
void sleep(clock_t wait)                    //关于时间的函数,让窗口停留一段时间再进行一下语句
{
	clock_t goal;
	goal=wait+clock();
	while(goal>clock());
	return;
}
bool choice()                                                 //提供选择,判断是否继续运行程序
{
	char temp='n';
	while(temp!='Y'&&temp!='N')
	{
		cout<<"                重新选择?(Y/N)";
		cin>>temp;
	}
	if(temp=='Y')
		return true;
	else 
        cout<<"                欢迎再次使用本系统!再见!"<<endl;      //退出管理系统
		return false;
}
/************************************主函数*****************************************/ 
void main()
{ 
	Manager mycontrol;
	int num;
	int id;
	string name;
	bool temp=true;
	while(temp)
	{
		showTable();
		cout<<"                请输入操作号:";
		cin>>num;
		switch(num)
		{
		case 1:
			int allcount;
			cout<<"                请输入学生数目(0至100):";
			cin>>allcount;
			while(allcount<0 || allcount>100)
			{
				cout<<"                请输入正确的数字!"<<endl<<"                请输入学生数目(0至100):";
				cin>>allcount;
			}
			mycontrol.InputAll(allcount);
			temp=choice();
			break;
		case 2:
			mycontrol.ShowStudents(BYID);
			temp=choice();
			break;
		case 3:
			mycontrol.ShowStudents(BYMARK);
			temp=choice();
			break;
		case 4:
			cout<<"                请输入搜索的学生学号:";
			cin>>id;
			mycontrol.ShowById(id);
			temp=choice();
			break;
		case 5:
			cout<<"                请输入搜索的学生名称:";
			cin>>name;
			mycontrol.ShowByName(name);
			temp=choice();
			break;
		case 6:
			do
			{
				cout<<"                确认清空所有信息吗?(Y/N)";
				cin>>name;
			}while("Y"!=name && "N"!=name);
			if("Y"==name)
				mycontrol.Clear();
			temp=choice();
			break;
		case 7:
			mycontrol.ShowSum();
			temp=choice();
			break;
		case 0:
			cout<<"                欢迎再次使用本系统!再见!"<<endl;        //退出管理系统
			exit(1);
		default:
			cout<<"                请输入正确的操作号!"<<endl<<"                重新输入请输入任意数字按回车:";
			cin>>num;
			break; 
		}
	}
}

⌨️ 快捷键说明

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