解析代表化学反应的字符串并验证反应是否可行 [英] Parsing a string that represents a chemical reaction and verify if the reaction is possible

查看:120
本文介绍了解析代表化学反应的字符串并验证反应是否可行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个程序,将用户的化学方程式作为输入,如12 CO2 + 6 H2O - > 2 C6H12O6 + 12 O2,并观察两个位点上的原子数量是否相同。有没有办法轻松计算和解析?

I have to write a program that takes a user's chemical equation as an input, like 12 CO2 + 6 H2O -> 2 C6H12O6 + 12 O2, and watch if the amount of Atoms is on both sites the same. Is there any way to calculate and parse this easily?

例如:

12 CO2 + 6 H2O - > 2 C6H12O6 + 12 O2

12 CO2 + 6 H2O -> 2 C6H12O6 + 12 O2

12 * 2 + 6 * 2 - > 2 * 6 + 2 * 12 + 2 * 6 + 12 * 2

12*2+6*2 -> 2*6+2*12+2*6+12*2

在这种情况下,应该输出false。

In this case there should be the Output "false".

这是我的代码,但它实际上只是尝试一些东西:

This is my code but it's actually is only to try out something:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    List<String> list = new ArrayList<String>();
    String input = "";
    while (!(input.equals("end"))) {
        input = s.nextLine();
        list.add(input);
    }
    list.remove(list.size() - 1);
    for (int i = 0; i < list.size(); i++) {
        int before = 0;
        int after = 0;
        String string = list.get(i);
        string = besserUmwandeln(string);
        System.out.println(string);
    }
}

public static String besserUmwandeln(String string) {
    string = string.replace("-", "");
    string = string.trim().replaceAll(("\\s+"), " ");
    string = string.replace(' ', '*');
    StringBuilder builder = new StringBuilder(string);
    System.out.println(string);
    for (int k = 0; k < builder.length(); k++) {
        if (Character.isUpperCase(builder.charAt(k))) {
            builder.setCharAt(k, ':');
        }
        if (Character.isLowerCase(builder.charAt(k))) {
            builder.setCharAt(k, '.');
        }
        if (Character.isDigit(builder.charAt(k))) {
        } else {
        }
    }
    for (int j = 0; j < builder.length(); j++) {
        if (j < builder.length() && builder.charAt(j) == ':' && builder.charAt(j + 1) == '.') {
            builder.deleteCharAt(j + 1);
        }
    }
    for (int i = 0; i < builder.length(); i++) {
        if (i < builder.length() - 1 && builder.charAt(i) == ':' && builder.charAt(i + 1) == ':') {
            builder.deleteCharAt(i);
        }
    }
    for (int i = 0; i < builder.length(); i++) {
        if (i < builder.length() - 1 && builder.charAt(i) == '+' && builder.charAt(i + 1) == '*') {
            builder.deleteCharAt(i + 1);
        }
    }
    for (int i = 0; i < builder.length(); i++) {
        if (i < builder.length() - 1 && builder.charAt(i) == '*' && builder.charAt(i + 1) == '+') {
            builder.deleteCharAt(i);
        }
    }
    for (int i = 0; i < builder.length(); i++) {
        if (i < builder.length() - 1 && builder.charAt(i) == '*' && builder.charAt(i + 1) == '>') {
            builder.deleteCharAt(i);
        }
    }
    for (int i = 0; i < builder.length(); i++) {
        if (i < builder.length() - 1 && builder.charAt(i) == '>' && builder.charAt(i + 1) == '*') {
            builder.deleteCharAt(i + 1);
        }
    }
    for (int i = 0; i < builder.length(); i++) {
        if (i < builder.length() - 1 && builder.charAt(i) == '*' && builder.charAt(i + 1) == ':') {
            builder.deleteCharAt(i + 1);
        }
    }


    return builder.toString();
}


推荐答案

所以,每次我需要使用 Java 解析一些文本,我最终只使用 Regex 。所以我建议你也这样做。

So, every time I need to parse some text with Java, I mostly end up just using Regex. So I'd recommend you to also do so.

你可以在 regex101.com

并且还可以在 Java中轻松使用它

final inputText = ...
final Pattern pattern = Patern.compile("Some regex code");
final Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
    System.out.println(matcher.group(0));
}

内部正则表达式你可以使用定义捕获组,然后通过 matcher.group获取结果( int)

Inside Regex you can define capturing groups with ( and ) and then grab the results by matcher.group(int).

例如,您可以先使用(。*) - >分隔等式。 (。*)

然后使用 find 循环左右组:(\d +)(\ w +)(?:\ + | - | $)

Then loop the left and right group using find with: (\d+) (\w+)(?: \+| -|$).

之后,您可以使用 group(1)获取金额,使用 group(2)获取元素。

After that you can use group(1) for the amount and group(2) for the element.

如果需要,还可以使用(\w)(\\\?)来迭代第二组(元素)以获得精确的元素分布。然后第一组是元素,例如对于文本 CO2 它产生两次点击,第一次点击具有 group(1) - > C 而没有第二组。第二个点击 group(1) - > O group(2) - > 2

And if needed also iterate the second group (the element) for the exact element distribution using (\w)(\d?). Then the first group is the element, for example for the text CO2 it yields two hits, the first hit has group(1) -> C and no second group. The second hit has group(1) -> O and group(2) -> 2.

在此测试您的正则表达式: regex101#Q6KMJo

Test your regex here: regex101#Q6KMJo

这篇关于解析代表化学反应的字符串并验证反应是否可行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆