从文件C ++中读取DFA

时间:2014-10-05 11:47:15

标签: c++

我正在做一个模拟确定性有限自动机的任务,我从文本文件输入:

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

第一行是状态数,第二行是初始状态,其他行是有序的:状态号,如果是可接受的状态则为1,如果不是,则为0,转换到另一个状态的数量,输出字母,并声明从当前状态进入该信件。

对于该文件,DFA就是这个:

如果其中一个条件不正确,我必须在文件中找到错误:

  • 只有一个初始状态

  • 每个州对字母表中的每一个字母都只有一个转换

我正在使用ifstream,但不知道如何逐行读取并分隔到令牌(每个字符用空格分隔或输入)这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

我认为你可以用“stringstream”来解决这个问题。您可以使用它读取每一行,并可以将每个值分配给适当的变量。此代码将为您的DFA提供矩阵实现,因此您可以使用此方法通过使用矩阵来解决问题。

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
using namespace glm;

int main() {

    // Initialise GLFW
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

                                                                   // Open a window and create its OpenGL context
    GLFWwindow* window; // (In the accompanying source code, this variable is global)
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
    if (window == NULL) {
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW
    glewExperimental = true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do {
        // Draw nothing, see you in tutorial 2 !

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

    } // Check if the ESC key was pressed or the window was closed
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
        glfwWindowShouldClose(window) == 0);

}