2

我有一个化学化合物 ex 'H',我想在其中添加 'H'、'C'、'O' 或 'N'。如何签入我要添加到当前化合物的代码?

其次,当我添加到我当前的化合物中时,它可能是 H2。因此,如果我添加 H,它将变成 H2H,但我希望它是 H3,我该如何解决这个问题?

我不太确定如何明智地进行编码,但是

String Compound = "H2";
String a="H";
String b="C";
String c="O";
string d="N";

if (I am adding "H" to Compound){

//do something

}

if(I am adding "C" to Compound) {

//do something else

}
4

4 回答 4

2

为了使事情更容易理解和更面向对象,我认为使用Compound您自己的类可能会按需要工作。

class Compound {
    String last;
    String compound;
    int lastSuffixCount;

    Compound() {
        this.last = "";
        this.compound = "";
        this.lastSuffixCount = 0;
    }

    public String getCompound() {
        return compound;
    }

    public void setCompound(String compound) {
        this.compound = compound;
    }

    public void add(String suffix, int times) {
        if (suffix.equals(this.last) && times > 0) 
        {
            this.compound = compound.replace(lastSuffixCount + "", "");
            this.lastSuffixCount = lastSuffixCount + times;
            this.compound += lastSuffixCount;
        } else if (times > 0) {
            this.compound += suffix + times;
        }
        this.lastSuffixCount = times;
        this.last = suffix;
    }
}

示例驱动程序:

public static void main(String[] args) {
    Compound c = new Compound();
    c.add("H", 2);
    System.out.println(c.getCompound());
    c.add("H", 1);
    System.out.println(c.getCompound());
    c.add("O", 6);
    c.add("H", 4);
    System.out.println(c.getCompound());
}

输出:

H2
H3
H3O6H4
于 2012-11-15T02:54:09.077 回答
0

您想要构建一个正则表达式,它将捕获大写字母、小写字母和数字。这会将公式分解为不同的元素及其计数。使用地图来保存计数应该可以。

输入像C8H10N4O2

你最终会得到类似的东西:

{
  "C" : 8,
  "H" : 10,
  "N" : 4,
  "O" : 2
}

对两个操作数执行此操作,并将计数加在一起。

遍历地图的键并将它们与它们的计数放在一起以获得您的答案。您如何迭代密钥可能必须由化学命名规则决定,我对此一无所知。

于 2012-11-15T02:55:31.130 回答
0

我感到有点困惑(I am adding "C" to Compound)

您想检测您添加的字符串是否包含“C”(或其他字符串)?

采用String.indexOf(String s)

例子:

String c = "C";
String somethingAdding = "CH3";
if(somethingAdding.indexOf(c) == -1)
{
    // NOT adding C
}
else
{
    // adding C
}

但我真的建议你制作一个类来包装数据,而不是使用字符串

于 2012-11-15T03:02:43.790 回答
0

这是我的面向对象方法:

public class Compound {

    private static Pattern elementPattern = Pattern.compile("[A-Z]\\d*");

    private Map<Character, Integer> elements;

    public Compound(String compoundString) {
        elements = new HashMap<Character, Integer>();

        Matcher matcher = elementPattern.matcher(compoundString);
        while(matcher.find()) {
            String elementAndCount = m.group()

            char element = elementAndCount.charAt(0);
            int count = (elementAndCount.length() > 1) ?
                    Integer.valueOf(elementAndCount.substring(1)) :
                    1;

            elements.put(element, count);
        }
    }

    public void addElement(Character element) {
        addElement(element, 1);
    }

    public void addElement(Character element, int count) {
        if (elements.containsKey(element)) {
            int currentCount = elements.get(element);
            elements.put(element, count + 1);
        } else {
            elements.put(element, 1);
        }
    }

    @Override
    public String toString() {
        StringBuilder toReturn = new StringBuilder();
        for (Map.Entry<Character, Integer> entry : elements.entrySet()) {
            toReturn.append("" + entry.getKey() + entry.getValue());
        }
        return toReturn.toString();
    }
}

            if (element   
于 2012-11-15T03:55:04.553 回答