1

I am doing an assignment to simulate a deterministic finite automaton, I have this input from a text file:

4
0
0 0 2 a 1 b 3
1 1 2 a 1 b 2
2 1 2 a 1 b 3
3 0 2 a 3 b 3

Where first line is number of states, second line is initial state, and the other lines are in order: state number, 1 if it is an acceptable state or 0 if its not, number of transitions to another state, output letter, and state to enter from current state with that letter.

For that file the DFA is this one:

I have to find an error in the file if one of this conditions isnt true:

  • There is only one initial state

  • Each state has one and only one transition for each one of the letters in the alphabet

I'm using ifstream, but don't know how to read line by line and separate to tokens (each character is separated by space or enter) Here's my code:

openingerror=1;
ifstream file;  
file.open(filename);
if(file.is_open()) {

    openingerror=0;

    NumberLetter dummy;
    short int dCurrent,dIsfinal,dReach;
    file >> total >> init;
    dfa.resize(total); current.resize(total); 
    isFinal.resize(total); reach.resize(total); 
    deathState.resize(total);

    for(short int i=0;i<total;i++){
        file >> dCurrent >> dIsfinal >> dReach; 
        current[i]=dCurrent; isFinal[i]=(bool)dIsfinal; reach[i]=dReach;
        for (short int j=0;j<reach[i];j++){
            file >> dummy.l >> dummy.n; 
            dfa[current[i]].push_back(dummy);
        }       
    }   

    cout << "File loaded" << endl;
}  
else {       
    file.close();
    cerr << "Erro loading" << endl;
}
bitspuke
  • 39
  • 7

1 Answers1

0

I think you can solve this problem with "stringstream". You can read each line with it and you can assign the each value to proper variable. This code will provide a matrix implementation for your DFA, so you can use this approach to solve problem by using matrix.

char matrix[maxNumberofState][maxNumberofState];        
int tempInt;
char tempChar;

getline(file,line);
stringstream valuesOfLine(line);
valuesOfLine >> state>> acceptingBool >> tempInt >> tempchar;

matrix[state][tempInt] = tempChar;

valuesOfLine >> tempInt >> tempChar;

while(!matrix[state][tempInt]){

matrix[state][tempInt] = tempChar;
valuesOfLine >> tempInt >> tempChar;

}
jrSteve
  • 1
  • 1