资源描述
/*题目:学生考勤管理系统
考勤信息记录了学生的缺课情况,它包括:缺课日期、第几节课(连续多节课用 begin-end 的形式表示)、课程名称(课程名称中不会出现空格)、学生姓名、缺课类型(迟到、早退、请假及旷课)。试设计一考勤管理系统,使之能提供以下功能:
1. 录入学生的缺课记录:从键盘输入数据(提示:为避免重复从键盘输入数据,测试时可将数据存储在文件中,利用输入重定向功能读入),输入格式为:
缺课日期 第几节课 课程名称 学生姓名 缺课类型
每行一条纪录。
例如:
2008-04-29 3-4 C++程序设计实验 张三 迟到
2008-04-28 3-4 C++程序设计 李四 旷课
2.修改某个学生的缺课记录:可以对缺课纪录的任意部分进行修改。
3.查询某个学生的缺课情况:查询结果按照日期升序排序,同一天内按照所缺课程的时间升序排序。
4.统计某段时间内(以天为单位),旷课学生姓名及旷课节数,查询结果先按旷课节数降序排序,旷课节数相同的学生按姓名升序排序;
5.统计某段时间内,有学生旷课的课程及旷课人次,按旷课人次由多到少排序,旷课人次相同的课程按课程名称升序排序;
6.系统以菜单方式工作。
通过几天的奋斗终于将这个设计做出来了,如下是该程序的详细过程*/
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <string>
#include <iterator>
#include <map>
using namespace std;
struct Student_info{
int year,month,day,lesson1,lesson2;
std::string course,name,type;
std::istream& read(std::istream&);
};
istream& Student_info::read(istream& in)
{
in>>year>>month>>day>>lesson1>>lesson2>>course>>name>>type;
return in;
}
istream& read_record(istream& in,vector<Student_info>& s)//第一个模块--------------输入学生的缺课记录
{
Student_info record;
s.clear();//调用s.clear()来清空s的记录
while(record.read(in))
{
s.push_back(record);
}
in.clear();//将记录的错误状态恢复正确,以继续读取记录
return in;
}
bool is_empty(vector<Student_info>& s)//判断输入学生的记录是否为空,为空则返回真
{
return s.empty();
}
bool compare(Student_info& x,Student_info& y)//按照时间排序,若日期相等则看课程时间
{
if(x.year==y.year&&x.month==y.month&&x.day==y.day){
return x.lesson1<y.lesson1;
}
else if(x.year==y.year&&x.month==y.month&&x.day!=y.day){
return x.day<y.day;
}
else if(x.year==y.year&&x.month!=y.month){
return x.month<y.month;
}
else {
return x.year<y.year;
}
}
bool time(const Student_info& stud,int y1,int m1,int d1,int y2,int m2,int d2)//筛选符合输入时间段的数据
{
if ((y1 == stud.year && stud.month == m1 && stud.day < d1) || (y2 == stud.year && stud.month == m2 && stud.day > d2))
return 0;
else if ((y1 == stud.year && stud.month < m1) || (y2 == stud.year && stud.month > m2))
return 0;
else if (y1 > stud.year || y2 < stud.year)
return 0;
else
return 1;
}
void rewrite(vector<Student_info>& s)//第二个模块--------------修改某个学生的缺课情况
{
if(!is_empty(s))
{
int n,m,j,k;
map<string,vector<Student_info> > s_name;
map<string,vector<Student_info> >::iterator ix2;
vector<Student_info>::iterator ix,iter,itera;
string name;//-------------------定义这些参数和迭代器必须在SWITHC外,不然它可能被忽略跳过
while(true)
{
cout<<" *********************************************************"<<endl;
cout<<" * 1.请输入你要修改缺课记录的学生姓名 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 2.返回上一级菜单 *"<<endl;
cout<<" *********************************************************"<<endl;
cout<<"请选择菜单选项: ";
cin>>n;
if(n==1)
{
s_name.clear();//------------------------------清空MAP容器
for(ix=s.begin();ix!=s.end();++ix)
{
s_name[(*ix).name].push_back(*ix);
}
s.clear();
cout<<"请输入该学生的姓名: ";
cin>>name;
cout<<endl;
if(s_name.find(name)!=s_name.end())
{
ix2=s_name.find(name); //----------找到符合名字要求的数据,并显示出
for(iter=(*ix2).second.begin();iter!=(*ix2).second.end();++iter)
{
cout<<(*iter).year<<"-"<<(*iter).month<<"-"<<(*iter).day<<"\t";
cout<<(*iter).lesson1<<"-"<<(*iter).lesson2;
cout<<"\t"<<(*iter).course<<"\t"<<(*iter).name<<"\t"<<(*iter).type<<endl;
}
itera=(*ix2).second.begin();
cout<<"请输入要更改记录的序号: ";
cin>>j;
for(k=0;k!=j-1;++k)
{
++itera;
}
cout<<" **************************************************************************"<<endl;
cout<<" * 需要更改的项目 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 1 日期 2 节次 3 课程名称 4 姓名 5 缺课类型 6 返回上一级 *"<<endl;
cout<<" * *"<<endl;
cout<<" **************************************************************************"<<endl;
cout<<"请选择要更改的项目号: ";
cin>>m;
switch(m)
{
case 1:
cout<<"请输入更改的日期(年 月 日): ";
cin>>(*itera).year>>(*itera).month>>(*itera).day;
break;
case 2:
cout<<"请输入更改的节次(L1、L2): ";
cin>>(*itera).lesson1>>(*itera).lesson2;
break;
case 3:
cout<<"请输入要更改的课程名称: ";
cin>>(*itera).course;
break;
case 4:
cout<<"请输入要更改的学生姓名: ";
cin>>(*itera).name;
break;
case 5:
cout<<"请输入缺课类型: ";
cin>>(*itera).type;
break;
case 6:
break;
default:
cout<<"你的选择错了,请看清楚!"<<endl;
break;
}
for(ix2=s_name.begin();ix2!=s_name.end();ix2++)//将改写后的数据放入vector容器中
{
for(iter=ix2->second.begin();iter!=ix2->second.end();iter++)
{
s.push_back(*iter);
}
}
}
else
{
cout<<"没有这个学生的缺课记录"<<endl;
cout<<endl;
}
break;
}
else if(n==2)
{
return ;
}
else
cout<<"你的选择错了,请看清楚!"<<endl;
}
}
else
cout<<"记录为空,没有学生的缺课记录!"<<endl;
}
void Search_record(vector<Student_info>& s)//第三个模块-------------查找某学生的缺课情况
{
if(!is_empty(s))
{
int i;
vector<Student_info>::iterator iter;
map<string,vector<Student_info> >::iterator it1;
map<string,vector<Student_info> > s_name;
map<string, vector<Student_info> >::iterator itera;
vector<Student_info>::iterator it;
string name;
while(true)
{
cout<<" **********************************************************"<<endl;
cout<<" * *"<<endl;
cout<<" * 1 输入你要查询的学生姓名 2 返回上一级 *"<<endl;
cout<<" * *"<<endl;
cout<<" **********************************************************"<<endl;
cout<<"请选择菜单: ";
cin>>i;
if(i==1){
s_name.clear();
for(it=s.begin();it!=s.end();++it)
{
s_name[(*it).name].push_back(*it);
}
for(itera=s_name.begin();itera!=s_name.end(); ++itera)
{
sort(itera->second.begin(),itera->second.end(),compare);
}
cout<<"请输入名字: ";
cin>>name;
cout<<endl;
if(s_name.find(name)!=s_name.end())
{
it1=s_name.find(name);
for(iter=(*it1).second.begin();iter!=(*it1).second.end();++iter)
{
cout<<(*iter).year<<"-"<<(*iter).month<<"-"<<(*iter).day<<"\t";
cout<<(*iter).lesson1<<"-"<<(*iter).lesson2;
cout<<"\t"<<(*iter).course<<"\t"<<(*iter).name<<"\t"<<(*iter).type<<endl;
}
}
else
{
cout<<"没有这个学生."<<endl;
cout<<endl;
}
}
else if(i==2)
{
return ;
}
else
cout<<"对不起,没有这个选项!"<<endl;
}
}
else
cout<<"没有这个学生的缺课记录"<<endl;
}
struct Search_struct{
string name,course;
int times;
};//定义另一个结构模块
bool compare2(Search_struct& x,Search_struct& y)//按照次数排序,次数相等的则按照名字排序
{
if(x.times==y.times)
return x.name<y.name;
else
return x.times>y.times;
}
void Search_s_t(vector<Student_info>& std)//第四个模块----------------查找某段时间内旷课学生姓名及旷课节数
{
if(!is_empty(std))
{
vector<Student_info>::iterator it;
vector<Search_struct>::iterator itera;
map<string,int>::iterator iter;
vector<Search_struct> vec;
int y1,m1,d1,y2,m2,d2;
map<string,int> ret;
cout<<"请输入你要查找的一个时间段(y1 m1 d1 y2 m2 d2): "<<endl;
cin>>y1>>m1>>d1>>y2>>m2>>d2;
for(it=std.begin();it!=std.end();++it)
{
if(time(*it,y1,m1,d1,y2,m2,d2))
{
ret[it->name]+=(it->lesson2-it->lesson1+1);
}
}
for(iter=ret.begin();iter!=ret.end();iter++)
{
Search_struct tmp;
tmp.name=iter->first;
tmp.times=iter->second;
vec.push_back(tmp);
}
sort(vec.begin(),vec.end(),compare2);
for(itera=vec.begin();itera!=vec.end();itera++)
{
cout<<itera->name<<"\t"<<itera->times<<endl;
}
}
else
cout<<"对不起,没有你要查找的记录!"<<endl;
}
void Search_c_t(vector<Student_info>& std)//第五个模块---------------查找某段时间内旷课的课程及旷课人次
{
if(!is_empty(std))
{
vector<Student_info>::iterator it;
vector<Search_struct>::iterator itera;
map<string,int>::iterator iter;
vector<Search_struct> vec;
int y1,m1,d1,y2,m2,d2;
map<string,int> ret;
cout<<"请输入你想要查找的时间段(y1 m1 d1 y2 m2 d2): "<<endl;
cin>>y1>>m1>>d1>>y2>>m2>>d2;
for(it=std.begin();it!=std.end();++it)//建立map容器
{
if(time(*it,y1,m1,d1,y2,m2,d2))
{
ret[it->course]+=1;
}
}
for(iter=ret.begin();iter!=ret.end();iter++)
{
Search_struct tmp;
tmp.course=iter->first;
tmp.times=iter->second;
vec.push_back(tmp);
}
sort(vec.begin(),vec.end(),compare2);
for(itera=vec.begin();itera!=vec.end();itera++)
{
cout<<itera->course<<"\t"<<itera->times<<endl;
}
}
else
cout<<"这段时间内没有学生的缺课纪录!"<<endl;
}
int main()
{
vector<Student_info> stu;
int choice;
while(true)
{
cout<<" *************************************************************"<<endl;
cout<<" * 学生考勤管理系统 *"<<endl;
cout<<" ************************************************************ "<<endl;
cout<<" * 1. 录入学生的缺课记录 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 2. 修改某个学生的缺课记录 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 3. 查找某学生的缺课情况 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 4. 查找某段时间内旷课学生姓名及旷课节数 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 5. 查找某段时间内旷课的课程及旷课人次 *"<<endl;
cout<<" * *"<<endl;
cout<<" * 6. 退出系统 *"<<endl;
cout<<" *************************************************************"<<endl;
cout<<"请选择菜单选项: ";
cin>>choice;
if(choice==1){
cout<<"请输入数据: "<<endl<<"年 月 日 节次 姓名 课程名称 缺课类型 "<<endl;
read_record(cin,stu);
cout<<endl;
}
else if(choice==2){
rewrite(stu);
cout<<endl;
}
else if(choice==3){
Search_record(stu);
cout<<endl;
}
else if(choice==4){
Search_s_t(stu);
cout<<endl;
}
else if(choice==5){
Search_c_t(stu);
cout<<endl;
}
else if(choice==6){
return 0;
}
else
cout<<"对不起,没有这个菜单项!"<<endl;
}
return 0;
}
5771001803090012095 579036822859633082
5771001803090012386 576137399735760696
5771001803090013594 578077579902515512
5771001803090012387 577164982601818051
5771001803090012138 572131192158918326
5771001803090012359 579036822361076053
5771001803090012356 576135286143791742
5771001803090012355 575087869704693279
17088100343355274 101229944325833379
17088100343355275 101866732938832008
17088100343356107 101581152501500522
17088100343356108 101000180059871732
17088100343354295 101074194142687017
17088100343356184 101878660869628802
17088100343356185 101775831174086674
17088100343356109 101086014373572846
17088100343356110 101152207216014916
17088100343355237 101027041605702709
17088100343355238 101229364861425414
17088100343356169 101862204402635718
17088100343354928 101760654089788804
展开阅读全文