posts - 183,  comments - 10,  trackbacks - 0

统一的考勤处理程序

总共分为 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 阅读(148) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理