字符串访问器使字符串成为int C ++

时间:2016-02-20 23:50:13

标签: c++ string accessor nfa

我正在尝试使用C ++编写NFA代码,该NFA可以识别字符串并确定该字符串是被接受还是被拒绝。我有一个字符串输入,将作为一个字符串进行评估。

但是,一旦我在evaluateInput()函数中访问它(在第三个嵌套的for循环中,if语句)由于某种原因,程序将无法编译,因为它表示in.at(i)被认为是int因此无法与字符串进行比较。

我很困惑为什么在整个程序中它是一个字符串,但是当它被访问时它会突然变成一个int?

我收到以下错误:

error: invalid operands to binary expression ('int' and
      'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >'))
                                if(in[i] == currStates[j].transitions[k].symb)

如果有帮助,我已提供了所有代码。

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <vector>

using namespace std;

string num, SoA1, SoA2, currentState, symbol, tranState;

struct Transition;

struct State{
    bool startS = false;
    bool acceptS = false;
    string sym;
    vector<Transition> transitions;//transition between states
};

struct Transition{
    string symb;
    State to;
    State from;
};

struct NFA{
    vector<State> NFAStates;
    State start;
};

void evaluateInput(string in, NFA F)
{

    vector<State> currStates;
    currStates.push_back(F.start);

    if(in[1] == "1")
    {
        cout << "yes" << endl;
    }

    for(int i = 0; i < in.length(); i++)
    {
        cout << "input #: " << in[i] << endl;
        vector<State> next;
        for(int j = 0; j < currStates.size(); j++)
        {
            for(int k = 0; k < currStates[j].transitions.size(); k++)
            {
                if(in.at(i) == currStates[j].transitions[k].symb)
                {
                    next.push_back(currStates[j].transitions[k].to);
                }
            }
        }
        currStates = next;
    }
}

int main(int argc, char* argv[])
{
    char* fileName;
    string stringInput;
    if(argc > 1)
    {
        fileName = argv[1];
        stringInput = argv[2];
        //cout << stringInput << endl;
    }

    ifstream file(fileName);

    if(file.is_open())
    {
        NFA FA;
        string line;
        while(getline(file, line)){
            //cout << "1: " << line << endl;
            string ST;

            stringstream ss(line);
            getline(ss, ST, '\t');

            if(ST == "state")
            {
                State S;
                getline(ss, num, '\t');
                getline(ss, SoA1, '\t');
                getline(ss, SoA2);

                int num_i = stoi(num);
                S.sym = num_i;
                if((SoA1 == "start") || (SoA2 == "start"))
                {
                    S.startS = true;
                }

                if((SoA1 == "accept") || (SoA2 == "accept"))
                {
                    S.acceptS = true;
                }

                FA.NFAStates.push_back(S);
                if(S.startS == true){
                    FA.start = S;
                }
            }else if(ST == "transition")
            {
                Transition T;
                getline(ss, currentState, '\t');
                getline(ss, symbol, '\t');
                getline(ss, tranState);

                int currentState_i = stoi(currentState);
                int tranState_i = stoi(tranState);
                T.symb = symbol;

                for(int j = 0; j < FA.NFAStates.size(); j++)
                {
                    if(FA.NFAStates[j].sym == tranState)
                    {
                        T.to = FA.NFAStates[j];
                    }
                }

                for(int i = 0; i < FA.NFAStates.size(); i++)
                {
                    if(FA.NFAStates[i].sym == currentState)
                    {
                        T.from = FA.NFAStates[i];
                        FA.NFAStates[i].transitions.push_back(T);
                    }
                }
            }
        }

        evaluateInput(stringInput, FA);
    }
}

感谢您的帮助。

0 个答案:

没有答案