Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include "database.h"
- #include "gen.h"
- int main() {
- 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);
- Database db("C:\\Users\\Pavel\\CLionProjects\\untitled\\db.txt");
- return 0;
- }
- #include <iostream>
- #include <ctime>
- #include <fstream>
- #include <vector>
- #include <string>
- #include "gen.h"
- void gen(std::string finname, std::string finfam, std::string finotch, std::string foutname, int SIZE) {
- std::srand(unsigned(std::time(0)));
- int y, m, d, t;
- std::string fio, ip, date, tin, tout, p;
- std::vector<std::string> trin, trout, fim,ffam,fotch;
- std::ifstream file(finname);
- while (!file.eof()){
- file >> p;
- fim.push_back(p);
- }
- file.close();
- std::ifstream file1(finfam);
- while (!file1.eof()){
- file1 >> p;
- ffam.push_back(p);
- }
- file1.close();
- std::ifstream file2(finotch);
- while (!file2.eof()){
- file2 >> p;
- fotch.push_back(p);
- }
- file2.close();
- std::ofstream file3(foutname);
- for (int i = 0;i<SIZE;i++)
- {
- fio = ffam[rand() % ffam.size()] + " " + fim[rand() % fim.size()] + " " + fotch[rand() % fotch.size()];
- file3 << fio + "\t";
- ip = std::to_string(rand() % 255) + "." + std::to_string(rand() % 255) + "." + std::to_string(rand() % 255) + "." + std::to_string(rand() % 255);
- file3 << ip + "\t";
- y = 2015 + rand() % 4;
- m = 1 + rand() % 12;
- // std::cout << "Record added successfully loop" << std::endl;
- switch (m) {
- case 1:
- d = 1 + rand() % 31;
- break;
- case 3:
- d = 1 + rand() % 31;
- break;
- case 5:
- d = 1 + rand() % 31;
- break;
- case 7:
- d = 1 + rand() % 31;
- break;
- case 8:
- d = 1 + rand() % 31;
- break;
- case 10:
- d = 1 + rand() % 31;
- break;
- case 12:
- d = 1 + rand() % 31;
- break;
- case 4:
- d = 1 + rand() % 30;
- break;
- case 6:
- d = 1 + rand() % 30;
- break;
- case 9:
- d = 1 + rand() % 30;
- break;
- case 11:
- d = 1 + rand() % 30;
- break;
- default:
- if (y % 4 == 0) {
- d = 1 + rand() % 29;
- } else {
- d = 1 + rand() % 28;
- }
- }
- date = std::to_string(y) + "-";
- date += std::string(2 - std::to_string(m).length(), '0') + std::to_string(m) + '-';
- date += std::string(2 - std::to_string(d).length(), '0') + std::to_string(d);
- file3 << date + "\t";
- tin = "( ";
- tout = "( ";
- for (int j = 0; j < 24; j++) {
- t = rand() % 10000;
- tin += std::to_string(t) + " ";
- t = rand() % 10000;
- tout += std::to_string(t) + " ";
- }
- tin += ")";
- tout += ")";
- file3 << tin + "\t";
- file3 << tout + "\t";
- if (i < SIZE - 1) file3 << '\n';
- // std::cout << "Record added successfully loop2" << std::endl;
- }
- file3.close();
- }
- #ifndef INC_4TERM_GEN_H
- #define INC_4TERM_GEN_H
- void gen(std::string finname, std::string finfam, std::string finotch, std::string foutname, int SIZE);
- #endif //INC_4TERM_GEN_H
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- #include "database.h"
- Database::Database() {
- std::cout << "no database file specified" << std::endl;
- }
- Database::Database(std::string fname) {
- dbname = fname;
- std::ifstream file(fname);
- if (!file.is_open()) {
- std::cout << "file doesn't exist" << std::endl;
- return;
- }
- record t;
- int c;
- std::string z;
- while (!file.eof()) {
- std::getline(file,t.fio,'\t');
- std::cout << t.fio << std::endl;
- std::getline(file,t.ip,'\t');
- std::cout << t.ip<< std::endl;
- std::getline(file,t.date,'\t');
- std::cout << t.date << std::endl;
- std::getline(file,t.tin,'\t');
- std::cout << t.tin << std::endl;
- std::getline(file,t.tout,'\t');
- std::cout << t.tout << std::endl;
- c++;
- std::getline(file,z,'\n');
- db.push_back(t);
- }
- std::cout << "added: " << db.size() << std::endl;
- file.close();
- }
- Database::~Database() {
- std::ofstream temp;
- temp.open("temp");
- for (size_t i = 0; i < db.size(); i++) {
- temp << db[i].fio + '\t' + db[i].ip + '\t' +
- db[i].date + '\t' + db[i].tin + '\t' +
- db[i].tout;
- if (i < db.size() - 1) temp << '\n';
- }
- temp.close();
- std::remove(dbname.c_str());
- std::rename("temp", dbname.c_str());
- }
- void Database::delete_recs(std::vector<int> s) {
- std::sort(s.begin(), s.end());
- while (!s.empty()) {
- //std::cout << s.back() << std::endl;
- db.erase(db.begin() + s.back());
- s.pop_back();
- }
- std::cout << "current size is : " << db.size() << std::endl;
- }
- void Database::add_rec(const std::string &fio, const std::string &ip,
- const std::string &date, const std::string &tin, const std::string &tout) {
- record t = {fio, ip, date, tin, tout};
- db.push_back(t);
- std::cout << "Record added successfully" << std::endl;
- }
- void Database::add_rec(record t) {
- std::cout << "Record added successfully" << std::endl;
- db.push_back(t);
- }
- std::vector<int> Database::select_recs(const std::string &ip_min, const std::string &ip_max,
- const std::string &date_min, const std::string &date_max,
- const std::string &tin_min, const std::string &tin_max,
- const std::string &tout_min, const std::string &tout_max) {
- std::vector<int> s;
- record t;
- for (size_t i = 0; i < db.size(); i++) {
- t = db[i];
- std::string d = "0";
- if (((ip_min == d && ip_max == d) || (t.ip>= ip_min && t.ip <= ip_max))
- &&((date_min == d && date_max == d) || (t.date >= date_min && t.date <= date_max))
- && ((tin_min == d && tin_max == d) || (t.tin >= tin_min && t.tin <= tin_max))
- &&((tout_min == d && tout_max == d) || (t.tout >= tout_min && t.tout <=tout_max)))
- s.push_back(i);
- }
- std::cout << "selected: " << s.size() << std::endl;
- return s;
- }
- #ifndef INC_4TERM_DATABASE_H
- #define INC_4TERM_DATABASE_H
- struct record {
- std::string fio, ip, date, tin, tout;
- };
- class Database{
- private:
- std::vector<record> db;
- std::string dbname;
- public:
- Database();
- explicit Database(std::string);
- ~Database();
- void delete_recs(std::vector<int>);
- void add_rec(const std::string&, const std::string&,
- const std::string&, const std::string&,
- const std::string&);
- void add_rec(record);
- std::vector<int> select_recs(const std::string&, const std::string&,
- const std::string&, const std::string&,
- const std::string&, const std::string&);
- };
- #endif //INC_4TERM_DATABASE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement