为了账号安全,请及时绑定邮箱和手机立即绑定

自动机 DFA 实现无法使用 Java

自动机 DFA 实现无法使用 Java

qq_遁去的一_1 2021-11-03 11:01:24
我现在正在我的大学学习 DFA 和 NFA 自动机以及如何使用 Java 代码实现其中的一些。我在这个练习中遇到了一些麻烦:我们有 4 个不同的实验室回合(T1、T2、T3 和 T4),我们需要编写代码来识别特定的字符串(由学生的大学徽章编号和他的名称,例如,123321Johnson) 对应于 T2 或 T3。我们知道:T1 轮到徽章编号和姓氏在“A”和“K”之间的奇数T2是“A”和“K”之间偶数徽章数字和姓氏的轮换T3是“L”和“Z”之间的奇数徽章号码和姓氏的轮换T4是“L”和“Z”之间偶数徽章数字和姓氏的轮换我们也知道字符串必须由至少一个数字和至少一个字母组成。例如,自动机必须接受"1232324Gac"or"1232323Lum"但不接受"121234Lum"or "121233Gac"。这是我写的代码:import java.util.Scanner;public class Es3 {    static Scanner sc = new Scanner(System.in);    String s = sc.next();    public static boolean scan(String s)    {        int state = 0;                              int i = 0;                                      while (state >= 0 && i < s.length()) {            final char ch = s.charAt(i++);            switch (state) {            case 0:                if (ch >= 0 && ch <= 9)                    state = 1;                else                    state = -1;                break;            case 1:                if (ch >=0 && ch <=9)                    state = 1;                else if (ch >='a' && ch <='k')                    if ((s.charAt(i--))%2==0)                        state = 2;                    else                        state = -1;                else if (ch >='l' && ch <='z')                    if ((s.charAt(i--))%2==1)                        state = 3;                    else                        state = -1;                else                    state = -1;                break;            case 2:                if (ch >='a' && ch <='z')                    state = 2;                else                    state = -1;                break;            case 3:                if (ch >='a' && ch <='z')                    state = 3;                else                     state = -1;                break;            }        }        return (state == 2 || state == 3);          }    public static void main(String[] args)    {        System.out.println(scan(args[0]) ? "OK" : "NO");    }}显然,代码不起作用,但这对于展示练习的一般目的很重要。有人可以帮助我吗?
查看完整描述

1 回答

?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

您的算法不起作用的原因是因为您试图将char值与int值进行比较,这不会给出预期的结果。此外,当您检查某个char值是否在某个字母范围内时,您没有考虑大写字母。


import java.util.Scanner;


public class Es3 

{

    static Scanner sc = new Scanner(System.in);

    String s = sc.next();

    public static boolean scan(String s)

    {

        int state = 0;

        int i = 0;


        while (state >= 0 && i < s.length()) {

            final char ch = s.charAt(i++);

            switch (state) {

            case 0:

                // Compare the char to the char values of the numbers

                if (ch >= '0' && ch <= '9')

                    state = 1;          

                else

                    state = -1;

                break;


            case 1:  

                // Same here, compare the char to the char values of the numbers

                if (ch >= '0' && ch <= '9')

                    state = 1;

                // Check if the char is capital, as well as lowercase

                else if ((ch >= 'a' && ch <= 'k') || (ch >= 'A' && ch <= 'K'))

                    // Convert the char to an int before performing the calculations

                    if ((Character.getNumericValue(s.charAt(i-1)))%2 == 0)

                        state = 2;

                    else

                        state = -1;

                // Check if the char is capital as well

                else if ((ch >= 'l' && ch <= 'z') || (ch >= 'L' && ch <= 'Z'))

                    // Convert from char to int before calculating

                    if ((Character.getNumericValue(s.charAt(i-1)))%2 == 1)

                        state = 3;

                    else

                        state = -1;

                else

                    state = -1;

                break;


            case 2:

                // Check if the char is capital as well

                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

                    state = 2;

                else

                    state = -1;

                break;


            case 3:

                // Check if the char is capital as well

                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

                    state = 3;

                else 

                    state = -1;

                break;

            }

        }

        System.out.println("State "+state);

        return (state == 2 || state == 3);      

    }


    public static void main(String[] args)

    {

        System.out.println(scan(args[0]) ? "OK" : "NO");

    }

}

我认为上面的代码应该做你想做的事情。


查看完整回答
反对 回复 2021-11-03
  • 1 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信