Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include "database.h"
  5. #include "gen.h"
  6.  
  7. int main() {
  8. gen("C:\\Users\\Pavel\\CLionProjects\\untitled\\name.txt","C:\\Users\\Pavel\\CLionProjects\\untitled\\fam.txt","C:\\Users\\Pavel\\CLionProjects\\untitled\\otch.txt","C:\\Users\\Pavel\\CLionProjects\\untitled\\db.txt",100);
  9. Database db("C:\\Users\\Pavel\\CLionProjects\\untitled\\db.txt");
  10. return 0;
  11. }
  12.  
  13. #include <iostream>
  14. #include <ctime>
  15. #include <fstream>
  16. #include <vector>
  17. #include <string>
  18. #include "gen.h"
  19.  
  20.  
  21. void gen(std::string finname, std::string finfam, std::string finotch, std::string foutname, int SIZE) {
  22. std::srand(unsigned(std::time(0)));
  23. int y, m, d, t;
  24. std::string fio, ip, date, tin, tout, p;
  25. std::vector<std::string> trin, trout, fim,ffam,fotch;
  26. std::ifstream file(finname);
  27. while (!file.eof()){
  28. file >> p;
  29. fim.push_back(p);
  30. }
  31. file.close();
  32. std::ifstream file1(finfam);
  33. while (!file1.eof()){
  34. file1 >> p;
  35. ffam.push_back(p);
  36. }
  37. file1.close();
  38. std::ifstream file2(finotch);
  39. while (!file2.eof()){
  40. file2 >> p;
  41. fotch.push_back(p);
  42. }
  43. file2.close();
  44. std::ofstream file3(foutname);
  45. for (int i = 0;i<SIZE;i++)
  46. {
  47. fio = ffam[rand() % ffam.size()] + " " + fim[rand() % fim.size()] + " " + fotch[rand() % fotch.size()];
  48. file3 << fio + "\t";
  49. ip = std::to_string(rand() % 255) + "." + std::to_string(rand() % 255) + "." + std::to_string(rand() % 255) + "." + std::to_string(rand() % 255);
  50. file3 << ip + "\t";
  51. y = 2015 + rand() % 4;
  52. m = 1 + rand() % 12;
  53. // std::cout << "Record added successfully loop" << std::endl;
  54. switch (m) {
  55. case 1:
  56. d = 1 + rand() % 31;
  57. break;
  58. case 3:
  59. d = 1 + rand() % 31;
  60. break;
  61. case 5:
  62. d = 1 + rand() % 31;
  63. break;
  64. case 7:
  65. d = 1 + rand() % 31;
  66. break;
  67. case 8:
  68. d = 1 + rand() % 31;
  69. break;
  70. case 10:
  71. d = 1 + rand() % 31;
  72. break;
  73. case 12:
  74. d = 1 + rand() % 31;
  75. break;
  76.  
  77.  
  78. case 4:
  79. d = 1 + rand() % 30;
  80. break;
  81. case 6:
  82. d = 1 + rand() % 30;
  83. break;
  84. case 9:
  85. d = 1 + rand() % 30;
  86. break;
  87. case 11:
  88. d = 1 + rand() % 30;
  89. break;
  90.  
  91.  
  92. default:
  93. if (y % 4 == 0) {
  94. d = 1 + rand() % 29;
  95. } else {
  96. d = 1 + rand() % 28;
  97. }
  98. }
  99. date = std::to_string(y) + "-";
  100. date += std::string(2 - std::to_string(m).length(), '0') + std::to_string(m) + '-';
  101. date += std::string(2 - std::to_string(d).length(), '0') + std::to_string(d);
  102. file3 << date + "\t";
  103. tin = "( ";
  104. tout = "( ";
  105. for (int j = 0; j < 24; j++) {
  106. t = rand() % 10000;
  107. tin += std::to_string(t) + " ";
  108. t = rand() % 10000;
  109. tout += std::to_string(t) + " ";
  110. }
  111. tin += ")";
  112. tout += ")";
  113. file3 << tin + "\t";
  114. file3 << tout + "\t";
  115. if (i < SIZE - 1) file3 << '\n';
  116.  
  117. // std::cout << "Record added successfully loop2" << std::endl;
  118. }
  119. file3.close();
  120. }
  121.  
  122. #ifndef INC_4TERM_GEN_H
  123. #define INC_4TERM_GEN_H
  124.  
  125. void gen(std::string finname, std::string finfam, std::string finotch, std::string foutname, int SIZE);
  126.  
  127. #endif //INC_4TERM_GEN_H
  128.  
  129. #include <iostream>
  130. #include <vector>
  131. #include <fstream>
  132. #include <algorithm>
  133. #include "database.h"
  134.  
  135. Database::Database() {
  136. std::cout << "no database file specified" << std::endl;
  137. }
  138.  
  139. Database::Database(std::string fname) {
  140. dbname = fname;
  141. std::ifstream file(fname);
  142. if (!file.is_open()) {
  143. std::cout << "file doesn't exist" << std::endl;
  144. return;
  145. }
  146. record t;
  147. int c;
  148. std::string z;
  149. while (!file.eof()) {
  150. std::getline(file,t.fio,'\t');
  151. std::cout << t.fio << std::endl;
  152. std::getline(file,t.ip,'\t');
  153. std::cout << t.ip<< std::endl;
  154. std::getline(file,t.date,'\t');
  155. std::cout << t.date << std::endl;
  156. std::getline(file,t.tin,'\t');
  157. std::cout << t.tin << std::endl;
  158. std::getline(file,t.tout,'\t');
  159. std::cout << t.tout << std::endl;
  160. c++;
  161. std::getline(file,z,'\n');
  162. db.push_back(t);
  163. }
  164. std::cout << "added: " << db.size() << std::endl;
  165. file.close();
  166.  
  167. }
  168.  
  169. Database::~Database() {
  170. std::ofstream temp;
  171. temp.open("temp");
  172. for (size_t i = 0; i < db.size(); i++) {
  173. temp << db[i].fio + '\t' + db[i].ip + '\t' +
  174. db[i].date + '\t' + db[i].tin + '\t' +
  175. db[i].tout;
  176. if (i < db.size() - 1) temp << '\n';
  177. }
  178. temp.close();
  179. std::remove(dbname.c_str());
  180. std::rename("temp", dbname.c_str());
  181. }
  182.  
  183. void Database::delete_recs(std::vector<int> s) {
  184. std::sort(s.begin(), s.end());
  185. while (!s.empty()) {
  186. //std::cout << s.back() << std::endl;
  187. db.erase(db.begin() + s.back());
  188. s.pop_back();
  189. }
  190. std::cout << "current size is : " << db.size() << std::endl;
  191. }
  192.  
  193. void Database::add_rec(const std::string &fio, const std::string &ip,
  194. const std::string &date, const std::string &tin, const std::string &tout) {
  195. record t = {fio, ip, date, tin, tout};
  196. db.push_back(t);
  197. std::cout << "Record added successfully" << std::endl;
  198. }
  199.  
  200. void Database::add_rec(record t) {
  201. std::cout << "Record added successfully" << std::endl;
  202. db.push_back(t);
  203. }
  204.  
  205. std::vector<int> Database::select_recs(const std::string &ip_min, const std::string &ip_max,
  206. const std::string &date_min, const std::string &date_max,
  207. const std::string &tin_min, const std::string &tin_max,
  208. const std::string &tout_min, const std::string &tout_max) {
  209. std::vector<int> s;
  210. record t;
  211. for (size_t i = 0; i < db.size(); i++) {
  212. t = db[i];
  213. std::string d = "0";
  214. if (((ip_min == d && ip_max == d) || (t.ip>= ip_min && t.ip <= ip_max))
  215. &&((date_min == d && date_max == d) || (t.date >= date_min && t.date <= date_max))
  216. && ((tin_min == d && tin_max == d) || (t.tin >= tin_min && t.tin <= tin_max))
  217. &&((tout_min == d && tout_max == d) || (t.tout >= tout_min && t.tout <=tout_max)))
  218. s.push_back(i);
  219. }
  220. std::cout << "selected: " << s.size() << std::endl;
  221. return s;
  222. }
  223.  
  224. #ifndef INC_4TERM_DATABASE_H
  225. #define INC_4TERM_DATABASE_H
  226.  
  227. struct record {
  228. std::string fio, ip, date, tin, tout;
  229. };
  230.  
  231. class Database{
  232. private:
  233. std::vector<record> db;
  234. std::string dbname;
  235. public:
  236. Database();
  237. explicit Database(std::string);
  238. ~Database();
  239. void delete_recs(std::vector<int>);
  240. void add_rec(const std::string&, const std::string&,
  241. const std::string&, const std::string&,
  242. const std::string&);
  243. void add_rec(record);
  244. std::vector<int> select_recs(const std::string&, const std::string&,
  245. const std::string&, const std::string&,
  246. const std::string&, const std::string&);
  247.  
  248.  
  249. };
  250.  
  251.  
  252. #endif //INC_4TERM_DATABASE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement