我已被分配创建一个Java程序,该程序读取字符串方程并计算其值。但是我设法创建这个代码的代码只能计算一位数的整数。我不知道如何使我的长整数工作。为了使用双倍甚至三位数整数,我的函数应该是什么样的?
public class Ex8 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String e = "1 - 3 * 18 / 4 + 2";
double t = executeStringEquation(e);
System.out.println(e + " = "+t);
}
public static double executeStringEquation(String inputEquation)
{
if("".equals(inputEquation))
{
return 0;
}
else if(inputEquation.length() == 1)
{
return Integer.valueOf(inputEquation);
}
else
{
String _a = String.valueOf(inputEquation.charAt(0));
String _b = String.valueOf(inputEquation.charAt(1));
if("+".equals(_b) || "-".equals(_b) )
{
if("+".equals(_b))
{
return Integer.valueOf(_a) + executeStringEquation(inputEquation.substring(2));
}
else
{// "-"
return Integer.valueOf(_a) - executeStringEquation(inputEquation.substring(2));
}
}
else
{// "*" or "/"
boolean isMulti = ("*".equals(_b));
String _c = String.valueOf(inputEquation.charAt(2));
Integer tmp = 0;
if(isMulti)
{
tmp = Integer.valueOf(_a) * Integer.valueOf(_c);
}
else
{
tmp = Integer.valueOf(_a) / Integer.valueOf(_c);
}
String newStr = String.valueOf(tmp) + inputEquation.substring(3);
return executeStringEquation(newStr);
}
}
}
}
答案 0 :(得分:0)
考虑读取整个字符串并在白色空间中分割。这可能不是一个有效的解析规则,但它可以让你获得多位整数。
答案 1 :(得分:0)
我正在开发一个函数,通过对多位数的后缀评估来评估您的表达式。这样做的想法是Postfix和Infix表示法,您可以在这里阅读:http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html
以下是此任务的代码段。 我更正了表达式评估器上的错误,现在评估是正确的,请检查出来:
package com.stackoverflow.infix;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.StringTokenizer;
public class InfixPostfixEvaluator {
private static final String operators = "-+/*";
private int getPrecedence(String input) {
char operator = input.charAt(0);
int ret = 0;
if (operator == '-' || operator == '+') {
ret = 1;
} else if (operator == '*' || operator == '/') {
ret = 2;
}
return ret;
}
private boolean operatorGreaterOrEqual(String op1, String op2) {
return getPrecedence(op1) >= getPrecedence(op2);
}
private boolean isOperator(String operator) {
return operators.indexOf(operator) >= 0;
}
private boolean isOperand(String val) {
try {
Double.parseDouble(val);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
public Stack<String> convert2Postfix(List<String> expressionInTokens) {
Stack<String> stack = new Stack<String>();
Stack<String> postExpression = new Stack<String>();
for (String c : expressionInTokens) {
if (isOperator(c)) {
while (!stack.isEmpty()) {
if (operatorGreaterOrEqual(stack.peek(), c)) {
postExpression.push(stack.pop());
} else {
break;
}
}
stack.push(c);
} else if (isOperand(c)) {
postExpression.push(c);
}
}
while(!stack.isEmpty()){
postExpression.push(stack.pop());
}
Collections.reverse(postExpression);
return postExpression;
}
public int evaluatePostfix(Stack<String> expression) {
Stack<Integer> stack = new Stack<Integer>();
while (!expression.isEmpty()) {
String c = expression.pop();
if (isOperand(String.valueOf(c))) {
stack.push(Integer.parseInt(c));
} else if (isOperator(String.valueOf(c))) {
int op1 = stack.pop();
int op2 = stack.pop();
int result;
switch (c) {
case "*":
result = op1 * op2;
stack.push(result);
break;
case "/":
result = op2 / op1;
stack.push(result);
break;
case "+":
result = op1 + op2;
stack.push(result);
break;
case "-":
result = op2 - op1;
stack.push(result);
break;
}
}
}
return stack.pop();
}
/**
* Takes an expression and parses it, returning a list with every element of
* the expression.The expression must not have white spaces, or characters
* different from numbers or the operands + - / *
*
*/
public List<String> split(String expression) {
List<String> a = new LinkedList<String>();
StringTokenizer st = new StringTokenizer(expression, "+*/-", true);
while (st.hasMoreTokens())
a.add(st.nextToken());
return a;
}
}
测试
package com.stackoverflow.infix;
import static org.junit.Assert.assertEquals;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.StringTokenizer;
import org.junit.Test;
public class TestInfixPostfixEvaluator {
@Test
public void testWhole() {
InfixPostfixEvaluator eval = new InfixPostfixEvaluator();
String infixExpr = "5+50-10";
List<String> expression = eval.split(infixExpr);
Stack<String> stack = eval.convert2Postfix(expression);
assertEquals(45, eval.evaluatePostfix(stack));
infixExpr = "2-3+4";
expression = eval.split(infixExpr);
stack = eval.convert2Postfix(expression);
assertEquals(3, eval.evaluatePostfix(stack));
infixExpr = "305-303+4";
expression = eval.split(infixExpr);
stack = eval.convert2Postfix(expression);
assertEquals(6, eval.evaluatePostfix(stack));
infixExpr = "2+3";
expression = eval.split(infixExpr);
stack = eval.convert2Postfix(expression);
assertEquals(5, eval.evaluatePostfix(stack));
infixExpr = "2*5+5";
expression = eval.split(infixExpr);
stack = eval.convert2Postfix(expression);
assertEquals(15, eval.evaluatePostfix(stack));
infixExpr = "2*5/5*5";
expression = eval.split(infixExpr);
stack = eval.convert2Postfix(expression);
assertEquals(10, eval.evaluatePostfix(stack));
infixExpr = "4+2*16/4";
expression = eval.split(infixExpr);
stack = eval.convert2Postfix(expression);
assertEquals(12, eval.evaluatePostfix(stack));
infixExpr = "16*3/4+2";
expression = eval.split(infixExpr);
stack = eval.convert2Postfix(expression);
assertEquals(14, eval.evaluatePostfix(stack));
}
@Test
public void testSplit() {
String exp = "15+20+84/205-1";
LinkedList<String> a = new LinkedList<String>();
StringTokenizer st = new StringTokenizer(exp, "+*/-", true);
while (st.hasMoreTokens())
a.add(st.nextToken());
System.out.println(a.toString());
}
}