java 逻辑字符串计算器,Java字符串计算器

I'm trying to make a simple calculator in Java which takes input in the form of a string and does a simple '+' and '-' operation.

Single digit inputs work but my problem is when i try to implement this for double digit

input string is: 5+20+5+11

list 1 = [5, 20, 2, 0, 5, 11, 1]

list 2 = [+, +, +]

Answer:27

I need to find a way where after storing [5] in list1 how i can add [5,20] instead of [5,20,2,0] which the current code is doing.

public int calC(String input) {

int len = input.length();

ArrayList list1 = new ArrayList();

ArrayList list2 = new ArrayList();

for (int i = 0; i < len; i++) {

if ((input.charAt(i) != '+') && (input.charAt(i) != '-')) {

// check if the number is double-digit

if ((i + 1 <= len - 1)) {

if ((input.charAt(i + 1) != '+')&& (input.charAt(i + 1) != '-')) {

String temp = "";

temp = temp + input.charAt(i) + input.charAt(i + 1);

int tempToInt = Integer.parseInt(temp);

// adding the double digit number

list1.add(tempToInt);

}

// add single digit number

list1.add(input.charAt(i) - '0');

}

} else {

// adding the symbols

list2.add(input.charAt(i));

}

}

int result = 0;

result = result + (int) list1.get(0);

for (int t = 0; t < list2.size(); t++) {

char oper = (char) list2.get(t);

if (oper == '+') {

result = result + (int) list1.get(t + 1);

} else if (oper == '-') {

result = result - (int) list1.get(t + 1);

}

}

return result;

}

Edit: working version

@Ker p pag thanks for the updated methods

input string is: 5+20+5+11

[5, 20, 5, 11]

[+, +, +]

Answer:41

I'll need to try to implement this with stack as suggested but the current version works

static boolean isDigit(char check) {

if (Character.isDigit(check)) {

return true;

}

return false;

}

public static int calC(String input) {

int len = input.length();

ArrayList list1 = new ArrayList();

ArrayList list2 = new ArrayList();

for (int i = 0; i < len; i++) {

if ((i + 1 <= len - 1)) {

if (isDigit(input.charAt(i)) && isDigit(input.charAt(i + 1))) {

String temp = input.charAt(i) + "" + input.charAt(i + 1);

int toInt = Integer.parseInt(temp);

list1.add(toInt);

i = i+1;

} else if (isDigit(input.charAt(i))) {

list1.add(input.charAt(i)- '0');

} else {

list2.add(input.charAt(i));

}

}

}

int result = 0;

result = result + (int) list1.get(0);

for (int t = 0; t < list2.size(); t++) {

char oper = (char) list2.get(t);

if (oper == '+') {

result = result + (int) list1.get(t + 1);

} else if (oper == '-') {

result = result - (int) list1.get(t + 1);

}

}

return result;

}

解决方案

If you want the result 41 for input string "5+20+5+11",

why not use ScriptEngineManager with JavaScript engine,

public double calC(String input) {

int result = 0;

ScriptEngineManager mgr = new ScriptEngineManager();

ScriptEngine engine = mgr.getEngineByName("JavaScript");

return (Double)engine.eval(input);

}

But note that the return type is double here.

If you want only int as return type in this case, try with this

return new BigDecimal(engine.eval(input).toString()).intValue();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值