首页 > 解决方案 > 使用堆栈计算分子量

问题描述

我的任务是使用一堆整数来计算给定分子的分子量。我应该使用数组自己实现 IntStack 类。然后我应该创建一个将字符串作为输入并评估分子的类。输入中的唯一字符将是左括号和右括号、数字 2-9、H(氢)、C(碳)和 O(氧)。我得到了三种元素的分子量分别为 1、12 和 16。

public class IntStack
{
private int[] stack;
public int index;

public IntStack()
{
    stack = new int[100];
    index = -1;
}

public void push(int x)
{
    stack[index + 1] = x;
    index++;
}

public int pop()
{
    if (index == -1)
    {
        return -1;
    }
    int num = stack[index];
    index--;
    return num;
}

public int peek()
{
    if (index == -1)
    {
        return 0;
    }
    return stack[index];
}
}


import java.util.Scanner;

public class MolecularMass
{
 private static IntStack stack;

public static void main(String[] args)
{
stack = new IntStack();
Scanner kb = new Scanner(System.in);
System.out.print("Enter the molecule: ");
String input = kb.nextLine();
int result = evaluate(input);
System.out.println("The Molecular Mass of " + input +  " is " + result);
}

public static int evaluate(String s)
{
int answer = 0;
int num = 0;

for(int i = 0; i < s.length(); i++)
{
  char c = s.charAt(i);
  switch(c)
  {
    case '2':
      num = stack.pop();
      num *= 2;
      stack.push(num);
      break;
    case '3':
      num = stack.pop();
      num *= 3;
      stack.push(num);
      break;
    case '4':
      num = stack.pop();
      num *= 4;
      stack.push(num);
      break;
    case '5':
      num = stack.pop();
      num *= 5;
      stack.push(num);
      break;
    case '6':
      num = stack.pop();
      num *= 6;
      stack.push(num);
      break;
    case '7':
      num = stack.pop();
      num *= 7;
      stack.push(num);
      break;
    case '8':
      num = stack.pop();
      num *= 8;
      stack.push(num);
      break;
    case '9':
      num = stack.pop();
      num *= 9;
      stack.push(num);
      break;
    case 'C':
      stack.push(12);
      break;
    case 'H':
      stack.push(1);
      break;
    case 'O':
      stack.push(16);
      break;
    case '(':
      stack.push(0);
      break;
    case ')':
      int result = 0;
      while(stack.peek() != 0)
      {
        result += stack.pop();
      }
      int throwaway = stack.pop();
      stack.push(result);
      break;
    default:
      break;
  }
}

for(int i = 0; i < stack.index; i++)
{
  answer += stack.pop();
}

return answer;
}
}

它应该按如下方式运行:

输入分子:((CH)2(OH2H)(C(H))O)3

((CH)2(OH2H)(C(H))O)3 的分子量为 222

我不断得到分子质量为 0 或 1

编辑:这是我的评估方法算法:如果字符是化学元素,程序会推动元素的分子量。如果字符是一个左括号,则程序将 0 压入堆栈。如果字符是右括号,则程序将括号内的所有内容相加,直到到达左括号(存储为 0) 如果字符是数字,则将数字弹出堆栈,将其乘以输入数字,并将其推回堆栈最后,它将堆栈中的所有内容相加并返回结果。

标签: javastackchemistry

解决方案


或者您可以做的是使用字符串堆栈。

  1. 遍历给定字符串的长度。如果第 i 个索引处的字符是 '(' ,则将其推入堆栈。

  2. 然后,当我们看到一个 ')' 时,我们定义一个变量来存储左括号和右括号之间的元素之和。

  3. 我们通过弹出堆栈直到找到 '(' 并将所有弹出的元素添加到变量中,然后最终将该变量推入堆栈来做到这一点。

  4. 如果找到“H”,请按 1,如果找到氧原子,请按 16,如果找到碳,请按 12。

  5. 一旦我们看到一个数字,我们就会弹出最上面的元素并将其与数字相乘。

  6. 最后,在循环结束后,只有 num 留在堆栈中,必须添加,使用 while 循环将它们相加。您可以参考此代码,如果您愿意,您可以创建自己的字符串堆栈。

    public class MassOfMolecules {
    
    private static int getValue(char c) {
        if (c == 'H')
            return 1;
        if (c == 'O')
            return 16;
        else
            return 12;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = str.length();
        long ans = 0;
        Stack<String> stack = new Stack<String>();
        String checkM = "OCH";
    
        for (int i = 0; i < n; i++) {
            char c = str.charAt(i);
            if(c=='(') {
                stack.push("(");
            }
            else if(checkM.contains(String.valueOf(c))) {
                stack.push(String.valueOf(getValue(c)));
            }
            else if(c==')') {
                long x = 0;
                while(!stack.peek().contains("(")) {
                    x=x+Long.parseLong(stack.pop());
                }
                stack.pop();
                stack.push(String.valueOf(x));
            }
            else if(Character.isDigit(c)) {
                long x = 0;
                if(!stack.isEmpty()) {
                    x = Long.parseLong(stack.pop())*Long.parseLong(String.valueOf(c));
                    stack.push(String.valueOf(x));
                }
            }
        }
        long x = 0;
        while(!stack.isEmpty()&&!stack.peek().contains("(")) {
            x = x + Long.parseLong(stack.pop());
        }
        System.out.println(x);
    }
    }


推荐阅读