有限状态机程序无效输出

nfg76nw0  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(446)

我正在做一个项目,我的代码不工作不知道为什么。给定测试程序和通用类,我需要一个满足以下逻辑正则表达式的程序:
l1:对于字母表{a,b},所有包含奇数个a和一个b的字符串。
测试输入:AAAAAAA,AAAAAAA,AAAAAAAB,AAAAA,AAAAA
应该是什么:
AAAAA错误
AAAAAAA真的
aabaaab假
Baaaaa假
AAAAAA真的
程序输出:(真、真、真、假、真)
我的测试程序:

import java.util.Scanner;

// Test Finite State Machine Class
public class TestFSML1
{
  public static void main(String[] args){
    String A = "ab";
    int[][] ST = {{1,3,0},
            {1,2,1},
            {2,2,2},
            {3,3,3}};
    int[] AS = {0,0,1,0};
    Scanner in = new Scanner(System.in);
    String inString;
    boolean accept1 = false;
    FSM FSM1 = new FSM(A, ST, AS);

// Input string is command line parameter
    System.out.println("     Input       Accepted:");

    for(int i=0;i<args.length;i++) {
      inString = args[i];
      accept1 = FSM1.validString(inString);

      System.out.printf("%10s%13s\n",inString, accept1);
    }
  } // end main
} // end class

fsm类

// Finite State Machine Class
public class FSM
{
  // Instance variables
  public  String alphabet;
  public  int    stateTrans[][];
  public  int    acceptState[];
  private int    cstate;

  // Constructor function

  public FSM(String A, int[][] ST, int[] AS)
  {
    int NSYMBOLS = A.length();
    int NSTATES  = AS.length;
    // Alphabet
    alphabet = "" + A;
    // State transition table
    stateTrans = new int[NSTATES][NSYMBOLS];
    for(int r = 0; r < NSTATES; r++)
      for(int c = 0; c < NSYMBOLS; c++)
        stateTrans[r][c] = ST[r][c];
    // Accept states
    acceptState = new int[NSTATES];
    for(int r = 0; r < NSTATES; r++)
      acceptState[r] = AS[r];
    // Start state
    cstate = 0;
  } 

  // Methods

  public int getState()
  {
    return cstate;
  }

  public void setState(int state)
  {
    cstate = state;
    return;
  }

  public int nextState(char symbol)
  {
    int nstate = -1;
    int col = alphabet.indexOf(symbol);
    if(col >= 0)
      nstate = stateTrans[cstate][col];
    return nstate;
  }

  public boolean accept(int state)
  {
    if(state < 0)
      return false;
    return (acceptState[state] != 0);
  }

  public boolean validString(String word)
  {
    cstate = 0;
    for(int k = 0; k < word.length(); k++){
      cstate = nextState(word.charAt(k));
      System.out.print(cstate);
      System.out.println("  " + word.charAt(k));
      if(cstate < 0)
        return false;
    }
    return accept(cstate);
  }

} // end class

谢谢!

kb5ga3dv

kb5ga3dv1#

这里有一个简单的方法,我键入了执行您想要的任务。

public static boolean validWord(String s) {

        int aCounter = 0;
        int bCounter = 0;
        char c;

        for (int i = 0; i < s.length(); i++) {

            c = s.charAt(i);

            if ((int) c == (int) 'a') {

                aCounter++;
            } else {

                bCounter++;
            }
        }

        return (aCounter % 2 == 1 && bCounter == 1);
    }

我很难理解你是如何实现你的方法的,我认为它可以简单得多。我确信您在fsm类中包含的示例变量还有其他用途,但是我认为您并不需要它们中的任何一个来分析字符串。只要使用这样的东西,就可以很容易地集成到代码中,因为只需要字符串。希望这有帮助!

相关问题