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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#include "header.h"
char check[] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
int weight[] = { 7, 9, 10, 5, 8, 4, 2, 1 , 6, 3, 7, 9, 10, 5, 8, 4, 2 };
std::vector<std::vector<std::string>> cities; std::vector<std::string> prov; std::vector<std::string> province_name;
bool is_init = false;
void usage() { std::cout << "argument input error!!!" << std::endl; std::cout << "Usage: ./bash_version <seed> <ID_file> <choose_to_print> <choose_to_write> [gen_count]\n"; std::cout << " seed: to generate random IDs;\n"; std::cout << " choose_to_print: 0 for not print and others for print;\n"; std::cout << " choose_to_write: 0 for not write and others for write.\n"; std::cout << " gen_count: How many IDs to generate, if not given, it will be 1(default).\n"; std::cout << "WARNING: If choose_to_print is opened, The text may be garbled and the process will be shut down!!!" << std::endl; std::cout << " If there are Chinese words in ID_file, choose_to_print is suggested to be 0." << std::endl; }
void initTable(std::fstream& fin) { if (is_init) { return; } std::string name; std::string id; while (!fin.eof()) { fin >> id >> name; if (id[2] == id[3] && id[3] == id[4] && id[4] == id[5] && id[2] == '0') { if (prov.size()) { cities.emplace_back(prov); prov.clear(); } province_name.emplace_back(name); continue; } prov.emplace_back(id); } cities.emplace_back(prov); is_init = true; }
void printTable() { for (auto& cit : cities) { int i = 0; for (std::string& c : cit) { std::cout << c << ' '; ++i; if (i % 5 == 0) { std::cout << std::endl; } } } std::cout << std::endl; }
void INFO(int argc, char** argv) { std::string exe = argv[0]; unsigned seed = atoi(argv[1]); std::string file = argv[2]; int print_ = atoi(argv[3]); int write_ = atoi(argv[4]); int cnt = argc == 5 ? 1 : atoi(argv[5]); std::cout << "\n\n*****************************************************************************\n"; std::cout << "** INFO: .exe file is : " << exe << std::endl; std::cout << "** INFO: seed is : " << seed << std::endl; std::cout << "** INFO: id file is : " << file << std::endl; std::cout << "** INFO: print/write or not : " << (print_ ? "To print" : "Not to print") << "; " << (write_ ? "To write" : "Not to write") << std::endl; std::cout << "** INFO: The process will generate " << cnt << " ID(s). The output file is bash_out.txt." << std::endl; std::cout << "*****************************************************************************\n\n"; }
char GetCheckCode(const std::string &id) { int total = 0; for (int i = 0; i < 17; ++i) { total += weight[i] * (id[i] - '0'); } return check[total % 11]; }
std::string generate_id() { int maxCnt_prov = cities.size(); int idx_outer = rand() % maxCnt_prov; int maxCnt_city = cities[idx_outer].size(); int idx_inner = rand() % maxCnt_city; std::string ret = cities[idx_outer][idx_inner]; int age_year = 2021 - (18 + rand() % 18); int age_month = 1 + rand() % 12; int age_day = 1 + rand() % 28; ret += std::to_string(age_year); if (age_month < 10) { ret += "0"; } ret += std::to_string(age_month); if (age_day < 10) { ret += "0"; } ret += std::to_string(age_day); int rnd = rand() % 1000; std::string rnd_str = std::to_string(rnd); int zero_cnt = 3 - rnd_str.size(); while (zero_cnt--) { ret += "0"; } ret += rnd_str; ret.push_back(GetCheckCode(ret));
ret += "(" + province_name[idx_outer] + ")"; return ret; }
void generateIDs(std::fstream& fin, int print_, int overwrite, int loops) { fin.open("bash_out.txt", std::ios::out); if (!fin.is_open()) { std::cerr << "open file error!" << std::endl; return; } std::cout << "bash_out.txt opened succeed..." << std::endl;
if (loops <= 0) { std::cerr << "ERROR occured: ID count error! (constraint: count > 0)"; return; }
int cnt = 1; while (loops--) { std::string ret = generate_id(); if (overwrite) { fin << cnt++ << ": " << ret << std::endl; } if (print_) { std::cout << cnt++ << ": " << ret << std::endl; } } fin.close(); std::cout << "bash_out.txt closed..." << std::endl; }
int main(int argc, char** argv) { if (argc != 5 && argc != 6) { usage(); return 0; } time_t begin_t = clock(); int cnt = 1; if (argc == 6) { cnt = atoi(argv[5]); } unsigned int seed = atoi(argv[1]); srand(seed); std::fstream fin; int print_ = atoi(argv[3]); int overwrite = atoi(argv[4]); std::string file_name = argv[2]; INFO(argc, argv); fin.open(file_name, std::ios::in); if (!fin.is_open()) { std::cerr << "ERROR occured: id.txt opened failed!!!" << std::endl; return 0; } std::cout << "id.txt opened succeed..." << std::endl; initTable(fin); fin.close(); std::cout << "id.txt closed..." << std::endl; time_t gen_begin = clock(); generateIDs(fin, print_, overwrite, cnt); time_t process_end = clock();
std::cout << "ID generate cost: " << ((double)(process_end - gen_begin) * 1000 / CLOCKS_PER_SEC) << " ms;\n"; std::cout << "The whole process cost: " << ((double)(process_end - begin_t) * 1000 / CLOCKS_PER_SEC) << " ms.\n";
return 0; }
|