1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #include <iostream> #include <string> #include <regex> #include <fstream> #include <ctime> #include <iomanip> #include <sstream> #include <string> using namespace std; string line; //存储每一行读取内容 smatch match; //存储正则匹配后内容 fstream fs1, fs2,fs3,fs4; //获取本地时间函数 std::string getTime() { // 获取当前时间 time_t now = time(NULL);
// 将当前时间转换为本地时间 struct tm *local_tm = localtime(&now);
// 使用 strftime 格式化时间 char buffer[80]; strftime(buffer, 80, "%Y%m%d", local_tm);
// 将 buffer 转换为 std::string 并返回 std::string time = buffer; return time; } //主函数 int main() { fs1.open("id_card.txt"); fs2.open("result.txt",ios::out | ios::trunc);
if (fs1.is_open() && fs2.is_open()) { cout << "1 Open successfully" << endl;
string line; smatch match; regex key1(R"((\d{17})(\d|X))"); // 读取18位数字的身份证号
while (getline(fs1, line)) { if (regex_search(line, match, key1)) { fs2 << match.str(0) << endl; } }
fs1.close(); // 关闭id_card.txt fs2.close(); // 关闭result.txt
fs2.open("result.txt",ios::in); fs3.open("result2.txt",ios::out | ios::trunc); if (fs3.is_open() && fs2.is_open()) { cout << "2 Open successfully" << endl;
regex key2(R"((\d{17})(\d))");//读取前17位置和最后一位 string nums_17; //存储前17位 string nums_18; //存储第18位 while (getline(fs2, line)) { if(regex_search(line,match,key2)){ nums_17 = match.str(1); nums_18 = match.str(2); //开始计算效验码 https://zhidao.baidu.com/question/174364608.html int coefficients[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; int a = 0; //a是乘系数求和的结果 int mod[11] = {1, 0, 10, 9,8,7,6,5,4,3,2}; for(int i=0;i<17;i++){ a += (nums_17[i] - '0') * coefficients[i]; } int b = a % 11; //b是余数 char c; // c用于存储就散出的校验码 if(b == 2){ c = 'X'; }else{ c = mod[b] + '0'; }
if(c == nums_18[0]){ //校验码正确后执行代码 fs3 << nums_17 << nums_18[0] << endl; } } } } fs2.close(); fs3.close(); fs3.open("result2.txt",ios::in); fs4.open("result3.txt",ios::out | ios::trunc); if (fs3.is_open() && fs4.is_open()) { cout << "3 Open successfully" << endl; regex key3(R"(\d{6}(\d{8})\d{4})"); //使用match.str(1)捕获匹配组 string now_time; string get_time; while(getline(fs3,line)){ if(regex_search(line,match,key3)){ now_time = getTime(); //获取当前时间 get_time = match.str(1); if(get_time > now_time){ cout << "恭喜世界上第一位未来人诞生!" << endl; }else{ fs4 << line << endl; } }
}
} fs3.close(); fs4.close();
} else { cout << "Open failed" << endl; }
return 0; }
|