Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. class DFA {
  8. public:
  9.     bool simulation(std::string& input, const std::map<char, std::function<void()>>& symbol_reaction(), const std::map<const std::string, std::function<void()>>& start_reaction(), const std::map<const std::string, std::function<void()>>& end_reaction()) {
  10.  
  11.         string currentState = "3";
  12.         for (auto ch : input) {
  13.  
  14.             if (currentState == "1") {
  15.                 start_reaction.at("1")();
  16.                 if (ch == 48)
  17.                 {
  18.                     currentState = "1";
  19.                     symbol_reaction[48];
  20.                 }
  21.                 if (ch == 49)
  22.                 {
  23.                     currentState = "2";
  24.                     symbol_reaction.at(49)();
  25.                 }
  26.                 end_reaction.at("1")();
  27.             }
  28.             if (currentState == "2") {
  29.                 start_reaction.at("2")();
  30.                 if (ch == 48)
  31.                 {
  32.                     currentState = "2";
  33.                     symbol_reaction.at(48)();
  34.                 }
  35.                 if (ch == 49)
  36.                 {
  37.                     currentState = "3";
  38.                     symbol_reaction.at(49)();
  39.                 }
  40.                 end_reaction.at("2")();
  41.             }
  42.             if (currentState == "3") {
  43.                 start_reaction.at("3")();
  44.                 if (ch == 48)
  45.                 {
  46.                     currentState = "3";
  47.                     symbol_reaction.at(48)();
  48.                 }
  49.                 if (ch == 49)
  50.                 {
  51.                     currentState = "1";
  52.                     symbol_reaction.at(49)();
  53.                 }
  54.                 end_reaction.at("3")();
  55.             }
  56.         }
  57.  
  58.         bool isFinalState = false;
  59.  
  60.         if (currentState == "1") {
  61.             isFinalState = false;
  62.         }
  63.         if (currentState == "2") {
  64.             isFinalState = false;
  65.         }
  66.         if (currentState == "3") {
  67.             isFinalState = true;
  68.         }
  69.         return isFinalState;
  70.     }
  71. };
  72.  
  73. int main() {
  74.  
  75.     DFA example;
  76.     example.simulation();
  77.  
  78.     return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement