Advertisement
Mike_be

ProgLang4 v6

Apr 6th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.44 KB | None | 0 0
  1. #include <vector>
  2. #include <ctime>
  3. #include <string>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7.  
  8. template <typename T>
  9. typename T::size_type levenshtein_distance(const T & src, const T & dst)
  10. {
  11.     const typename T::size_type m = src.size();
  12.     const typename T::size_type n = dst.size();
  13.     if (m == 0)
  14.     {
  15.         return n;
  16.     }
  17.     if (n == 0)
  18.     {
  19.         return m;
  20.     }
  21.  
  22.     std::vector<std::vector<typename T::size_type>> matrix(m + 1);
  23.  
  24.     for (typename T::size_type i = 0; i <= m; ++i)
  25.     {
  26.         matrix[i].resize(n + 1);
  27.         matrix[i][0] = i;
  28.     }
  29.     for (typename T::size_type i = 0; i <= n; ++i)
  30.     {
  31.         matrix[0][i] = i;
  32.     }
  33.  
  34.     typename T::size_type above_cell, left_cell, diagonal_cell, cost;
  35.  
  36.     for (typename T::size_type i = 1; i <= m; ++i)
  37.     {
  38.         for (typename T::size_type j = 1; j <= n; ++j)
  39.         {
  40.             cost = src[i - 1] == dst[j - 1] ? 0 : 1;
  41.             above_cell = matrix[i - 1][j];
  42.             left_cell = matrix[i][j - 1];
  43.             diagonal_cell = matrix[i - 1][j - 1];
  44.             matrix[i][j] = std::min(std::min(above_cell + 1, left_cell + 1), diagonal_cell + cost);
  45.         }
  46.     }
  47.  
  48.     return matrix[m][n];
  49. }
  50.  
  51. #include <Windows.h>
  52.  
  53. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  54.  
  55. inline bool print_str(std::string str, int k, int n)
  56. {
  57.     SetConsoleTextAttribute(hConsole, k);
  58.     for (int l = 0; l < int(str.size()); l++)
  59.     {
  60.         std::cout << str[l];
  61.         Sleep(n);
  62.     }
  63.     SetConsoleTextAttribute(hConsole, 14);
  64.     return true;
  65. }
  66.  
  67. struct node
  68. {
  69.     int data;
  70.     struct node *next;
  71.     struct node *prev;
  72. };
  73.  
  74. void add(struct node** start, double value)
  75. {
  76.     if (*start == nullptr)
  77.     {
  78.         struct node* new_node = new node;
  79.         new_node->data = value;
  80.         new_node->next = new_node->prev = new_node;
  81.         *start = new_node;
  82.         return;
  83.     }
  84.     node *last = (*start)->prev;
  85.     struct node* new_node = new node;
  86.     new_node->data = value;
  87.     new_node->next = *start;
  88.     (*start)->prev = new_node;
  89.     new_node->prev = last;
  90.     last->next = new_node;
  91. }
  92.  
  93. /* Function to sort an array using insertion sort*/
  94. void insertion_sort(struct node *start)
  95. {
  96.     struct node *temp = start->next, *curr;
  97.     int key, pos = 0;
  98.     while (temp != start)
  99.     {
  100.         curr = temp->next;
  101.         key = temp->data;
  102.         while (key < temp->prev->data && temp != start)
  103.         {
  104.             temp->data = temp->prev->data;
  105.             temp = temp->prev;
  106.         }
  107.         temp->data = key;
  108.         while (temp != curr)
  109.             temp = temp->next;
  110.     }
  111. }
  112.  
  113. //Prints linked list
  114. void display(struct node* start)
  115. {
  116.     struct node *temp = start;
  117.     int k = 100;
  118.     print_str("\nTraversal in forward direction \n", 10, 10);
  119.     while (temp->next != start && k--)
  120.     {
  121.         printf("%d", temp->data);
  122.         print_str(", ", 11, 4);
  123.         temp = temp->next;
  124.     }
  125.     std::cout << "\b\b  " << std::endl;
  126. }
  127.  
  128. void rand_fill(struct node **list, int n) //Randomly generates numbers and fills vector
  129. {
  130.     int i = 0;
  131.     while (i < n) //Fills vector with absolutely random numbers
  132.     {
  133.         add(&*list, rand());
  134.         i++;
  135.     }
  136. }
  137.  
  138. void nearly_fill(struct node **list, int n)
  139. {
  140.     for (int k = rand(), i = n; n > i / 2; k = k - rand() % 6 + 1, n--)
  141.         add(&*list, k);
  142.     for (int i = rand(); n; i = i + rand() % 6 + 1, n--)
  143.         add(&*list, i);
  144. }
  145.  
  146. //Reverse fill with 0-3 step
  147. void reverse_fill(struct node **list, int n)
  148. {
  149.     for (int k = rand(); n; k = k - rand() % 4, n--)
  150.         add(&*list, k);
  151. }
  152.  
  153. //Function for repeating cycle
  154. bool yes_no(const std::string& str)
  155. {
  156.     bool err = false;
  157.     print_str(str, 15, 10);
  158.     std::string temp;
  159.     std::getline(std::cin, temp);
  160.     do
  161.     {
  162.         err = false;
  163.         if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return true; // Returning result for yes answer
  164.         else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return false; // For no answer
  165.         else
  166.         {
  167.             // If input is incorrect
  168.             print_str("Type [Yes] or [No]: ", 15, 15);
  169.             getline(std::cin, temp);
  170.             err = true;
  171.         }
  172.     } while (err);
  173.     std::cout << "An error occurred!" << std::endl;
  174.     return true;
  175. }
  176.  
  177. //Checks for empty input in console
  178. bool empty(const std::string& str)
  179. {
  180.     bool empty = true;
  181.     if (!str.empty())
  182.     {
  183.         for (unsigned int i = 0; i < str.length(); i++)
  184.         {
  185.             if (str[i] != ' ')
  186.             {
  187.                 empty = false;
  188.             }
  189.         }
  190.     }
  191.     return empty;
  192. }
  193.  
  194.  
  195. //Choice of future actions
  196. int choice(std::string str)
  197. {
  198.     std::string rnd = "Random", reverse = "Reverse", nearly = "Nearly", display = "display";
  199.     if (levenshtein_distance(str, rnd) < 3)
  200.         return 1;
  201.     if (levenshtein_distance(str, nearly) < 3)
  202.         return 2;
  203.     if (levenshtein_distance(str, reverse) < 3)
  204.         return 3;
  205.     if (levenshtein_distance(str, display) < 3)
  206.         return 4;
  207.     return 0;
  208. }
  209.  
  210. //Some funny error check
  211. void read_error(bool& only_digits, bool& negative, int& dot_count, std::string& input)
  212. {
  213.     const std::string null_err = "You wrote nothing, stop it";
  214.     const std::string mess_err = "S-stop testing me, you baka! >/////<";
  215.     const std::string neg_str_err = "How do you think letters would be negative? Are you so silly? Please";
  216.     const std::string neg_dot_err = "You are not supposed to write negative fractional number here";
  217.     const std::string dot_str_err = "Developer doesn't know what to write here, but this is a error, so please";
  218.     const std::string string_err = "This is not a number, do you think i can read it? Please";
  219.     const std::string dot_err = "This number can't be fractional, you silly. Please";
  220.     const std::string neg_err = "You number is way too negative";
  221.     const std::string repeat = ", type again: ";
  222.     if (!only_digits && dot_count != 0 && negative) { print_str(mess_err, 13, 15); print_str(repeat, 13, 15); }
  223.     else if (!only_digits && negative) { print_str(neg_str_err, 4, 15); print_str(repeat, 4, 15); }
  224.     else if (!only_digits && dot_count != 0) { print_str(dot_str_err, 4, 15); print_str(repeat, 4, 15); }
  225.     else if (dot_count != 0 && negative) { print_str(neg_dot_err, 4, 15); print_str(repeat, 4, 15); }
  226.     else if (!only_digits) { print_str(string_err, 4, 15); print_str(repeat, 4, 15); }
  227.     else if (dot_count != 0) { print_str(dot_err, 4, 15); print_str(repeat, 4, 15); }
  228.     else if (negative) { print_str(neg_err, 4, 15); print_str(repeat, 4, 15); }
  229.     else if (empty(input)) { print_str(null_err, 4, 15); print_str(repeat, 4, 15); }
  230. }
  231.  
  232. //Checks for digit, can process cyrillic
  233. bool is_digit(char s)
  234. {
  235.     if (s >= '0' && s <= '9') return true;
  236.     else return false;
  237. }
  238.  
  239. int read_uint()
  240. {
  241.     //Checks variable for correct input (number)
  242.     std::string input;
  243.     getline(std::cin, input);
  244.     int dot_count = 0, k = 0;
  245.     bool only_digits = true, negative = false;
  246.     while (!only_digits || dot_count == 0 || input.length() == 0)
  247.     {
  248.         if (input[0] == '-')
  249.         {
  250.             k = 1;
  251.             negative = true;
  252.         }
  253.         for (int i = input.length() - 1; i >= k; i--)
  254.         {
  255.             if (!is_digit(input[i]))
  256.             {
  257.                 if (input[i] == '.' || input[i] == ',') dot_count += 1;
  258.                 else only_digits = false;
  259.             }
  260.         }
  261.         if (only_digits && dot_count == 0 && !negative && input.length() != 0)
  262.         {
  263.             return strtol(input.c_str(), NULL, 0);
  264.         }
  265.         else
  266.         {
  267.             read_error(only_digits, negative, dot_count, input);
  268.             getline(std::cin, input);
  269.             only_digits = true;
  270.             dot_count = 0;
  271.             negative = false;
  272.             k = 0;
  273.         }
  274.     }
  275.     return 0;
  276. }
  277.  
  278. void destroy(struct node *list, int n)
  279. {
  280.     struct node *temp;
  281.     int k = 0;
  282.     while(k++ < n)
  283.     {
  284.         temp = list->next;
  285.         free(list);
  286.         list = temp;
  287.     }
  288. }
  289.  
  290. int main()
  291. {
  292.     std::string str;
  293.     int n;
  294.     do
  295.     {
  296.         struct node* list = nullptr;
  297.         print_str("Write number of elements of your list: ", 10, 10);
  298.         do
  299.         {
  300.             n = read_uint();
  301.         }
  302.         while (n == 0 && print_str("Why would you write zero elements for list? Type again: ", 4, 15));
  303.         print_str("Do you want to fill list ", 10, 10);
  304.         std::cout << "random";
  305.         print_str(" , ", 10, 10);
  306.         std::cout << "nearly";
  307.         print_str(" sorted or ", 10, 10);
  308.         std::cout << "reverse";
  309.         print_str("? ", 10, 10);
  310.         getline(std::cin, str);
  311.         do
  312.         {
  313.             if (choice(str) == 1)
  314.                 rand_fill(&list, n);
  315.             else if (choice(str) == 2)
  316.                 nearly_fill(&list, n);
  317.             else if (choice(str) == 3)
  318.                 reverse_fill(&list, n);
  319.             else if (choice(str) == 4)
  320.                 display(list);
  321.         }
  322.         while (!choice(str) && print_str("Please, type [random/nearly/reverse]: ", 13, 10) && getline(std::cin, str));
  323.         display(list);
  324.         double t = clock(); //Clock function from ctime, gets current clock time and logs it into "t" integer
  325.         insertion_sort(list);
  326.         t = (clock() - static_cast<double>(t)) / CLOCKS_PER_SEC; //Gets current time minus previous logged time
  327.         display(list);
  328.         print_str("Time in seconds of your life wasted on sorting: ", 11, 10);
  329.         std::cout << t << std::endl;
  330.         destroy(list, n);
  331.     }while (yes_no("Do you want to continue? "));
  332.     return 0;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement