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 <streambuf>
- #include <malloc.h>
- #include <Windows.h>
- const int matrix[3][9] =
- { //S, A1, A2, A3, A4, A5, A6, A7
- 1, 2, 3, 4, 5, 6, 8, 8, 8,
- 0, 7, 7, 7, 7, 7, 7, 8, 9,
- 8, 8, 8, 8, 8, 8, 8, 8, 8
- };
- /*
- * Most important function in automaton. Add states or new conditions in it for new functionality
- * This one is for searching fractional numbers
- */
- int row(char& a)
- {
- if (a >= '0' && a <= '9' || a >= 'а' && a <= 'я' || a >= 'А' && a <= 'Я')
- return 0;
- if (a == ' ' || a == '\n')
- return 1;
- return 2;
- }
- /*
- Automaton
- Usage: automaton(string_you_want_to_check, vec_you_want_to_put_results);
- */
- void auto_rifle(char* &str, std::vector<char*>& vec_1, std::vector<char*>& vec_2)
- {
- int size = 16;
- char* word = (char*)malloc(size * sizeof(char));
- int state = 0, i = 0;
- for (unsigned long long k = 0; k < strlen(str); k++)
- {
- if (state == 0)
- {
- i = 0;
- word = (char*)malloc(size * sizeof(char));
- }
- state = matrix[(row(str[k]))][state];
- word[i] = str[k];
- i++;
- if (i == size)
- {
- size *= 2;
- word = (char*)realloc(word, size * sizeof(char));
- }
- if (state == 7)
- {
- word[i-1] = 0;
- vec_1.push_back(word);
- state = 0;
- i = 0;
- word = 0;
- }
- if (state == 9)
- {
- word[i-1] = 0;
- vec_2.push_back(word);
- state = 0;
- i = 0;
- word = 0;
- }
- }
- return;
- }
- HANDLE hConsole = GetStdHandle(((DWORD)-11)); //Console address to change text color
- /*
- Printing color messages in console
- Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
- Colors: https://i.stack.imgur.com/ZG625.png
- */
- inline bool print_str(char* str, int k, int n)
- {
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(strlen(str)); l++)
- {
- std::cout << str[l];
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- return true;
- }
- /*
- * Read function, reads from given path/.../file
- * Usage: read(vec_you_want_to_fill)
- * Now automatically reads everything and fills linked string to further work
- */
- void read(char* &file)
- {
- int size = 256, i = -1;
- std::ifstream in;
- char ch;
- print_str("Write file name or path to it: ", 10, 10);
- do {
- char* file_path = (char*)malloc(256 * sizeof(char));
- std::cin.getline(file_path, 256);
- if (file_path[255])
- {
- std::cin.clear();
- std::cout << "dick move";
- }
- in.open(file_path);
- free(file_path);
- } while (!in.is_open() && print_str("Couldn't read the file, type again: ", 4, 10));
- do
- {
- if (i == size)
- {
- size *= 2;
- file = (char*)realloc(file, size * sizeof(char));
- }
- file[++i] = in.get();
- } while (!in.eof());
- if (i == size)
- {
- size *= 2;
- file = (char*)realloc(file, size * sizeof(char));
- }
- file[i++] = '\n';
- file[i] = 0;
- in.close();
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- std::vector<char*> right, wrong;
- char*file;
- file = (char*)malloc(256 * sizeof(char));
- read(file);
- std::cout << file;
- auto_rifle(file, right, wrong);
- free(file);
- std::cout << std::endl; std::cout << std::endl;
- for (int i = 0; i < right.size(); i++)
- std::cout << right[i] << ' ';
- std::cout << std::endl << "xyi\n";
- std::cout << std::endl;
- for (int i = 0; i < wrong.size(); i++)
- std::cout << wrong[i] << ' ';
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement