Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +#include <iostream>
- +#include <string>
- +
- +using std::cin;
- +using std::string;
- +
- +namespace FiniteAutomata {
- +
- + const char keySymbols[] = "'\"\\/*\n";
- + const int keySymbolsCount = 6;
- + const int DEFAULT_EVENT = 6;
- + const int transitionTable[9][7] = {
- + {3, 4, 0, 1, 0, 0, 0},
- + {0, 0, 0, 5, 6, 0, 0},
- + {6, 6, 6, 0, 2, 6, 6},
- + {0, 3, 7, 3, 3, 3, 3},
- + {4, 0, 8, 4, 4, 4, 4},
- + {5, 5, 5, 5, 5, 0, 5},
- + {6, 6, 6, 6, 2, 6, 6},
- + {3, 3, 3, 3, 3, 3, 3},
- + {4, 4, 4, 4, 4, 4, 4}
- + };
- +
- + string makeTransition(int & state, char previousCharacter, char currentCharacter) {
- + string result;
- +
- + int eventNumber = DEFAULT_EVENT;
- + for (size_t i = 0; i < keySymbolsCount; ++i) {
- + if (currentCharacter == keySymbols[i]) {
- + eventNumber = i;
- + break;
- + }
- + }
- +
- + int newState = transitionTable[state][eventNumber];
- +
- + switch (state) {
- + case 0:
- + if (newState != 1) {
- + result += currentCharacter;
- + }
- + break;
- + case 1:
- + if (newState != 5 && newState != 6) {
- + result += previousCharacter;
- + result += currentCharacter;
- + }
- + break;
- + case 3:
- + case 4:
- + case 7:
- + case 8:
- + result += currentCharacter;
- + break;
- + case 5:
- + if (newState == 0) {
- + result += currentCharacter;
- + }
- + break;
- + }
- + state = newState;
- +
- + return result;
- + }
- +
- + string removeComments(const string & source) {
- + string result;
- +
- + int state = 0;
- + for (size_t index = 0; index < source.length(); ++index) {
- + char currentCharacter = source[index];
- + char previousCharacter = 0;
- + if (index > 0) {
- + previousCharacter = source[index - 1];
- + }
- + result += makeTransition(state, previousCharacter, currentCharacter);
- + }
- +
- + return result;
- + }
- +
- +}
- +
- +string sourceInput() {
- + string result;
- + int nextCharacter = cin.get();
- + while (!cin.eof()) {
- + result += (char)nextCharacter;
- + nextCharacter = cin.get();
- + }
- + return result;
- +}
- +
- +
- +
- +
- +
- +int main() {
- + std::ios_base::sync_with_stdio(false);
- + string source = sourceInput();
- + std::cout << FiniteAutomata::removeComments(source);
- + return 0;
- +}
Advertisement
Add Comment
Please, Sign In to add comment