Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <ctime>
- #include <string>
- #include <iostream>
- #include <algorithm>
- 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>
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- inline bool print_str(std::string str, int k, int n)
- {
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(str.size()); l++)
- {
- std::cout << str[l];
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- return true;
- }
- struct node
- {
- int data;
- struct node *next;
- struct node *prev;
- };
- void add(struct node** start, double value)
- {
- if (*start == nullptr)
- {
- struct node* new_node = new node;
- new_node->data = value;
- new_node->next = new_node->prev = new_node;
- *start = new_node;
- return;
- }
- node *last = (*start)->prev;
- struct node* new_node = new node;
- new_node->data = value;
- new_node->next = *start;
- (*start)->prev = new_node;
- new_node->prev = last;
- last->next = new_node;
- }
- /* Function to sort an array using insertion sort*/
- void insertion_sort(struct node *start)
- {
- struct node *temp = start->next, *curr;
- int key, pos = 0;
- while (temp != start)
- {
- curr = temp->next;
- key = temp->data;
- while (key < temp->prev->data && temp != start)
- {
- temp->data = temp->prev->data;
- temp = temp->prev;
- }
- temp->data = key;
- while (temp != curr)
- temp = temp->next;
- }
- }
- //Prints linked list
- void display(struct node* start)
- {
- struct node *temp = start;
- int k = 100;
- print_str("\nTraversal in forward direction \n", 10, 10);
- while (temp->next != start && k--)
- {
- printf("%d", temp->data);
- print_str(", ", 11, 4);
- temp = temp->next;
- }
- std::cout << "\b\b " << std::endl;
- }
- void rand_fill(struct node **list, int n) //Randomly generates numbers and fills vector
- {
- int i = 0;
- while (i < n) //Fills vector with absolutely random numbers
- {
- add(&*list, rand());
- i++;
- }
- }
- void nearly_fill(struct node **list, int n)
- {
- for (int k = rand(), i = n; n > i / 2; k = k - rand() % 6 + 1, n--)
- add(&*list, k);
- for (int i = rand(); n; i = i + rand() % 6 + 1, n--)
- add(&*list, i);
- }
- //Reverse fill with 0-3 step
- void reverse_fill(struct node **list, int n)
- {
- for (int k = rand(); n; k = k - rand() % 4, n--)
- add(&*list, k);
- }
- //Function for repeating cycle
- bool yes_no(const std::string& str)
- {
- bool err = false;
- print_str(str, 15, 10);
- std::string temp;
- std::getline(std::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(std::cin, temp);
- err = true;
- }
- } while (err);
- std::cout << "An error occurred!" << std::endl;
- return true;
- }
- //Checks for empty input in console
- bool empty(const std::string& str)
- {
- bool empty = true;
- if (!str.empty())
- {
- for (unsigned int i = 0; i < str.length(); i++)
- {
- if (str[i] != ' ')
- {
- empty = false;
- }
- }
- }
- return empty;
- }
- //Choice of future actions
- int choice(std::string str)
- {
- std::string rnd = "Random", reverse = "Reverse", nearly = "Nearly", display = "display";
- if (levenshtein_distance(str, rnd) < 3)
- return 1;
- if (levenshtein_distance(str, nearly) < 3)
- return 2;
- if (levenshtein_distance(str, reverse) < 3)
- return 3;
- if (levenshtein_distance(str, display) < 3)
- return 4;
- return 0;
- }
- //Some funny error check
- void read_error(bool& only_digits, bool& negative, int& dot_count, std::string& input)
- {
- const std::string null_err = "You wrote nothing, stop it";
- const std::string mess_err = "S-stop testing me, you baka! >/////<";
- const std::string neg_str_err = "How do you think letters would be negative? Are you so silly? Please";
- const std::string neg_dot_err = "You are not supposed to write negative fractional number here";
- const std::string dot_str_err = "Developer doesn't know what to write here, but this is a error, so please";
- const std::string string_err = "This is not a number, do you think i can read it? Please";
- const std::string dot_err = "This number can't be fractional, you silly. Please";
- const std::string neg_err = "You number is way too negative";
- const std::string repeat = ", type again: ";
- if (!only_digits && dot_count != 0 && negative) { print_str(mess_err, 13, 15); print_str(repeat, 13, 15); }
- else if (!only_digits && negative) { print_str(neg_str_err, 4, 15); print_str(repeat, 4, 15); }
- else if (!only_digits && dot_count != 0) { print_str(dot_str_err, 4, 15); print_str(repeat, 4, 15); }
- else if (dot_count != 0 && negative) { print_str(neg_dot_err, 4, 15); print_str(repeat, 4, 15); }
- else if (!only_digits) { print_str(string_err, 4, 15); print_str(repeat, 4, 15); }
- else if (dot_count != 0) { print_str(dot_err, 4, 15); print_str(repeat, 4, 15); }
- else if (negative) { print_str(neg_err, 4, 15); print_str(repeat, 4, 15); }
- else if (empty(input)) { print_str(null_err, 4, 15); print_str(repeat, 4, 15); }
- }
- //Checks for digit, can process cyrillic
- bool is_digit(char s)
- {
- if (s >= '0' && s <= '9') return true;
- else return false;
- }
- int read_uint()
- {
- //Checks variable for correct input (number)
- std::string input;
- getline(std::cin, input);
- int dot_count = 0, k = 0;
- bool only_digits = true, negative = false;
- while (!only_digits || dot_count == 0 || input.length() == 0)
- {
- if (input[0] == '-')
- {
- k = 1;
- negative = true;
- }
- for (int i = input.length() - 1; i >= k; i--)
- {
- if (!is_digit(input[i]))
- {
- if (input[i] == '.' || input[i] == ',') dot_count += 1;
- else only_digits = false;
- }
- }
- if (only_digits && dot_count == 0 && !negative && input.length() != 0)
- {
- return strtol(input.c_str(), NULL, 0);
- }
- else
- {
- read_error(only_digits, negative, dot_count, input);
- getline(std::cin, input);
- only_digits = true;
- dot_count = 0;
- negative = false;
- k = 0;
- }
- }
- return 0;
- }
- void destroy(struct node *list, int n)
- {
- struct node *temp;
- int k = 0;
- while(k++ < n)
- {
- temp = list->next;
- free(list);
- list = temp;
- }
- }
- int main()
- {
- std::string str;
- int n;
- do
- {
- struct node* list = nullptr;
- print_str("Write number of elements of your list: ", 10, 10);
- do
- {
- n = read_uint();
- }
- while (n == 0 && print_str("Why would you write zero elements for list? Type again: ", 4, 15));
- print_str("Do you want to fill list ", 10, 10);
- std::cout << "random";
- print_str(" , ", 10, 10);
- std::cout << "nearly";
- print_str(" sorted or ", 10, 10);
- std::cout << "reverse";
- print_str("? ", 10, 10);
- getline(std::cin, str);
- do
- {
- if (choice(str) == 1)
- rand_fill(&list, n);
- else if (choice(str) == 2)
- nearly_fill(&list, n);
- else if (choice(str) == 3)
- reverse_fill(&list, n);
- else if (choice(str) == 4)
- display(list);
- }
- while (!choice(str) && print_str("Please, type [random/nearly/reverse]: ", 13, 10) && getline(std::cin, str));
- display(list);
- double t = clock(); //Clock function from ctime, gets current clock time and logs it into "t" integer
- insertion_sort(list);
- t = (clock() - static_cast<double>(t)) / CLOCKS_PER_SEC; //Gets current time minus previous logged time
- display(list);
- print_str("Time in seconds of your life wasted on sorting: ", 11, 10);
- std::cout << t << std::endl;
- destroy(list, n);
- }while (yes_no("Do you want to continue? "));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement