Advertisement
lyingchannel

Untitled

Mar 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <locale.h>
  5. #include <vector>
  6.  
  7. using std::cout;
  8. using std::vector;
  9.  
  10. //Состояния автомата
  11. enum AState { S, A, V, L };
  12.  
  13. //Матрица переходов
  14. int matrix[4][3] = {
  15.     V,  A,  S,
  16.     V,  A,  S,
  17.     L,  A,  S,
  18.     L,  L,  S
  19. };
  20.  
  21. //Контейнер лексемы
  22. struct Lex {
  23.     bool valid; //Флаг корректности лексемы (соответствия заданию)
  24.     char* str; //Текст лексемы
  25. };
  26.  
  27. bool findChar(char* str, char val) {
  28.     for (int i = 0; str[i] != '\0'; ++i)
  29.         if (str[i] == val)
  30.             return true;
  31.     return false;
  32. }
  33.  
  34. //Функция преобразования символа в соответствующий номер столбца матрицы переходов
  35. int GetMatrixCol(char curr) {
  36.     char vowel[] = "аеёиоуюяыэАЕЁИОУЮЯЫЭ";
  37.     char conson[] = "бвгджзйклмнпрстфхцчшщъьБВГДЖЗЙКЛМНПРСТФХЦЧШЩЪЬ1234567890wertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM[{}(,./\\|)]";
  38.     if (findChar(vowel, curr)) return 0;
  39.     if (findChar(conson, curr)) return 1;
  40.     return 2;
  41. }
  42.  
  43. //Функция лексического анализа
  44. void LexAnalysis(vector<Lex>& res, char* str) {
  45.     Lex lex; //Текущая лексема
  46.     int pos, firstPos; //Текущая позиция в строке, позиция начала лексемы
  47.     char curr;
  48.     AState state = AState::S;
  49.  
  50.     for (pos = 0; (curr = str[pos]) != '\0'; ++pos) {
  51.         if (curr != ' ') {
  52.             if (state == AState::S) {
  53.                 firstPos = pos;
  54.                 lex.valid = true;
  55.             }
  56.             state = (AState)matrix[state][GetMatrixCol(curr)];
  57.         }
  58.         else {
  59.             if ((state == AState::A) || (state == AState::V))
  60.                 lex.valid = false;
  61.             int length = pos - firstPos;
  62.             lex.str = new char[length + 1];
  63.             for (int i = 0; i < length; ++i)
  64.                 lex.str[i] = str[firstPos + i];
  65.             lex.str[length] = '\0';
  66.             res.push_back(lex);
  67.             state = AState::S;
  68.         }
  69.     }
  70.     if ((state == AState::A) || (state == AState::V))
  71.         lex.valid = false;
  72.     int length = pos - firstPos;
  73.     lex.str = new char[length + 1];
  74.     for (int i = 0; i < length; ++i)
  75.         lex.str[i] = str[firstPos + i];
  76.     lex.str[length] = '\0';
  77.     res.push_back(lex);
  78.     state = AState::S;
  79. }
  80.  
  81. int main() {
  82.     setlocale(LC_ALL, "en_GB");
  83.     //Чтение
  84.     std::ifstream fin("input.txt");
  85.  
  86.     fin.seekg(0, std::ios::end);
  87.     int length = fin.tellg();
  88.     fin.seekg(0, std::ios::beg);
  89.     char* str = new char[length];
  90.     fin.read(&str[0], length);
  91.     str[length] = '\0';
  92.     for (int i = 0; i < length; ++i)
  93.         if ((str[i] == '\n') || (str[i] == '\r') || (str[i] == '\t'))
  94.             str[i] = ' ';
  95.  
  96.     //Анализ
  97.     vector<Lex> res;
  98.     LexAnalysis(res, str);
  99.  
  100.     fin.close();
  101.  
  102.     //Вывод
  103.     std::ofstream fout("output.txt");
  104.     int a = res.size();
  105.  
  106.     fout << "\tПодходит:\n";
  107.     for (int i = 0; i < a; ++i)
  108.         if (res[i].valid) {
  109.             fout << res[i].str << '\n';
  110.         }
  111.  
  112.     fout << '\n'
  113.         << "\tНеподходит:\n";
  114.  
  115.     for (int i = 0; i < a; ++i)
  116.         if (!res[i].valid) {
  117.             fout << res[i].str << '\n';
  118.         }
  119.  
  120.     fout.close();
  121.  
  122.     for (int i = 0; i < a; ++i)
  123.         delete[] res[i].str;
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement