1. 程式人生 > >課程設計------學生考勤管理系統

課程設計------學生考勤管理系統

#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<<"年 月 日 L1 L2 姓名 課程名稱 缺課型別 "<<endl;
read_record(cin,stu);
}
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;
}