0

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;
        }
    }
}


    
  • See https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h?r=SearchResults&s=1|313.4023 – Eduardo Maroto Campos May 28 '21 at 16:21
  • 2
    That is a lot of code to simply determine if a character occurs 3 or more times in a row. Also, how many groups of 3 are here: `aaaa`? Is it 1? or 2? or none (since there are 4 in a row)? – PaulMcKenzie May 28 '21 at 16:29
  • What is an NFA? Some kind of directed graph where the current state is characterized by a set of active nodes. I don't quite see that in your code. – dratenik May 28 '21 at 16:33
  • If you declare your function as returning `bool`, it should always return a bool. At least clang and gcc do horrible things in c++ mode if you don't return a value you promised. – dratenik May 28 '21 at 16:36
  • I think "NFA" here is non-deterministic finite automata ... – jwezorek May 28 '21 at 17:12
  • although im not sure what is not deterministic in the above in that there are no epsilon transitions. Anyway the OP should use a table to represent the DFA not a rats nest of if statements – jwezorek May 28 '21 at 17:19

0 Answers0