Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <locale.h>
- #include <vector>
- using std::cout;
- using std::vector;
- //Состояния автомата
- enum AState { S, A, V, L };
- //Матрица переходов
- int matrix[4][3] = {
- V, A, S,
- V, A, S,
- L, A, S,
- L, L, S
- };
- //Контейнер лексемы
- struct Lex {
- bool valid; //Флаг корректности лексемы (соответствия заданию)
- char* str; //Текст лексемы
- };
- bool findChar(char* str, char val) {
- for (int i = 0; str[i] != '\0'; ++i)
- if (str[i] == val)
- return true;
- return false;
- }
- //Функция преобразования символа в соответствующий номер столбца матрицы переходов
- int GetMatrixCol(char curr) {
- char vowel[] = "аеёиоуюяыэАЕЁИОУЮЯЫЭ";
- char conson[] = "бвгджзйклмнпрстфхцчшщъьБВГДЖЗЙКЛМНПРСТФХЦЧШЩЪЬ1234567890wertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM[{}(,./\\|)]";
- if (findChar(vowel, curr)) return 0;
- if (findChar(conson, curr)) return 1;
- return 2;
- }
- //Функция лексического анализа
- void LexAnalysis(vector<Lex>& res, char* str) {
- Lex lex; //Текущая лексема
- int pos, firstPos; //Текущая позиция в строке, позиция начала лексемы
- char curr;
- AState state = AState::S;
- for (pos = 0; (curr = str[pos]) != '\0'; ++pos) {
- if (curr != ' ') {
- if (state == AState::S) {
- firstPos = pos;
- lex.valid = true;
- }
- state = (AState)matrix[state][GetMatrixCol(curr)];
- }
- else {
- if ((state == AState::A) || (state == AState::V))
- lex.valid = false;
- int length = pos - firstPos;
- lex.str = new char[length + 1];
- for (int i = 0; i < length; ++i)
- lex.str[i] = str[firstPos + i];
- lex.str[length] = '\0';
- res.push_back(lex);
- state = AState::S;
- }
- }
- if ((state == AState::A) || (state == AState::V))
- lex.valid = false;
- int length = pos - firstPos;
- lex.str = new char[length + 1];
- for (int i = 0; i < length; ++i)
- lex.str[i] = str[firstPos + i];
- lex.str[length] = '\0';
- res.push_back(lex);
- state = AState::S;
- }
- int main() {
- setlocale(LC_ALL, "en_GB");
- //Чтение
- std::ifstream fin("input.txt");
- fin.seekg(0, std::ios::end);
- int length = fin.tellg();
- fin.seekg(0, std::ios::beg);
- char* str = new char[length];
- fin.read(&str[0], length);
- str[length] = '\0';
- for (int i = 0; i < length; ++i)
- if ((str[i] == '\n') || (str[i] == '\r') || (str[i] == '\t'))
- str[i] = ' ';
- //Анализ
- vector<Lex> res;
- LexAnalysis(res, str);
- fin.close();
- //Вывод
- std::ofstream fout("output.txt");
- int a = res.size();
- fout << "\tПодходит:\n";
- for (int i = 0; i < a; ++i)
- if (res[i].valid) {
- fout << res[i].str << '\n';
- }
- fout << '\n'
- << "\tНеподходит:\n";
- for (int i = 0; i < a; ++i)
- if (!res[i].valid) {
- fout << res[i].str << '\n';
- }
- fout.close();
- for (int i = 0; i < a; ++i)
- delete[] res[i].str;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement