我的任务是使用一堆整数来计算给定分子的分子质量。我应该自己使用数组实现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的分子质量为2 2 2。
我一直得到分子质量为0或1
编辑:这是我给出的评估方法的算法:如果字符是化学元素,程序将推动元素的分子量。如果字符是开括号,则程序将0推送到堆栈上。如果字符是闭括号,则程序将括号内的所有内容加起来,直到到达打开的括号(存储为0)为止--如果字符是数字,则会弹出堆栈上的数字,将其乘以输入号,并将其推回堆栈末尾的堆栈上,将堆栈中的所有内容加起来并返回结果。
发布于 2019-09-12 01:21:32
假设您正在学习代码,我建议您遵循一个自己发现错误的过程。我建议你这样做:
IntStack完全符合您的预期。evaluate逻辑移动到单独的类(该类使用IntStack作为隐藏实现细节)evaluate的各个部分,如element(Character)、count(Character)、startGroup()、endGroup()。这会比我们中的一个指出你的错误花费更长的时间,但我保证你会学到更多。
发布于 2020-08-02 09:19:14
或者你能做的就是使用一个字符串堆栈。
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);
}
}https://stackoverflow.com/questions/57898422
复制相似问题