Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <functional>
- using namespace std;
- class DFA {
- public:
- 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()) {
- string currentState = "3";
- for (auto ch : input) {
- if (currentState == "1") {
- start_reaction.at("1")();
- if (ch == 48)
- {
- currentState = "1";
- symbol_reaction[48];
- }
- if (ch == 49)
- {
- currentState = "2";
- symbol_reaction.at(49)();
- }
- end_reaction.at("1")();
- }
- if (currentState == "2") {
- start_reaction.at("2")();
- if (ch == 48)
- {
- currentState = "2";
- symbol_reaction.at(48)();
- }
- if (ch == 49)
- {
- currentState = "3";
- symbol_reaction.at(49)();
- }
- end_reaction.at("2")();
- }
- if (currentState == "3") {
- start_reaction.at("3")();
- if (ch == 48)
- {
- currentState = "3";
- symbol_reaction.at(48)();
- }
- if (ch == 49)
- {
- currentState = "1";
- symbol_reaction.at(49)();
- }
- end_reaction.at("3")();
- }
- }
- bool isFinalState = false;
- if (currentState == "1") {
- isFinalState = false;
- }
- if (currentState == "2") {
- isFinalState = false;
- }
- if (currentState == "3") {
- isFinalState = true;
- }
- return isFinalState;
- }
- };
- int main() {
- DFA example;
- example.simulation();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement