Advertisement
Mike_be

Switching to PC

May 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <streambuf>
  7. #include <malloc.h>
  8. #include <algorithm>
  9.  
  10. template <typename T>
  11. typename T::size_type levenshtein_distance(const T & src, const T & dst)
  12. {
  13.     const typename T::size_type m = src.size();
  14.     const typename T::size_type n = dst.size();
  15.     if (m == 0)
  16.     {
  17.         return n;
  18.     }
  19.     if (n == 0)
  20.     {
  21.         return m;
  22.     }
  23.  
  24.     std::vector<std::vector<typename T::size_type>> matrix(m + 1);
  25.  
  26.     for (typename T::size_type i = 0; i <= m; ++i)
  27.     {
  28.         matrix[i].resize(n + 1);
  29.         matrix[i][0] = i;
  30.     }
  31.     for (typename T::size_type i = 0; i <= n; ++i)
  32.     {
  33.         matrix[0][i] = i;
  34.     }
  35.  
  36.     typename T::size_type above_cell, left_cell, diagonal_cell, cost;
  37.  
  38.     for (typename T::size_type i = 1; i <= m; ++i)
  39.     {
  40.         for (typename T::size_type j = 1; j <= n; ++j)
  41.         {
  42.             cost = src[i - 1] == dst[j - 1] ? 0 : 1;
  43.             above_cell = matrix[i - 1][j];
  44.             left_cell = matrix[i][j - 1];
  45.             diagonal_cell = matrix[i - 1][j - 1];
  46.             matrix[i][j] = std::min(std::min(above_cell + 1, left_cell + 1), diagonal_cell + cost);
  47.         }
  48.     }
  49.  
  50.     return matrix[m][n];
  51. }
  52.  
  53. #include <Windows.h>
  54.  
  55.  
  56. /*
  57. Printing color messages in console
  58. Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
  59. Colors: https://i.stack.imgur.com/ZG625.png
  60. */
  61. inline bool print_str(char* str, int k, int n)
  62. {
  63.     HANDLE hConsole = GetStdHandle(((DWORD)-11)); //Console address to change text color
  64.     SetConsoleTextAttribute(hConsole, k);
  65.     for (int l = 0; l < int(strlen(str)); l++)
  66.     {
  67.         std::cout << str[l];
  68.         Sleep(n);
  69.     }
  70.     SetConsoleTextAttribute(hConsole, 14);
  71.     return true;
  72. }
  73.  
  74. const int matrix[8][9] = {
  75. //  0  1  2  3  4  5  6  7  8
  76.     1, 1, 1, 8, 8, 8, 8, 8, 8, //0
  77.     2, 1, 2, 8, 8, 8, 8, 8, 8, //1
  78.     3, 8, 8, 8, 8, 8, 8, 8, 8, //2
  79.     4, 8, 8, 8, 6, 8, 8, 8, 8, //3
  80.     5, 8, 8, 8, 6, 6, 8, 8, 8, //4
  81.     6, 8, 8, 8, 8, 8, 7, 8, 8, //5
  82.     0, 13, 9, 10, 11, 11, 11, 11, 12, //6
  83.     8, 8, 8, 8, 8, 8, 8, 8, 8 //7
  84. };
  85.  
  86. /*
  87. * Most important function in automaton. Determines how automaton will work. Add states or new conditions in it for new functionality
  88. */
  89.  
  90. bool keyword(char* word)
  91. {
  92.     bool is_keyword = false;
  93.     std::vector<std::string> keywords = { "if", "then", "elseif", "end", "or", "not", "and" };
  94.     for (int i = 0; i < 7; i++)
  95.         if (word == keywords[i])
  96.             is_keyword = true;
  97.     return  is_keyword;
  98. }
  99.  
  100. int row(char& a)
  101. {
  102.     if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z'))
  103.         return 0;
  104.     if (a >= '0' && a <= '9')
  105.         return 1;
  106.     if (a == '+' || a == '-')
  107.         return 2;
  108.     if (a == '<')
  109.         return 3;
  110.     if (a == '>')
  111.         return 4;
  112.     if (a == '=')
  113.         return 5;
  114.     if (a == ' ' || a == '\t' || a == '\n' || a == '\r' || a == '\0')
  115.         return 6;
  116.     return 7;
  117. }
  118.  
  119. /*
  120. Automaton
  121. Usage: automaton(string_you_want_to_check, results_1, ... , results_n);
  122. */
  123. void auto_rifle(char* str, std::vector<char*>& vec_1, std::vector<int>& vec_2)
  124. {
  125.     int size = 16;
  126.     char* word = static_cast<char*>(malloc(size * sizeof(char)));
  127.     int state = 0, i = 0;
  128.     for (unsigned long long k = 0; k < strlen(str); k++)
  129.     {
  130.         if (state == 0)
  131.         {
  132.             i = 0;
  133.             word = static_cast<char*>(malloc(size * sizeof(char)));
  134.         }
  135.         state = matrix[(row(str[k]))][state];
  136.         word[i] = str[k];
  137.         i++;
  138.         if (i == size)
  139.         {
  140.             size *= 2;
  141.             word = static_cast<char*>(realloc(word, size * sizeof(char)));
  142.         }
  143.         if (state == 13)
  144.         {
  145.             word[i - 1] = 0;
  146.             if (keyword(word))
  147.             {
  148.                 vec_2.push_back(1);
  149.                  print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Keyword\n", 11, 10);
  150.             }
  151.             else
  152.             {
  153.                 vec_2.push_back(2);
  154.                 print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Variable / Identifier\n", 10, 10);
  155.             }
  156.         }
  157.         if (state == 9)
  158.         {
  159.             vec_2.push_back(3);
  160.             word[i - 1] = 0;
  161.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Constant numbers\n", 9, 10);
  162.         }if (state == 10)
  163.         {
  164.             vec_2.push_back(4);
  165.             word[i - 1] = 0;
  166.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Arithmetic operations\n", 15, 10);
  167.         }if (state == 11)
  168.         {
  169.             vec_2.push_back(5);
  170.             word[i - 1] = 0;
  171.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Comparison operations\n", 13, 10);
  172.         }
  173.         if (state == 12)
  174.         {
  175.             vec_2.push_back(6);
  176.             word[i - 1] = 0;
  177.             print_str("\" ", 10, 10); std::cout << word; print_str(" \"  ", 10, 10); print_str("Can't analyze\n", 12, 10);
  178.         }
  179.         if (state >= 9 && state <= 13)
  180.         {
  181.             vec_1.push_back(word);
  182.             state = 0;
  183.             i = 0;
  184.             word = 0;
  185.         }
  186.     }
  187.     return;
  188. }
  189.  
  190. #undef max
  191.  
  192. /*
  193. * Read function, reads from given path/.../file
  194. * Usage: read(vec_you_want_to_fill)
  195. * Now automatically reads everything and fills linked string to further work
  196. */
  197. void read(char* &file)
  198. {
  199.     int size = 256, i = -1, k = 0;
  200.     std::ifstream in;
  201.     char ch;
  202.     print_str("Write file name or path to it: ", 10, 10);
  203.     do {
  204.         char* file_path = static_cast<char*>(malloc(256 * sizeof(char)));
  205.         file_path[255] = '\n';
  206.         std::cin.getline(file_path, 256);
  207.         if (file_path[255] != '\n')
  208.         {
  209.             print_str("You wrote too long file path and i ", 4, 10);
  210.             std::cin.clear();
  211.             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  212.         }
  213.         in.open(file_path);
  214.         free(file_path);
  215.     } while (!in.is_open() && print_str("Couldn't read the file, type again: ", 4, 10));
  216.     do
  217.     {
  218.         if (i == size)
  219.         {
  220.             size *= 2;
  221.             file = static_cast<char*>(realloc(file, size * sizeof(char)));
  222.         }
  223.         file[++i] = in.get();
  224.     } while (!in.eof());
  225.     if (i == size)
  226.     {
  227.         size *= 2;
  228.         file = static_cast<char*>(realloc(file, size * sizeof(char)));
  229.     }
  230.     file[i++] = '\n';
  231.     file[i] = 0;
  232.     in.close();
  233. }
  234.  
  235. int main()
  236. {
  237.     setlocale(LC_ALL, "Russian");
  238.     std::vector<char*> variables;
  239.     std::vector<int> type;
  240.     char* file = static_cast<char*>(malloc(256 * sizeof(char)));
  241.     read(file);
  242.     std::cout << file;
  243.     auto_rifle(file, variables, type);
  244.     free(file);
  245.     print_str("Variable / Identifier:\n", 10, 10);
  246.     for (unsigned int i = 0; i < type.size(); i++)
  247.     {
  248.         if (type[i] == 2)
  249.         {
  250.             print_str("\" ", 10, 10);
  251.             std::cout << variables[i];
  252.             print_str(" \"  ", 10, 10);
  253.         }
  254.     }
  255.     print_str("\nConstant numbers:\n", 9, 10);
  256.     for (unsigned int i = 0; i < type.size(); i++)
  257.     {
  258.         if (type[i] == 3)
  259.         {
  260.             print_str("\" ", 10, 10);
  261.             std::cout << variables[i];
  262.             print_str(" \"  ", 10, 10);
  263.         }
  264.     }
  265.     print_str("\n\nPress enter to continue. . .", 7, 5);
  266.     getchar();
  267.     return 0;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement