//Stephen Bassoli #include #include #include #ifndef STATE_H #define STATE_H extern int currStateName; extern char alphabet[10]; class state { private: state* connectingStates[50]; char stateConnectorValue[50]; int stateConnectorName[50]; int numberOfConnections; int stateName; bool NFAstable; bool DFAstable; bool startState; bool endState; public: state(void); state(int stateNum); /* EASY FUNCTIONS */ int getName(); int getNumberConnections(); int* getStateNames(); int* getEconnections(); int* getConnections(char a); bool NFAstableFunc(); bool DFAstableFunc(); state* selfAddress(); void printState(); void setEndState(); void disconnectAll(); bool IsEndState(); /* HARD FUNCTIONS */ int Stabilize(); void Connect(state*, char); bool Disconnect(int name, char trans); }; state::state(void) { currStateName++; stateName = currStateName; if (currStateName == 0) startState = true; else startState = false; endState = false; numberOfConnections = 0; NFAstable = false; DFAstable = false; } state::state(int stateNum) { stateName = stateNum; if (currStateName == 0) startState = true; else startState = false; endState = false; numberOfConnections = 0; NFAstable = false; DFAstable = false; } void state::Connect(state* connectTo, char moveToOn) { numberOfConnections++; connectingStates[numberOfConnections-1] = connectTo; stateConnectorValue[numberOfConnections-1] = moveToOn; stateConnectorValue[numberOfConnections] = '\0'; stateConnectorName[numberOfConnections-1] = connectTo->getName(); } int state::getName(void) { return stateName; } void state::printState(void) { if (startState) cout << "-> "; else cout << " "; if (endState) cout << "F "; else cout << " "; cout << stateName << " "; if (numberOfConnections == 0) cout << endl; for (int i = 0 ; i < numberOfConnections ; i++) { if (i > 0) cout << " "; cout << stateConnectorValue[i] << " "; cout << stateConnectorName[i] << " \n"; } } void state::setEndState(void) { endState = true; } state* state::selfAddress(void) { return this; } bool state::Disconnect(int name, char trans) { for (int i = 0 ; i < numberOfConnections ; i++) { if (stateConnectorName[i] == name) { if (stateConnectorValue[i] == trans) { for (int j = i ; j < 50 ; j++) { connectingStates[j] = connectingStates[j+1]; stateConnectorValue[j] = stateConnectorValue[j+1]; stateConnectorName[j] = stateConnectorName[j+1]; } numberOfConnections--; return true; } } } return false; } int state::Stabilize(void) { unsigned int count = 0; char alphabetHolder[10]; strcpy(alphabetHolder, alphabet); for (unsigned int i = 0; i < strlen(stateConnectorValue) ; i++) { if (stateConnectorValue[i] == 'E') return 0; else NFAstable = true; } if (strlen(stateConnectorValue) == strlen(alphabet)) { for (unsigned int j = 0; j < strlen(stateConnectorValue) ; j++) { for (unsigned int k = 0 ; k < strlen(stateConnectorValue) ; k++) { if (stateConnectorValue[j] == alphabetHolder[k]) { alphabetHolder[k] = ' '; count++; } } } } if ((NFAstable)&&(!(count == strlen(stateConnectorValue)))) return 1; else { DFAstable = true; return 2; } } bool state::NFAstableFunc(void) { return NFAstable; } bool state::DFAstableFunc(void) { return DFAstable; } int state::getNumberConnections(void) { return numberOfConnections; } int* state::getStateNames(void) { return stateConnectorName; } /* The first number in the array is the number of E connections in the state */ /* Any number following is the state number that is connected with an E connection */ int* state::getEconnections(void) { static int* Econnections; delete Econnections; Econnections = new int[10]; int count = 1; for (int i = 0 ; i < numberOfConnections ; i++) { if (stateConnectorValue[i] == 'E') Econnections[count++] = stateConnectorName[i]; } Econnections[0] = count - 1; return Econnections; } int* state::getConnections(char a) { static int* Connections; delete Connections; Connections = new int[10]; int count = 1; for (int i = 0 ; i < numberOfConnections ; i++) { if (stateConnectorValue[i] == a) Connections[count++] = stateConnectorName[i]; } Connections[0] = count - 1; return Connections; } void state::disconnectAll(void) { numberOfConnections = 0; } bool state::IsEndState(void) { return endState; } #endif