题解 | #四则运算#
四则运算
http://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e
import java.util.*;
import javax.script.*;
public class Main{
public static void main(String[] args)throws ScriptException{
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
int result = calculate(input);
System.out.println(result);
}
public static int calculate(String s){
Stack<Integer>stack = new Stack<>();
int num = 0;
// 记录第一个数字前面的符号,初始化为正号
char sign ='+';
char[]Arr = s.toCharArray();
int length = Arr.length;
// a. 遍历每一位,并压栈
for(int i =0;i<length;i++){
char c = Arr[i];
// 1. 转换连续的数字
if(Character.isDigit(c)){
num = 10*num +(c-'0');
}
// 2. 递归处理括号
if(c == '(' ||c == '{' || c == '[' ){
int start = i;
int times =0;
for(;i<length;i++){
if(Arr[i] == '(' ||Arr[i] == '{' || Arr[i] == '[' ){
++times;
}
if(Arr[i] == ')' ||Arr[i] == '}' || Arr[i] == ']' ){
--times;
}
if(times == 0){
break;
}
}
// 开始索引,结束索引
// 递归,将括号内的字符串截取,调用本函数求值
num = calculate(s.substring(start+1,i));
}
// 3. 判断符号,计算,并入栈
// 注意:到最后个数字时,也要计算并且入栈
boolean isSign = !Character.isDigit(c) && c!=' ';
if(isSign || i == length-1){
int temp =0;
// 加减乘除
switch(sign){
case'+':
stack.push(num);
break;
case'-':
stack.push(-1*num);
break;
case'*':
temp = stack.peek();
stack.pop();
stack.push(num*temp);
break;
case'/':
temp = stack.peek();
stack.pop();
stack.push(temp/num);
break;
}
// 核心: 操作完了,更新符号
// 因为最后一个数字的时候,要用到前面保存的符号
sign = c;
num = 0;
}
}
// b. for循环结束,将栈中所有结果求和,就是要返回的答案
int answer = 0;
while(!stack.empty()){
answer = answer+stack.peek();
stack.pop();
}
return answer;
}
}
import javax.script.*;
public class Main{
public static void main(String[] args)throws ScriptException{
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
int result = calculate(input);
System.out.println(result);
}
public static int calculate(String s){
Stack<Integer>stack = new Stack<>();
int num = 0;
// 记录第一个数字前面的符号,初始化为正号
char sign ='+';
char[]Arr = s.toCharArray();
int length = Arr.length;
// a. 遍历每一位,并压栈
for(int i =0;i<length;i++){
char c = Arr[i];
// 1. 转换连续的数字
if(Character.isDigit(c)){
num = 10*num +(c-'0');
}
// 2. 递归处理括号
if(c == '(' ||c == '{' || c == '[' ){
int start = i;
int times =0;
for(;i<length;i++){
if(Arr[i] == '(' ||Arr[i] == '{' || Arr[i] == '[' ){
++times;
}
if(Arr[i] == ')' ||Arr[i] == '}' || Arr[i] == ']' ){
--times;
}
if(times == 0){
break;
}
}
// 开始索引,结束索引
// 递归,将括号内的字符串截取,调用本函数求值
num = calculate(s.substring(start+1,i));
}
// 3. 判断符号,计算,并入栈
// 注意:到最后个数字时,也要计算并且入栈
boolean isSign = !Character.isDigit(c) && c!=' ';
if(isSign || i == length-1){
int temp =0;
// 加减乘除
switch(sign){
case'+':
stack.push(num);
break;
case'-':
stack.push(-1*num);
break;
case'*':
temp = stack.peek();
stack.pop();
stack.push(num*temp);
break;
case'/':
temp = stack.peek();
stack.pop();
stack.push(temp/num);
break;
}
// 核心: 操作完了,更新符号
// 因为最后一个数字的时候,要用到前面保存的符号
sign = c;
num = 0;
}
}
// b. for循环结束,将栈中所有结果求和,就是要返回的答案
int answer = 0;
while(!stack.empty()){
answer = answer+stack.peek();
stack.pop();
}
return answer;
}
}
查看11道真题和解析