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 <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>
- /*
- 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)
- {
- HANDLE hConsole = GetStdHandle(((DWORD)-11)); //Console address to change text color
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(strlen(str)); l++)
- {
- std::cout << str[l];
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- return true;
- }
- const int matrix[8][9] = {
- // 0 1 2 3 4 5 6 7 8
- 1, 1, 1, 8, 8, 8, 8, 8, 8, //0
- 2, 1, 2, 8, 8, 8, 8, 8, 8, //1
- 3, 8, 8, 8, 8, 8, 8, 8, 8, //2
- 4, 8, 8, 8, 6, 8, 8, 8, 8, //3
- 5, 8, 8, 8, 6, 6, 8, 8, 8, //4
- 6, 8, 8, 8, 8, 8, 7, 8, 8, //5
- 0, 13, 9, 10, 11, 11, 11, 11, 12, //6
- 8, 8, 8, 8, 8, 8, 8, 8, 8 //7
- };
- /*
- * Most important function in automaton. Determines how automaton will work. Add states or new conditions in it for new functionality
- */
- bool keyword(char* word)
- {
- bool is_keyword = false;
- std::vector<std::string> keywords = { "if", "then", "elseif", "end", "or", "not", "and" };
- for (int i = 0; i < 7; i++)
- if (word == keywords[i])
- is_keyword = true;
- return is_keyword;
- }
- int row(char& a)
- {
- if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z'))
- return 0;
- if (a >= '0' && a <= '9')
- return 1;
- if (a == '+' || a == '-')
- return 2;
- if (a == '<')
- return 3;
- if (a == '>')
- return 4;
- if (a == '=')
- return 5;
- if (a == ' ' || a == '\t' || a == '\n' || a == '\r' || a == '\0')
- return 6;
- return 7;
- }
- /*
- Automaton
- Usage: automaton(string_you_want_to_check, results_1, ... , results_n);
- */
- void auto_rifle(char* str, std::vector<char*>& vec_1, std::vector<int>& vec_2)
- {
- int size = 16;
- char* word = static_cast<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 = static_cast<char*>(malloc(size * sizeof(char)));
- }
- state = matrix[(row(str[k]))][state];
- word[i] = str[k];
- i++;
- if (i == size)
- {
- size *= 2;
- word = static_cast<char*>(realloc(word, size * sizeof(char)));
- }
- if (state == 13)
- {
- word[i - 1] = 0;
- if (keyword(word))
- {
- vec_2.push_back(1);
- print_str("\" ", 10, 10); std::cout << word; print_str(" \" ", 10, 10); print_str("Keyword\n", 11, 10);
- }
- else
- {
- vec_2.push_back(2);
- print_str("\" ", 10, 10); std::cout << word; print_str(" \" ", 10, 10); print_str("Variable / Identifier\n", 10, 10);
- }
- }
- if (state == 9)
- {
- vec_2.push_back(3);
- word[i - 1] = 0;
- print_str("\" ", 10, 10); std::cout << word; print_str(" \" ", 10, 10); print_str("Constant numbers\n", 9, 10);
- }if (state == 10)
- {
- vec_2.push_back(4);
- word[i - 1] = 0;
- print_str("\" ", 10, 10); std::cout << word; print_str(" \" ", 10, 10); print_str("Arithmetic operations\n", 15, 10);
- }if (state == 11)
- {
- vec_2.push_back(5);
- word[i - 1] = 0;
- print_str("\" ", 10, 10); std::cout << word; print_str(" \" ", 10, 10); print_str("Comparison operations\n", 13, 10);
- }
- if (state == 12)
- {
- vec_2.push_back(6);
- word[i - 1] = 0;
- print_str("\" ", 10, 10); std::cout << word; print_str(" \" ", 10, 10); print_str("Can't analyze\n", 12, 10);
- }
- if (state >= 9 && state <= 13)
- {
- vec_1.push_back(word);
- state = 0;
- i = 0;
- word = 0;
- }
- }
- return;
- }
- #undef max
- /*
- * 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, k = 0;
- std::ifstream in;
- char ch;
- print_str("Write file name or path to it: ", 10, 10);
- do {
- char* file_path = static_cast<char*>(malloc(256 * sizeof(char)));
- file_path[255] = '\n';
- std::cin.getline(file_path, 256);
- if (file_path[255] != '\n')
- {
- print_str("You wrote too long file path and i ", 4, 10);
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- 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 = static_cast<char*>(realloc(file, size * sizeof(char)));
- }
- file[++i] = in.get();
- } while (!in.eof());
- if (i == size)
- {
- size *= 2;
- file = static_cast<char*>(realloc(file, size * sizeof(char)));
- }
- file[i++] = '\n';
- file[i] = 0;
- in.close();
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- std::vector<char*> variables;
- std::vector<int> type;
- char* file = static_cast<char*>(malloc(256 * sizeof(char)));
- read(file);
- std::cout << file;
- auto_rifle(file, variables, type);
- free(file);
- print_str("Variable / Identifier:\n", 10, 10);
- for (unsigned int i = 0; i < type.size(); i++)
- {
- if (type[i] == 2)
- {
- print_str("\" ", 10, 10);
- std::cout << variables[i];
- print_str(" \" ", 10, 10);
- }
- }
- print_str("\nConstant numbers:\n", 9, 10);
- for (unsigned int i = 0; i < type.size(); i++)
- {
- if (type[i] == 3)
- {
- print_str("\" ", 10, 10);
- std::cout << variables[i];
- print_str(" \" ", 10, 10);
- }
- }
- print_str("\n\nPress enter to continue. . .", 7, 5);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement