如果不使用这么多语言,是否有一种更简单的方法来解决此问题?如何



我们得到了一个问题,要求使用字母qdn(四分之一,角钱,镍(创建有限状态机,该机器仅在增加40美分时接受。我有使用IF语句概念的基础知识。我想知道是否有更简单的方法需要更少的时间?

我已经尝试制作大量的if情况,但是使用该方法有很多步骤。

public class FSA2_rpf4961 {
public static void main(String[] args) {
    //The program will read in a sequence of strings and test them against a 
    //FSM. Your strings may not contain blank spaces
    System.out.println("Enter string to test or q to terminate");
    Scanner in = new Scanner (System.in);
    String testString = in.next();
    while (!testString.equals("q"))
    {
        String testOutput = applyFSA2(testString);
        System.out.println("For the test string "+testString+
                                ", the FSM output is "+testOutput);
        System.out.println("Enter next string to test or q to terminate:");
        testString = in.next();
    }
    in.close();
}
public static String applyFSA(String s) {
   String currentOut = "0";                // initial output in s0
   String currentState = "s0";             // initial state
   int i = 0;
   while (i<s.length())
   {
            //quarter first
           if (currentState.equals("s0") && s.charAt(i) == 'q')
           {
                    currentState = "s1";  
                    currentOut += 25;  // collect output on move to s1
           }
           else if (currentState.equals("s1") && s.charAt(i)  == 'd') {
            currentState = "s2";
            currentOut += 10;
           }
           else if (currentState.equals("s2") && s.charAt(i)  == 'n') {
            currentState = "s3";
            currentOut += 5;
           }
           else if (currentState.equals("s1") && s.charAt(i)  == 'n') {
            currentState = "s4";
            currentOut += 5;
           }
           else if (currentState.equals("s4") && s.charAt(i)  == 'd') {
            currentState = "s3";
            currentOut += 10;
           }
           //dime first
           else if (currentState.equals("s0") && s.charAt(i) == 'd')
           {
                    currentState = "s5";
                    currentOut += 10;   // d
            }

我们需要它仅在增加40美分的情况下才能接受。这让我很困惑。

我将在这里一般说话,因为您当前的代码很奇怪,以至于几乎确定的破碎。我不太了解您的"输出"应该是什么。

a FSM是一组状态的定义以及输入如何导致状态过渡。看来您开始大致这样做,但是currentOut被打破了。如果您的目标是获得一笔款项,那么您将打败练习的全部要点。总和是什么都没关系。重要的是您最终处于哪种状态 - 尤其是您是否处于"接受"状态,在这种情况下,字符串等于40美分的状态。

至于如何实现没有if s的FSM,您通常可以将状态存储在数组中,而将您的状态名称为状态号。那时,理论上您可以摆脱所有您的if s。(但是,在现实世界中,您可能仍然希望一个人忽略或拒绝(q | d | n(以外的其他字符。(

考虑这样的东西(伪代码(:

    // Each array in `graph` represents a state.
    // Each entry is the state number (ie: index into `graph`) to go to
    // when you're in that state and see the corresponding char.
    //
    // BTW, the state graph makes a lot more sense when you consider nickels first.
    // A nickel takes you to the "next" state, and dimes and quarters act like
    // 2 and 5 nickels, respectively. When you do that, a pattern shows up.
    graph = [
      //  n  d  q
      //-----------
        [ 1, 2, 5 ], // s0
        [ 2, 3, 6 ], // s1
        [ 3, 4, 7 ], // s2
        [ 4, 5, 8 ], // s3
        [ 5, 6, 9 ], // s4
        [ 6, 7, 9 ], // s5
        [ 7, 8, 9 ], // s6
        [ 8, 9, 9 ], // s7
        [ 9, 9, 9 ], // s8
        [ 9, 9, 9 ]  // s9 (fail state)
    ]
    start = 0
    accept = 8
    fail = 9
    // at this point, walking the graph is trivial.
    state = start
    for each char c in s:
        index = "ndq".indexOf(c) // n->0, d->1, q->2, others -> -1
        state = graph[state][index]

    // Once the loop's done:
    //   if state == accept, you have exactly 40c.
    //   if state == fail, you have >40c. A FSM won't tell you how much,
    //      because FSMs can't count.
    // any other state represents a known amount that's less than 40c.

最新更新