2021年4月,为了简化班级早自习考勤签到流程,我利用校园卡,独立制作了一个简易考勤打卡系统。整个打卡系统一共花了我32元人民币。
首先我在网购软件上购买了一个能读取IC卡卡号的读卡器(现在网购软件上随手一找最便宜的那种就行了,现在好像20元左右就可以了,我的主要是不确定我们校园卡的ID号长度所以买的读取长度长),将所有同学的校园卡卡号存储到CSV文件中并与学号和姓名一一对应。
image.png
然后是程序的编写——通过C++文件流对CSV文件读取得到学生信息并存储到结构体中,运用标志数组判断每个同学是否已考勤……反正实现其实很简单,主要是知道读卡器上刷卡相当于在电脑在编辑的文件or程序框里面输入卡号并换行。
之后的早自习考勤,我只需要将读卡器连上电脑并运行程序,班级同学在读卡器刷一下校园卡或输入学号就可以进行考勤。
image.png
在考勤过程中,我还可以通过自己的查询命令,实时查询当次还没打卡的同学名单。同时,考勤程序实时将考勤信息输出到CSV文件,考勤结束后我可以通过输出的文件得知各位同学考勤的具体时间。
image.png
以下给出具体代码
#include<cstdio>
#include<iostream>
#include<cstring>
#include<windows.h>
#include<time.h>
using namespace std;
struct Student{
string CardNum,StudentID,Name;
int Class,Num;
bool Sex;
}student[10000];
bool IsBeated[5050];
bool Checked[5050];
static string GetCurrentDateStr()
{
time_t t = time(NULL);
char ch[64] = {0};
strftime(ch, sizeof(ch) - 1, "%Y%m%d%p", localtime(&t));
return ch;
}
static string GetCurrentTimeStr()
{
time_t t = time(NULL);
char ch[64] = {0};
strftime(ch, sizeof(ch) - 1, "%H:%M:%S", localtime(&t));
return ch;
}
int main(){
cout<<"----------------------------------------------------------"<<endl;
cout<<"Welcome to AA Grade 2020,Class 1 of UESTC BeatCard System!"<<endl;
cout<<"----------------------------------------------------------"<<endl<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<"Loading data..."<<endl;
freopen("data.csv","r",stdin);
// freopen("datatry.csv","r",stdin);
string temp;
int num1=0;
while(cin>>temp){
student[num1].CardNum=temp.substr(1,18);
student[num1].StudentID=temp.substr(21,13);
student[num1].Name=temp.substr(37,temp.length());
if(temp.substr(35,1).compare("b")==0)student[num1].Sex=1;
else student[num1].Sex=0;
char* ID=(char*)student[num1].StudentID.substr(8,5).c_str();
student[num1].Class=(ID[0]-'0')*10+(ID[1]-'0');
student[num1].Num=(ID[3]-'0')*10+(ID[4]-'0');
num1++;
}
// for(int i=0;i<num1;i++)cout<<student[i].Name<<' '<<student[i].StudentID<<' '<<student[i].CardNum<<endl;
cin.clear();
for(int i=0;i<num1;i++){
IsBeated[student[i].Class*100+student[i].Num]=0;
}
cout<<"Data loading sucessfully! Ready to BeatCard!"<<endl;
cout<<"--------------------------------------------"<<endl<<endl;
cout<<"---------------------------------------------"<<endl;
cout<<"Just put your student ID card on the machine."<<endl;
cout<<"Enter \"quit\" / \"exit\" to exit the system."<<endl;
cout<<"---------------------------------------------"<<endl<<endl;
cout<<"------------------"<<endl;
freopen("CON","r",stdin);
string Filename;
string Time=GetCurrentDateStr();
Filename="log//"+Time+".csv";
freopen(Filename.c_str(),"a",stdout);
cout<<"StudentID"<<','<<"Class"<<','<<"Num"<<','<<"Name"<<','<<"EnterTime"<<endl;
flag:
while(cin>>temp){
Time=GetCurrentTimeStr();
if(temp.compare("exit")==0||temp.compare("quit")==0)break;
bool IsFound=0;
// cout<<temp<<endl;
for(int i=0;i<num1&&!IsFound;i++){
if(temp.compare(student[i].CardNum)==0||temp.compare(student[i].StudentID)==0){
IsFound=1;
if(IsBeated[student[i].Class*100+student[i].Num]==1){
freopen("CON","w",stdout);
cout<<"You had beat the card!"<<endl<<endl;
freopen(Filename.c_str(),"a",stdout);
break;
}
IsBeated[student[i].Class*100+student[i].Num]=1;
cout<<"#"+student[i].StudentID<<','<<student[i].Class<<','<<student[i].Num<<','<<student[i].Name<<','<<Time<<endl;
freopen("CON","w",stdout);
string Adj;
if(student[i].Sex)Adj="handsome ";
else Adj="beautiful ";
cout<<"The time is "+Time<<endl;
cout<<"Welcome "+Adj+student[i].Name<<" !"<<endl<<endl;
freopen(Filename.c_str(),"a",stdout);
}
}
if(IsFound==0){
freopen("CON","w",stdout);
cout<<"Error, please change another card!"<<endl<<endl;
freopen(Filename.c_str(),"a",stdout);
}
}
freopen("CON","w",stdout);
cout<<"------------------"<<endl;
cout<<endl<<"-------------------------------------"<<endl;
memset(Checked,0,sizeof(Checked));
int NoHereNum=0;
for(int i=0;i<num1;i++){
if(IsBeated[student[i].Class*100+student[i].Num]==0&&Checked[student[i].Class*100+student[i].Num]==0){
NoHereNum++;
cout<<"Class."<<student[i].Class<<" NO.";
if(student[i].Num<10)cout<<'0';
cout<<student[i].Num;
if(student[i].Name.length()==4)cout<<" ";
cout<<' '<<student[i].Name;
if(student[i].Name.length()==4)cout<<" ";
cout<<" is not here!"<<endl;
Checked[student[i].Class*100+student[i].Num]=1;
// IsBeated[student[i].Class][student[i].Num]=1;
}
}
if(NoHereNum==0)cout<<"Congratulations! All people are here!"<<endl;
cout<<"-------------------------------------"<<endl<<endl;
cout<<"--------------------------------------------------------------"<<endl;
cout<<"Enter \"quit\" / \"exit\" to quit or enter \"continue\" to continue."<<endl;
cout<<"--------------------------------------------------------------"<<endl<<endl;
cout<<"------------------"<<endl;
while(cin>>temp){
if(temp.compare("exit")==0||temp.compare("quit")==0)break;
if(temp.compare("continue")==0){
cout<<"------------------"<<endl<<endl;
cout<<"------------------"<<endl;
freopen(Filename.c_str(),"a",stdout);
goto flag;
}
}
cout<<"------------------"<<endl;
return 0;
}
小彩蛋~看看当时学期始末的考勤签到情况表!绿色是请假,黄色是迟到,红色是缺勤。有坏蛋一直不来!!!
学期初考勤情况
学期末考勤情况