So there this c++ code for the following question: NFA to accept strings that have at least one character occurring in a multiple of 3.
The problem is it's showing "accepting" as the output for all inputs. for example: bbaacc output should be "not accepting" but this code is showing excepting
I am not able to figure out where I went wrong. Can somebody help me find where?
#include <bits/stdc++.h>
int nfa = 1;
int flag = 0;
using namespace std;
void state1(char c)
{
if (c == 'a')
nfa = 2;
else if (c == 'b' || c == 'c')
nfa = 1;
else
flag = 1;
}
void state2(char c)
{
if (c == 'a')
nfa = 3;
else if (c == 'b' || c == 'c')
nfa = 2;
else
flag = 1;
}
void state3(char c)
{
if (c == 'a')
nfa = 1;
else if (c == 'b' || c == 'c')
nfa = 3;
else
flag = 1;
}
void state4(char c)
{
if (c == 'b')
nfa = 5;
else if (c == 'a' || c == 'c')
nfa = 4;
else
flag = 1;
}
void state5(char c)
{
if (c == 'b')
nfa = 6;
else if (c == 'a' || c == 'c')
nfa = 5;
else
flag = 1;
}
void state6(char c)
{
if (c == 'b')
nfa = 4;
else if (c == 'a' || c == 'c')
nfa = 6;
else
flag = 1;
}
void state7(char c)
{
if (c == 'c')
nfa = 8;
else if (c == 'b' || c == 'a')
nfa = 7;
else
flag = 1;
}
void state8(char c)
{
if (c == 'c')
nfa = 9;
else if (c == 'b' || c == 'a')
nfa = 8;
else
flag = 1;
}
void state9(char c)
{
if (c == 'c')
nfa = 7;
else if (c == 'b' || c == 'a')
nfa = 9;
else
flag = 1;
}
bool checkA(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 1)
state1(s[i]);
else if (nfa == 2)
state2(s[i]);
else if (nfa == 3)
state3(s[i]);
}
if (nfa == 1) {
return true;
}
else {
nfa = 4;
}
}
bool checkB(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 4)
state4(s[i]);
else if (nfa == 5)
state5(s[i]);
else if (nfa == 6)
state6(s[i]);
}
if (nfa == 4) {
return true;
}
else {
nfa = 7;
}
}
bool checkC(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 7)
state7(s[i]);
else if (nfa == 8)
state8(s[i]);
else if (nfa == 9)
state9(s[i]);
}
if (nfa == 7) {
return true;
}
}
int main()
{
string s = "bbbca";
int x = 5;
if (checkA(s, x) || checkB(s, x) || checkC(s, x)) {
cout << "ACCEPTED";
}
else {
if (flag == 0) {
cout << "NOT ACCEPTED";
return 0;
}
else {
cout << "INPUT OUT OF DICTIONARY.";
return 0;
}
}
}