Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <regex>
- #include <streambuf>
- #include <filesystem>
- int matrix[3][10] =
- { //S, A1, A2, A3, A4, A5, A6, A7, A8, A9
- 1, 2, 0, 4, 5, 0, 7, 8, 9, 10, //A for 0-9
- 0, 0, 3, 0, 0, 6, 0, 0, 0, 0, //A for "-./"
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //S for everything else
- };
- int row(char& a)
- {
- if (a >= '0' && a <= '9')
- return 0;
- if (a == '.' || a == '-' || a == '/')
- return 1;
- return 2;
- }
- void auto_rifle(std::string& str, std::string& str_out)
- {
- std::string date = "";
- int state = 0;
- for (unsigned long long k = 0; k < str.size(); k++)
- {
- if (state == 0)
- date.clear();
- state = matrix[(row(str[k]))][state];
- date.push_back(str[k]);
- if (state == 10)
- {
- str_out += date + " ";
- state = 0;
- }
- }
- return;
- }
- template <typename T>
- typename T::size_type levenshtein_distance(const T & src, const T & dst)
- {
- const typename T::size_type m = src.size();
- const typename T::size_type n = dst.size();
- if (m == 0)
- {
- return n;
- }
- if (n == 0)
- {
- return m;
- }
- std::vector<std::vector<typename T::size_type>> matrix(m + 1);
- for (typename T::size_type i = 0; i <= m; ++i)
- {
- matrix[i].resize(n + 1);
- matrix[i][0] = i;
- }
- for (typename T::size_type i = 0; i <= n; ++i)
- {
- matrix[0][i] = i;
- }
- typename T::size_type above_cell, left_cell, diagonal_cell, cost;
- for (typename T::size_type i = 1; i <= m; ++i)
- {
- for (typename T::size_type j = 1; j <= n; ++j)
- {
- cost = src[i - 1] == dst[j - 1] ? 0 : 1;
- above_cell = matrix[i - 1][j];
- left_cell = matrix[i][j - 1];
- diagonal_cell = matrix[i - 1][j - 1];
- matrix[i][j] = std::min(std::min(above_cell + 1, left_cell + 1), diagonal_cell + cost);
- }
- }
- return matrix[m][n];
- }
- #include <Windows.h>
- using namespace std;
- using namespace experimental::filesystem;
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- inline bool print_str(string str, int k, int n)
- {
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(str.size()); l++)
- {
- cout << str[l];
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- return true;
- }
- bool yes_no()
- {
- bool err = false;
- string temp;
- getline(cin, temp);
- do
- {
- err = false;
- if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return true; // Returning result for yes answer
- else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return false; // For no answer
- else
- {
- // If input is incorrect
- print_str("Type [Yes] or [No]: ", 15, 15);
- getline(cin, temp);
- err = true;
- }
- }
- while (err);
- cout << "An error occurred!" << endl;
- return true;
- }
- bool yes_no(const string& str)
- {
- bool err = false;
- print_str(str, 15, 10);
- string temp;
- getline(cin, temp);
- do
- {
- err = false;
- if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return true; // Returning result for yes answer
- else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return false; // For no answer
- else
- {
- // If input is incorrect
- print_str("Type [Yes] or [No]: ", 15, 15);
- getline(cin, temp);
- err = true;
- }
- }
- while (err);
- cout << "An error occurred!" << endl;
- return true;
- }
- string make_path(string file, string& directory)
- {
- string extension;
- for (unsigned int i = file.size() - 4; i < file.size(); i++)
- {
- extension.push_back(file[i]);
- }
- if (extension == ".txt")
- file = directory + "\\" + file;
- else
- file = directory + "\\" + file + ".txt";
- return file;
- }
- void filling_strings(vector<string>& vec)
- {
- unsigned int max_l = 0;
- for (unsigned int i = 0; i < vec.size(); i++)
- {
- if (vec[i].length() > max_l)
- max_l = vec[i].size();
- }
- for (unsigned int i = 0; i < vec.size(); i++)
- {
- while (vec[i].length() < max_l)
- vec[i].push_back(' ');
- }
- }
- string shorting_string(string str)
- {
- while (str[str.length() - 1] == ' ')
- str.pop_back();
- return str;
- }
- string vec_search(const vector<string>& vec, const string& str)
- {
- unsigned int i = 0, k = 0, dist = 100;
- while (k < vec.size())
- {
- if (levenshtein_distance(vec[k], str) < dist)
- {
- dist = levenshtein_distance(vec[k], str);
- i = k;
- }
- k++;
- }
- return vec[i];
- }
- void regex_result(string& str, vector<string>& vec, const regex& rgx)
- {
- regex_token_iterator<string::iterator> rend;
- regex_token_iterator<string::iterator> a(str.begin(), str.end(), rgx);
- while (a != rend) vec.push_back(*a++);
- }
- bool empty(const string& str)
- {
- bool empty = true;
- if (!str.empty())
- {
- for (unsigned int i = 0; i < str.length(); i++)
- {
- if (str[i] != ' ')
- {
- empty = false;
- }
- }
- }
- return empty;
- }
- /*
- namespace fs = std::filesystem;
- int main()
- {
- fs::create_directories("sandbox/a/b");
- std::ofstream("sandbox/file1.txt");
- std::ofstream("sandbox/file2.txt");
- for(auto& p: fs::directory_iterator("sandbox"))
- std::cout << p << '\n';
- fs::remove_all("sandbox");
- }
- */
- void memory(string& directory)
- {
- ifstream in;
- string temp;
- in.open("Logs.txt");
- if (in.is_open())
- {
- temp.assign((istreambuf_iterator<char>(in)),
- istreambuf_iterator<char>());
- print_str("Do you want to continue from \"", 13, 10); cout << temp; print_str("\"? ", 13, 10);
- if (yes_no())
- {
- directory = temp;
- return;
- }
- }
- print_str("Write path: ", 11, 10);
- getline(cin, directory);
- }
- void files(vector<string>& vec, string& directory)
- {
- bool err, no_txt = false;
- do {
- err = false, no_txt = true;
- memory(directory);
- for (auto p : directory_iterator(directory))
- {
- if (p.path().string().length() > directory.length() + 5)
- vec.push_back(p.path().string());
- }
- if (vec.size() == 0)
- {
- print_str("This path doesn't exist or it's empty.\n", 12, 10);
- err = true;
- continue;
- }
- for (unsigned int i = 0; i < vec.size(); i++)
- {
- string extension;
- for (unsigned int j = vec[i].size() - 4; j < vec[i].size(); j++)
- {
- extension.push_back(vec[i][j]);
- }
- if (extension == ".txt")
- {
- no_txt = false;
- break;
- }
- }
- if (no_txt)
- {
- print_str("There is no txt file.\n", 12, 10);
- }
- } while (err || no_txt);
- filling_strings(vec);
- ofstream logs("Logs.txt");
- logs << directory;
- logs.close();
- }
- void check_files(vector<string>& vec, string& directory)
- {
- unsigned int i;
- vector<string> vec_2;
- for (auto p : directory_iterator(directory))
- {
- vec_2.push_back(p.path().string());
- }
- filling_strings(vec_2);
- if (vec_2.size() != vec.size())
- {
- vec.clear();
- vec = vec_2;
- filling_strings(vec);
- }
- else
- {
- i = vec.size();
- for (unsigned j = 0; j < i; j++)
- {
- if (vec[j] != vec_2[j])
- {
- vec = vec_2;
- return;
- }
- }
- }
- }
- int main()
- {
- print_str(
- "Hello user!\
- \nThis is a program for loading files and searching for earliest date in them. \
- I am your personal assistant, AI Cortana.\
- \nI have few featerues like: \n",
- 3, 5);
- print_str(
- "1. Finding existing date (within 32 days and month and more than 12 months) \nincluding leap years;\
- \n2. Suggesting file names if input was incorrect (Pretty unstable, needs \nto be modified);\
- \n3. Looping for easy multiple requests.\
- \nHave fun!\n",
- 10, 5);
- //smatch match;
- //regex date(
- // "(((0[1-9]|[12][0-9]|3[01])[-/.](0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[-/.](0[469]|11)|(0[1-9]|1\\d|2[0-8])[-/.]02)[-/.]\\d{4}|29[-/.]02[-/.](\\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))",
- // regex_constants::nosubs);
- vector<string> books;// , dates;
- string file, read, directory;
- files(books, directory);
- do
- {
- print_str("Write file's name: ", 15, 10);
- do
- {
- getline(cin, file);
- }
- while (empty(file) && cout << "Why would you write nothing? Type again: ");
- ifstream in;
- check_files(books, directory);
- //dates.clear();
- in.open(make_path(file, directory));
- if (!in.is_open())
- {
- print_str("Did you mean \"", 15, 10);
- cout << shorting_string(vec_search(books, make_path(file, directory)));
- print_str("\"?\n", 15, 10);
- if (yes_no())
- in.open(vec_search(books, make_path(file, directory)));
- }
- read.assign((istreambuf_iterator<char>(in)),
- istreambuf_iterator<char>());
- //string auto_dates = "";
- cout << read;
- //auto_rifle(read, auto_dates);
- //regex_result(auto_dates, dates, date);
- if (!in.is_open())
- print_str("Couldn't load the file.\n", 7, 10);
- //else if (dates.empty())
- // print_str("There is no dates in this text file.\n", 15, 10);
- /*else
- {
- string final_date = dates[0];
- for (unsigned int i = 1; i < dates.size(); i++)
- {
- int k = 0;
- int j = dates[i].length() - 4;
- while (final_date[j] == dates[i][j])
- {
- if (j == dates[i].length() - 1)
- {
- j -= 7;
- }
- if (j == dates[i].length() - 6)
- {
- j -= 4;
- k++;
- if (k == 2)
- break;
- }
- j++;
- }
- if (final_date[j] > dates[i][j])
- final_date = dates[i];
- }
- print_str("The earliest date is ", 15, 15);
- cout << final_date << endl;
- }*/
- in.close();
- }
- while (yes_no("\nDo you want to continue?\n"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement