統一的考勤處理程序
總共分為 6 個處理步驟,如下:
0. 進行考勤數據的預處理,其中有篩選打卡成功的記錄,并對成功的記錄進行排序。這一步比較靈活,根據不同的考勤數據做不同的預處理以得到后續可以處理的數據。
1. 針對打卡成功的考勤記錄,找出每天的最早時間和最晚時間。
2. 根據最早時間和最晚時間得到遲到和早退記錄。
3. 由于可能存在缺勤的情況,所以需要在制定工作日內找到未存在考勤記錄的記錄,作為缺勤記錄。
4. 將前面的遲到和早退記錄與缺勤記錄進行整合。
5. 根據整合的結果輸出考勤異常的記錄。輸出有多種形式,前面的遲到、早退、缺勤等數據一定了,這里的輸出格式可以按照:按人名-行輸出、按人名-列輸出、按日期-行輸出、按日期-列輸出、人名-日期輸出、日期-人名輸出等多種方式。
具體的程序實現如下:
0.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ifstream fin_kaoqin("kaoqin.txt");
ofstream fout_0("0.txt");
if (!fin_kaoqin || !fout_0)
{
cerr << "File error!" << endl;
system("PAUSE");
exit(1);
}
vector<string> checkins;
string name, date, time;
string s1, s2, s3, ind;
string line;
while (getline(fin_kaoqin, line))
{
istringstream sin(line);
sin >> s1 >> s2 >> name >> date >> time >> s3 >> ind;
if (ind != "成功")
{
continue;
}
checkins.push_back(name + '\t' + date + '\t' + time);
}
sort(checkins.begin(), checkins.end());
for (vector<string>::size_type i = 0; i != checkins.size(); ++i)
{
fout_0 << checkins[i] << endl;
}
fin_kaoqin.close();
fout_0.close();
return 0;
}
1.
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream fin("0.txt");
ofstream fout("1.txt");
ofstream fout_names("names.txt");
ofstream fout_dates("dates.txt");
if (!fin || !fout || !fout_names || !fout_dates)
{
cerr << "File error!" << endl;
system("PAUSE");
exit(1);
}
map<string, vector<string> > checkins;
set<string> names, dates;
string line;
string name, date, time;
while (getline(fin, line))
{
istringstream sin(line);
sin >> name >> date >> time;
names.insert(name);
dates.insert(date);
checkins[name + '\t' + date].push_back(time);
}
string earliest, lastest;
for (map<string, vector<string> >::const_iterator cit = checkins.begin(); cit != checkins.end(); ++cit)
{
earliest = lastest = cit->second[0];
for (vector<string>::size_type i = 0; i != cit->second.size(); ++i)
{
if (earliest > cit->second[i])
{
earliest = cit->second[i];
}
else if (lastest < cit->second[i])
{
lastest = cit->second[i];
}
}
fout << cit->first << '\t' << earliest << endl;
fout << cit->first << '\t' << lastest << endl;
}
for (set<string>::const_iterator cit = names.begin(); cit != names.end(); ++cit)
{
fout_names << *cit << endl;
}
for (set<string>::const_iterator cit = dates.begin(); cit != dates.end(); ++cit)
{
fout_dates << *cit << endl;
}
fin.close();
fout.close();
fout_names.close();
fout_dates.close();
return 0;
}
2.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
struct checkin
{
string name;
string date;
string time;
};
void foo(ofstream& fout_nor, ofstream& fout_abn, const checkin& ci1, const checkin& ci2, const string& start, const string& end)
{
if (ci1.time <= start)
{
fout_nor << ci1.name << '\t' << ci1.date << '\t' << ci1.time << endl;
}
else
{
fout_abn << ci1.name << '\t' << ci1.date << '\t' << ci1.time << "遲到" << endl;
}
if (ci2.time >= end)
{
fout_nor << ci2.name << '\t' << ci2.date << '\t' << ci2.time << endl;
}
else
{
fout_abn << ci2.name << '\t' << ci2.date << '\t' << ci2.time << "早退" << endl;
}
}
int main()
{
ifstream fin("1.txt");
ofstream fout_nor("2-normal.txt"), fout_abn("2-abnormal.txt");
if (!fin || !fout_nor || !fout_abn)
{
cerr << "File error!" << endl;
system("PAUSE");
exit(1);
}
string line1, line2;
checkin ci1, ci2;
string start = "083000", end = "173000";
while (getline(fin, line1) && getline(fin, line2))
{
istringstream sin1(line1), sin2(line2);
sin1 >> ci1.name >> ci1.date >> ci1.time;
sin2 >> ci2.name >> ci2.date >> ci2.time;
foo(fout_nor, fout_abn, ci1, ci2, start, end);
}
fin.close();
fout_nor.close();
fout_abn.close();
return 0;
}
3.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;
int main()
{
ifstream fin_ci("0.txt");
ifstream fin_name("names-filter.txt");
ifstream fin_date("dates-filter.txt");
ofstream fout_nowork("2.1-nowork.txt");
if (!fin_ci || !fin_name || !fin_date || !fout_nowork)
{
cerr << "File error!" << endl;
system("PAUSE");
exit(1);
}
map<string, set<string> > name_date;
string line, name, date;
while (getline(fin_ci, line))
{
istringstream sin(line);
sin >> name >> date;
name_date[name].insert(date);
}
vector<string> names;
while (fin_name >> line)
{
names.push_back(line);
}
vector<string> dates;
while (fin_date >> line)
{
dates.push_back(line);
}
for (vector<string>::size_type i = 0; i != names.size(); ++i)
{
for (vector<string>::size_type j = 0; j != dates.size(); ++j)
{
if (name_date[names[i]].find(dates[j]) == name_date[names[i]].end())
{
fout_nowork << names[i] << '\t' << dates[j] << '\t' << "無出勤記錄!" << endl;
}
}
}
fin_ci.close();
fin_name.close();
fin_date.close();
fout_nowork.close();
return 0;
}
4.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ifstream fin_abnormal("2-abnormal.txt");
ifstream fin_nowork("2.1-nowork.txt");
ofstream fout_excep("2.2-exception.txt");
if (!fin_abnormal || !fin_nowork || !fout_excep)
{
cerr << "File error!" << endl;
system("PAUSE");
exit(1);
}
vector<string> checkins;
string line;
while (getline(fin_abnormal, line))
{
checkins.push_back(line);
}
while (getline(fin_nowork, line))
{
checkins.push_back(line);
}
sort(checkins.begin(), checkins.end());
for (vector<string>::size_type i = 0; i != checkins.size(); ++i)
{
fout_excep << checkins[i] << endl;
}
fin_abnormal.close();
fin_nowork.close();
fout_excep.close();
return 0;
}
5.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;
struct date_time
{
string date;
string time;
};
int main()
{
ifstream fin_ci("2.2-exception.txt");
ifstream fin_names("names-filter.txt");
ifstream fin_dates("dates-filter.txt");
ofstream fout_row("3-name-row-filter.txt");
ofstream fout_col("3-name-column-filter.txt");
if (!fin_ci || !fout_row || !fout_col || !fin_names || !fin_dates)
{
cerr << "File error!" << endl;
system("PAUSE");
exit(1);
}
map<string, vector<date_time> > bypers;
vector<string> names;
string line, name;
date_time dt;
int MAXNUM = 0;
// 讀取想要的人名
set<string> names_filter;
while (fin_names >> name)
{
names_filter.insert(name);
}
// 讀取想要的日期
set<string> dates_filter;
while (fin_dates >> line)
{
dates_filter.insert(line);
}
while (getline(fin_ci, line))
{
istringstream sin(line);
sin >> name >> dt.date >> dt.time;
// 提取想要的人名和日期
if (names_filter.find(name) == names_filter.end() || dates_filter.find(dt.date) == dates_filter.end())
{
continue;
}
bypers[name].push_back(dt);
}
// 同名字下相同日期的合并
for (map<string, vector<date_time> >::iterator cit = bypers.begin(); cit != bypers.end(); ++cit)
{
// 提取名字
names.push_back(cit->first);
vector<date_time> tmp, hold = cit->second;
vector<date_time>::size_type i = 0;
while (i < hold.size() - 1)
{
if (hold[i].date == hold[i+1].date)
{
dt.date = hold[i].date;
dt.time = hold[i].time + hold[i + 1].time;
tmp.push_back(dt);
i += 2;
}
else
{
dt.date = hold[i].date;
dt.time = hold[i].time;
tmp.push_back(dt);
++i;
}
}
// 最后兩個如果相等,那么 i 一下子回跳到 hold.size()。
// 如果不相等,那么 i 變成 hold.size() - 1, 推出循環。
// 也就是說推出循環有兩種情況,分別是 i 為 hold.size() 和 hold.size() - 1
// i 為 hold.size() 的情況沒有遺漏記錄,i 為 hold.size() - 1 遺漏了數據,所以在此補充。
if (i == hold.size() - 1)
{
dt.date = hold[i].date;
dt.time = hold[i].time;
tmp.push_back(dt);
++i;
}
// 記錄最大記錄個數
if (MAXNUM < tmp.size())
{
MAXNUM = tmp.size();
}
cit->second = tmp;
}
// 按行輸出
for (map<string, vector<date_time> >::const_iterator cit = bypers.begin(); cit != bypers.end(); ++cit)
{
fout_row << cit->first << '\t';
for (vector<date_time>::size_type i = 0; i != cit->second.size(); ++i)
{
fout_row << cit->second[i].date << '\t' << cit->second[i].time << '\t';
}
fout_row << endl;
}
// 先輸出人名
for (vector<string>::size_type i = 0; i != names.size(); ++i)
{
fout_col << names[i] << '\t';
}
fout_col << endl;
// 按列輸出
for (int i = 0; i != MAXNUM; ++i)
{
for (vector<string>::size_type j = 0; j != names.size(); ++j)
{
if (bypers[names[j]].size() > i)
{
fout_col << bypers[names[j]][i].date << ',' << bypers[names[j]][i].time << '\t';
}
else
{
fout_col /* << "XXXXXX" */ << '\t';
// 這里的輸出 "XXXXXX" 可以省略,即可以直接 fout_col << '\t';
}
}
fout_col << endl;
}
fin_ci.close();
fout_row.close();
fout_col.close();
return 0;
}
posted on 2012-10-29 17:04
unixfy 閱讀(162)
評論(0) 編輯 收藏 引用