Advertisement
Mike_be

OTYAP v9

Mar 16th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 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 <Windows.h>
  9.  
  10. const int matrix[3][9] =
  11. { //S, A1, A2, A3, A4, A5, A6, A7
  12.     1, 2,  3,  4,  5,  6,  8,  8,  8,
  13.     0, 7,  7,  7,  7,  7,  7,  8,  9,
  14.     8, 8,  8,  8,  8,  8,  8,  8,  8
  15. };
  16.  
  17. /*
  18. * Most important function in automaton. Add states or new conditions in it for new functionality
  19. * This one is for searching fractional numbers
  20. */
  21. int row(char& a)
  22. {
  23.     if (a >= '0' && a <= '9' || a >= 'а' && a <= 'я' || a >= 'А' && a <= 'Я')
  24.         return 0;
  25.     if (a == ' ' || a == '\n')
  26.         return 1;
  27.     return 2;
  28. }
  29.  
  30. /*
  31. Automaton
  32. Usage: automaton(string_you_want_to_check, vec_you_want_to_put_results);
  33. */
  34. void auto_rifle(char* &str, std::vector<char*>& vec_1, std::vector<char*>& vec_2)
  35. {
  36.     int size = 16;
  37.     char* word = (char*)malloc(size * sizeof(char));
  38.     int state = 0, i = 0;
  39.     for (unsigned long long k = 0; k < strlen(str); k++)
  40.     {
  41.         if (state == 0)
  42.         {
  43.             i = 0;
  44.             word = (char*)malloc(size * sizeof(char));
  45.         }
  46.         state = matrix[(row(str[k]))][state];
  47.         word[i] = str[k];
  48.         i++;
  49.         if (i == size)
  50.         {
  51.             size *= 2;
  52.             word = (char*)realloc(word, size * sizeof(char));
  53.         }
  54.         if (state == 7)
  55.         {
  56.             word[i-1] = 0;
  57.             vec_1.push_back(word);
  58.             state = 0;
  59.             i = 0;
  60.             word = 0;
  61.         }
  62.         if (state == 9)
  63.         {
  64.             word[i-1] = 0;
  65.             vec_2.push_back(word);
  66.             state = 0;
  67.             i = 0;
  68.             word = 0;
  69.         }
  70.     }
  71.     return;
  72. }
  73.  
  74. HANDLE hConsole = GetStdHandle(((DWORD)-11)); //Console address to change text color
  75.  
  76. /*
  77. Printing color messages in console
  78. Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
  79. Colors: https://i.stack.imgur.com/ZG625.png
  80. */
  81. inline bool print_str(char* str, int k, int n)
  82. {
  83.     SetConsoleTextAttribute(hConsole, k);
  84.     for (int l = 0; l < int(strlen(str)); l++)
  85.     {
  86.         std::cout << str[l];
  87.         Sleep(n);
  88.     }
  89.     SetConsoleTextAttribute(hConsole, 14);
  90.     return true;
  91. }
  92.  
  93.  
  94. /*
  95. * Read function, reads from given path/.../file
  96. * Usage: read(vec_you_want_to_fill)
  97. * Now automatically reads everything and fills linked string to further work
  98. */
  99. void read(char* &file)
  100. {
  101.     int size = 256, i = -1;
  102.     std::ifstream in;
  103.     char ch;
  104.     print_str("Write file name or path to it: ", 10, 10);
  105.     do {
  106.         char* file_path = (char*)malloc(256 * sizeof(char));
  107.         std::cin.getline(file_path, 256);
  108.         if (file_path[255])
  109.         {
  110.             std::cin.clear();
  111.             std::cout << "dick move";
  112.         }
  113.         in.open(file_path);
  114.         free(file_path);
  115.     } while (!in.is_open() && print_str("Couldn't read the file, type again: ", 4, 10));
  116.     do
  117.     {
  118.         if (i == size)
  119.         {
  120.             size *= 2;
  121.             file = (char*)realloc(file, size * sizeof(char));
  122.         }
  123.         file[++i] = in.get();
  124.     } while (!in.eof());
  125.     if (i == size)
  126.     {
  127.         size *= 2;
  128.         file = (char*)realloc(file, size * sizeof(char));
  129.     }
  130.     file[i++] = '\n';
  131.     file[i] = 0;
  132.     in.close();
  133. }
  134.  
  135. int main()
  136. {
  137.     setlocale(LC_ALL, "Russian");
  138.     std::vector<char*> right, wrong;
  139.     char*file;
  140.     file = (char*)malloc(256 * sizeof(char));
  141.     read(file);
  142.     std::cout << file;
  143.     auto_rifle(file, right, wrong);
  144.     free(file);
  145.     std::cout << std::endl; std::cout << std::endl;
  146.     for (int i = 0; i < right.size(); i++)
  147.         std::cout << right[i] << ' ';
  148.     std::cout << std::endl << "xyi\n";
  149.     std::cout << std::endl;
  150.     for (int i = 0; i < wrong.size(); i++)
  151.         std::cout << wrong[i] << ' ';
  152.     getchar();
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement